001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.prefs;
018:
019: import java.util.prefs.BackingStoreException;
020: import java.util.prefs.Preferences;
021:
022: import junit.framework.Test;
023: import junit.framework.TestSuite;
024:
025: import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
026:
027: /**
028: * <p>
029: * Unit testing for {@link Preferences}.
030: * </p>
031: *
032: * @author <a href="dlestrat@yahoo.com">David Le Strat </a>
033: */
034: public class TestPreferences extends DatasourceEnabledSpringTestCase {
035:
036: /**
037: * @see junit.framework.TestCase#setUp()
038: */
039: public void setUp() throws Exception {
040: super .setUp();
041:
042: // Make sure we are starting with a clean slate
043: clearChildren(Preferences.userRoot());
044: clearChildren(Preferences.systemRoot());
045: }
046:
047: /**
048: * @see junit.framework.TestCase#tearDown()
049: */
050: public void tearDown() throws Exception {
051: super .tearDown();
052: }
053:
054: /**
055: * @return The test suite.
056: */
057: public static Test suite() {
058: // All methods starting with "test" will be executed in the test suite.
059: return new TestSuite(TestPreferences.class);
060: }
061:
062: /**
063: * <p>
064: * Test user root.
065: * </p>
066: */
067: public void testUserRoot() {
068:
069: Preferences prefs = Preferences.userRoot();
070: if (null != prefs) {
071: assertTrue("expected user root == '/', "
072: + prefs.absolutePath(), prefs.absolutePath()
073: .equals("/"));
074: } else {
075: assertTrue("expected user root == '/', " + prefs, false);
076: }
077: }
078:
079: /**
080: * <p>
081: * Test system root.
082: * </p>
083: */
084: public void testSystemRoot() {
085: Preferences prefs = Preferences.systemRoot();
086: if (null != prefs) {
087: assertTrue("expected system root == '/', "
088: + prefs.absolutePath(), prefs.absolutePath()
089: .equals("/"));
090: } else {
091: assertTrue("expected system root == '/', " + prefs, false);
092: }
093: }
094:
095: /**
096: * <p>
097: * Test node and whether children exist under a given node.
098: * </p>
099: */
100: public void testNodeAndChildrenNames() {
101: Preferences prefs = Preferences.userRoot();
102: // Test without children.
103: try {
104: String[] childrenNames = prefs.childrenNames();
105: if (childrenNames.length > 0) {
106: assertTrue("expected no children, "
107: + childrenNames.length + ", "
108: + childrenNames[0], childrenNames.length == 0);
109: }
110: } catch (BackingStoreException bse) {
111: assertTrue("backing store exception: " + bse, false);
112: }
113:
114: // Absolute path.
115: // 1. The node does not exist. Create it.
116: Preferences prefs0 = Preferences.userRoot().node("/an1/san1");
117: assertNotNull("should not be null", prefs0);
118: assertTrue("expected node == /an1/san1, "
119: + prefs0.absolutePath(), prefs0.absolutePath().equals(
120: "/an1/san1"));
121:
122: // 2. If node exists. Get it.
123: Preferences prefs1 = Preferences.userRoot().node("/an1/san1");
124: assertNotNull("should not be null", prefs1);
125: assertTrue("expected node == /an1/san1, "
126: + prefs1.absolutePath(), prefs1.absolutePath().equals(
127: "/an1/san1"));
128:
129: // Relative path.
130: Preferences prefs3 = Preferences.userRoot().node("/an1");
131: Preferences prefs4 = prefs3.node("rn1/srn1");
132: assertNotNull("should not be null", prefs4);
133: assertTrue("expected node == /an1/rn1/srn1, "
134: + prefs4.absolutePath(), prefs4.absolutePath().equals(
135: "/an1/rn1/srn1"));
136:
137: try {
138: String[] childrenNames = prefs3.childrenNames();
139: assertEquals("should have 2 children", 2,
140: childrenNames.length);
141: } catch (BackingStoreException bse) {
142: assertTrue("backing store exception: " + bse, false);
143: }
144:
145: // Remove all nodes.
146: try {
147: prefs3.removeNode();
148: } catch (BackingStoreException bse) {
149: assertTrue("backing store exception: " + bse, false);
150: }
151:
152: }
153:
154: /**
155: * <p>
156: * Test adding properties to a property set node and get property keys for a given node.
157: * </p>
158: *
159: * @throws Exception
160: */
161: public void testPropertyAndPropertyKeys() throws Exception {
162: // 1. Current node does not have any property associated to it. We are adding
163: // a property at the user root level.
164: // No property has been defined nor added to the node. This should return
165: // the property value
166: Preferences pref0 = Preferences.userRoot();
167: try {
168: String[] propertyKeys = pref0.keys();
169: if (propertyKeys.length > 0) {
170: assertTrue("expected no children, "
171: + propertyKeys.length + ", " + propertyKeys[0],
172: propertyKeys.length == 0);
173: }
174: } catch (BackingStoreException bse) {
175: assertTrue("backing store exception: " + bse, false);
176: }
177:
178: pref0.put("propertyName0", "true");
179: String prop = pref0.get("propertyName0", null);
180: assertTrue("should be prop == true.", prop.equals("true"));
181:
182: // 2. Current node has properties associated to it.
183: Preferences pref1 = Preferences.userRoot().node(
184: "/user/principal1/propertyset1");
185: pref1.put("propertyName0", "true");
186: String prop1 = pref1.get("propertyName0", null);
187: assertTrue("expected prop1 == true, " + prop1, prop1
188: .equals("true"));
189:
190: // There should be 1 property under pref1.
191: try {
192: String[] propertyKeys = pref1.keys();
193: assertEquals("expected 1 child, ", 1, propertyKeys.length);
194: } catch (BackingStoreException bse) {
195: assertTrue("backing store exception: " + bse, false);
196: }
197:
198: // Test remove property.
199: pref1.remove("propertyName0");
200: prop1 = pref1.get("propertyName0", null);
201: assertNull("should be null.", prop1);
202:
203: // Remove all nodes with properties assigned to property sets.
204: pref1.put("propertyName0", "true");
205: prop1 = pref1.get("propertyName0", null);
206: assertTrue("expected prop1 == true, " + prop1, prop1
207: .equals("true"));
208:
209: try {
210: Preferences pref2 = Preferences.userRoot().node("/user");
211: pref2.removeNode();
212: } catch (BackingStoreException bse) {
213: assertTrue("backing store exception: " + bse, false);
214: }
215:
216: }
217:
218: public void testNodeRemoval() throws Exception {
219: Preferences prefs = Preferences.userRoot();
220:
221: final String test_node = "removeTest";
222:
223: assertFalse(prefs.nodeExists(test_node));
224:
225: Preferences removeNode = prefs.node(test_node);
226:
227: assertNotNull(removeNode);
228:
229: // now remove then re-add and see if a IllegalStateException is thrown
230:
231: removeNode.removeNode();
232: assertFalse(prefs.nodeExists(test_node));
233:
234: try {
235: removeNode.childrenNames();
236: assertFalse(
237: "An IllegalStateException should have been thrown by the AbtractPreferences class",
238: true);
239: } catch (IllegalStateException e) {
240:
241: }
242: }
243:
244: /**
245: * <p>
246: * Clear all the children.
247: * </p>
248: *
249: * @param node
250: * @throws Exception
251: */
252: protected void clearChildren(Preferences node) throws Exception {
253: String[] names = node.childrenNames();
254: for (int i = 0; i < names.length; i++) {
255: node.node(names[i]).removeNode();
256: }
257: // Remove the properties of the current node.
258: String[] keys = node.keys();
259: for (int j = 0; j < keys.length; j++) {
260: node.remove(keys[j]);
261: }
262: }
263:
264: /**
265: * @see org.apache.jetspeed.components.test.AbstractSpringTestCase#getConfigurations()
266: */
267: protected String[] getConfigurations() {
268: return new String[] { "prefs.xml", "transaction.xml",
269: "cache.xml" };
270: }
271: }
|