Skip to content

Commit 43a0881

Browse files
committed
Test clean up; add a failing test for #442
1 parent 5e544db commit 43a0881

File tree

4 files changed

+82
-32
lines changed

4 files changed

+82
-32
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.fasterxml.jackson.dataformat.xml.failing;
2+
3+
import com.fasterxml.jackson.core.JsonToken;
4+
5+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
6+
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
7+
import com.fasterxml.jackson.dataformat.xml.deser.FromXmlParser;
8+
9+
public class XmlParser442Test extends XmlTestBase
10+
{
11+
private final XmlMapper MAPPER = newMapper();
12+
13+
// For [dataformat-xml#442]
14+
public void testMixedContentAfter() throws Exception
15+
{
16+
try (FromXmlParser xp = (FromXmlParser) MAPPER.createParser(
17+
"<root>\n" // START_OBJECT
18+
+" <branch>\n" // *Missing* START_OBJECT
19+
+" text\n"
20+
+" <leaf>stuff</leaf>\n"
21+
+" </branch>\n" // END_OBJECT
22+
+"</root>\n" // END_OBJECT
23+
24+
/*
25+
"<SomeXml>\n" // START_OBJECT
26+
+" <ParentElement>\n" // *Missing* START_OBJECT
27+
+" text\n"
28+
+" <ChildElement someAttribute=\"value\"/>\n" // START_OBJECT/END_OBJECT
29+
+" further text\n"
30+
+" </ParentElement>\n" // END_OBJECT
31+
+"</SomeXml>\n" // END_OBJECT
32+
*/
33+
)) {
34+
assertToken(JsonToken.START_OBJECT, xp.nextToken());
35+
assertToken(JsonToken.FIELD_NAME, xp.nextToken());
36+
assertEquals("branch", xp.currentName());
37+
38+
// Here's what we are missing:
39+
assertToken(JsonToken.START_OBJECT, xp.nextToken());
40+
assertToken(JsonToken.FIELD_NAME, xp.nextToken());
41+
assertEquals("", xp.currentName());
42+
43+
assertToken(JsonToken.VALUE_STRING, xp.nextToken());
44+
assertEquals("text", xp.getText().trim());
45+
46+
assertToken(JsonToken.FIELD_NAME, xp.nextToken());
47+
assertEquals("leaf", xp.currentName());
48+
assertToken(JsonToken.VALUE_STRING, xp.nextToken());
49+
assertEquals("stuff", xp.getText().trim());
50+
51+
assertToken(JsonToken.END_OBJECT, xp.nextToken());
52+
assertToken(JsonToken.END_OBJECT, xp.nextToken());
53+
54+
assertNull(xp.nextToken());
55+
}
56+
}
57+
}

src/test/java/com/fasterxml/jackson/dataformat/xml/node/JsonNodeMixedContent403Test.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fasterxml.jackson.dataformat.xml.node;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
34
import com.fasterxml.jackson.databind.ObjectMapper;
45
import com.fasterxml.jackson.databind.json.JsonMapper;
56
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
@@ -46,8 +47,8 @@ public void testMixed226() throws Exception
4647
+"<a>mixed1 <b>leaf</b>"
4748
+" mixed2</a>\n"
4849
+"</root>";
49-
assertEquals(JSON_MAPPER.readTree(
50-
a2q("{'a':{'':['mixed1 ',' mixed2'],'b':'leaf'}}")),
51-
XML_MAPPER.readTree(XML));
50+
JsonNode fromJson = JSON_MAPPER.readTree(
51+
a2q("{'a':{'':['mixed1 ',' mixed2'],'b':'leaf'}}"));
52+
assertEquals(fromJson, XML_MAPPER.readTree(XML));
5253
}
5354
}

src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlParserTest.java

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,14 @@
1414

