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.aikp;
022:
023: import java.sql.*;
024: import java.util.*;
025:
026: import junit.framework.*;
027:
028: import com.methodhead.test.*;
029: import com.methodhead.persistable.*;
030: import org.apache.commons.beanutils.*;
031:
032: public class AutoIntKeyPersistableTest extends DbTestCase {
033:
034: protected static class TestClass extends AutoIntKeyPersistable {
035: public TestClass() {
036: super (null);
037: }
038:
039: public TestClass(DynaClass dynaClass) {
040: super (dynaClass);
041: }
042:
043: public List loadAll() {
044: return null;
045: }
046: }
047:
048: AutoIntKeyPersistable impl_ = null;
049: AutoIntKeyPersistable impl1_ = null;
050: static Map fields_ = null;
051: java.util.Date date1_ = null;
052: DynaClass dynaClass_ = null;
053:
054: public AutoIntKeyPersistableTest(String name) {
055: super (name);
056: }
057:
058: protected void createData() throws PersistableException {
059: impl1_ = new TestClass(dynaClass_);
060: impl1_.setString("string_field", "string_value");
061: impl1_.setInt("int_field", 666);
062: impl1_.setBoolean("boolean_field", true);
063: impl1_.setDouble("double_field", 6.66);
064: impl1_.setDate("date_field", date1_);
065: impl1_.saveNew();
066: }
067:
068: protected void setUp() throws SQLException {
069:
070: try {
071: ConnectionSingleton.runUpdate("DROP TABLE mh_id");
072: ConnectionSingleton.runUpdate("DROP TABLE persistable");
073: } catch (SQLException e) {
074: }
075:
076: ConnectionSingleton
077: .runUpdate("CREATE TABLE mh_id (name VARCHAR(32), value INT)");
078:
079: if (ConnectionSingleton.getDatabaseType().equals(
080: ConnectionSingleton.DBTYPE_SQLSERVER)) {
081: ConnectionSingleton.runUpdate("CREATE TABLE persistable ( "
082: + " id INT, "
083: + " string_field VARCHAR(32), "
084: + " int_field INT, " + " boolean_field BIT, "
085: + " double_field REAL, "
086: + " date_field DATETIME " + ")");
087: } else {
088: ConnectionSingleton.runUpdate("CREATE TABLE persistable ( "
089: + " id INT, "
090: + " string_field VARCHAR(32), "
091: + " int_field INT, " + " boolean_field BIT, "
092: + " double_field REAL, "
093: + " date_field TIMESTAMP " + ")");
094: }
095:
096: DynaProperty[] dynaProperties = new DynaProperty[] {
097: new DynaProperty("id", Integer.class),
098: new DynaProperty("string_field", String.class),
099: new DynaProperty("int_field", Integer.class),
100: new DynaProperty("boolean_field", Boolean.class),
101: new DynaProperty("double_field", Double.class),
102: new DynaProperty("date_field", java.util.Date.class), };
103:
104: dynaClass_ = new BasicDynaClass("persistable", TestClass.class,
105: dynaProperties);
106:
107: impl_ = new TestClass(dynaClass_);
108:
109: Calendar cal = new GregorianCalendar();
110:
111: cal.set(2003, 1, 20, 20, 20, 10);
112: date1_ = cal.getTime();
113: }
114:
115: protected void tearDown() {
116: }
117:
118: public void testNewKey() {
119: try {
120: AutoIntKeyPersistable impl = new TestClass();
121:
122: assertEquals(new IntKey(1), impl.newKey("name1"));
123: assertEquals(new IntKey(2), impl.newKey("name1"));
124: assertEquals(new IntKey(1), impl.newKey("name2"));
125: } catch (Exception e) {
126: fail(e.toString());
127: }
128: }
129:
130: public void testSaveNew() {
131: ResultSet rs = null;
132: try {
133: AutoIntKeyPersistable impl = new TestClass(dynaClass_);
134: impl.setString("string_field", "string_value");
135: impl.setInt("int_field", 666);
136: impl.setBoolean("boolean_field", true);
137: impl.setDouble("double_field", 6.66);
138: impl.setDate("date_field", date1_);
139: impl.saveNew();
140:
141: rs = ConnectionSingleton
142: .runQuery("SELECT string_field, int_field, boolean_field, double_field, date_field FROM persistable WHERE id="
143: + impl.getInt("id"));
144:
145: assertNotNull(rs);
146: assertTrue(rs.next());
147: assertEquals("string_value", rs.getString("string_field"));
148: assertEquals(666, rs.getInt("int_field"));
149: assertEquals(true, rs.getBoolean("boolean_field"));
150: assertEquals(6.66, rs.getDouble("double_field"), 0.01);
151: assertDatesEqual(date1_, rs.getTimestamp("date_field"));
152: assertTrue(!rs.next());
153:
154: ConnectionSingleton.close(rs);
155: } catch (Exception e) {
156: if (rs != null)
157: ConnectionSingleton.close(rs);
158: e.printStackTrace();
159: fail();
160: }
161: }
162:
163: public void testLoad() {
164: try {
165: createData();
166: impl_.load("int_field=666");
167:
168: assertEquals(impl1_.getInt("id"), impl_.getInt("id"));
169: } catch (Exception e) {
170: fail(e.toString());
171: }
172: }
173:
174: public void testLoadAll() {
175: try {
176: createData();
177: List l = AutoIntKeyPersistable.loadAll(dynaClass_, null,
178: null);
179:
180: assertEquals(1, l.size());
181:
182: impl_ = (AutoIntKeyPersistable) l.get(0);
183:
184: assertEquals(new IntKey(1), impl_.getKey());
185: } catch (Exception e) {
186: fail(e.toString());
187: }
188: }
189: }
|