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.persistable;
022:
023: import java.sql.*;
024: import java.util.*;
025: import junit.framework.*;
026: import com.methodhead.test.*;
027: import com.methodhead.persistable.ConnectionSingleton;
028: import org.apache.commons.beanutils.*;
029:
030: public class KeyedPersistableTest extends DbTestCase {
031:
032: KeyedPersistable impl_ = null;
033: KeyedPersistable impl1_ = null;
034: static Map fields_ = null;
035: java.util.Date date1_ = null;
036: java.util.Date date2_ = null;
037: DynaClass dynaClass_ = null;
038:
039: private static class TestKey implements Key {
040:
041: public boolean equals(Object o) {
042: if (o == null)
043: return false;
044:
045: if (!getClass().isInstance(o))
046: return false;
047:
048: return true;
049: }
050:
051: public String getWhereClause() {
052: return "int_field=666";
053: }
054:
055: public void setProperties(Persistable persistable) {
056: persistable.set("int_field", new Integer(666));
057: }
058:
059: public void setKeyValue(Persistable persistable) {
060: // getWhereClause is hard coded
061: }
062: }
063:
064: public static class TestKeyFactory implements KeyFactory {
065: public Key newInstance() {
066: return new TestKey();
067: }
068: }
069:
070: public KeyedPersistableTest(String name) {
071: super (name);
072: }
073:
074: protected void createData() {
075: impl1_ = new KeyedPersistable(dynaClass_);
076: impl1_.setString("string_field", "string_value");
077: impl1_.setBoolean("boolean_field", true);
078: impl1_.setDouble("double_field", 6.66);
079: impl1_.setDate("date_field", date1_);
080: impl1_.saveNew(new TestKey());
081: }
082:
083: protected void setUp() {
084: try {
085: try {
086: ConnectionSingleton.runUpdate("DROP TABLE persistable");
087: } catch (SQLException e) {
088: }
089:
090: if (ConnectionSingleton.getDatabaseType().equals(
091: ConnectionSingleton.DBTYPE_SQLSERVER)) {
092: ConnectionSingleton
093: .runUpdate("CREATE TABLE persistable ( "
094: + " string_field VARCHAR(32), "
095: + " int_field INT, "
096: + " boolean_field BIT, "
097: + " double_field FLOAT, "
098: + " date_field DATETIME " + ")");
099: } else {
100: ConnectionSingleton
101: .runUpdate("CREATE TABLE persistable ( "
102: + " string_field VARCHAR(32), "
103: + " int_field INT, "
104: + " boolean_field BIT, "
105: + " double_field FLOAT, "
106: + " date_field TIMESTAMP " + ")");
107: }
108:
109: DynaProperty[] dynaProperties = new DynaProperty[] {
110: new DynaProperty("string_field", String.class),
111: new DynaProperty("int_field", Integer.class),
112: new DynaProperty("boolean_field", Boolean.class),
113: new DynaProperty("double_field", Double.class),
114: new DynaProperty("date_field", java.util.Date.class), };
115:
116: dynaClass_ = new BasicDynaClass("persistable",
117: KeyedPersistable.class, dynaProperties);
118:
119: impl_ = new KeyedPersistable(dynaClass_);
120:
121: Calendar cal = new GregorianCalendar();
122:
123: cal.set(2003, 1, 20, 20, 20, 10);
124: date1_ = cal.getTime();
125:
126: cal.set(2003, 2, 20, 20, 20, 10);
127: date2_ = cal.getTime();
128: } catch (Exception e) {
129: fail(e.getMessage());
130: }
131: }
132:
133: protected void tearDown() {
134: }
135:
136: public void testSaveNew() {
137: ResultSet rs = null;
138: try {
139: impl_.setString("string_field", "string_value");
140: impl_.setBoolean("boolean_field", true);
141: impl_.setDouble("double_field", 6.66);
142: impl_.setDate("date_field", date1_);
143: impl_.saveNew(new TestKey());
144: rs = ConnectionSingleton
145: .runQuery("SELECT string_field, int_field, boolean_field, double_field, date_field FROM persistable WHERE int_field=666");
146:
147: assertNotNull(rs);
148: assertTrue(rs.next());
149: assertEquals("string_value", rs.getString("string_field"));
150: assertEquals(666, rs.getInt("int_field"));
151: assertEquals(true, rs.getBoolean("boolean_field"));
152: assertEquals(6.66, rs.getDouble("double_field"), 0.01);
153: assertDatesEqual(date1_, rs.getTimestamp("date_field"));
154: assertTrue(!rs.next());
155:
156: ConnectionSingleton.close(rs);
157: } catch (Exception e) {
158: if (rs != null)
159: ConnectionSingleton.close(rs);
160: e.printStackTrace();
161: fail();
162: }
163: }
164:
165: public void testLoad() {
166: try {
167: createData();
168: impl_.load(new TestKey());
169:
170: assertEquals("string_value", impl_
171: .getString("string_field"));
172: assertEquals(666, impl_.getInt("int_field"));
173: assertEquals(true, impl_.getBoolean("boolean_field"));
174: assertEquals(6.66, impl_.getDouble("double_field"), 0.01);
175: assertDatesEqual(date1_, impl_.getDate("date_field"));
176: } catch (Exception e) {
177: fail(e.toString());
178: }
179: }
180:
181: public void testLoadWhere() {
182: try {
183: createData();
184: impl_.load("int_field=666", new TestKeyFactory());
185:
186: assertEquals(new TestKey(), impl_.key_);
187: } catch (Exception e) {
188: fail(e.getMessage());
189: }
190: }
191:
192: public void testLoadAll() {
193: try {
194: createData();
195: List l = KeyedPersistable.loadAll(dynaClass_, null, null,
196: new TestKeyFactory());
197:
198: assertEquals(1, l.size());
199:
200: impl_ = (KeyedPersistable) l.get(0);
201:
202: assertNotNull(impl_.key_);
203: assertEquals("int_field=666", impl_.key_.getWhereClause());
204: } catch (Exception e) {
205: fail(e.getMessage());
206: }
207: }
208:
209: public void testSave() {
210: ResultSet rs = null;
211: try {
212: createData();
213: impl_.load(new TestKey());
214: impl_.setString("string_field", "string_value2");
215: impl_.setBoolean("boolean_field", false);
216: impl_.setDouble("double_field", 7.77);
217: impl_.setDate("date_field", date2_);
218: impl_.save();
219: rs = ConnectionSingleton
220: .runQuery("SELECT string_field, int_field, boolean_field, double_field, date_field FROM persistable WHERE int_field=666");
221:
222: assertNotNull(rs);
223: assertTrue(rs.next());
224: assertEquals("string_value2", rs.getString("string_field"));
225: assertEquals(666, rs.getInt("int_field"));
226: assertEquals(false, rs.getBoolean("boolean_field"));
227: assertEquals(7.77, rs.getDouble("double_field"), 0.01);
228: assertDatesEqual(date2_, rs.getTimestamp("date_field"));
229: assertTrue(!rs.next());
230:
231: ConnectionSingleton.close(rs);
232: } catch (Exception e) {
233: if (rs != null)
234: ConnectionSingleton.close(rs);
235: fail(e.getMessage());
236: }
237: }
238:
239: public void testDelete() {
240: ResultSet rs = null;
241: try {
242: createData();
243: impl_.load(new TestKey());
244: impl_.delete();
245: rs = ConnectionSingleton
246: .runQuery("SELECT int_field FROM persistable WHERE int_field=666");
247:
248: assertNotNull(rs);
249: assertTrue(!rs.next());
250:
251: ConnectionSingleton.close(rs);
252: } catch (Exception e) {
253: if (rs != null)
254: ConnectionSingleton.close(rs);
255: fail(e.toString());
256: }
257: }
258: }
|