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.Iterator;
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: import org.apache.jetspeed.prefs.om.Node;
027: import org.apache.jetspeed.prefs.om.Property;
028:
029: /**
030: * <p>
031: * Unit testing for {@link Preferences}.
032: * </p>
033: *
034: * @author <a href="dlestrat@yahoo.com">David Le Strat </a>
035: */
036: public class TestPreferencesProvider extends
037: DatasourceEnabledSpringTestCase {
038: private PreferencesProvider provider;
039:
040: /**
041: * @see junit.framework.TestCase#setUp()
042: */
043: public void setUp() throws Exception {
044: super .setUp();
045: provider = (PreferencesProvider) ctx.getBean("prefsProvider");
046:
047: // Make sure we are starting with a clean slate
048: clearChildren(Preferences.userRoot());
049: clearChildren(Preferences.systemRoot());
050: }
051:
052: /**
053: * @see junit.framework.TestCase#tearDown()
054: */
055: public void tearDown() throws Exception {
056: super .tearDown();
057: }
058:
059: /**
060: * @return The test suite.
061: */
062: public static Test suite() {
063: // All methods starting with "test" will be executed in the test suite.
064: return new TestSuite(TestPreferencesProvider.class);
065: }
066:
067: public void testLookupProperty() throws Exception {
068: Preferences info = Preferences.userRoot().node(
069: "/user/dynamite/userinfo");
070: info.put("user.name.family", "Dynamite");
071: info.put("user.name.given", "Napolean");
072: info.put("user.email", "napolean@dynamite.xxx");
073: info.flush();
074:
075: Iterator result = provider.lookupPreference("userinfo",
076: "user.email", "napolean@dynamite.xxx").iterator();
077: int count = 0;
078: while (result.hasNext()) {
079: Node node = (Node) result.next();
080: System.out.println("node = " + node.getFullPath());
081: Iterator props = node.getNodeProperties().iterator();
082: while (props.hasNext()) {
083: Property prop = (Property) props.next();
084: String name = prop.getPropertyName();
085: String value = prop.getPropertyValue();
086: if ("user.name.family".equals(name)) {
087: assertTrue("family name wrong " + value, "Dynamite"
088: .equals(value));
089: } else if ("user.name.given".equals(name)) {
090: assertTrue("given name wrong " + value, "Napolean"
091: .equals(value));
092: } else if ("user.email".equals(name)) {
093: assertTrue("email is wrong " + value,
094: "napolean@dynamite.xxx".equals(value));
095: } else {
096: assertTrue("bad property name " + name, false);
097: }
098: }
099: count++;
100: }
101: assertTrue("test-1: count is one " + count, count == 1);
102: }
103:
104: /**
105: * <p>
106: * Clears all test data.
107: * </p>
108: *
109: * @param node
110: * @throws Exception
111: */
112: protected void clearChildren(Preferences node) throws Exception {
113: String[] names = node.childrenNames();
114: for (int i = 0; i < names.length; i++) {
115: node.node(names[i]).removeNode();
116: }
117: }
118:
119: /**
120: * @see org.apache.jetspeed.components.test.AbstractSpringTestCase#getConfigurations()
121: */
122: protected String[] getConfigurations() {
123: return new String[] { "prefs.xml", "transaction.xml",
124: "cache.xml" };
125: }
126: }
|