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.sitecontext;
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.aikp.*;
030: import com.methodhead.test.*;
031: import com.methodhead.*;
032: import org.apache.commons.beanutils.*;
033:
034: public class SiteContextAikpTest extends TestCase {
035:
036: SiteContextAikp siteContextAikp = null;
037: ResultSet rs = null;
038: List list = null;
039:
040: static {
041: TestUtils.initLogger();
042: TestUtils.initDb();
043: }
044:
045: public SiteContextAikpTest(String name) {
046: super (name);
047: }
048:
049: protected void setUp() {
050: try {
051: ConnectionSingleton.runBatchUpdate(new FileReader(
052: "webapp/WEB-INF/db/transfer-reset.sql"));
053: } catch (Exception e) {
054: fail(e.getMessage());
055: }
056: }
057:
058: protected void tearDown() {
059: }
060:
061: public void testProperties() {
062: try {
063: TestData.createDynaClassAndTable();
064:
065: siteContextAikp = new SiteContextAikp(TestData.dynaClass);
066:
067: //
068: // should throw an exception when site context has not been set
069: //
070: try {
071: siteContextAikp.getSiteContext();
072: fail("No exception thrown");
073: } catch (RuntimeException e) {
074: // success
075: }
076:
077: //
078: // should set and get as expected
079: //
080: siteContextAikp.setSiteContext(TestData.siteContext1);
081: assertEquals(TestData.siteContext1, siteContextAikp
082: .getSiteContext());
083: assertEquals(1, siteContextAikp.getInt("sitecontext_id"));
084: } catch (Exception e) {
085: e.printStackTrace();
086: fail();
087: }
088: }
089:
090: public void testSaveNew() {
091: try {
092: TestData.createDynaClassAndTable();
093:
094: //
095: // initialize the aikp
096: //
097: siteContextAikp = new SiteContextAikp(TestData.dynaClass);
098: siteContextAikp.setInt("id", 0);
099: siteContextAikp.setInt("sitecontext_id", 0);
100: siteContextAikp.setString("field", "value");
101:
102: //
103: // should throw an exception when site context has not been set
104: //
105: try {
106: siteContextAikp.saveNew();
107: fail("No exception thrown");
108: } catch (RuntimeException e) {
109: // success
110: }
111:
112: //
113: // set site context and save new
114: //
115: siteContextAikp.setSiteContext(TestData.siteContext1);
116: siteContextAikp.saveNew();
117:
118: //
119: // should save site context id
120: //
121: rs = ConnectionSingleton
122: .runQuery("SELECT field FROM sitecontextaikp WHERE sitecontext_id=1");
123: assertNotNull(rs);
124: assertTrue(rs.next());
125: assertEquals("value", rs.getString("field"));
126: assertTrue(!rs.next());
127:
128: ConnectionSingleton.close(rs);
129: } catch (Exception e) {
130: e.printStackTrace();
131: fail();
132: }
133: }
134:
135: public void testSave() {
136: try {
137: TestData.createDynaClassAndTable();
138:
139: //
140: // create a new testaikp
141: //
142: siteContextAikp = new SiteContextAikp(TestData.dynaClass);
143: siteContextAikp.setSiteContext(TestData.siteContext1);
144: siteContextAikp.setInt("id", 0);
145: siteContextAikp.setInt("sitecontext_id", 0);
146: siteContextAikp.setString("field", "value");
147: siteContextAikp.saveNew();
148:
149: //
150: // load it
151: //
152: siteContextAikp = new SiteContextAikp(TestData.dynaClass);
153: siteContextAikp.setSiteContext(TestData.siteContext1);
154: siteContextAikp.load(new IntKey("1"));
155:
156: //
157: // force it's sitecontext_id to 0 (this happens in
158: // AikpAction.populatePersistable)
159: //
160: siteContextAikp.setInt("sitecontext_id", 0);
161:
162: siteContextAikp.save();
163:
164: //
165: // should save site context id
166: //
167: rs = ConnectionSingleton
168: .runQuery("SELECT field FROM sitecontextaikp WHERE sitecontext_id=1");
169: assertNotNull(rs);
170: assertTrue(rs.next());
171: assertEquals("value", rs.getString("field"));
172: assertTrue(!rs.next());
173:
174: ConnectionSingleton.close(rs);
175: } catch (Exception e) {
176: e.printStackTrace();
177: fail();
178: }
179: }
180:
181: public void testLoad() {
182: try {
183: TestData.createSiteContextAikps();
184:
185: siteContextAikp = new SiteContextAikp(TestData.dynaClass);
186:
187: //
188: // should throw an exception when site context has not been set
189: //
190: try {
191: siteContextAikp.load(new IntKey(1));
192: fail("No exception thrown");
193: } catch (RuntimeException e) {
194: // success
195: }
196:
197: //
198: // set site context and load
199: //
200: siteContextAikp.setSiteContext(TestData.siteContext1);
201: siteContextAikp.load(new IntKey(1));
202:
203: //
204: // should save site context id
205: //
206: assertEquals("siteContextAikp1", siteContextAikp
207: .getString("field"));
208:
209: //
210: // set wrong site context and load
211: //
212: try {
213: siteContextAikp.setSiteContext(TestData.siteContext2);
214: siteContextAikp.load(new IntKey(1));
215: fail("No exception thrown");
216: } catch (RuntimeException e) {
217: // success
218: }
219: } catch (Exception e) {
220: e.printStackTrace();
221: fail();
222: }
223: }
224:
225: public void testLoadWhere() {
226: try {
227: TestData.createSiteContextAikps();
228:
229: siteContextAikp = new SiteContextAikp(TestData.dynaClass);
230:
231: //
232: // should throw an exception when site context has not been set
233: //
234: try {
235: siteContextAikp.load("field='siteContextAikp1'");
236: fail("No exception thrown");
237: } catch (RuntimeException e) {
238: // success
239: }
240:
241: //
242: // set site context and load
243: //
244: siteContextAikp.setSiteContext(TestData.siteContext1);
245: siteContextAikp.load("field='siteContextAikp1'");
246:
247: assertEquals(1, siteContextAikp.getInt("id"));
248:
249: //
250: // set wrong site context and load
251: //
252: try {
253: siteContextAikp.setSiteContext(TestData.siteContext2);
254: siteContextAikp.load("field='siteContextAikp1'");
255: fail("No exception thrown");
256: } catch (RuntimeException e) {
257: // success
258: }
259: } catch (Exception e) {
260: e.printStackTrace();
261: fail();
262: }
263: }
264:
265: public void testLoadAll() {
266: try {
267: TestData.createSiteContextAikps();
268:
269: siteContextAikp = new SiteContextAikp(TestData.dynaClass);
270:
271: //
272: // should throw an exception when site context has not been set
273: //
274: try {
275: siteContextAikp.loadAll(null, "field");
276: fail("No exception thrown");
277: } catch (RuntimeException e) {
278: // success
279: }
280:
281: //
282: // set site context and load all
283: //
284: siteContextAikp.setSiteContext(TestData.siteContext1);
285: list = siteContextAikp.loadAll(null, "field");
286:
287: //
288: // should get site context 1 aikps; their sitecontext_id and site context
289: // should be set
290: //
291: assertEquals(2, list.size());
292:
293: siteContextAikp = (SiteContextAikp) list.get(0);
294: assertNotNull(siteContextAikp);
295: assertEquals("siteContextAikp1", siteContextAikp
296: .getString("field"));
297: assertEquals(TestData.siteContext1, siteContextAikp
298: .getSiteContext());
299: assertEquals(1, siteContextAikp.getInt("sitecontext_id"));
300:
301: siteContextAikp = (SiteContextAikp) list.get(1);
302: assertNotNull(siteContextAikp);
303: assertEquals("siteContextAikp2", siteContextAikp
304: .getString("field"));
305: assertEquals(TestData.siteContext1, siteContextAikp
306: .getSiteContext());
307: assertEquals(1, siteContextAikp.getInt("sitecontext_id"));
308: } catch (Exception e) {
309: e.printStackTrace();
310: fail();
311: }
312: }
313: }
|