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.Collection;
020: import java.util.Iterator;
021:
022: import junit.framework.Test;
023: import junit.framework.TestSuite;
024:
025: import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
026: import org.apache.jetspeed.prefs.om.Node;
027: import org.apache.jetspeed.prefs.om.Property;
028:
029: public class TestNodePreferences extends
030: DatasourceEnabledSpringTestCase {
031: private PreferencesProvider provider;
032:
033: /**
034: * @see junit.framework.TestCase#setUp()
035: */
036: public void setUp() throws Exception {
037: super .setUp();
038: provider = (PreferencesProvider) ctx.getBean("prefsProvider");
039: }
040:
041: /**
042: * @see junit.framework.TestCase#tearDown()
043: */
044: public void tearDown() throws Exception {
045: super .tearDown();
046: }
047:
048: /**
049: * @return The test suite.
050: */
051: public static Test suite() {
052: // All methods starting with "test" will be executed in the test suite.
053: return new TestSuite(TestNodePreferences.class);
054: }
055:
056: /**
057: * <p>
058: * Test node and whether children exist under a given node.
059: * </p>
060: */
061: String[] users = { "guest", "david", "admin" };
062: int ENTITY_SIZE = 50;
063: int PREF_SIZE = 20;
064: boolean reset = false;
065: boolean disableReads = false;
066:
067: public void testNodes() {
068: if (1 == 1)
069: return; // disable this test, its a performance test
070: assertNotNull("provider is null", provider);
071: Node entityRoot = null;
072: try {
073: boolean hasBeenPopulated = false;
074: Node root = provider.getNode("/", 0);
075: assertNotNull("root node is null", root);
076: if (!provider.nodeExists("/portlet_entity", 0)) {
077: entityRoot = provider.createNode(root,
078: "portlet_entity", 0, "/portlet_entity");
079: assertNotNull("entity-root node is null", entityRoot);
080: } else {
081: if (reset) {
082: Node pe = provider.getNode("/portlet_entity", 0);
083: provider.removeNode(root, pe);
084: entityRoot = provider.createNode(root,
085: "portlet_entity", 0, "/portlet_entity");
086: assertNotNull("entity-root node is null",
087: entityRoot);
088: } else
089: hasBeenPopulated = true;
090: }
091: if (entityRoot == null)
092: entityRoot = provider.getNode("/portlet_entity", 0);
093: if (hasBeenPopulated == false) {
094: for (int ix = 0; ix < ENTITY_SIZE; ix++) {
095: String path = "/portlet_entity/" + ix;
096: Node entity = provider.createNode(entityRoot,
097: new Integer(ix).toString(), 0, path);
098: assertNotNull(path, entity);
099: for (int iy = 0; iy < users.length; iy++) {
100: String uPath = "/portlet_entity/" + ix + "/"
101: + users[iy];
102: Node uEntity = provider.createNode(entity,
103: users[iy], 0, uPath);
104: assertNotNull(uPath, uEntity);
105: String pPath = uPath + "/preferences";
106: Node pEntity = provider.createNode(uEntity,
107: "preferences", 0, pPath);
108: assertNotNull(pPath, pEntity);
109: for (int iz = 0; iz < PREF_SIZE; iz++) {
110: String zPath = pPath + "/pref-" + iz;
111: Node zEntity = provider.createNode(pEntity,
112: "pref-" + iz, 0, zPath);
113: assertNotNull(zPath, zEntity);
114: // size node
115: Node size = provider.createNode(zEntity,
116: "size", 0, zPath + "/size");
117: assertNotNull(zPath + "/size", size);
118: // values node
119: Node values = provider.createNode(zEntity,
120: "values", 0, zPath + "/values");
121: assertNotNull(values + "/values", values);
122: // size property
123: Property sizeProp = provider
124: .createProperty(size, "size", "1");
125: size.getNodeProperties().add(sizeProp);
126: provider.storeNode(size);
127: // values property
128: Property valueProp = provider
129: .createProperty(values, "0",
130: new Integer(iz + 1000)
131: .toString());
132: values.getNodeProperties().add(valueProp);
133: provider.storeNode(values);
134: }
135: }
136: }
137: }
138: // Test for data using both new and old paths
139: if (disableReads == false) {
140: long start = System.currentTimeMillis();
141: for (int ix = 0; ix < ENTITY_SIZE; ix++) {
142: for (int iy = 0; iy < users.length; iy++) {
143: for (int iz = 0; iz < PREF_SIZE; iz++) {
144: Node n;
145: String key = "/portlet_entity/" + ix + "/"
146: + users[iy] + "/preferences/pref-"
147: + iz;
148: n = provider.getNode(key, 0);
149: assertNotNull("null pref: " + key, n);
150: Collection c = provider.getChildren(n);
151: assertNotNull("null collection ", c);
152: Iterator it = c.iterator();
153: while (it.hasNext()) {
154: Node child = (Node) it.next();
155: if (child.getNodeName().equals("size")) {
156: Object props[] = child
157: .getNodeProperties()
158: .toArray();
159: assertTrue(
160: "props isa ",
161: (props[0] instanceof Property));
162: Property p = (Property) props[0];
163: String size = p.getPropertyValue();
164: assertTrue("child size name ",
165: "size".equals(p
166: .getPropertyName()));
167: assertTrue("child size value ", "1"
168: .equals(size));
169: } else if (child.getNodeName().equals(
170: "values")) {
171: Object props[] = child
172: .getNodeProperties()
173: .toArray();
174: assertTrue(
175: "props isa ",
176: (props[0] instanceof Property));
177: Property p = (Property) props[0];
178: String value = p.getPropertyValue();
179: assertTrue("child value name ",
180: "0".equals(p
181: .getPropertyName()));
182: assertTrue("child value value ",
183: new Integer(iz + 1000)
184: .toString().equals(
185: value));
186: }
187:
188: }
189: }
190: }
191: }
192: long end = System.currentTimeMillis();
193: System.out.println("Retrieval time total: "
194: + (end - start));
195: }
196: } catch (Exception e) {
197: e.printStackTrace();
198: assertTrue("exception in testNodes", false);
199: }
200: }
201:
202: /**
203: * @see org.apache.jetspeed.components.test.AbstractSpringTestCase#getConfigurations()
204: */
205: protected String[] getConfigurations() {
206: return new String[] { "prefs.xml", "transaction.xml",
207: "cache.xml" };
208: }
209: }
|