Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

StringUtilTest.java

00001 package edu.virtualschool.jwaa.test;
00002 
00003 import edu.virtualschool.jwaa.StringUtil;
00004 
00005 
00010 public class StringUtilTest extends junit.framework.TestCase
00011 {
00012   public String foo = "foo";
00013   public int bar = 23;
00014   public double gag = 23.3;
00015   // public Object enclosed = new StringUtilTest();
00016 
00017   public StringUtilTest(String name) 
00018   {
00019     super(name);
00020   }
00021   public static void main(String args[])
00022   {
00023     junit.textui.TestRunner.run(StringUtilTest.class);
00024   }
00025   public void testJoin() throws Exception
00026   {
00027     assertTrue(StringUtil.join(',', new String[] { "Foo", "Bar" }).equals("Foo,Bar"));
00028   }
00029   public void testAlign() throws Exception
00030   {
00031     String[] v = new String[] {
00032       "",
00033       "0",
00034       "0123456789",
00035       "01234567890123456789",
00036       "012345678901234567890123456789",
00037       "0123456789012345678901234567890123456789",
00038     };
00039     for (int i = 0; i < v.length; i++)
00040     {
00041       String s = StringUtil.alignLeft(32, v[i]);
00042       assertTrue(s, s.length() == 32);
00043       if (v[i].length() <= 32)
00044         assertTrue(s, s.trim().equals(v[i]));
00045     }
00046   }
00047   public void testSplit() throws Exception
00048   {
00049     String[] v = new String[] {
00050       "0123456789",
00051       "0123456789,0123456789",
00052       "0123456789,0123456789,0123456789",
00053       "0123456789,0123456789,0123456789,0123456789",
00054       "0123456789,0123456789,0123456789,0123456789,0123456789",
00055     };
00056     for (int i = 0; i < v.length; i++)
00057     {
00058       String[] list = StringUtil.split(',', v[i]);
00059       assertTrue(i+"", list.length == i+1);
00060       for (int j = 0; j < list.length; j++)
00061         assertTrue(list[j], list[j].equals("0123456789"));
00062     }
00063   }
00064   public void testReplace() throws Exception
00065   {
00066     assertTrue(StringUtil.replace("Foo", "Bar", "FooBar").equals("BarBar"));
00067     assertTrue(StringUtil.replace("Bar", "Foo", "FooBar").equals("FooFoo"));
00068     assertTrue(StringUtil.replace("Longer", "Foo", "LongerBar").equals("FooBar"));
00069     assertTrue(StringUtil.replace("[A-Z][a-z]+", "Foo", "LongerBar").equals("FooBar"));
00070     assertTrue(StringUtil.replace(".* @ ", "", "DemoVideo2 @ $0.01 per use").equals("$0.01 per use"));
00071     assertTrue(StringUtil.replace("(^Account)|(^Product)", "", "Product32").equals("32"));
00072   }
00073   public void testWrap() throws Exception
00074   {
00075     int max = 50;
00076     String s = "0123456789";
00077     for (int i = 0; i < max; i++)
00078     {
00079       String orig = StringUtil.replicate(i, s);
00080       int origL = orig.length();
00081       assertTrue(origL == i * 10);
00082       String comp = StringUtil.toJavaString(max, orig);
00083       int compL = comp.length();
00084       int fullLines = origL/max;
00085       int extraLine = (fullLines * max == origL) ? 0 : 1;
00086       int cl = origL + 4*(fullLines + extraLine) + 2;
00087       assertTrue("orig:" + orig + 
00088       "\norigL:" + origL + 
00089       "\ncomp:" + comp + 
00090       "\ncompL:" + compL +
00091       "\nfullLines:" + fullLines + 
00092       "\nextraLine:" + extraLine + 
00093       "\ncl:" + cl, compL == cl);
00094     }
00095   }
00096   public StringUtilTest() 
00097   {
00098     super("StringUtilTest");
00099   }
00100 }