[Jsptest-svn-commits] SF.net SVN: jsptest: [224] trunk
Status: Alpha
Brought to you by:
lkoskela
From: <lko...@us...> - 2008-04-24 17:12:25
|
Revision: 224 http://jsptest.svn.sourceforge.net/jsptest/?rev=224&view=rev Author: lkoskela Date: 2008-04-24 10:12:17 -0700 (Thu, 24 Apr 2008) Log Message: ----------- Replaced JDK 1.5 API calls with JDK 1.4 compliant implementations Modified Paths: -------------- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/Path.java trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/XML.java trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/DOMAssertion.java trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/TestHtmlTestCaseElementAssertions.java trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperCompiler.java trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/MockHttpServletRequest.java Added Paths: ----------- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/Strings.java trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/StringsTest.java trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/XMLTest.java Modified: trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/Path.java =================================================================== --- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/Path.java 2008-04-16 20:12:52 UTC (rev 223) +++ trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/Path.java 2008-04-24 17:12:17 UTC (rev 224) @@ -73,7 +73,7 @@ String prefix = "jar:file:"; if (url.startsWith(prefix)) { String file = url.substring(prefix.length()); - if (file.contains("!")) { + if (file.indexOf("!") > -1) { file = file.substring(0, file.indexOf('!')); } add(new File(file)); Added: trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/Strings.java =================================================================== --- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/Strings.java (rev 0) +++ trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/Strings.java 2008-04-24 17:12:17 UTC (rev 224) @@ -0,0 +1,18 @@ +package net.sf.jsptest.utils; + +public class Strings { + + public static String replace(String fromWhere, String what, String withWhat) { + if (what.length() > 0) { + int indexOfWhat = fromWhere.indexOf(what); + if (indexOfWhat != -1) { + String beforeMatch = fromWhere.substring(0, indexOfWhat); + String afterMatch = fromWhere.substring(indexOfWhat + + what.length()); + fromWhere = beforeMatch + withWhat + + replace(afterMatch, what, withWhat); + } + } + return fromWhere; + } +} Modified: trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/XML.java =================================================================== --- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/XML.java 2008-04-16 20:12:52 UTC (rev 223) +++ trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/utils/XML.java 2008-04-24 17:12:17 UTC (rev 224) @@ -10,23 +10,39 @@ import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import org.w3c.dom.Element; import org.w3c.dom.Node; +import org.w3c.dom.NodeList; public class XML { - private static final String APACHE_INDENTATION = "{http://xml.apache.org/xslt}indent-amount"; + private static final String APACHE_INDENTATION = "{http://xml.apache.org/xslt}indent-amount"; - public static String toString(Node xml) { - try { - TransformerFactory f = TransformerFactory.newInstance(); - Transformer t = f.newTransformer(); - t.setOutputProperty(OutputKeys.INDENT, "yes"); - t.setOutputProperty(APACHE_INDENTATION, "2"); - OutputStream out = new ByteArrayOutputStream(); - t.transform(new DOMSource(xml), new StreamResult(out)); - return out.toString(); - } catch (TransformerException e) { - throw new RuntimeException(e); - } - } + public static String toString(Node xml) { + try { + TransformerFactory f = TransformerFactory.newInstance(); + Transformer t = f.newTransformer(); + t.setOutputProperty(OutputKeys.INDENT, "yes"); + t.setOutputProperty(APACHE_INDENTATION, "2"); + OutputStream out = new ByteArrayOutputStream(); + t.transform(new DOMSource(xml), new StreamResult(out)); + return out.toString(); + } catch (TransformerException e) { + throw new RuntimeException(e); + } + } + + public static String textContentOf(Element element) { + StringBuffer textContent = new StringBuffer(100); + NodeList children = element.getChildNodes(); + for (int i = 0; i < children.getLength(); i++) { + Node child = children.item(i); + if (child.getNodeType() == Node.ELEMENT_NODE) { + textContent.append(textContentOf((Element) child)); + } else { + textContent.append(child.getNodeValue().trim()); + } + } + return textContent.toString(); + } } Added: trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/StringsTest.java =================================================================== --- trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/StringsTest.java (rev 0) +++ trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/StringsTest.java 2008-04-24 17:12:17 UTC (rev 224) @@ -0,0 +1,35 @@ +package net.sf.jsptest.utils; + +import junit.framework.TestCase; + +public class StringsTest extends TestCase { + + public void testReplacingNonexistentNeedle() throws Exception { + assertEquals("no such thing here", Strings.replace( + "no such thing here", "needle", "foo")); + } + + public void testReplacingOneInstance() throws Exception { + assertEquals("Macs are cool", Strings.replace("what are cool", "what", + "Macs")); + } + + public void testReplacingMultipleInstances() throws Exception { + assertEquals("Macs and Macs are cool", Strings.replace( + "what and what are cool", "what", "Macs")); + } + + public void testReplacingWithTheSameValue() throws Exception { + assertEquals("abcabcabc", Strings.replace("abcabcabc", "bc", "bc")); + } + + public void testReplacingWithSomethingThatContainsTheSameValue() + throws Exception { + assertEquals("a_bc_a_bc_a_bc_", Strings.replace("abcabcabc", "bc", + "_bc_")); + } + + public void testReplacingEmptyStrings() throws Exception { + assertEquals("", Strings.replace("", "", "")); + } +} Added: trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/XMLTest.java =================================================================== --- trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/XMLTest.java (rev 0) +++ trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/utils/XMLTest.java 2008-04-24 17:12:17 UTC (rev 224) @@ -0,0 +1,43 @@ +package net.sf.jsptest.utils; + +import java.io.StringReader; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import junit.framework.TestCase; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.InputSource; + +public class XMLTest extends TestCase { + + public void testCollectingTextContentForEmptyElement() throws Exception { + Element element = parse("<root></root>"); + assertEquals("", XML.textContentOf(element)); + } + + public void testCollectingTextContentForNonEmptyElement() throws Exception { + Element element = parse("<parent>text</parent>"); + assertEquals("text", XML.textContentOf(element)); + } + + public void testCollectingTextContentElementWithChildren() throws Exception { + Element element = parse("<parent>parent<child>child</child></parent>"); + assertEquals("parentchild", XML.textContentOf(element)); + } + + public void testWhitespaceBetweenTextNodesIsRemoved() throws Exception { + Element element = parse("<parent>\n parent\n <child>\n child\n </child>\n</parent>"); + assertEquals("parentchild", XML.textContentOf(element)); + } + + private Element parse(String xmlSnippet) throws Exception { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder parser = dbf.newDocumentBuilder(); + String xml = "<?xml version='1.0'?>\n" + xmlSnippet; + Document doc = parser.parse(new InputSource(new StringReader(xml))); + return doc.getDocumentElement(); + } +} Modified: trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/DOMAssertion.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/DOMAssertion.java 2008-04-16 20:12:52 UTC (rev 223) +++ trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/DOMAssertion.java 2008-04-24 17:12:17 UTC (rev 224) @@ -32,7 +32,7 @@ * The (partial) content that should be found. */ public void shouldContain(String text) { - assertContains(context.getTextContent(), text); + assertContains(XML.textContentOf(context), text); } /** @@ -42,7 +42,7 @@ * The (partial) content that should not be found. */ public void shouldNotContain(String text) { - assertDoesNotContain(context.getTextContent(), text); + assertDoesNotContain(XML.textContentOf(context), text); } /** Modified: trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/TestHtmlTestCaseElementAssertions.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/TestHtmlTestCaseElementAssertions.java 2008-04-16 20:12:52 UTC (rev 223) +++ trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/TestHtmlTestCaseElementAssertions.java 2008-04-24 17:12:17 UTC (rev 224) @@ -18,6 +18,8 @@ import junit.framework.AssertionFailedError; +import net.sf.jsptest.utils.XML; + import org.w3c.dom.Element; /** @@ -102,6 +104,6 @@ Element element = testcase.element("//P[@CLASS='p2']") .getElement(); assertNotNull(element); - assertEquals("this is p2", element.getTextContent()); + assertEquals("this is p2", XML.textContentOf(element)); } } Modified: trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperCompiler.java =================================================================== --- trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperCompiler.java 2008-04-16 20:12:52 UTC (rev 223) +++ trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperCompiler.java 2008-04-24 17:12:17 UTC (rev 224) @@ -39,6 +39,7 @@ import net.sf.jsptest.compiler.jsp20.mock.MockServletConfig; import net.sf.jsptest.compiler.jsp20.mock.MockTagInfo; import net.sf.jsptest.utils.Path; +import net.sf.jsptest.utils.Strings; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -376,7 +377,7 @@ } private void resolveClassFileLocation(JspCompilationInfo info) { - String file = info.getJavaSource().replace(".java", ".class"); + String file = Strings.replace(info.getJavaSource(), ".java", ".class"); info.setClassFile(file); } Modified: trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/MockHttpServletRequest.java =================================================================== --- trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/MockHttpServletRequest.java 2008-04-16 20:12:52 UTC (rev 223) +++ trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/MockHttpServletRequest.java 2008-04-24 17:12:17 UTC (rev 224) @@ -318,7 +318,7 @@ } public String toString() { - StringBuilder buffer = new StringBuilder(); + StringBuffer buffer = new StringBuffer(); buffer.append(getClass().getName()).append("{"); Enumeration enumeration = getParameterNames(); while (enumeration.hasMoreElements()) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |