[Htmlparser-cvs] htmlparser/src/org/htmlparser/tests/utilTests AllTests.java,1.39,1.40 BeanTest.java
Brought to you by:
derrickoswald
From: <der...@us...> - 2003-07-16 00:17:29
|
Update of /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/utilTests In directory sc8-pr-cvs1:/tmp/cvs-serv6564/org/htmlparser/tests/utilTests Modified Files: AllTests.java BeanTest.java Log Message: Add some StringBean tests for Joshua. Index: AllTests.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/utilTests/AllTests.java,v retrieving revision 1.39 retrieving revision 1.40 diff -C2 -d -r1.39 -r1.40 *** AllTests.java 13 Jul 2003 12:15:12 -0000 1.39 --- AllTests.java 16 Jul 2003 00:17:23 -0000 1.40 *************** *** 46,52 **** super(name); } ! public static void main(String[] args) { ! new junit.awtui.TestRunner().start(new String[] {"org.htmlparser.tests.AllTests"}); ! } /** * Insert the method's description here. --- 46,113 ---- super(name); } ! ! /** ! * Mainline for all suites of tests. ! * @param args Command line arguments. The following options ! * are understood: ! * <pre> ! * -text -- use junit.textui.TestRunner ! * -awt -- use junit.awtui.TestRunner ! * -swing -- use junit.swingui.TestRunner (default) ! * </pre> ! * All other options are passed on to the junit framework. ! */ ! public static void main(String[] args) ! { ! String runner; ! int i; ! String arguments[]; ! Class cls; ! ! runner = null; ! for (i = 0; (i < args.length) && (null == runner); i++) ! { ! if (args[i].equalsIgnoreCase ("-text")) ! runner = "junit.textui.TestRunner"; ! else if (args[i].equalsIgnoreCase ("-awt")) ! runner = "junit.awtui.TestRunner"; ! else if (args[i].equalsIgnoreCase ("-swing")) ! runner = "junit.swingui.TestRunner"; ! } ! if (null != runner) ! { ! // remove it from the arguments ! arguments = new String[args.length - 1]; ! System.arraycopy (args, 0, arguments, 0, i - 1); ! System.arraycopy (args, i, arguments, i - 1, args.length - i); ! args = arguments; ! } ! else ! runner = "junit.swingui.TestRunner"; ! ! // append the test class ! arguments = new String[args.length + 1]; ! System.arraycopy (args, 0, arguments, 0, args.length); ! arguments[args.length] = "org.htmlparser.tests.utilTests.AllTests"; ! ! // invoke main() of the test runner ! try ! { ! cls = Class.forName (runner); ! java.lang.reflect.Method method = cls.getDeclaredMethod ( ! "main", new Class[] { String[].class }); ! method.invoke ( ! null, ! new Object[] { arguments }); ! } ! catch (Throwable t) ! { ! System.err.println ( ! "cannot run unit test (" ! + t.getMessage () ! + ")"); ! } ! } ! /** * Insert the method's description here. Index: BeanTest.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/utilTests/BeanTest.java,v retrieving revision 1.29 retrieving revision 1.30 diff -C2 -d -r1.29 -r1.30 *** BeanTest.java 13 Jul 2003 12:15:12 -0000 1.29 --- BeanTest.java 16 Jul 2003 00:17:23 -0000 1.30 *************** *** 33,39 **** --- 33,42 ---- import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; + import java.io.File; + import java.io.FileWriter; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; + import java.io.PrintWriter; import java.net.URL; import java.util.Vector; *************** *** 89,92 **** --- 92,129 ---- } + /** + * Makes sure that the bean returns text when passed the html. + */ + protected void check (StringBean bean, String html, String text) + { + String path; + File file; + PrintWriter out; + String string; + + path = System.getProperty ("user.dir"); + if (!path.endsWith (File.separator)) + path += File.separator; + file = new File (path + "delete_me.html"); + try + { + out = new PrintWriter (new FileWriter (file)); + out.println (html); + out.close (); + bean.setURL (file.getAbsolutePath ()); + string = bean.getStrings (); + } + catch (Exception e) + { + fail (e.toString ()); + string = null; // never reached + } + finally + { + file.delete (); + } + assertEquals ("stringbean text differs", text, string); + } + public void testZeroArgConstructor () throws *************** *** 266,269 **** --- 303,379 ---- "Links property change not fired for URL change", hit[0].booleanValue ()); + } + + /** + * Test no text returns empty string. + */ + public void testCollapsed1 () + { + StringBean sb; + + sb = new StringBean (); + sb.setLinks (false); + sb.setReplaceNonBreakingSpaces (true); + sb.setCollapse (false); + check (sb, "<html><head></head><body></body></html>", ""); + check (sb, "<html><head></head><body> </body></html>", " "); + check (sb, "<html><head></head><body>\t</body></html>", "\t"); + sb.setCollapse (true); + check (sb, "<html><head></head><body></body></html>", ""); + check (sb, "<html><head></head><body> </body></html>", ""); + check (sb, "<html><head></head><body>\t</body></html>", ""); + } + + /** + * Test multiple whitespace returns empty string. + */ + public void testCollapsed2 () + { + StringBean sb; + + sb = new StringBean (); + sb.setLinks (false); + sb.setReplaceNonBreakingSpaces (true); + sb.setCollapse (false); + check (sb, "<html><head></head><body> </body></html>", " "); + check (sb, "<html><head></head><body>\t\t</body></html>", "\t\t"); + check (sb, "<html><head></head><body> \t\t</body></html>", " \t\t"); + check (sb, "<html><head></head><body>\t \t</body></html>", "\t \t"); + check (sb, "<html><head></head><body>\t\t </body></html>", "\t\t "); + sb.setCollapse (true); + check (sb, "<html><head></head><body> </body></html>", ""); + check (sb, "<html><head></head><body>\t\t</body></html>", ""); + check (sb, "<html><head></head><body> \t\t</body></html>", ""); + check (sb, "<html><head></head><body>\t \t</body></html>", ""); + check (sb, "<html><head></head><body>\t\t </body></html>", ""); + } + + /** + * Test text preceded or followed by whitespace returns just text. + */ + public void testCollapsed3 () + { + StringBean sb; + + sb = new StringBean (); + sb.setLinks (false); + sb.setReplaceNonBreakingSpaces (true); + sb.setCollapse (false); + check (sb, "<html><head></head><body>x </body></html>", "x "); + check (sb, "<html><head></head><body>x\t\t</body></html>", "x\t\t"); + check (sb, "<html><head></head><body>x \t\t</body></html>", "x \t\t"); + check (sb, "<html><head></head><body>x\t \t</body></html>", "x\t \t"); + check (sb, "<html><head></head><body>x\t\t </body></html>", "x\t\t "); + sb.setCollapse (true); + check (sb, "<html><head></head><body>x </body></html>", "x"); + check (sb, "<html><head></head><body>x\t\t</body></html>", "x"); + check (sb, "<html><head></head><body>x \t\t</body></html>", "x"); + check (sb, "<html><head></head><body>x\t \t</body></html>", "x"); + check (sb, "<html><head></head><body>x\t\t </body></html>", "x"); + check (sb, "<html><head></head><body> x</body></html>", "x"); + check (sb, "<html><head></head><body>\t\tx</body></html>", "x"); + check (sb, "<html><head></head><body> \t\tx</body></html>", "x"); + check (sb, "<html><head></head><body>\t \tx</body></html>", "x"); + check (sb, "<html><head></head><body>\t\t x</body></html>", "x"); } } |