/** * Handles unknown fields (required and not required) * * @throws FeathercoinURIParseException If something goes wrong */ @Test public void testUnknown() throws FeathercoinURIParseException { // Unknown not required field testObject = new FeathercoinURI( NetworkParameters.prodNet(), FeathercoinURI.LITECOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS + "?aardvark=true"); assertEquals( "FeathercoinURI['address'='LQz2pJYaeqntA9BFB8rDX5AL2TTKGd5AuN','aardvark'='true']", testObject.toString()); assertEquals("true", (String) testObject.getParameterByName("aardvark")); // Unknown not required field (isolated) try { testObject = new FeathercoinURI( NetworkParameters.prodNet(), FeathercoinURI.LITECOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS + "?aardvark"); fail("Expecting FeathercoinURIParseException"); } catch (FeathercoinURIParseException e) { assertTrue(e.getMessage().contains("cannot parse name value pair")); } // Unknown and required field try { testObject = new FeathercoinURI( NetworkParameters.prodNet(), FeathercoinURI.LITECOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS + "?req-aardvark=true"); fail("Expecting FeathercoinURIParseException"); } catch (FeathercoinURIParseException e) { assertTrue(e.getMessage().contains("req-aardvark")); } }
/** * Handles a simple amount * * @throws FeathercoinURIParseException If something goes wrong */ @Test public void testGood_Amount() throws FeathercoinURIParseException { // Test the decimal parsing testObject = new FeathercoinURI( NetworkParameters.prodNet(), FeathercoinURI.LITECOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS + "?amount=9876543210.12345678"); assertEquals("987654321012345678", testObject.getAmount().toString()); // Test the decimal parsing testObject = new FeathercoinURI( NetworkParameters.prodNet(), FeathercoinURI.LITECOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS + "?amount=.12345678"); assertEquals("12345678", testObject.getAmount().toString()); // Test the integer parsing testObject = new FeathercoinURI( NetworkParameters.prodNet(), FeathercoinURI.LITECOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS + "?amount=9876543210"); assertEquals("987654321000000000", testObject.getAmount().toString()); }
@Test public void brokenURIs() throws FeathercoinURIParseException { // Check we can parse the incorrectly formatted URIs produced by blockchain.info and its iPhone // app. String str = "feathercoin://1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH?amount=0.01000000"; FeathercoinURI uri = new FeathercoinURI(str); assertEquals("1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH", uri.getAddress().toString()); assertEquals(Utils.toNanoCoins(0, 1), uri.getAmount()); }
/** * Handles a simple label with an embedded ampersand and plus * * @throws FeathercoinURIParseException If something goes wrong * @throws UnsupportedEncodingException */ @Test public void testGood_LabelWithAmpersandAndPlus() throws FeathercoinURIParseException, UnsupportedEncodingException { String testString = "Hello Earth & Mars + Venus"; String encodedLabel = FeathercoinURI.encodeURLString(testString); testObject = new FeathercoinURI( NetworkParameters.prodNet(), FeathercoinURI.LITECOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS + "?label=" + encodedLabel); assertEquals(testString, testObject.getLabel()); }
/** * Test the simplest well-formed URI * * @throws FeathercoinURIParseException If something goes wrong */ @Test public void testGood_Simple() throws FeathercoinURIParseException { testObject = new FeathercoinURI( NetworkParameters.prodNet(), FeathercoinURI.LITECOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS); assertNotNull(testObject); assertNull("Unexpected amount", testObject.getAmount()); assertNull("Unexpected label", testObject.getLabel()); assertEquals("Unexpected label", 20, testObject.getAddress().getHash160().length); }
/** * Handles a Russian label (Unicode test) * * @throws FeathercoinURIParseException If something goes wrong * @throws UnsupportedEncodingException */ @Test public void testGood_LabelWithRussian() throws FeathercoinURIParseException, UnsupportedEncodingException { // Moscow in Russian in Cyrillic String moscowString = "\u041c\u043e\u0441\u043a\u0432\u0430"; String encodedLabel = FeathercoinURI.encodeURLString(moscowString); testObject = new FeathercoinURI( NetworkParameters.prodNet(), FeathercoinURI.LITECOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS + "?label=" + encodedLabel); assertEquals(moscowString, testObject.getLabel()); }
/** * Handles a simple message * * @throws FeathercoinURIParseException If something goes wrong */ @Test public void testGood_Message() throws FeathercoinURIParseException { testObject = new FeathercoinURI( NetworkParameters.prodNet(), FeathercoinURI.LITECOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS + "?message=Hello%20World"); assertEquals("Hello World", testObject.getMessage()); }
/** * Handles various well-formed combinations * * @throws FeathercoinURIParseException If something goes wrong */ @Test public void testGood_Combinations() throws FeathercoinURIParseException { testObject = new FeathercoinURI( NetworkParameters.prodNet(), FeathercoinURI.LITECOIN_SCHEME + ":" + PRODNET_GOOD_ADDRESS + "?amount=9876543210&label=Hello%20World&message=Be%20well"); assertEquals( "FeathercoinURI['address'='LQz2pJYaeqntA9BFB8rDX5AL2TTKGd5AuN','amount'='987654321000000000','label'='Hello World','message'='Be well']", testObject.toString()); }
/** * Tests conversion to Feathercoin URI * * @throws FeathercoinURIParseException If something goes wrong * @throws AddressFormatException */ @Test public void testConvertToFeathercoinURI() throws Exception { Address goodAddress = new Address(NetworkParameters.prodNet(), PRODNET_GOOD_ADDRESS); // simple example assertEquals( "feathercoin:" + PRODNET_GOOD_ADDRESS + "?amount=12.34&label=Hello&message=AMessage", FeathercoinURI.convertToFeathercoinURI( goodAddress, Utils.toNanoCoins("12.34"), "Hello", "AMessage")); // example with spaces, ampersand and plus assertEquals( "feathercoin:" + PRODNET_GOOD_ADDRESS + "?amount=12.34&label=Hello%20World&message=Mess%20%26%20age%20%2B%20hope", FeathercoinURI.convertToFeathercoinURI( goodAddress, Utils.toNanoCoins("12.34"), "Hello World", "Mess & age + hope")); // amount negative try { FeathercoinURI.convertToFeathercoinURI( goodAddress, Utils.toNanoCoins("-0.1"), "hope", "glory"); fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException e) { assertTrue(e.getMessage().contains("Amount must be positive")); } // no amount, label present, message present assertEquals( "feathercoin:" + PRODNET_GOOD_ADDRESS + "?label=Hello&message=glory", FeathercoinURI.convertToFeathercoinURI(goodAddress, null, "Hello", "glory")); // amount present, no label, message present assertEquals( "feathercoin:" + PRODNET_GOOD_ADDRESS + "?amount=0.1&message=glory", FeathercoinURI.convertToFeathercoinURI( goodAddress, Utils.toNanoCoins("0.1"), null, "glory")); assertEquals( "feathercoin:" + PRODNET_GOOD_ADDRESS + "?amount=0.1&message=glory", FeathercoinURI.convertToFeathercoinURI(goodAddress, Utils.toNanoCoins("0.1"), "", "glory")); // amount present, label present, no message assertEquals( "feathercoin:" + PRODNET_GOOD_ADDRESS + "?amount=12.34&label=Hello", FeathercoinURI.convertToFeathercoinURI( goodAddress, Utils.toNanoCoins("12.34"), "Hello", null)); assertEquals( "feathercoin:" + PRODNET_GOOD_ADDRESS + "?amount=12.34&label=Hello", FeathercoinURI.convertToFeathercoinURI( goodAddress, Utils.toNanoCoins("12.34"), "Hello", "")); // amount present, no label, no message assertEquals( "feathercoin:" + PRODNET_GOOD_ADDRESS + "?amount=1000", FeathercoinURI.convertToFeathercoinURI(goodAddress, Utils.toNanoCoins("1000"), null, null)); assertEquals( "feathercoin:" + PRODNET_GOOD_ADDRESS + "?amount=1000", FeathercoinURI.convertToFeathercoinURI(goodAddress, Utils.toNanoCoins("1000"), "", "")); // no amount, label present, no message assertEquals( "feathercoin:" + PRODNET_GOOD_ADDRESS + "?label=Hello", FeathercoinURI.convertToFeathercoinURI(goodAddress, null, "Hello", null)); // no amount, no label, message present assertEquals( "feathercoin:" + PRODNET_GOOD_ADDRESS + "?message=Agatha", FeathercoinURI.convertToFeathercoinURI(goodAddress, null, null, "Agatha")); assertEquals( "feathercoin:" + PRODNET_GOOD_ADDRESS + "?message=Agatha", FeathercoinURI.convertToFeathercoinURI(goodAddress, null, "", "Agatha")); // no amount, no label, no message assertEquals( "feathercoin:" + PRODNET_GOOD_ADDRESS, FeathercoinURI.convertToFeathercoinURI(goodAddress, null, null, null)); assertEquals( "feathercoin:" + PRODNET_GOOD_ADDRESS, FeathercoinURI.convertToFeathercoinURI(goodAddress, null, "", "")); }