Skip to content

Commit

Permalink
Update Atom Tests, fix Checkstyles (googleapis#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
geri-m committed Oct 14, 2018
1 parent 606717a commit a12b274
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,26 +211,39 @@ public boolean stopAfterEndTag(String namespace, String localName) {
* @param customizeParser optional parser customizer or {@code null} for none
*/
public static void parseElement(XmlPullParser parser, Object destination,
XmlNamespaceDictionary namespaceDictionary, CustomizeParser customizeParser)
XmlNamespaceDictionary namespaceDictionary,
CustomizeParser customizeParser)
throws IOException, XmlPullParserException {
ArrayList<Type> context = new ArrayList<Type>();
if (destination != null) {
context.add(destination.getClass());
}
parseElementInternal(parser, context, destination, null, namespaceDictionary, customizeParser);
parseElementInternal(parser, context, destination, null, namespaceDictionary,
customizeParser);
}

/**
* Returns whether the customize parser has requested to stop or reached end of document.
* Otherwise, identical to
* {@link #parseElement(XmlPullParser, Object, XmlNamespaceDictionary, CustomizeParser)} .
* {@link #parseElement(XmlPullParser, Object, XmlNamespaceDictionary, CustomizeParser)}.
*
* @param parser
* @param context
* @param destination this is the list of objects, that the XML will be parsed into
* @param valueType this the most recent/root element of where the XML is going to mapped
* @param namespaceDictionary
* @param customizeParser
* @return
* @throws IOException
* @throws XmlPullParserException
*/
private static boolean parseElementInternal(XmlPullParser parser,
ArrayList<Type> context, // this is the list of objects, that the XML will be parsed into
Object destination, // this the most recent/root element of where the XML is going to mapped
ArrayList<Type> context,
Object destination,
Type valueType,
XmlNamespaceDictionary namespaceDictionary,
CustomizeParser customizeParser) throws IOException, XmlPullParserException {
CustomizeParser customizeParser)
throws IOException, XmlPullParserException {
// TODO(yanivi): method is too long; needs to be broken down into smaller methods and comment
// better

Expand Down Expand Up @@ -282,6 +295,9 @@ private static boolean parseElementInternal(XmlPullParser parser,
}
Field field;
ArrayValueMap arrayValueMap = new ArrayValueMap(destination);

// is stopped is required for the ATOM Parser, just in case the parsing isStopped
// during parsing at some time.
boolean isStopped = false;
// TODO(yanivi): support Void type as "ignore" element/attribute
main: while (true) {
Expand Down Expand Up @@ -317,6 +333,7 @@ private static boolean parseElementInternal(XmlPullParser parser,
&& customizeParser.stopBeforeStartTag(parser.getNamespace(), parser.getName())) {
isStopped = true;
breakFromMain = true;
break;
}
// not sure how the case looks like, when this happens.
if (destination == null) {
Expand Down Expand Up @@ -409,9 +426,6 @@ private static boolean parseElementInternal(XmlPullParser parser,
} // end -- main: while (true)
arrayValueMap.setValues();

if (isStopped) {
throw new RuntimeException("Return Value is TRUE. Really?");
}
return isStopped;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@
*/
public class AtomTest {


private static final String SAMPLE_FEED = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed xmlns=\"http://www.w3.org/2005/Atom\"> <title>Example Feed</title> <link " +
"href=\"http://example.org/\"/> <updated>2003-12-13T18:31:02Z</updated> <author> <name>John Doe</name> </author> " +
"<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id> <entry> <title>Atom-Powered Robots Run Amok</title> <link href=\"http://example.org/2003/12/13/atom03\"/> " +
" <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> <updated>2003-12-13T18:30:02Z</updated> <summary>Some text.</summary> </entry>" +
"<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id> " +
"<entry> <title>Atom-Powered Robots Run Amok</title> <link href=\"http://example.org/2003/12/13/atom03\"/> " +
"<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> <updated>2003-12-13T18:30:02Z</updated> <summary>Some text.</summary> </entry>" +
"<entry> <title>Atom-Powered Robots Run Amok!</title> <link href=\"http://example.org/2003/12/13/atom02\"/> " +
"<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa62</id> <updated>2003-12-13T18:32:02Z</updated> <summary>Some other text.</summary> </entry>" +
"</feed>";

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -78,23 +82,46 @@ public void testAtomFeedParser() throws XmlPullParserException, IOException {
parser.setInput(new StringReader(SAMPLE_FEED));
InputStream stream = new ByteArrayInputStream(SAMPLE_FEED.getBytes());
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
AbstractAtomFeedParser atomParser = new AtomFeedParser<Feed, FeedEntry>(namespaceDictionary, parser, stream, Feed.class, FeedEntry.class);
Feed feed = (Feed)atomParser.parseFeed();
assertNotNull(feed.entry);
assertEquals(1, feed.entry.length);
assertEquals("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a", feed.entry[0].id);
assertEquals("2003-12-13T18:30:02Z", feed.entry[0].updated);
assertEquals("Some text.", feed.entry[0].summary);
assertEquals("Atom-Powered Robots Run Amok", feed.entry[0].title);
assertEquals("http://example.org/2003/12/13/atom03", feed.entry[0].link.href);
AbstractAtomFeedParser atomParser = new AtomFeedParser<Feed, FeedEntry>(namespaceDictionary,
parser, stream, Feed.class, FeedEntry.class);

Feed feed = (Feed) atomParser.parseFeed();

assertEquals("John Doe", feed.author.name);
assertEquals("urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6", feed.id);
assertEquals("2003-12-13T18:31:02Z", feed.updated);
assertEquals("Example Feed", feed.title);
assertEquals("http://example.org/", feed.link.href);

Object obj = atomParser.parseNextEntry();
assertNull(obj);
FeedEntry entry1 = (FeedEntry)atomParser.parseNextEntry();
//assertNotNull(feed.entry);
assertEquals("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a", entry1.id);
assertEquals("2003-12-13T18:30:02Z", entry1.updated);
assertEquals("Some text.", entry1.summary);
assertEquals("Atom-Powered Robots Run Amok", entry1.title);
assertEquals("http://example.org/2003/12/13/atom03", entry1.link.href);

FeedEntry entry2 = (FeedEntry)atomParser.parseNextEntry();
assertEquals("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa62", entry2.id);
assertEquals("2003-12-13T18:32:02Z", entry2.updated);
assertEquals("Some other text.", entry2.summary);
assertEquals("Atom-Powered Robots Run Amok!", entry2.title);
assertEquals("http://example.org/2003/12/13/atom02", entry2.link.href);
FeedEntry entry3 = (FeedEntry)atomParser.parseNextEntry();

assertNull(entry3);

}


@Test
public void testAtomFeedParserManual() throws XmlPullParserException, IOException {
Feed xml = new Feed();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(SAMPLE_FEED));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
assertNotNull(xml);
}

@SuppressWarnings("unchecked")
Expand All @@ -107,10 +134,16 @@ public void testHeiseFeedParser() throws IOException, XmlPullParserException {
Reader atomReader = new FileReader(atomFile);
parser.setInput(atomReader);
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
AbstractAtomFeedParser atomParser = new AtomFeedParser<Feed, FeedEntry>(namespaceDictionary, parser, stream, Feed.class, FeedEntry.class);
AbstractAtomFeedParser atomParser = new AtomFeedParser<Feed, FeedEntry>(namespaceDictionary,
parser, stream, Feed.class, FeedEntry.class);
Feed feed = (Feed)atomParser.parseFeed();
assertNotNull(feed);
assertEquals(1, feed.entry.length);

int counter = 0;
FeedEntry entry = (FeedEntry)atomParser.parseNextEntry();
assertNotNull(entry);

// TODO: Iterative over Entries.
atomReader.close();
stream.close();
}
Expand Down

0 comments on commit a12b274

Please sign in to comment.