XmlFeedReader { public static void read(InputStream inputStream, FeedClient feedClient, AtomicInteger numSent) throws Exception { SAXParserFactory parserFactory = SAXParserFactory.newInstance(); parserFactory.setFeature("http: parserFactory.setValidating(false); parserFactory.setNamespaceAware(false); SAXParser parser = parserFactory.newSAXParser(); SAXClientFeeder saxClientFeeder = new SAXClientFeeder(feedClient, numSent); InputSource inputSource = new InputSource(); inputSource.setEncoding(StandardCharsets.UTF_8.displayName()); inputSource.setByteStream(inputStream); parser.setProperty("http: parser.parse(inputSource, saxClientFeeder); } private  XmlFeedReader(); static void read(InputStream inputStream, FeedClient feedClient, AtomicInteger numSent);  }
@Test public void testReadUpdate() throws Exception { InputStream stream = new ByteArrayInputStream(updateDocUpdate.getBytes(StandardCharsets.UTF_8)); AtomicInteger numSent = new AtomicInteger(0); FeedClient feedClient = mock(FeedClient.class); XmlFeedReader.read(stream, feedClient, numSent); assertThat(numSent.get(), is(1)); }
@Test public void testReadRemove() throws Exception { InputStream stream = new ByteArrayInputStream(updateDocRemove.getBytes(StandardCharsets.UTF_8)); AtomicInteger numSent = new AtomicInteger(0); FeedClient feedClient = mock(FeedClient.class); XmlFeedReader.read(stream, feedClient, numSent); assertThat(numSent.get(), is(2)); }
@Test public void testInsert() throws Exception { InputStream stream = new ByteArrayInputStream(insertDocOperation.getBytes(StandardCharsets.UTF_8)); AtomicInteger numSent = new AtomicInteger(0); FeedClient feedClient = mock(FeedClient.class); XmlFeedReader.read(stream, feedClient, numSent); assertThat(numSent.get(), is(2)); }
@Test public void testNonDocument() throws Exception { InputStream stream = new ByteArrayInputStream(badperation.getBytes(StandardCharsets.UTF_8)); AtomicInteger numSent = new AtomicInteger(0); FeedClient feedClient = mock(FeedClient.class); XmlFeedReader.read(stream, feedClient, numSent); assertThat(numSent.get(), is(0)); }
@Test(expected=SAXParseException.class) public void testGarbage() throws Exception { InputStream stream = new ByteArrayInputStream("eehh".getBytes(StandardCharsets.UTF_8)); AtomicInteger numSent = new AtomicInteger(0); FeedClient feedClient = mock(FeedClient.class); XmlFeedReader.read(stream, feedClient, numSent); }
@Test public void testEncoding() throws Exception { InputStream stream = new ByteArrayInputStream("<?xml version=\"1.0\" encoding=\"utf8\"?><vespafeed><remove documentid=\"id:&amp;\"/></vespafeed>" .getBytes(StandardCharsets.UTF_8)); AtomicInteger numSent = new AtomicInteger(0); FeedClient feedClient = mock(FeedClient.class); doAnswer(new Answer<Object>() { public Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); String docId = (String) args[0]; CharSequence value = (CharSequence)args[1]; assertThat(value.toString(), is("<remove documentid=\"id:&amp;\"></remove>")); assertThat(docId, is("id:&")); return null; } }).when(feedClient).stream(anyString(), any()); XmlFeedReader.read(stream, feedClient, numSent); assertThat(numSent.get(), is(1)); }
@Test public void testCharacterEndcoding() throws Exception { InputStream stream = new ByteArrayInputStream(characterDocs.getBytes(StandardCharsets.UTF_8)); AtomicInteger numSent = new AtomicInteger(0); FeedClient feedClient = mock(FeedClient.class); final AtomicBoolean success = new AtomicBoolean(false); doAnswer(new Answer<Object>() { public Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); String docId = (String) args[0]; CharSequence value = (CharSequence)args[1]; assertThat(value.toString(), is( "<document documenttype=\"simple\" documentid=\"id:test::&amp;http: " <language><![CDATA[ja]]></language>\n" + " <title><![CDATA[test document1]]></title>\n" + " <description><![CDATA[Bjørnen' blåbær på øy nærheten.]]></description>\n" + " <date>1091356845</date>\n" + " <surl><![CDATA[http: " </document>")); success.set(true); return null; } }).when(feedClient).stream(anyString(), any()); XmlFeedReader.read(stream, feedClient, numSent); assertThat(numSent.get(), is(1)); assert(success.get()); }
@Test public void testRealData() throws Exception { InputStream inputStream = XmlFeedReaderTest.class.getResourceAsStream(feedResource); BufferedInputStream bis = new BufferedInputStream(inputStream); AtomicInteger numSent = new AtomicInteger(0); FeedClient feedClient = mock(FeedClient.class); XmlFeedReader.read(bis, feedClient, numSent); assertThat(numSent.get(), is(6)); }