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.transfer;
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.property.*;
030: import com.methodhead.test.*;
031: import com.methodhead.sitecontext.*;
032: import com.methodhead.*;
033:
034: public class SiteExtensionTest extends TestCase {
035:
036: SiteExtension siteExtension = null;
037: Extension[] siteExtensions = null;
038: List list = null;
039:
040: static {
041: TestUtils.initLogger();
042: TestUtils.initDb();
043: }
044:
045: public SiteExtensionTest(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 testGetInstalledExtensions() {
062:
063: //
064: // don't define extensions property
065: //
066: siteExtension = new SiteExtension();
067: siteExtensions = siteExtension.getInstalledExtensions();
068:
069: assertNotNull(siteExtensions);
070: assertEquals(0, siteExtensions.length);
071:
072: //
073: // define extensions
074: //
075: Property
076: .setProperty(
077: SiteContext.getDefaultContext(),
078: SiteExtension.PROPERTY_EXTENSIONS,
079: "com.methodhead.transfer.MockExtension, com.methodhead.transfer.MockExtension,\ncom.methodhead.transfer.MockExtension");
080:
081: siteExtensions = siteExtension.getInstalledExtensions();
082:
083: assertNotNull(siteExtensions);
084: assertEquals(3, siteExtensions.length);
085:
086: assertEquals("MockExtension", siteExtensions[0].getName());
087: assertEquals("MockExtension", siteExtensions[1].getName());
088: assertEquals("MockExtension", siteExtensions[2].getName());
089: }
090:
091: public void testLoadAllForSiteContext() {
092: TestData.createSiteExtensions();
093:
094: //
095: // load for site context 1
096: //
097: siteExtension = new SiteExtension();
098: list = siteExtension
099: .loadAllForSiteContext(TestData.siteContext1);
100:
101: assertNotNull(list);
102: assertEquals(1, list.size());
103:
104: siteExtension = (SiteExtension) list.get(0);
105: assertEquals("com.methodhead.transfer.MockExtension",
106: siteExtension.getString("class_name"));
107:
108: //
109: // load for site context 2
110: //
111: siteExtension = new SiteExtension();
112: list = siteExtension
113: .loadAllForSiteContext(TestData.siteContext2);
114:
115: assertNotNull(list);
116: assertEquals(0, list.size());
117: }
118:
119: public void testGetExtensionsForSiteContext() {
120: TestData.createSiteExtensions();
121:
122: //
123: // load for site context 1
124: //
125: siteExtension = new SiteExtension();
126: siteExtensions = siteExtension
127: .getExtensionsForSiteContext(TestData.siteContext1);
128:
129: assertNotNull(siteExtensions);
130: assertEquals(1, siteExtensions.length);
131: assertEquals("MockExtension", siteExtensions[0].getName());
132:
133: //
134: // load for site context 2
135: //
136: siteExtension = new SiteExtension();
137: siteExtensions = siteExtension
138: .getExtensionsForSiteContext(TestData.siteContext2);
139:
140: assertNotNull(siteExtensions);
141: assertEquals(0, siteExtensions.length);
142: }
143:
144: public void testLoad() {
145: TestData.createSiteExtensions();
146:
147: //
148: // load a specific site extension
149: //
150: siteExtension = new SiteExtension();
151: siteExtension.load(TestData.siteContext1,
152: "com.methodhead.transfer.MockExtension");
153:
154: assertEquals(1, siteExtension.getInt("sitecontext_id"));
155: assertEquals(true, siteExtension.getBoolean("enabled"));
156: assertEquals("com.methodhead.transfer.MockExtension",
157: siteExtension.getString("class_name"));
158:
159: //
160: // load a non-existant site extension
161: //
162: try {
163: siteExtension.load(TestData.siteContext1, "invalid");
164: fail("No exception thrown.");
165: } catch (Exception e) {
166: }
167: }
168:
169: public void testSave() {
170: TestData.createSiteExtensions();
171:
172: //
173: // save an unload site extension
174: //
175: try {
176: siteExtension = new SiteExtension();
177: siteExtension.save();
178: fail();
179: } catch (Exception e) {
180: }
181:
182: //
183: // update a site extension
184: //
185: TestData.siteExtension1.setBoolean("enabled", false);
186: TestData.siteExtension1.save();
187:
188: siteExtension = new SiteExtension();
189: siteExtension.load(TestData.siteContext1,
190: "com.methodhead.transfer.MockExtension");
191:
192: assertEquals(false, siteExtension.getBoolean("enabled"));
193: }
194:
195: public void testDelete() {
196: TestData.createSiteExtensions();
197:
198: //
199: // delete an unload site extension
200: //
201: try {
202: siteExtension = new SiteExtension();
203: siteExtension.delete();
204: fail();
205: } catch (Exception e) {
206: }
207:
208: //
209: // delete a site extension
210: //
211: TestData.siteExtension1.delete();
212:
213: siteExtension = new SiteExtension();
214:
215: try {
216: siteExtension.load(TestData.siteContext1,
217: "com.methodhead.transfer.MockExtension");
218: fail();
219: } catch (Exception e) {
220: }
221: }
222:
223: public void testIsExtensionActive() {
224: TestData.createSiteExtensions();
225:
226: siteExtension = new SiteExtension();
227:
228: //
229: // an extension that exists for a site context and is enabled is active
230: //
231: assertEquals(true, siteExtension.isExtensionActive(
232: TestData.siteContext1,
233: "com.methodhead.transfer.MockExtension"));
234:
235: //
236: // an extension that exists for a site context but is disabled is not active
237: //
238: TestData.siteExtension1.setBoolean("enabled", false);
239: TestData.siteExtension1.save();
240:
241: assertEquals(false, siteExtension.isExtensionActive(
242: TestData.siteContext1,
243: "com.methodhead.transfer.MockExtension"));
244:
245: //
246: // an extension that doesn't exist for a site context is not active
247: //
248: TestData.siteExtension1.delete();
249:
250: assertEquals(false, siteExtension.isExtensionActive(
251: TestData.siteContext1,
252: "com.methodhead.transfer.MockExtension"));
253: }
254: }
|