### Question:
SegwitAddress extends Address { public static SegwitAddress fromBech32(@Nullable NetworkParameters params, String bech32) throws AddressFormatException { Bech32.Bech32Data bechData = Bech32.decode(bech32); if (params == null) { for (NetworkParameters p : Networks.get()) { if (bechData.hrp.equals(p.getSegwitAddressHrp())) return new SegwitAddress(p, bechData.data); } throw new AddressFormatException.InvalidPrefix("No network found for " + bech32); } else { if (bechData.hrp.equals(params.getSegwitAddressHrp())) return new SegwitAddress(params, bechData.data); throw new AddressFormatException.WrongNetwork(bechData.hrp); } } private  SegwitAddress(NetworkParameters params, int witnessVersion, byte[] witnessProgram); private  SegwitAddress(NetworkParameters params, byte[] data); int getWitnessVersion(); byte[] getWitnessProgram(); @Override byte[] getHash(); @Override Script.ScriptType getOutputScriptType(); @Override String toString(); static SegwitAddress fromBech32(@Nullable NetworkParameters params, String bech32); static SegwitAddress fromHash(NetworkParameters params, byte[] hash); static SegwitAddress fromKey(NetworkParameters params, ECKey key); String toBech32(); static final int WITNESS_PROGRAM_LENGTH_PKH; static final int WITNESS_PROGRAM_LENGTH_SH; static final int WITNESS_PROGRAM_MIN_LENGTH; static final int WITNESS_PROGRAM_MAX_LENGTH; }### Answer:
@Test public void invalidAddresses() { for (String invalid : INVALID_ADDRESSES) { try { SegwitAddress.fromBech32(null, invalid); fail(invalid); } catch (AddressFormatException x) { } } }
@Test(expected = AddressFormatException.InvalidDataLength.class) public void fromBech32_version0_invalidLength() { SegwitAddress.fromBech32(null, "QC1QR508D6QEJXTDG4Y5R3ZARVARYV9TJGHK"); }
@Test(expected = AddressFormatException.InvalidDataLength.class) public void fromBech32_tooShort() { SegwitAddress.fromBech32(null, "qc1rw5zrsf3y"); }
@Test(expected = AddressFormatException.InvalidDataLength.class) public void fromBech32_tooLong() { SegwitAddress.fromBech32(null, "qc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw56attdf"); }
@Test(expected = AddressFormatException.InvalidPrefix.class) public void fromBech32_invalidHrp() { SegwitAddress.fromBech32(null, "tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty"); }
@Test(expected = AddressFormatException.WrongNetwork.class) public void fromBech32_wrongNetwork() { SegwitAddress.fromBech32(TESTNET, "bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj"); }
@Test public void testJavaSerialization() throws Exception { SegwitAddress address = SegwitAddress.fromBech32(null, "QC1SW50QE9VY82"); ByteArrayOutputStream os = new ByteArrayOutputStream(); new ObjectOutputStream(os).writeObject(address); PrefixedChecksummedBytes addressCopy = (PrefixedChecksummedBytes) new ObjectInputStream( new ByteArrayInputStream(os.toByteArray())).readObject(); assertEquals(address, addressCopy); assertEquals(address.params, addressCopy.params); assertArrayEquals(address.bytes, addressCopy.bytes); }