Allow to call AbstractDiskHttpData.setContent(Unpooled.EMPTY_BUFFER) multiple times. (#7890)

Motivation:

It should be possible to call setContent(Unpooled.EMPTY_BUFFER) multiple times just like its possible to do the same with a non empty buffer.

Modifications:

- Correctly reset underlying storage if called multiple times.
- Add tests

Result:

Fixes https://github.com/netty/netty/issues/6418
This commit is contained in:
Norman Maurer 2018-04-30 08:39:24 +02:00 committed by GitHub
parent b5fde3e0cd
commit 0261e00662
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 3 deletions

View File

@ -115,7 +115,13 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData {
if (buffer.readableBytes() == 0) {
// empty file
if (!file.createNewFile()) {
throw new IOException("file exists already: " + file);
if (file.length() == 0) {
return;
} else {
if (!file.delete() || !file.createNewFile()) {
throw new IOException("file exists already: " + file);
}
}
}
return;
}

View File

@ -15,15 +15,66 @@
*/
package io.netty.handler.codec.http.multipart;
import org.junit.Assert;
import io.netty.buffer.Unpooled;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class DiskFileUploadTest {
@Test
public final void testDiskFileUploadEquals() {
DiskFileUpload f2 =
new DiskFileUpload("d1", "d1", "application/json", null, null, 100);
Assert.assertEquals(f2, f2);
assertEquals(f2, f2);
f2.delete();
}
@Test
public void testEmptyBufferSetMultipleTimes() throws IOException {
DiskFileUpload f =
new DiskFileUpload("d1", "d1", "application/json", null, null, 100);
f.setContent(Unpooled.EMPTY_BUFFER);
assertTrue(f.getFile().exists());
assertEquals(0, f.getFile().length());
f.setContent(Unpooled.EMPTY_BUFFER);
assertTrue(f.getFile().exists());
assertEquals(0, f.getFile().length());
f.delete();
}
@Test
public void testEmptyBufferSetAfterNonEmptyBuffer() throws IOException {
DiskFileUpload f =
new DiskFileUpload("d1", "d1", "application/json", null, null, 100);
f.setContent(Unpooled.wrappedBuffer(new byte[] { 1, 2, 3, 4 }));
assertTrue(f.getFile().exists());
assertEquals(4, f.getFile().length());
f.setContent(Unpooled.EMPTY_BUFFER);
assertTrue(f.getFile().exists());
assertEquals(0, f.getFile().length());
f.delete();
}
@Test
public void testNonEmptyBufferSetMultipleTimes() throws IOException {
DiskFileUpload f =
new DiskFileUpload("d1", "d1", "application/json", null, null, 100);
f.setContent(Unpooled.wrappedBuffer(new byte[] { 1, 2, 3, 4 }));
assertTrue(f.getFile().exists());
assertEquals(4, f.getFile().length());
f.setContent(Unpooled.wrappedBuffer(new byte[] { 1, 2}));
assertTrue(f.getFile().exists());
assertEquals(2, f.getFile().length());
f.delete();
}
}