mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
Added JUnit tests for XMLTokenerTest
This commit is contained in:
parent
197afddbfb
commit
a729c2077a
81
src/test/java/org/json/junit/XMLTokenerTest.java
Normal file
81
src/test/java/org/json/junit/XMLTokenerTest.java
Normal file
@ -0,0 +1,81 @@
|
||||
package org.json.junit;
|
||||
|
||||
import org.json.XMLTokener;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Tests for JSON-Java XMLTokener.java
|
||||
*/
|
||||
public class XMLTokenerTest {
|
||||
|
||||
/**
|
||||
* Tests that nextCDATA() correctly extracts content from within a CDATA section.
|
||||
*/
|
||||
@Test
|
||||
public void testNextCDATA() {
|
||||
String xml = "This is <![CDATA[ some <CDATA> content ]]> after";
|
||||
XMLTokener tokener = new XMLTokener(new StringReader(xml));
|
||||
tokener.skipPast("<![CDATA[");
|
||||
String cdata = tokener.nextCDATA();
|
||||
assertEquals(" some <CDATA> content ", cdata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that nextContent() returns plain text content before a tag.
|
||||
*/
|
||||
@Test
|
||||
public void testNextContentWithText() {
|
||||
String xml = "Some content<nextTag>";
|
||||
XMLTokener tokener = new XMLTokener(xml);
|
||||
Object content = tokener.nextContent();
|
||||
assertEquals("Some content", content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that nextContent() returns '<' character when starting with a tag.
|
||||
*/
|
||||
@Test
|
||||
public void testNextContentWithTag() {
|
||||
String xml = "<tag>";
|
||||
XMLTokener tokener = new XMLTokener(xml);
|
||||
Object content = tokener.nextContent();
|
||||
assertEquals('<', content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that nextEntity() resolves a known entity like & correctly.
|
||||
*/
|
||||
@Test
|
||||
public void testNextEntityKnown() {
|
||||
XMLTokener tokener = new XMLTokener("amp;");
|
||||
Object result = tokener.nextEntity('&');
|
||||
assertEquals("&", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that nextEntity() preserves unknown entities by returning them unchanged.
|
||||
*/
|
||||
@Test
|
||||
public void testNextEntityUnknown() {
|
||||
XMLTokener tokener = new XMLTokener("unknown;");
|
||||
tokener.next(); // skip 'u'
|
||||
Object result = tokener.nextEntity('&');
|
||||
assertEquals("&nknown;", result); // malformed start to simulate unknown
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests skipPast() to ensure the cursor moves past the specified string.
|
||||
*/
|
||||
@Test
|
||||
public void testSkipPast() {
|
||||
String xml = "Ignore this... endHere more text";
|
||||
XMLTokener tokener = new XMLTokener(xml);
|
||||
tokener.skipPast("endHere");
|
||||
assertEquals(' ', tokener.next()); // should be the space after "endHere"
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user