Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blakemcbride committed Sep 13, 2024
1 parent 1722d17 commit db0e2b0
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/test/core/org/kissweb/URLBuilderTest.java
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("@#$%"));
}

}

64 changes: 64 additions & 0 deletions src/test/core/org/kissweb/XMLTest.java
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());
}
}

0 comments on commit db0e2b0

Please sign in to comment.