001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.util;
022:
023: import java.util.*;
024: import java.sql.*;
025: import java.io.*;
026: import junit.framework.*;
027: import org.apache.log4j.*;
028: import com.methodhead.persistable.*;
029: import com.methodhead.test.*;
030: import org.apache.commons.beanutils.*;
031:
032: public class MhfStringUtilsTest extends TestCase {
033:
034: Map map = null;
035:
036: //
037: // plain java beans for testing
038: //
039: public static class TestJavaBean1 {
040: public String getField1() {
041: return "value1";
042: }
043: }
044:
045: public static class TestJavaBean2 {
046: public String getField1() {
047: return "value1";
048: }
049:
050: public String getField2() {
051: return "value2";
052: }
053: }
054:
055: //
056: // dyna beans for testing (see setUp())
057: //
058: DynaBean dynaBean1 = null;
059: DynaBean dynaBean2 = null;
060:
061: static {
062: TestUtils.initLogger();
063: TestUtils.initDb();
064: }
065:
066: public MhfStringUtilsTest(String name) {
067: super (name);
068: }
069:
070: protected void setUp() {
071: try {
072: //
073: // set up the dynabeans
074: //
075: DynaClass dynaClass1 = null;
076: DynaClass dynaClass2 = null;
077: DynaProperty[] dynaProperties = null;
078:
079: dynaProperties = new DynaProperty[] { new DynaProperty(
080: "field1", String.class) };
081:
082: dynaClass1 = new BasicDynaClass("dynaClass1",
083: BasicDynaBean.class, dynaProperties);
084:
085: dynaProperties = new DynaProperty[] {
086: new DynaProperty("field1", String.class),
087: new DynaProperty("field2", String.class) };
088:
089: dynaClass2 = new BasicDynaClass("dynaClass2",
090: BasicDynaBean.class, dynaProperties);
091:
092: dynaBean1 = dynaClass1.newInstance();
093: dynaBean1.set("field1", "value1");
094:
095: dynaBean2 = dynaClass2.newInstance();
096: dynaBean2.set("field1", "value1");
097: dynaBean2.set("field2", "value2");
098: } catch (Exception e) {
099: fail(e.getMessage());
100: }
101: }
102:
103: protected void tearDown() {
104: }
105:
106: public void testExtractProperties() {
107: Map map = MhfStringUtils.extractProperties(dynaBean2);
108:
109: assertEquals("value1", map.get("field1"));
110: assertEquals("value2", map.get("field2"));
111: }
112:
113: public void testMergeMap() {
114: map = new HashMap();
115: map.put("field1", "value1");
116: map.put("field2", "value2");
117:
118: //
119: // we should get what we started with if there are no fields to be merged
120: //
121: assertEquals("This is a test.", MhfStringUtils.merge(
122: "This is a test.", map));
123:
124: //
125: // an exception should be thrown if the template contains fields not
126: // provided in the map
127: //
128: try {
129: MhfStringUtils.merge("This is a test of {field3}.", map);
130: fail("No exception thrown.");
131: } catch (RuntimeException e) {
132: // success
133: }
134:
135: //
136: // merge a single field
137: //
138: assertEquals("This is a test of value1.", MhfStringUtils.merge(
139: "This is a test of {field1}.", map));
140:
141: //
142: // merge repeated field
143: //
144: assertEquals(
145: "This is a test of value1 and value1.",
146: MhfStringUtils
147: .merge(
148: "This is a test of {field1} and {field1}.",
149: map));
150:
151: //
152: // merge two fields
153: //
154: assertEquals(
155: "This is a test of value1 and value2.",
156: MhfStringUtils
157: .merge(
158: "This is a test of {field1} and {field2}.",
159: map));
160:
161: //
162: // merge a value containing dollar signs
163: //
164: map.put("field1", "$1");
165: map.put("field2", "$2");
166: assertEquals("This is a test of $1 and $2.", MhfStringUtils
167: .merge("This is a test of {field1} and {field2}.", map));
168: }
169:
170: public void testMergeJavaBean() {
171:
172: //
173: // we should get what we started with if there are no fields to be merged
174: //
175: assertEquals("This is a test.", MhfStringUtils.merge(
176: "This is a test.", new TestJavaBean1()));
177:
178: //
179: // an exception should be thrown if the template contains fields not
180: // provided in the map
181: //
182: try {
183: MhfStringUtils.merge("This is a test of {field3}.",
184: new TestJavaBean1());
185: fail("No exception thrown.");
186: } catch (RuntimeException e) {
187: // success
188: }
189:
190: //
191: // merge a single field
192: //
193: assertEquals("This is a test of value1.", MhfStringUtils.merge(
194: "This is a test of {field1}.", new TestJavaBean1()));
195:
196: //
197: // merge repeated field
198: //
199: assertEquals("This is a test of value1 and value1.",
200: MhfStringUtils.merge(
201: "This is a test of {field1} and {field1}.",
202: new TestJavaBean1()));
203:
204: //
205: // merge two fields
206: //
207: assertEquals("This is a test of value1 and value2.",
208: MhfStringUtils.merge(
209: "This is a test of {field1} and {field2}.",
210: new TestJavaBean2()));
211: }
212:
213: public void testMergeDynaBean() {
214:
215: //
216: // we should get what we started with if there are no fields to be merged
217: //
218: assertEquals("This is a test.", MhfStringUtils.merge(
219: "This is a test.", dynaBean1));
220:
221: //
222: // an exception should be thrown if the template contains fields not
223: // provided in the map
224: //
225: try {
226: MhfStringUtils.merge("This is a test of {field3}.",
227: dynaBean1);
228: fail("No exception thrown.");
229: } catch (RuntimeException e) {
230: // success
231: }
232:
233: //
234: // merge a single field
235: //
236: assertEquals("This is a test of value1.", MhfStringUtils.merge(
237: "This is a test of {field1}.", dynaBean1));
238:
239: //
240: // merge repeated field
241: //
242: assertEquals("This is a test of value1 and value1.",
243: MhfStringUtils.merge(
244: "This is a test of {field1} and {field1}.",
245: dynaBean1));
246:
247: //
248: // merge two fields
249: //
250: assertEquals("This is a test of value1 and value2.",
251: MhfStringUtils.merge(
252: "This is a test of {field1} and {field2}.",
253: dynaBean2));
254: }
255:
256: public void testHashAndEncode() {
257:
258: //
259: // password should be encrypted
260: //
261: assertEquals("X03MO1qnZdYdgyfeuILPmQ==", MhfStringUtils
262: .hashAndEncode("password"));
263:
264: //
265: // result should be different for a different password
266: //
267: assertEquals("bLdfZSqbUnmOts8iAQV8cw==", MhfStringUtils
268: .hashAndEncode("password2"));
269:
270: //
271: // result should be less than 128 characters even for a long password
272: //
273: assertEquals(
274: "f3v9NIcJ3uqs4Z4/U1+MVA==",
275: MhfStringUtils
276: .hashAndEncode("0123456789012345678901234567890123456789012345678901234567890123"));
277: }
278:
279: /*
280: public void testMergeDynaBean() {
281: try {
282: MhfStringUtils.merge( dynaBean1 );
283: fail( "No exception thrown." );
284: }
285: catch ( RuntimeException e ) {
286: // success
287: }
288:
289: //
290: // a typical merge
291: //
292: assertEquals( "This is MailTemplate2. Field1 is value1. Field2 is value2.", MhfStringUtils.merge( dynaBean2 ) );
293: }
294: */
295: }
|