### Question:
B2ContentOutputStreamWriter extends B2ContentWriter { public static Builder builder(Helper helper) { return new Builder(helper); } private  B2ContentOutputStreamWriter(Helper helper); static Builder builder(Helper helper);  }### Answer:
@Test public void testHappyPath_noVerify() throws B2Exception { final HelperSimulator helper = new HelperSimulator(); helper.verifySha1ByRereading = false; final B2ContentOutputStreamWriter writer = B2ContentOutputStreamWriter.builder(helper).build(); writer.readContent(makeHeaderWithSha1(), in); helper.check( 1, 0, 1, 0 ); assertArrayEquals(bytes, helper.getBytes()); }
@Test public void testHappyPath_verify() throws B2Exception { final HelperSimulator helper = new HelperSimulator(); helper.verifySha1ByRereading = true; final B2ContentOutputStreamWriter writer = B2ContentOutputStreamWriter.builder(helper).build(); writer.readContent(makeHeaderWithSha1(), in); helper.check( 1, 1, 1, 0 ); assertArrayEquals(bytes, helper.getBytes()); }
@Test public void testTroubleDuringDownload() throws B2Exception { final HelperSimulator helper = new HelperSimulator(); helper.throwDuringDownload = true; final B2ContentOutputStreamWriter writer = B2ContentOutputStreamWriter.builder(helper).build(); try { writer.readContent(makeHeaderWithSha1(), in); fail("should've thrown!"); } catch (RuntimeException ignored) { } helper.check( 1, 0, 0, 1 ); }
@Test public void testSha1MismatchDuringDownload() { final HelperSimulator helper = new HelperSimulator(); bytes[0] = (byte) ((bytes[0]+1) % 256); final B2ContentOutputStreamWriter writer = B2ContentOutputStreamWriter.builder(helper).build(); try { writer.readContent(makeHeaderWithSha1(), in); fail("should've thrown!"); } catch (B2Exception e) { assertTrue(e.getMessage().startsWith("sha1 mismatch from network.")); } helper.check( 1, 0, 0, 1 ); }
@Test public void testSha1MismatchDuringRereading() { final HelperSimulator helper = new HelperSimulator(); helper.verifySha1ByRereading = true; helper.mangleBeforeRereading = true; final B2ContentOutputStreamWriter writer = B2ContentOutputStreamWriter.builder(helper).build(); try { writer.readContent(makeHeaderWithSha1(), in); fail("should've thrown!"); } catch (B2Exception e) { assertTrue(e.getMessage().startsWith("sha1 mismatch from destination.")); } helper.check( 1, 1, 0, 1 ); }