1515
public class XmlParserTest extends XmlTestBase
1616
{
17-
protected JsonFactory _jsonFactory;
18-
protected XmlFactory _xmlFactory;
19-
protected XmlMapper _xmlMapper;
20-
21-
// let's actually reuse XmlMapper to make things bit faster
22-
@Override
23-
public void setUp() throws Exception {
24-
super.setUp();
25-
_jsonFactory = new JsonFactory();
26-
_xmlFactory = new XmlFactory();
27-
_xmlMapper = new XmlMapper();
28-
}
17+
protected final JsonFactory _jsonFactory = new JsonFactory();
18+
protected final XmlMapper _xmlMapper = newMapper();
19+
protected XmlFactory _xmlFactory = _xmlMapper.getFactory();
2920

3021
/*
31-
/**********************************************************
22+
/**********************************************************************
3223
/* Unit tests, simplest/manual
33-
/**********************************************************
24+
/**********************************************************************
3425
*/
3526

3627
public void testSimplest() throws Exception
@@ -119,11 +110,11 @@ public void testRootMixed() throws Exception
119110
assertNull(p.nextToken());
120111
}
121112
}
122-
113+
123114
/*
124-
/**********************************************************
115+
/**********************************************************************
125116
/* Unit tests, slightly bigger, automated
126-
/**********************************************************
117+
/**********************************************************************
127118
*/
128119

129120
public void testSimpleNested() throws Exception
@@ -370,9 +361,9 @@ public void testInferredNumbers() throws Exception
370361
}
371362

372363
/*
373-
/**********************************************************
364+
/**********************************************************************
374365
/* Helper methods
375-
/**********************************************************
366+
/**********************************************************************
376367
*/
377368

378369
private String _readXmlWriteJson(String xml) throws IOException
@@ -383,14 +374,13 @@ private String _readXmlWriteJson(String xml) throws IOException
383374
private String _readXmlWriteJson(XmlFactory xmlFactory, String xml) throws IOException
384375
{
385376
StringWriter w = new StringWriter();
386-
387-
JsonParser p = xmlFactory.createParser(xml);
388-
JsonGenerator jg = _jsonFactory.createGenerator(w);
389-
while (p.nextToken() != null) {
390-
jg.copyCurrentEvent(p);
377+
try (JsonParser p = xmlFactory.createParser(xml)) {
378+
try (JsonGenerator jg = _jsonFactory.createGenerator(w)) {
379+
while (p.nextToken() != null) {
380+
jg.copyCurrentEvent(p);
381+
}
382+
}
391383
}
392-
p.close();
393-
jg.close();
394384
return w.toString();
395385
}
396386
}

src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlTokenStreamTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import javax.xml.stream.*;
66

77
import com.fasterxml.jackson.core.io.ContentReference;
8+
9+
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
810
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
911
import com.fasterxml.jackson.dataformat.xml.deser.FromXmlParser;
1012
import com.fasterxml.jackson.dataformat.xml.deser.XmlTokenStream;
1113

1214
public class XmlTokenStreamTest extends XmlTestBase
1315
{
14-
private final XMLInputFactory _staxInputFactory = XMLInputFactory.newInstance();
16+
private final XmlFactory XML_FACTORY = newMapper().getFactory();
1517

1618
public void testSimple() throws Exception
1719
{
@@ -170,10 +172,10 @@ public void testMixedContentAfter() throws Exception
170172
private XmlTokenStream _tokensFor(String doc) throws Exception {
171173
return _tokensFor(doc, FromXmlParser.Feature.collectDefaults());
172174
}
173-
175+
174176
private XmlTokenStream _tokensFor(String doc, int flags) throws Exception
175177
{
176-
XMLStreamReader sr = _staxInputFactory.createXMLStreamReader(new StringReader(doc));
178+
XMLStreamReader sr = XML_FACTORY.getXMLInputFactory().createXMLStreamReader(new StringReader(doc));
177179
// must point to START_ELEMENT, so:
178180
sr.nextTag();
179181
XmlTokenStream stream = new XmlTokenStream(sr, ContentReference.rawReference(doc), flags);

0 commit comments

Comments
 (0)