Skip to content

Commit 1eed44a

Browse files
authored
Merge pull request #993 from surajdm123/add-tests-2
Added JUnit tests for XMLTokenerTest
2 parents 7eccade + a729c20 commit 1eed44a

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.json.junit;
2+
3+
import org.json.XMLTokener;
4+
import org.junit.Test;
5+
6+
import java.io.StringReader;
7+
8+
import static org.junit.Assert.*;
9+
10+
/**
11+
* Tests for JSON-Java XMLTokener.java
12+
*/
13+
public class XMLTokenerTest {
14+
15+
/**
16+
* Tests that nextCDATA() correctly extracts content from within a CDATA section.
17+
*/
18+
@Test
19+
public void testNextCDATA() {
20+
String xml = "This is <![CDATA[ some <CDATA> content ]]> after";
21+
XMLTokener tokener = new XMLTokener(new StringReader(xml));
22+
tokener.skipPast("<![CDATA[");
23+
String cdata = tokener.nextCDATA();
24+
assertEquals(" some <CDATA> content ", cdata);
25+
}
26+
27+
/**
28+
* Tests that nextContent() returns plain text content before a tag.
29+
*/
30+
@Test
31+
public void testNextContentWithText() {
32+
String xml = "Some content<nextTag>";
33+
XMLTokener tokener = new XMLTokener(xml);
34+
Object content = tokener.nextContent();
35+
assertEquals("Some content", content);
36+
}
37+
38+
/**
39+
* Tests that nextContent() returns '<' character when starting with a tag.
40+
*/
41+
@Test
42+
public void testNextContentWithTag() {
43+
String xml = "<tag>";
44+
XMLTokener tokener = new XMLTokener(xml);
45+
Object content = tokener.nextContent();
46+
assertEquals('<', content);
47+
}
48+
49+
/**
50+
* Tests that nextEntity() resolves a known entity like &amp; correctly.
51+
*/
52+
@Test
53+
public void testNextEntityKnown() {
54+
XMLTokener tokener = new XMLTokener("amp;");
55+
Object result = tokener.nextEntity('&');
56+
assertEquals("&", result);
57+
}
58+
59+
/**
60+
* Tests that nextEntity() preserves unknown entities by returning them unchanged.
61+
*/
62+
@Test
63+
public void testNextEntityUnknown() {
64+
XMLTokener tokener = new XMLTokener("unknown;");
65+
tokener.next(); // skip 'u'
66+
Object result = tokener.nextEntity('&');
67+
assertEquals("&nknown;", result); // malformed start to simulate unknown
68+
}
69+
70+
/**
71+
* Tests skipPast() to ensure the cursor moves past the specified string.
72+
*/
73+
@Test
74+
public void testSkipPast() {
75+
String xml = "Ignore this... endHere more text";
76+
XMLTokener tokener = new XMLTokener(xml);
77+
tokener.skipPast("endHere");
78+
assertEquals(' ', tokener.next()); // should be the space after "endHere"
79+
}
80+
81+
}

0 commit comments

Comments
 (0)