1 20 21 package com.methodhead.util; 22 23 import java.util.*; 24 import java.sql.*; 25 import java.io.*; 26 import junit.framework.*; 27 import org.apache.log4j.*; 28 import com.methodhead.persistable.*; 29 import com.methodhead.test.*; 30 import org.apache.commons.beanutils.*; 31 32 public class MhfStringUtilsTest extends TestCase { 33 34 Map map = null; 35 36 public static class TestJavaBean1 { 40 public String getField1() { 41 return "value1"; 42 } 43 } 44 45 public static class TestJavaBean2 { 46 public String getField1() { 47 return "value1"; 48 } 49 50 public String getField2() { 51 return "value2"; 52 } 53 } 54 55 DynaBean dynaBean1 = null; 59 DynaBean dynaBean2 = null; 60 61 static { 62 TestUtils.initLogger(); 63 TestUtils.initDb(); 64 } 65 66 public MhfStringUtilsTest( String name ) { 67 super( name ); 68 } 69 70 protected void setUp() { 71 try { 72 DynaClass dynaClass1 = null; 76 DynaClass dynaClass2 = null; 77 DynaProperty[] dynaProperties = null; 78 79 dynaProperties = 80 new DynaProperty[] { 81 new DynaProperty( "field1", String .class ) 82 }; 83 84 dynaClass1 = 85 new BasicDynaClass( 86 "dynaClass1", BasicDynaBean.class, dynaProperties ); 87 88 dynaProperties = 89 new DynaProperty[] { 90 new DynaProperty( "field1", String .class ), 91 new DynaProperty( "field2", String .class ) 92 }; 93 94 dynaClass2 = 95 new BasicDynaClass( 96 "dynaClass2", BasicDynaBean.class, dynaProperties ); 97 98 dynaBean1 = dynaClass1.newInstance(); 99 dynaBean1.set( "field1", "value1" ); 100 101 dynaBean2 = dynaClass2.newInstance(); 102 dynaBean2.set( "field1", "value1" ); 103 dynaBean2.set( "field2", "value2" ); 104 } 105 catch ( Exception e ) { 106 fail( e.getMessage() ); 107 } 108 } 109 110 protected void tearDown() { 111 } 112 113 public void testExtractProperties() { 114 Map map = MhfStringUtils.extractProperties( dynaBean2 ); 115 116 assertEquals( "value1", map.get( "field1" ) ); 117 assertEquals( "value2", map.get( "field2" ) ); 118 } 119 120 public void testMergeMap() { 121 map = new HashMap(); 122 map.put( "field1", "value1" ); 123 map.put( "field2", "value2" ); 124 125 assertEquals( "This is a test.", MhfStringUtils.merge( "This is a test.", map ) ); 129 130 try { 135 MhfStringUtils.merge( "This is a test of {field3}.", map ); 136 fail( "No exception thrown." ); 137 } 138 catch ( RuntimeException e ) { 139 } 141 142 assertEquals( "This is a test of value1.", MhfStringUtils.merge( "This is a test of {field1}.", map ) ); 146 147 assertEquals( "This is a test of value1 and value1.", MhfStringUtils.merge( "This is a test of {field1} and {field1}.", map ) ); 151 152 assertEquals( "This is a test of value1 and value2.", MhfStringUtils.merge( "This is a test of {field1} and {field2}.", map ) ); 156 157 map.put( "field1", "$1" ); 161 map.put( "field2", "$2" ); 162 assertEquals( "This is a test of $1 and $2.", MhfStringUtils.merge( "This is a test of {field1} and {field2}.", map ) ); 163 } 164 165 public void testMergeJavaBean() { 166 167 assertEquals( "This is a test.", MhfStringUtils.merge( "This is a test.", new TestJavaBean1() ) ); 171 172 try { 177 MhfStringUtils.merge( "This is a test of {field3}.", new TestJavaBean1() ); 178 fail( "No exception thrown." ); 179 } 180 catch ( RuntimeException e ) { 181 } 183 184 assertEquals( "This is a test of value1.", MhfStringUtils.merge( "This is a test of {field1}.", new TestJavaBean1() ) ); 188 189 assertEquals( "This is a test of value1 and value1.", MhfStringUtils.merge( "This is a test of {field1} and {field1}.", new TestJavaBean1() ) ); 193 194 assertEquals( "This is a test of value1 and value2.", MhfStringUtils.merge( "This is a test of {field1} and {field2}.", new TestJavaBean2() ) ); 198 } 199 200 public void testMergeDynaBean() { 201 202 assertEquals( "This is a test.", MhfStringUtils.merge( "This is a test.", dynaBean1 ) ); 206 207 try { 212 MhfStringUtils.merge( "This is a test of {field3}.", dynaBean1 ); 213 fail( "No exception thrown." ); 214 } 215 catch ( RuntimeException e ) { 216 } 218 219 assertEquals( "This is a test of value1.", MhfStringUtils.merge( "This is a test of {field1}.", dynaBean1 ) ); 223 224 assertEquals( "This is a test of value1 and value1.", MhfStringUtils.merge( "This is a test of {field1} and {field1}.", dynaBean1 ) ); 228 229 assertEquals( "This is a test of value1 and value2.", MhfStringUtils.merge( "This is a test of {field1} and {field2}.", dynaBean2 ) ); 233 } 234 235 public void testHashAndEncode() { 236 237 assertEquals( "X03MO1qnZdYdgyfeuILPmQ==", MhfStringUtils.hashAndEncode( "password" ) ); 241 242 assertEquals( "bLdfZSqbUnmOts8iAQV8cw==", MhfStringUtils.hashAndEncode( "password2" ) ); 246 247 assertEquals( "f3v9NIcJ3uqs4Z4/U1+MVA==", MhfStringUtils.hashAndEncode( "0123456789012345678901234567890123456789012345678901234567890123" ) ); 251 } 252 253 254 270 } 271 | Popular Tags |