/** * Wraps the provided string into an InputStream in the default charset: * Charset.defaultCharset(). * * @param str String to wrap. * @return InputStream containing the string argument. * @deprecated it is always safer to supply a charset. See * {@link #wrapString(String, Charset)}. */ @Deprecated public static InputStream wrapString(String str) { return wrapString(str, Charset.defaultCharset()); }
/** * Wraps the provided string into an InputStream in the default charset: * Charset.defaultCharset(). * * @param str String to wrap. * @return InputStream containing the string argument. * @deprecated it is always safer to supply a charset. See * {@link #wrapString(String, Charset)}. */ @Deprecated public static InputStream wrapString(String str) { return wrapString(str, Charset.defaultCharset()); }
/** * Test method for * {@link com.google.api.ads.common.lib.utils.Streams#wrapString(String, Charset)}. */ @Test public void testWrapString_sameCharset() throws Exception { String testString = "Testing©ÿ»"; final InputStream inputStream = Streams.wrapString(testString, Charset.forName(UTF_8)); byte[] stringBytes = testString.getBytes(Charset.forName(UTF_8)); stringBytes = Arrays.copyOf(stringBytes, stringBytes.length); assertTrue("Streams should be equal", Arrays.equals(stringBytes, ByteStreams.toByteArray(inputStream))); }
/** * Test method for * {@link com.google.api.ads.common.lib.utils.Streams#wrapString(String, Charset)}. */ @Test public void testWrapString_wrongCharset() throws Exception { String testString = "Testing©ÿ»"; final InputStream inputStream = Streams.wrapString(testString, Charset.forName(UTF_8)); byte[] stringBytes = testString.getBytes(Charset.forName(ISO_8859_1)); stringBytes = Arrays.copyOf(stringBytes, stringBytes.length); assertFalse("Streams should not be equal", Arrays.equals(stringBytes, ByteStreams.toByteArray(inputStream))); }
/** * Test method for * {@link com.google.api.ads.common.lib.utils.Streams#wrapString(String)}. */ @SuppressWarnings({"javadoc", "deprecation"}) @Test public void testWrapString() throws Exception { String testString = "Testing©ÿ»"; final InputStream inputStream = Streams.wrapString(testString); byte[] stringBytes = testString.getBytes(Charset.defaultCharset()); stringBytes = Arrays.copyOf(stringBytes, stringBytes.length); assertTrue("Streams should be equal", Arrays.equals(stringBytes, ByteStreams.toByteArray(inputStream))); }
@Test public void testXPathExistsWithoutNamespaces() throws Exception { String xml = "<Header><requestId>123456</requestId></Header>"; Document document = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(Streams.wrapString(xml, StandardCharsets.UTF_8)); Node node = nodeExtractor.extractNode(document, Lists.newArrayList("Header", "requestId")); assertEquals( "Given an xpath in the XML, should return its node", "123456", node.getFirstChild().getNodeValue()); }
@Test public void testXPathEmpty() throws Exception { String xml = "<Header><requestId>123456</requestId></Header>"; Document document = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(Streams.wrapString(xml, StandardCharsets.UTF_8)); Node node = nodeExtractor.extractNode(document, Collections.<String>emptyList()); assertNull("Given an empty xpath elements list, should return null", node); } }
@Test public void testXPathExistsMultipleTimesWithoutNamespaces() throws Exception { String xml = "<Header><requestId>123456</requestId><requestId>78910</requestId></Header>"; Document document = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(Streams.wrapString(xml, StandardCharsets.UTF_8)); Node node = nodeExtractor.extractNode(document, Lists.newArrayList("Header", "requestId")); assertEquals( "Given an xpath in the XML, should return its node", "123456", node.getFirstChild().getNodeValue()); }
@Test public void testXPathDoesNotExist() throws Exception { String xml = "<Header><requestId>123456</requestId></Header>"; Document document = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(Streams.wrapString(xml, StandardCharsets.UTF_8)); Node node = nodeExtractor.extractNode(document, Lists.newArrayList("Foo")); assertNull("Given an xpath not in the XML, should return null", node); }
@Test public void testXPathExistsWithNamespaces() throws Exception { String xml = "<?xml version='1.0' encoding='UTF-8'?>" + "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" + " <soap:Header>" + " <ResponseHeader xmlns='https://adwords.google.com/api/adwords/cm/v209901'>" + " <requestId>123456</requestId>" + " <serviceName>BatchJobService</serviceName>" + " <methodName>mutate</methodName>" + " <operations>1</operations>" + " <responseTime>247</responseTime>" + " </ResponseHeader>" + " </soap:Header>" + "</soap:Envelope>"; DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); Document document = documentBuilderFactory .newDocumentBuilder() .parse(Streams.wrapString(xml, StandardCharsets.UTF_8)); Node node = nodeExtractor.extractNode( document, Lists.newArrayList("Envelope", "Header", "ResponseHeader", "requestId")); assertNotNull(node); assertEquals( "Given an xpath in the XML, should return its node", "123456", node.getFirstChild().getNodeValue()); }