メソッドが期待どおりに機能していることを確認するために、私がリファクタリングしたメソッドのJunitテストを記述しようとしています。私が把握できなかった奇妙な行動を指摘しました。ByteArrayOutputStream.toByteArray()はランダムなバイトを返しますか?
はなぜJavaのByteArrayOutputStream.toByteArrayは()いくつかランダムバイトを返すのですか?これはおそらくメモリと関係があると思います。配列内の常に11バイトはランダムです。どんな洞察?
私がリファクタリングした方法は次のとおりです。
package foo;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
public static byte[] createZip(Map<String, byte[]> files) throws IOException {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final ZipOutputStream zipfile = new ZipOutputStream(bos);
String fileName = null;
ZipEntry zipentry = null;
for (Map.Entry<String, byte[]> entry : files.entrySet()) {
fileName = entry.getKey();
zipentry = new ZipEntry(fileName);
zipfile.putNextEntry(zipentry);
zipfile.write(files.get(fileName));
}
zipfile.close();
return bos.toByteArray();
}
}
テストクラス
package foo;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({})
public class ZipUtilTest {
@Test
public void test_createZip() throws Exception {
// Setup
Map<String, byte[]> files = new HashMap<String, byte[]>();
byte[] byteArr = new byte[] { 49, 17, 23,
-29, -126, 111, -72, -112, 48, 32, 91, -28, -14, 112 };
files.put("foo.txt", byteArr);
// Test
byte[] result = ZipUtil.createZip(files);
// Validations
byte[] expectedByteArray1 = new byte[] { 80, 75, 3, 4, 20, 0, 8, 8, 8, 0, 46, -120, -11, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
0, 0, 0, 102, 111, 111, 46, 116, 120, 116, 51, 20, 20, 127, -36, -108, -65, 99, -126, -127, 66, -12, -109, 79, 5, 0, 80,
75, 7, 8, -99, 100, -122, -62, 16, 0, 0, 0, 14, 0, 0, 0, 80, 75, 1, 2, 20, 0, 20, 0, 8, 8, 8, 0, 46, -120, -11, 72, -99,
100, -122, -62, 16, 0, 0, 0, 14, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 111, 111, 46, 116,
120, 116, 80, 75, 5, 6, 0, 0, 0, 0, 1, 0, 1, 0, 53, 0, 0, 0, 69, 0, 0, 0, 0, 0 };
Assert.assertNotNull(result);
Assert.assertEquals(144, result.length);
// a few bytes are "random". So test the first 9 never random bytes
for (int i = 0; i < 10; i++) {
Assert.assertEquals(expectedByteArray1[i], result[i]);
}
// This fails
// Assert.assertEquals(expectedByteArray1[10], result[10]);
for (int i = 11; i < 70; i++) {
Assert.assertEquals(expectedByteArray1[i], result[i]);
}
}
}
'zipfile.write(entry.getValue())'は 'zipfile.write(files.get(fileName))'よりも優れています。 – Matthieu
ループの外で 'fileName'と' zipEntry'を宣言したのはなぜですか? –