KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > util > MhfStringUtilsTest


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

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   //
37
// plain java beans for testing
38
//
39
public static class TestJavaBean1 {
40     public String JavaDoc getField1() {
41       return "value1";
42     }
43   }
44
45   public static class TestJavaBean2 {
46     public String JavaDoc getField1() {
47       return "value1";
48     }
49
50     public String JavaDoc getField2() {
51       return "value2";
52     }
53   }
54
55   //
56
// dyna beans for testing (see setUp())
57
//
58
DynaBean dynaBean1 = null;
59   DynaBean dynaBean2 = null;
60
61   static {
62     TestUtils.initLogger();
63     TestUtils.initDb();
64   }
65
66   public MhfStringUtilsTest( String JavaDoc name ) {
67     super( name );
68   }
69
70   protected void setUp() {
71     try {
72       //
73
// set up the dynabeans
74
//
75
DynaClass dynaClass1 = null;
76       DynaClass dynaClass2 = null;
77       DynaProperty[] dynaProperties = null;
78
79       dynaProperties =
80         new DynaProperty[] {
81           new DynaProperty( "field1", String JavaDoc.class )
82         };
83
84       dynaClass1 =
85         new BasicDynaClass(
86           "dynaClass1", BasicDynaBean.class, dynaProperties );
87
88       dynaProperties =
89         new DynaProperty[] {
90           new DynaProperty( "field1", String JavaDoc.class ),
91           new DynaProperty( "field2", String JavaDoc.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 JavaDoc 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     //
126
// we should get what we started with if there are no fields to be merged
127
//
128
assertEquals( "This is a test.", MhfStringUtils.merge( "This is a test.", map ) );
129
130     //
131
// an exception should be thrown if the template contains fields not
132
// provided in the map
133
//
134
try {
135       MhfStringUtils.merge( "This is a test of {field3}.", map );
136       fail( "No exception thrown." );
137     }
138     catch ( RuntimeException JavaDoc e ) {
139       // success
140
}
141
142     //
143
// merge a single field
144
//
145
assertEquals( "This is a test of value1.", MhfStringUtils.merge( "This is a test of {field1}.", map ) );
146
147     //
148
// merge repeated field
149
//
150
assertEquals( "This is a test of value1 and value1.", MhfStringUtils.merge( "This is a test of {field1} and {field1}.", map ) );
151
152     //
153
// merge two fields
154
//
155
assertEquals( "This is a test of value1 and value2.", MhfStringUtils.merge( "This is a test of {field1} and {field2}.", map ) );
156
157     //
158
// merge a value containing dollar signs
159
//
160
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     //
168
// we should get what we started with if there are no fields to be merged
169
//
170
assertEquals( "This is a test.", MhfStringUtils.merge( "This is a test.", new TestJavaBean1() ) );
171
172     //
173
// an exception should be thrown if the template contains fields not
174
// provided in the map
175
//
176
try {
177       MhfStringUtils.merge( "This is a test of {field3}.", new TestJavaBean1() );
178       fail( "No exception thrown." );
179     }
180     catch ( RuntimeException JavaDoc e ) {
181       // success
182
}
183
184     //
185
// merge a single field
186
//
187
assertEquals( "This is a test of value1.", MhfStringUtils.merge( "This is a test of {field1}.", new TestJavaBean1() ) );
188
189     //
190
// merge repeated field
191
//
192
assertEquals( "This is a test of value1 and value1.", MhfStringUtils.merge( "This is a test of {field1} and {field1}.", new TestJavaBean1() ) );
193
194     //
195
// merge two fields
196
//
197
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     //
203
// we should get what we started with if there are no fields to be merged
204
//
205
assertEquals( "This is a test.", MhfStringUtils.merge( "This is a test.", dynaBean1 ) );
206
207     //
208
// an exception should be thrown if the template contains fields not
209
// provided in the map
210
//
211
try {
212       MhfStringUtils.merge( "This is a test of {field3}.", dynaBean1 );
213       fail( "No exception thrown." );
214     }
215     catch ( RuntimeException JavaDoc e ) {
216       // success
217
}
218
219     //
220
// merge a single field
221
//
222
assertEquals( "This is a test of value1.", MhfStringUtils.merge( "This is a test of {field1}.", dynaBean1 ) );
223
224     //
225
// merge repeated field
226
//
227
assertEquals( "This is a test of value1 and value1.", MhfStringUtils.merge( "This is a test of {field1} and {field1}.", dynaBean1 ) );
228
229     //
230
// merge two fields
231
//
232
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     //
238
// password should be encrypted
239
//
240
assertEquals( "X03MO1qnZdYdgyfeuILPmQ==", MhfStringUtils.hashAndEncode( "password" ) );
241
242     //
243
// result should be different for a different password
244
//
245
assertEquals( "bLdfZSqbUnmOts8iAQV8cw==", MhfStringUtils.hashAndEncode( "password2" ) );
246
247     //
248
// result should be less than 128 characters even for a long password
249
//
250
assertEquals( "f3v9NIcJ3uqs4Z4/U1+MVA==", MhfStringUtils.hashAndEncode( "0123456789012345678901234567890123456789012345678901234567890123" ) );
251   }
252
253
254 /*
255   public void testMergeDynaBean() {
256     try {
257       MhfStringUtils.merge( dynaBean1 );
258       fail( "No exception thrown." );
259     }
260     catch ( RuntimeException e ) {
261       // success
262     }
263
264     //
265     // a typical merge
266     //
267     assertEquals( "This is MailTemplate2. Field1 is value1. Field2 is value2.", MhfStringUtils.merge( dynaBean2 ) );
268   }
269 */

270 }
271
Popular Tags