1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.prefs;
18  
19  import java.util.Collection;
20  import java.util.Iterator;
21  
22  import junit.framework.Test;
23  import junit.framework.TestSuite;
24  
25  import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
26  import org.apache.jetspeed.prefs.om.Node;
27  import org.apache.jetspeed.prefs.om.Property;
28  
29  public class TestNodePreferences extends DatasourceEnabledSpringTestCase
30  {
31      private PreferencesProvider provider;
32  
33      /***
34       * @see junit.framework.TestCase#setUp()
35       */
36      public void setUp() throws Exception
37      {
38          super.setUp();      
39          provider = (PreferencesProvider) ctx.getBean("prefsProvider");        
40      }
41  
42      /***
43       * @see junit.framework.TestCase#tearDown()
44       */
45      public void tearDown() throws Exception
46      {
47          super.tearDown();
48      }
49  
50      /***
51       * @return The test suite.
52       */
53      public static Test suite()
54      {
55          // All methods starting with "test" will be executed in the test suite.
56          return new TestSuite(TestNodePreferences.class);
57      }
58      
59  
60      /***
61       * <p>
62       * Test node and whether children exist under a given node.
63       * </p>
64       */
65      String [] users = { "guest", "david", "admin" };
66      int ENTITY_SIZE = 50;
67      int PREF_SIZE = 20;
68      boolean reset = false;
69      boolean disableReads = false;
70      
71      public void testNodes()
72      {
73          if (1 == 1) return ; // disable this test, its a performance test
74          assertNotNull("provider is null", provider);
75          Node entityRoot = null;
76          try
77          {
78              boolean hasBeenPopulated = false;
79              Node root = provider.getNode("/", 0);
80              assertNotNull("root node is null", root);
81              if (!provider.nodeExists("/portlet_entity", 0))
82              {
83                  entityRoot = provider.createNode(root, "portlet_entity", 0, "/portlet_entity");
84                  assertNotNull("entity-root node is null", entityRoot);                
85              }
86              else
87              {
88                  if (reset)
89                  {
90                      Node pe = provider.getNode("/portlet_entity", 0);
91                      provider.removeNode(root, pe);
92                      entityRoot = provider.createNode(root, "portlet_entity", 0, "/portlet_entity");
93                      assertNotNull("entity-root node is null", entityRoot);
94                  }
95                  else
96                      hasBeenPopulated = true;                
97              }
98              if (entityRoot == null)
99                  entityRoot = provider.getNode("/portlet_entity", 0);
100             if (hasBeenPopulated == false)
101             {
102                 for (int ix = 0; ix < ENTITY_SIZE; ix++)
103                 {
104                     String path = "/portlet_entity/" + ix;
105                     Node entity = provider.createNode(entityRoot, new Integer(ix).toString(), 0, path);
106                     assertNotNull(path, entity);
107                     for (int iy = 0; iy < users.length; iy++)
108                     {
109                         String uPath = "/portlet_entity/" + ix + "/" + users[iy];
110                         Node uEntity = provider.createNode(entity, users[iy], 0, uPath);
111                         assertNotNull(uPath, uEntity);
112                         String pPath = uPath + "/preferences";
113                         Node pEntity = provider.createNode(uEntity, "preferences", 0, pPath);
114                         assertNotNull(pPath, pEntity);
115                         for (int iz = 0; iz < PREF_SIZE; iz++)
116                         {
117                             String zPath = pPath + "/pref-" + iz;
118                             Node zEntity = provider.createNode(pEntity, "pref-" + iz, 0, zPath);
119                             assertNotNull(zPath, zEntity);
120                             // size node
121                             Node size = provider.createNode(zEntity, "size", 0, zPath + "/size" );
122                             assertNotNull(zPath + "/size", size);                        
123                             // values node
124                             Node values = provider.createNode(zEntity, "values", 0, zPath + "/values" );
125                             assertNotNull(values + "/values", values);                        
126                             // size property
127                             Property sizeProp = provider.createProperty(size, "size", "1");
128                             size.getNodeProperties().add(sizeProp);
129                             provider.storeNode(size);
130                             // values property
131                             Property valueProp = provider.createProperty(values, "0", new Integer(iz + 1000).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             {
141                 long start = System.currentTimeMillis();
142                 for (int ix = 0; ix < ENTITY_SIZE; ix++)
143                 {
144                     for (int iy = 0; iy < users.length; iy++)
145                     {
146                         for (int iz = 0; iz < PREF_SIZE; iz++)
147                         {
148                             Node n;
149                             String key = "/portlet_entity/" + ix + "/" + users[iy] + "/preferences/pref-" + iz;                                                
150                             n = provider.getNode(key, 0);
151                             assertNotNull("null pref: " + key, n);
152                             Collection c = provider.getChildren(n);
153                             assertNotNull("null collection ", c);
154                             Iterator it = c.iterator();
155                             while (it.hasNext())
156                             {
157                                 Node child = (Node)it.next();
158                                 if (child.getNodeName().equals("size"))
159                                 {
160                                     Object props[] = child.getNodeProperties().toArray();
161                                     assertTrue("props isa ", (props[0] instanceof Property));
162                                     Property p = (Property)props[0];
163                                     String size = p.getPropertyValue();
164                                     assertTrue("child size name ", "size".equals(p.getPropertyName()));                                
165                                     assertTrue("child size value ", "1".equals(size));
166                                 }
167                                 else if (child.getNodeName().equals("values"))
168                                 {
169                                     Object props[] = child.getNodeProperties().toArray();
170                                     assertTrue("props isa ", (props[0] instanceof Property));
171                                     Property p = (Property)props[0];
172                                     String value = p.getPropertyValue();
173                                     assertTrue("child value name ", "0".equals(p.getPropertyName()));                                
174                                     assertTrue("child value value ", new Integer(iz + 1000).toString().equals(value));
175                                 }
176                                 
177                             }
178                         }
179                     }
180                 }
181                 long end = System.currentTimeMillis();
182                 System.out.println("Retrieval time total: " +  (end - start));
183             }
184         }
185         catch (Exception e)
186         {
187             e.printStackTrace();
188             assertTrue("exception in testNodes", false);
189         }
190     }
191 
192     /***
193      * @see org.apache.jetspeed.components.test.AbstractSpringTestCase#getConfigurations()
194      */
195     protected String[] getConfigurations()
196     {
197         return new String[]
198         { "prefs.xml", "transaction.xml", "cache.xml" };
199     }
200 }