Skip to content

Commit

Permalink
[Test] Added example with 2 CDATA elements in RecordParserTest
Browse files Browse the repository at this point in the history
[Fix] Added additional while within ListSetParser so it doesn't stop reading data after the first CDATA element is found
  • Loading branch information
mmalmeida committed Jul 31, 2014
1 parent 7399c6b commit c7f118d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.lyncode.xml.matchers.QNameMatchers.localPart;
import static com.lyncode.xml.matchers.XmlEventMatchers.aStartElement;
import static com.lyncode.xml.matchers.XmlEventMatchers.anEndElement;
import static com.lyncode.xml.matchers.XmlEventMatchers.elementName;
import static com.lyncode.xml.matchers.XmlEventMatchers.text;
import static com.lyncode.xml.matchers.XmlEventMatchers.theEndOfDocument;
Expand Down Expand Up @@ -86,11 +87,14 @@ private Set parseSet() throws XmlReaderException {
reader.next(aStartElement());
QName elementName = reader.getName();
reader.next(text());

String extractedText = reader.getText();
while(reader.next(anEndElement(),text()).current(text())){
extractedText += reader.getText();
}
if(elementName.getLocalPart().equals("setName")) {
setName = reader.getText();
setName = extractedText;
} else if(elementName.getLocalPart().equals("setSpec")) {
setSpec = reader.getText();
setSpec = extractedText;
}
}
set.withName(setName);
Expand All @@ -106,6 +110,9 @@ private Matcher<XMLEvent> errorElement() {
private Matcher<XMLEvent> setElement() {
return allOf(aStartElement(), elementName(localPart(equalTo("set"))));
}
private Matcher<XMLEvent> endSetElement() {
return allOf(anEndElement(), elementName(localPart(equalTo("set"))));
}

/**
* Parses its xml completely
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,20 @@ public void cdataIsParsed(){
OAI_DC_SETS_CDATA_XML);

List<Set> sets = parseXML(inputStream);
assertEquals("setOne",sets.get(0).getSpec());
assertEquals("cdataSPEC",sets.get(0).getSpec());
assertEquals("Set with CDATA",sets.get(0).getName());

}


@Test
public void multipleCDATAParsed(){
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(
OAI_DC_SETS_CDATA_XML);

List<Set> sets = parseXML(inputStream);
assertEquals(2,sets.size());
assertEquals("First CDATA, Set 2 with CDATA",sets.get(1).getName());
}


private List<Set> parseXML(InputStream inputStream){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void cdataParsing() throws Exception {
XmlReader reader = new XmlReader(input);
Record record = parser.parse(reader);
assertEquals(1,record.getMetadata().getValue().searcher().findAll("dc.title").size());
assertEquals("Article Title",record.getMetadata().getValue().searcher().findOne("dc.title"));
assertEquals("Article Title-additional CDATA",record.getMetadata().getValue().searcher().findOne("dc.title"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/
http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
<dc:title><![CDATA[Article Title]]></dc:title>
<dc:title><![CDATA[Article Title]]><![CDATA[-additional CDATA]]></dc:title>
</oai_dc:dc>
</metadata>
</record>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
<ListSets>

<set>
<setSpec>setOne</setSpec>
<setSpec><![CDATA[cdataSPEC]]></setSpec>
<setName><![CDATA[Set with CDATA]]></setName>
</set>
<set>
<setSpec>setTwo</setSpec>
<setName><![CDATA[First CDATA, ]]><![CDATA[Set 2 with CDATA]]><![CDATA[]]></setName>
<setName><![CDATA[First CDATA, ]]><![CDATA[Set 2 with CDATA]]></setName>
</set>
</ListSets>
</OAI-PMH>

0 comments on commit c7f118d

Please sign in to comment.