### Question:
VMUtils { public static void saveProgramTraceFile(Path basePath, String txHash, boolean compress, ProgramTrace trace) throws IOException { if (compress) { try(final FileOutputStream fos = new FileOutputStream(basePath.resolve(txHash + ".zip").toFile()); final ZipOutputStream zos = new ZipOutputStream(fos) ) { ZipEntry zipEntry = new ZipEntry(txHash + ".json"); zos.putNextEntry(zipEntry); Serializers.serializeFieldsOnly(trace, true, zos); } } else { try (final OutputStream out = Files.newOutputStream(basePath.resolve(txHash + ".json"))) { Serializers.serializeFieldsOnly(trace, true, out); } } } private  VMUtils(); static void saveProgramTraceFile(Path basePath, String txHash, boolean compress, ProgramTrace trace); static void saveProgramTraceFile(String txHash, ProgramTrace trace, String databaseDir, String vmTraceDir, boolean vmTraceCompressed);  }### Answer:
@Test public void savePlainProgramTraceFile() throws Exception { Path traceFilePath = tempRule.newFolder().toPath(); ProgramTrace mockTrace = new ProgramTrace(config.getVmConfig(), null); String mockTxHash = "1234"; VMUtils.saveProgramTraceFile( traceFilePath, mockTxHash, false, mockTrace ); String trace = new String(Files.readAllBytes(traceFilePath.resolve(mockTxHash + ".json"))); Assert.assertThat(trace, is(Serializers.serializeFieldsOnly(mockTrace, true))); }
@Test public void saveZippedProgramTraceFile() throws Exception { Path traceFilePath = tempRule.newFolder().toPath(); ProgramTrace mockTrace = new ProgramTrace(config.getVmConfig(), null); String mockTxHash = "1234"; VMUtils.saveProgramTraceFile( traceFilePath, mockTxHash, true, mockTrace ); ZipInputStream zipIn = new ZipInputStream(Files.newInputStream(traceFilePath.resolve(mockTxHash + ".zip"))); ZipEntry zippedTrace = zipIn.getNextEntry(); Assert.assertThat(zippedTrace.getName(), is(mockTxHash + ".json")); ByteArrayOutputStream unzippedTrace = new ByteArrayOutputStream(); byte[] traceBuffer = new byte[2048]; int len; while ((len = zipIn.read(traceBuffer)) > 0) { unzippedTrace.write(traceBuffer, 0, len); } Assert.assertThat(new String(unzippedTrace.toByteArray()), is(Serializers.serializeFieldsOnly(mockTrace, true))); }
@Test public void savePlainProgramTraceFile() throws Exception { Path traceFilePath = tempRule.newFolder().toPath(); DetailedProgramTrace mockTrace = new DetailedProgramTrace(config.getVmConfig(), null); String mockTxHash = "1234"; VMUtils.saveProgramTraceFile( traceFilePath, mockTxHash, false, mockTrace ); String trace = new String(Files.readAllBytes(traceFilePath.resolve(mockTxHash + ".json"))); Assert.assertThat(trace, is(Serializers.serializeFieldsOnly(mockTrace, true))); }
@Test public void saveZippedProgramTraceFile() throws Exception { Path traceFilePath = tempRule.newFolder().toPath(); DetailedProgramTrace mockTrace = new DetailedProgramTrace(config.getVmConfig(), null); String mockTxHash = "1234"; VMUtils.saveProgramTraceFile( traceFilePath, mockTxHash, true, mockTrace ); ZipInputStream zipIn = new ZipInputStream(Files.newInputStream(traceFilePath.resolve(mockTxHash + ".zip"))); ZipEntry zippedTrace = zipIn.getNextEntry(); Assert.assertThat(zippedTrace.getName(), is(mockTxHash + ".json")); ByteArrayOutputStream unzippedTrace = new ByteArrayOutputStream(); byte[] traceBuffer = new byte[2048]; int len; while ((len = zipIn.read(traceBuffer)) > 0) { unzippedTrace.write(traceBuffer, 0, len); } Assert.assertThat(new String(unzippedTrace.toByteArray()), is(Serializers.serializeFieldsOnly(mockTrace, true))); }