-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1722d17
commit db0e2b0
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.kissweb; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class URLBuilderTest { | ||
|
||
@Test | ||
public void testBuildURLWithoutParameters() { | ||
URLBuilder builder = new URLBuilder("http://example.com"); | ||
assertEquals("http://example.com", builder.build()); | ||
} | ||
|
||
@Test | ||
public void testBuildURLWithSingleParameter() { | ||
URLBuilder builder = new URLBuilder("http://example.com"); | ||
builder.addParameter("key", "value"); | ||
assertEquals("http://example.com?key=value", builder.build()); | ||
} | ||
|
||
@Test | ||
public void testBuildURLWithMultipleParameters() { | ||
URLBuilder builder = new URLBuilder("http://example.com"); | ||
builder.addParameter("key1", "value1") | ||
.addParameter("key2", "value2"); | ||
assertEquals("http://example.com?key1=value1&key2=value2", builder.build()); | ||
} | ||
|
||
@Test | ||
public void testBuildURLWithSpaceInURL() { | ||
URLBuilder builder = new URLBuilder("http://example.com/search query"); | ||
assertEquals("http://example.com/search%20query", builder.build()); | ||
} | ||
|
||
@Test | ||
public void testAddParameterWithSpace() { | ||
URLBuilder builder = new URLBuilder("http://example.com"); | ||
builder.addParameter("search", "a b c"); | ||
assertEquals("http://example.com?search=a+b+c", builder.build()); | ||
} | ||
|
||
@Test | ||
public void testAddParameterWithSpecialCharacters() { | ||
URLBuilder builder = new URLBuilder("http://example.com"); | ||
builder.addParameter("name", "John Doe & Sons"); | ||
assertEquals("http://example.com?name=John+Doe+%26+Sons", builder.build()); | ||
} | ||
|
||
@Test | ||
public void testEncodeURLString() { | ||
assertEquals("Hello+World", URLBuilder.encodeURLString("Hello World")); | ||
assertEquals("a%2Fb%2Fc", URLBuilder.encodeURLString("a/b/c")); | ||
assertEquals("%40%23%24%25", URLBuilder.encodeURLString("@#$%")); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.kissweb; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.w3c.dom.Document; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class XMLTest { | ||
|
||
@Test | ||
public void testParseValidXML() { | ||
String xml = "<root><child>Content</child></root>"; | ||
Document doc = XML.parse(xml); | ||
assertNotNull(doc); | ||
assertEquals("root", doc.getDocumentElement().getNodeName()); | ||
} | ||
|
||
@Test | ||
public void testParseInvalidXMLThrowsException() { | ||
String invalidXml = "<root><child>Content</child>"; | ||
Exception exception = assertThrows(RuntimeException.class, () -> { | ||
XML.parse(invalidXml); | ||
}); | ||
assertNotNull(exception); | ||
} | ||
|
||
@Test | ||
public void testFormatXMLDocument() { | ||
String xml = "<root><child>Content</child></root>"; | ||
Document doc = XML.parse(xml); | ||
String formattedXML = XML.format(doc); | ||
|
||
String expectedFormattedXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" | ||
+ "<root>\n" | ||
+ " <child>Content</child>\n" | ||
+ "</root>\n"; | ||
assertEquals(expectedFormattedXML.trim(), formattedXML.trim()); | ||
} | ||
|
||
@Test | ||
public void testFormatXMLString() { | ||
String unformattedXML = "<root><child>Content</child></root>"; | ||
String formattedXML = XML.format(unformattedXML); | ||
|
||
String expectedFormattedXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" | ||
+ "<root>\n" | ||
+ " <child>Content</child>\n" | ||
+ "</root>\n"; | ||
assertEquals(expectedFormattedXML.trim(), formattedXML.trim()); | ||
} | ||
|
||
@Test | ||
public void testFormatComplexXMLString() { | ||
String unformattedXML = "<root><child1>Content1</child1><child2>Content2</child2></root>"; | ||
String formattedXML = XML.format(unformattedXML); | ||
|
||
String expectedFormattedXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" | ||
+ "<root>\n" | ||
+ " <child1>Content1</child1>\n" | ||
+ " <child2>Content2</child2>\n" | ||
+ "</root>\n"; | ||
assertEquals(expectedFormattedXML.trim(), formattedXML.trim()); | ||
} | ||
} | ||
|