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.property;
022:
023: import java.util.*;
024: import java.io.*;
025: import java.sql.*;
026: import junit.framework.*;
027: import org.apache.log4j.*;
028: import com.methodhead.persistable.*;
029: import com.methodhead.test.*;
030: import com.methodhead.sitecontext.*;
031:
032: public class PropertyTest extends DbTestCase {
033:
034: SiteContext siteContext1_ = null;
035:
036: ResultSet rs = null;
037: Property p = null;
038:
039: private void createData() {
040: siteContext1_ = new SiteContext();
041: siteContext1_.saveNew();
042: }
043:
044: public PropertyTest(String name) {
045: super (name);
046: }
047:
048: protected void setUp() {
049: //setLogLevel( Level.DEBUG );
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 testSetProperty() {
062: try {
063: createData();
064:
065: Property.setProperty(siteContext1_, "property", "value",
066: "description", new Boolean(false));
067: rs = ConnectionSingleton
068: .runQuery("SELECT value, description, system_property FROM mh_property WHERE name='property' AND sitecontext_id="
069: + siteContext1_.getInt("id"));
070:
071: assertTrue(rs.next());
072: assertEquals("value", rs.getString("value"));
073: assertEquals("description", rs.getString("description"));
074: assertEquals(0, rs.getInt("system_property"));
075: assertTrue(!rs.next());
076:
077: ConnectionSingleton.close(rs);
078: Property.setProperty(siteContext1_, "property", "value2",
079: null, null);
080: rs = ConnectionSingleton
081: .runQuery("SELECT value, description, system_property FROM mh_property WHERE name='property' AND sitecontext_id="
082: + siteContext1_.getInt("id"));
083:
084: assertTrue(rs.next());
085: assertEquals("value2", rs.getString("value"));
086: assertEquals("description", rs.getString("description"));
087: assertEquals(0, rs.getInt("system_property"));
088: assertTrue(!rs.next());
089:
090: ConnectionSingleton.close(rs);
091: Property.setProperty(siteContext1_, "property", null,
092: "description2", null);
093: rs = ConnectionSingleton
094: .runQuery("SELECT value, description, system_property FROM mh_property WHERE name='property' AND sitecontext_id="
095: + siteContext1_.getInt("id"));
096:
097: assertTrue(rs.next());
098: assertEquals("value2", rs.getString("value"));
099: assertEquals("description2", rs.getString("description"));
100: assertEquals(0, rs.getInt("system_property"));
101: assertTrue(!rs.next());
102:
103: ConnectionSingleton.close(rs);
104: Property.setProperty(siteContext1_, "property", null, null,
105: new Boolean(true));
106: rs = ConnectionSingleton
107: .runQuery("SELECT value, description, system_property FROM mh_property WHERE name='property' AND sitecontext_id="
108: + siteContext1_.getInt("id"));
109:
110: assertTrue(rs.next());
111: assertEquals("value2", rs.getString("value"));
112: assertEquals("description2", rs.getString("description"));
113: assertEquals(1, rs.getInt("system_property"));
114: assertTrue(!rs.next());
115:
116: ConnectionSingleton.close(rs);
117: Property.setProperty(siteContext1_, "property2", null,
118: null, null);
119: rs = ConnectionSingleton
120: .runQuery("SELECT value, description, system_property FROM mh_property WHERE name='property2' AND sitecontext_id="
121: + siteContext1_.getInt("id"));
122:
123: assertTrue(rs.next());
124: assertEquals("", rs.getString("value"));
125: assertEquals("", rs.getString("description"));
126: assertEquals(0, rs.getInt("system_property"));
127: assertTrue(!rs.next());
128:
129: ConnectionSingleton.close(rs);
130: Property.setProperty(siteContext1_, "property3", "value3");
131: rs = ConnectionSingleton
132: .runQuery("SELECT value, description, system_property FROM mh_property WHERE name='property3' AND sitecontext_id="
133: + siteContext1_.getInt("id"));
134:
135: assertTrue(rs.next());
136: assertEquals("value3", rs.getString("value"));
137: assertEquals("", rs.getString("description"));
138: assertEquals(0, rs.getInt("system_property"));
139: assertTrue(!rs.next());
140:
141: ConnectionSingleton.close(rs);
142: Property.setProperty(siteContext1_, "property3", "value4");
143: rs = ConnectionSingleton
144: .runQuery("SELECT value, description, system_property FROM mh_property WHERE name='property3' AND sitecontext_id="
145: + siteContext1_.getInt("id"));
146:
147: assertTrue(rs.next());
148: assertEquals("value4", rs.getString("value"));
149: assertEquals("", rs.getString("description"));
150: assertEquals(0, rs.getInt("system_property"));
151: assertTrue(!rs.next());
152:
153: ConnectionSingleton.close(rs);
154: } catch (Exception e) {
155: e.printStackTrace();
156: fail();
157: }
158: }
159:
160: public void testGetProperty() {
161: try {
162: createData();
163:
164: assertNull(Property.getProperty(siteContext1_, "property"));
165:
166: Property.setProperty(siteContext1_, "property", "value");
167:
168: assertNotNull(Property.getProperty(siteContext1_,
169: "property"));
170: assertEquals("value", Property.getProperty(siteContext1_,
171: "property"));
172: assertEquals("value", Property.getProperty(siteContext1_,
173: "property", "default"));
174: assertEquals("default", Property.getProperty(siteContext1_,
175: "property2", "default"));
176: } catch (Exception e) {
177: e.printStackTrace();
178: fail();
179: }
180: }
181:
182: public void testGetPropertyArray() {
183: try {
184: createData();
185:
186: String[] vals = null;
187:
188: assertNull(Property.getProperty(siteContext1_, "property"));
189:
190: Property.setProperty(siteContext1_, "property", "value");
191:
192: vals = Property.getPropertyArray(siteContext1_, "property");
193:
194: assertNotNull(vals);
195: assertEquals(1, vals.length);
196: assertEquals("value", vals[0]);
197:
198: Property.setProperty(siteContext1_, "property",
199: " value1, value2 ");
200:
201: vals = Property.getPropertyArray(siteContext1_, "property");
202:
203: assertNotNull(vals);
204: assertEquals(2, vals.length);
205: assertEquals("value1", vals[0]);
206: assertEquals("value2", vals[1]);
207: } catch (Exception e) {
208: e.printStackTrace();
209: fail();
210: }
211: }
212:
213: public void testLoadForName() {
214: try {
215: createData();
216:
217: Property.setProperty(siteContext1_, "property", "value");
218:
219: p = new Property();
220: try {
221: p.loadForName("property");
222: fail("No exception thrown.");
223: } catch (PersistableException e) {
224: }
225:
226: p = new Property();
227: p.setSiteContext(siteContext1_);
228: p.loadForName("property");
229:
230: assertEquals("property", p.getString("name"));
231: assertEquals("value", p.getString("value"));
232: assertEquals("", p.getString("description"));
233: assertEquals(false, p.getBoolean("system_property"));
234: } catch (Exception e) {
235: e.printStackTrace();
236: fail();
237: }
238: }
239:
240: public void testLoadAll() {
241: try {
242: createData();
243:
244: Property property = null;
245: List properties = null;
246:
247: Property.setProperty(siteContext1_, "property2", "value");
248: Property.setProperty(siteContext1_, "property1", "value");
249:
250: properties = Property.loadAll(null);
251: assertNotNull(properties);
252: assertEquals(0, properties.size());
253:
254: properties = Property.loadAll(siteContext1_);
255: assertNotNull(properties);
256: assertEquals(2, properties.size());
257:
258: property = (Property) properties.get(0);
259: assertEquals("property1", property.getString("name"));
260:
261: property = (Property) properties.get(1);
262: assertEquals("property2", property.getString("name"));
263: } catch (Exception e) {
264: e.printStackTrace();
265: fail();
266: }
267: }
268: }
|