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.profiler;
18  
19  import java.util.Collection;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Locale;
23  import java.util.Map;
24  import java.util.Properties;
25  import java.util.Set;
26  
27  import javax.security.auth.Subject;
28  
29  import junit.framework.Test;
30  import junit.framework.TestSuite;
31  
32  import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
33  import org.apache.jetspeed.mockobjects.request.MockRequestContext;
34  import org.apache.jetspeed.profiler.impl.JetspeedProfilerImpl;
35  import org.apache.jetspeed.profiler.rules.ProfileResolvers;
36  import org.apache.jetspeed.profiler.rules.ProfilingRule;
37  import org.apache.jetspeed.profiler.rules.RuleCriterion;
38  import org.apache.jetspeed.profiler.rules.impl.RoleFallbackProfilingRule;
39  import org.apache.jetspeed.profiler.rules.impl.StandardProfilingRule;
40  import org.apache.jetspeed.request.RequestContext;
41  import org.apache.jetspeed.security.SecurityHelper;
42  import org.apache.jetspeed.security.impl.PrincipalsSet;
43  import org.apache.jetspeed.security.impl.RolePrincipalImpl;
44  import org.apache.jetspeed.security.impl.UserPrincipalImpl;
45  
46  /***
47   * TestProfiler
48   * 
49   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
50   * @version $Id: TestProfiler.java 517719 2007-03-13 15:05:48Z ate $
51   */
52  public class TestProfiler extends DatasourceEnabledSpringTestCase
53  {
54      private Profiler profiler = null;
55      private ProfileResolvers resolvers = null;
56      
57      protected static final Properties TEST_PROPS = new Properties();
58  
59      static
60      {
61          TEST_PROPS.put("defaultRule", "j1");
62          TEST_PROPS.put("anonymousUser", "anon");
63          TEST_PROPS.put("locator.impl", "org.apache.jetspeed.profiler.impl.JetspeedProfileLocator");
64          TEST_PROPS.put("principalRule.impl", "org.apache.jetspeed.profiler.rules.impl.PrincipalRuleImpl");
65          TEST_PROPS.put("profilingRule.impl", "org.apache.jetspeed.profiler.rules.impl.AbstractProfilingRule");
66      }
67  
68      /*
69       * (non-Javadoc)
70       * 
71       * @see junit.framework.TestCase#tearDown()
72       */
73      protected void tearDown() throws Exception
74      {
75          super.tearDown();
76      }
77  
78      /***
79       * Start the tests.
80       * 
81       * @param args
82       *            the arguments. Not used
83       */
84      public static void main(String args[])
85      {
86          junit.awtui.TestRunner.main(new String[]
87          { TestProfiler.class.getName() });
88      }
89  
90      
91      protected void setUp() throws Exception
92      {
93          super.setUp();
94          this.profiler = (Profiler) ctx.getBean("profiler");
95          JetspeedProfilerImpl profilerImpl = (JetspeedProfilerImpl) ctx.getBean("profilerImpl");
96          assertNotNull("profiler not found ", profiler);
97          ProfileResolvers resolvers = (ProfileResolvers) ctx.getBean("ProfileResolvers");
98          assertNotNull("resolvers not found ", resolvers);
99          profilerImpl.setDefaultRule(JetspeedProfilerImpl.DEFAULT_RULE);
100     }
101 
102     public static Test suite()
103     {
104         // All methods starting with "test" will be executed in the test suite.
105         return new TestSuite(TestProfiler.class);
106     }
107 
108     private static final String DEFAULT_RULE = "j1";
109 
110     private static final String FALLBACK_RULE = "role-fallback";
111 
112     private static final int EXPECTED_STANDARD = 1;
113 
114     private static final int EXPECTED_FALLBACK = 1;
115 
116     private static final String DEFAULT_PAGE = "default-page";
117 
118     private static final String URF_CRITERIA [] =
119     {
120         "user",
121         "navigation",
122         "role",
123         "path.session"
124     };
125 
126     private static final String URCF_CRITERIA [] =
127     {
128         "user",
129         "navigation",
130         "rolecombo",
131         "path.session"
132     };
133 
134     
135     
136     public void testUserRoleFallback() 
137     throws Exception
138     {
139         assertNotNull("profiler service is null", profiler);
140         System.out.println("START: running test user role fallback...");
141         
142         // make sure rule is set correctly
143         ProfilingRule rule = profiler.getRule("user-role-fallback");
144         assertNotNull("rule is null ", rule);
145         Iterator c = rule.getRuleCriteria().iterator();
146         int ix = 0;
147         while (c.hasNext())
148         {
149             RuleCriterion rc = (RuleCriterion)c.next();
150             assertTrue("criterion type check " + rc.getType(), rc.getType().equals(URF_CRITERIA[ix]));
151             System.out.println(rc.getType());
152             ix++;
153         }
154         
155         // test applying it
156         RequestContext context = new MockRequestContext();
157         Subject subject = createSubject();
158         context.setPath("/homepage.psml");        
159         context.setSubject(subject);
160         ProfileLocator locator = rule.apply(context, profiler);
161         System.out.println("locator = " + locator);
162         assertTrue("locator string " + locator.toString(), locator.toString().equals("/homepage.psml:user:david:navigation:/:role:ATP:role:NB:role:ATP-NB:page:/homepage.psml"));
163         
164         System.out.println("COMPLETED: running test user role fallback.");
165     }
166 
167     public void testUserRoleComboFallback() 
168     throws Exception
169     {
170         assertNotNull("profiler service is null", profiler);
171         System.out.println("START: running test user rolecombo fallback...");
172         
173         // make sure rule is set correctly
174         ProfilingRule rule = profiler.getRule("user-rolecombo-fallback");
175         assertNotNull("rule is null ", rule);
176         Iterator c = rule.getRuleCriteria().iterator();
177         int ix = 0;
178         while (c.hasNext())
179         {
180             RuleCriterion rc = (RuleCriterion)c.next();
181             assertTrue("criterion type check " + rc.getType(), rc.getType().equals(URCF_CRITERIA[ix]));
182             System.out.println(rc.getType());
183             ix++;
184         }
185         
186         // test applying it
187         RequestContext context = new MockRequestContext();
188         Subject subject = createSubject2();
189         context.setPath("/homepage.psml");        
190         context.setSubject(subject);
191         ProfileLocator locator = rule.apply(context, profiler);
192         System.out.println("locator = " + locator);
193         assertTrue("locator string " + locator.toString(), locator.toString().equals("/homepage.psml:user:david:navigation:/:role:ATP-NB:page:/homepage.psml"));
194         
195         System.out.println("COMPLETED: running test user role fallback.");
196     }
197     
198     protected Subject createSubject()
199     {
200         Set principals = new PrincipalsSet();
201         Set publicCredentials = new HashSet();
202         Set privateCredentials = new HashSet();
203         
204         principals.add(new UserPrincipalImpl("david"));
205         principals.add(new RolePrincipalImpl("ATP"));
206         principals.add(new RolePrincipalImpl("NB"));        
207         principals.add(new RolePrincipalImpl("ATP-NB"));        
208         Subject subject = new Subject(true, principals, publicCredentials, privateCredentials);        
209         return subject;
210     }
211 
212     protected Subject createSubject2()
213     {
214         Set principals = new PrincipalsSet();
215         Set publicCredentials = new HashSet();
216         Set privateCredentials = new HashSet();
217         
218         principals.add(new UserPrincipalImpl("david"));
219         principals.add(new RolePrincipalImpl("ATP"));
220         principals.add(new RolePrincipalImpl("NB"));        
221         Subject subject = new Subject(true, principals, publicCredentials, privateCredentials);        
222         return subject;
223     }
224     
225     /***
226      * Tests
227      * 
228      * @throws Exception
229      */
230     public void testRules() throws Exception
231     {
232         assertNotNull("profiler service is null", profiler);
233 
234         // Test Default Rule
235         ProfilingRule rule = profiler.getDefaultRule();
236         assertNotNull("Default profiling rule is null", rule);
237         assertTrue("default rule unexpected, = " + rule.getId(), rule.getId().equals(DEFAULT_RULE));
238         assertTrue("default rule class not mapped", rule instanceof StandardProfilingRule);
239 
240         // Test anonymous principal-rule
241         ProfilingRule anonRule = profiler.getRuleForPrincipal(new UserPrincipalImpl("anon"),
242                 ProfileLocator.PAGE_LOCATOR);
243         assertNotNull("anonymous rule is null", anonRule);
244         assertTrue("anonymous rule is j1", anonRule.getId().equals(DEFAULT_RULE));
245 
246         // Test Retrieving All Rules
247         int standardCount = 0;
248         int fallbackCount = 0;
249         Iterator rules = profiler.getRules().iterator();
250         while (rules.hasNext())
251         {
252             rule = (ProfilingRule) rules.next();
253             if (rule.getId().equals(DEFAULT_RULE))
254             {
255                 assertTrue("standard rule class not mapped", rule instanceof StandardProfilingRule);
256                 checkStandardCriteria(rule);
257                 standardCount++;
258             }
259             else if (rule.getId().equals(FALLBACK_RULE))
260             {
261                 assertTrue("role fallback rule class not mapped", rule instanceof RoleFallbackProfilingRule);
262                 checkFallbackCriteria(rule);
263                 fallbackCount++;
264             }
265             else
266             {
267                 // assertTrue("Unknown rule encountered: " + rule.getId(),
268                 // false);
269             }
270 
271         }
272         assertTrue("didnt find expected number of standard rules, expected = " + EXPECTED_STANDARD, standardCount == 1);
273         assertTrue("didnt find expected number of fallback rules, expected = " + EXPECTED_FALLBACK, fallbackCount == 1);
274 
275     }
276 
277     private void checkStandardCriteria(ProfilingRule rule)
278     {
279         Collection criteriaCollection = rule.getRuleCriteria();
280         assertNotNull("Criteria is null", criteriaCollection);
281         Iterator criteria = criteriaCollection.iterator();
282         int count = 0;
283         while (criteria.hasNext())
284         {
285             RuleCriterion criterion = (RuleCriterion) criteria.next();
286             assertNotNull("criteria type ", criterion.getType());
287             System.out.println("criteria name = " + criterion.getName());
288             switch (count)
289             {
290             case 0:
291                 assertTrue("criteria name " + criterion.getName(), criterion.getName().equals(
292                         ProfilingRule.STANDARD_PAGE));
293                 assertNotNull("criteria value", criterion.getValue());
294                 assertTrue("criteria value", criterion.getValue().equals(DEFAULT_PAGE));
295                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_STOP);
296                 break;
297             case 1:
298                 assertTrue("criteria name", criterion.getName().equals(ProfilingRule.STANDARD_USER));
299                 assertNull("criteria value", criterion.getValue());
300                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_STOP);
301                 break;
302             case 2:
303                 assertTrue("criteria name", criterion.getName().equals(ProfilingRule.STANDARD_MEDIATYPE));
304                 assertNull("criteria value", criterion.getValue());
305                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
306                 break;
307             case 3:
308                 assertTrue("criteria name", criterion.getName().equals(ProfilingRule.STANDARD_LANGUAGE));
309                 assertNull("criteria value", criterion.getValue());
310                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
311                 break;
312             case 4:
313                 assertTrue("criteria name", criterion.getName().equals(ProfilingRule.STANDARD_COUNTRY));
314                 assertNull("criteria value", criterion.getValue());
315                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
316                 break;
317             }
318             count++;
319         }
320     }
321     
322 
323 
324     private void checkFallbackCriteria(ProfilingRule rule)
325     {
326         Collection criteriaCollection = rule.getRuleCriteria();
327         assertNotNull("Criteria is null", criteriaCollection);
328         Iterator criteria = criteriaCollection.iterator();
329         int count = 0;
330         while (criteria.hasNext())
331         {
332             RuleCriterion criterion = (RuleCriterion) criteria.next();
333             assertNotNull("fallback criteria type", criterion.getType());
334 
335             switch (count)
336             {
337             case 0:
338                 assertTrue("fallback criteria name", criterion.getName().equals(ProfilingRule.STANDARD_ROLE));
339                 assertNull("fallback criteria value", criterion.getValue());
340                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_LOOP);
341                 break;
342             case 1:
343                 assertTrue("fallback criteria name", criterion.getName().equals(ProfilingRule.STANDARD_PAGE));
344                 assertNotNull("fallback criteria value", criterion.getValue());
345                 assertTrue("fallback criteria value", criterion.getValue().equals(DEFAULT_PAGE));
346                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_STOP);
347                 break;
348             case 2:
349                 assertTrue("fallback criteria name", criterion.getName().equals(ProfilingRule.STANDARD_MEDIATYPE));
350                 assertNull("fallback criteria value", criterion.getValue());
351                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
352                 break;
353             case 3:
354                 assertTrue("fallback criteria name", criterion.getName().equals(ProfilingRule.STANDARD_LANGUAGE));
355                 assertNull("fallback criteria value", criterion.getValue());
356                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
357                 break;
358             case 4:
359                 assertTrue("fallback criteria name", criterion.getName().equals(ProfilingRule.STANDARD_COUNTRY));
360                 assertNull("fallback criteria value", criterion.getValue());
361                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
362                 break;
363             }
364             count++;
365         }
366     }
367 
368     public void testStandardRule() throws Exception
369     {
370         assertNotNull("profiler service is null", profiler);
371 
372         RequestContext request = new MockRequestContext("default-other");
373 
374         request.setSubject(SecurityHelper.createSubject("anon"));
375         request.setLocale(new Locale("en", "US"));
376         request.setMediaType("HTML");
377         request.setMimeType("text/html");
378         Map params = request.getParameterMap();
379         params.put("page", "default-other");
380         params.put("path", "/sports/football/nfl/chiefs");
381 
382         ProfileLocator locator = profiler.getProfile(request, ProfileLocator.PAGE_LOCATOR);
383         assertNotNull("rule test on getProfile returned null", locator);
384         String path = locator.getLocatorPath();
385         System.out.println("locator = " + path);
386         assertTrue("locator key value unexpected: " + path, path
387                 .equals("page:default-other:user:anon:mediatype:HTML:language:en:country:US"));
388 
389         // test fallback
390         Iterator fallback = locator.iterator();
391         int count = 0;
392         while (fallback.hasNext())
393         {
394             ProfileLocatorProperty[] locatorProperties = (ProfileLocatorProperty[]) fallback.next();
395             assertTrue("locatorProperties is not null", (locatorProperties != null));
396             String locatorPath = locator.getLocatorPath(locatorProperties);
397             switch (count)
398             {
399             case 0:
400                 assertTrue("locatorPath[0]: " + locatorPath, locatorPath
401                         .equals("page:default-other:user:anon:mediatype:HTML:language:en:country:US"));
402                 break;
403             case 1:
404                 assertTrue("locatorPath[1]: " + locatorPath, locatorPath
405                         .equals("page:default-other:user:anon:mediatype:HTML:language:en"));
406                 break;
407             case 2:
408                 assertTrue("locatorPath[2]: " + locatorPath, locatorPath
409                         .equals("page:default-other:user:anon:mediatype:HTML"));
410                 break;
411             case 3:
412                 assertTrue("locatorPath[3]: " + locatorPath, locatorPath.equals("page:default-other:user:anon"));
413                 break;
414 
415             }
416             count++;
417             System.out.println("path = " + locatorPath);
418         }
419         assertTrue("fallback count = 4, " + count, count == 4);
420 
421         // create a Simple locator
422         RequestContext request2 = new MockRequestContext("/test");
423         ProfileLocator locator2 = profiler.createLocator(request2);
424         locator2.add("page", "test");
425         fallback = locator2.iterator();
426         count = 0;
427         while (fallback.hasNext())
428         {
429             ProfileLocatorProperty[] locatorProperties = (ProfileLocatorProperty[]) fallback.next();
430             assertTrue("locatorProperties is not null", (locatorProperties != null));
431             String locatorPath = locator.getLocatorPath(locatorProperties);
432             assertTrue("locatorPath: " + locatorPath, locatorPath.equals("page:test"));
433 
434             System.out.println("Simple Test: path = " + locatorPath);
435             count++;
436         }
437         assertTrue("fallback count = 1, " + count, count == 1);
438 
439         // create an empty locator
440         RequestContext request3 = new MockRequestContext("/");
441         ProfileLocator locator3 = profiler.createLocator(request3);
442         fallback = locator3.iterator();
443         count = 0;
444         while (fallback.hasNext())
445         {
446             fallback.next();
447             count++;
448         }
449         assertTrue("fallback count = 0, " + count, count == 0);
450 
451     }
452 
453     public void testPage() throws Exception
454     {
455         assertNotNull("profiler service is null", profiler);
456 
457         RequestContext request = new MockRequestContext();
458 
459         request.setSubject(SecurityHelper.createSubject("anon"));
460         request.setLocale(new Locale("en", "US"));
461         request.setMediaType("HTML");
462         request.setMimeType("text/html");
463 
464         ProfileLocator locator = profiler.getProfile(request, ProfileLocator.PAGE_LOCATOR);
465         assertNotNull("rule test on getProfile returned null", locator);
466         System.out.println("page = " + locator.getValue("page"));
467     }
468 
469     public void testPath() throws Exception
470     {
471         assertNotNull("profiler service is null", profiler);
472 
473         RequestContext request = new MockRequestContext("/football/nfl/chiefs");
474         ProfilingRule rule = profiler.getRule("path");
475         ProfileLocator locator = profiler.getProfile(request, rule);
476         assertNotNull("rule test on getProfile returned null", locator);
477         String path = locator.getLocatorPath();
478         System.out.println("locator = " + path);
479         assertTrue("locator path: " + path, path.equals("path:/football/nfl/chiefs"));
480     }
481 
482     public void testGetLocatorNames() throws Exception
483     {
484         assertNotNull("profiler service is null", profiler);
485         String[] result = profiler.getLocatorNamesForPrincipal(new UserPrincipalImpl("guest"));
486         for (int ix = 0; ix < result.length; ix++)
487         {
488             System.out.println("$$$ result = " + result[ix]);
489             assertTrue("locator name = " + result[ix], result[ix].equals("page"));
490         }
491     }
492 
493     public void testMaintenance() throws Exception
494     {
495         System.out.println("Maintenance tests commencing....");
496         assertNotNull("profiler service is null", profiler);
497         ProfilingRule rule = new StandardProfilingRule(resolvers);
498         rule.setClassname("org.apache.jetspeed.profiler.rules.impl.StandardProfilingRule");
499         rule.setId("testmo");
500         rule.setTitle("The Grand Title");
501         profiler.storeProfilingRule(rule);
502         ProfilingRule rule2 = profiler.getRule("testmo");
503         assertNotNull("rule couldnt be added", rule2);
504         assertTrue("rule id bad", rule.getId().equals(rule2.getId()));
505 
506         rule2.setTitle("The New Title");
507         profiler.storeProfilingRule(rule2);
508 
509         ProfilingRule rule3 = profiler.getRule("testmo");
510         assertNotNull("rule couldnt be retrieved", rule3);
511         assertTrue("rule title is bad", rule3.getTitle().equals(rule2.getTitle()));
512 
513         profiler.deleteProfilingRule(rule);
514         ProfilingRule rule4 = profiler.getRule("testmo");
515         assertNull("rule couldnt be deleted", rule4);
516 
517         System.out.println("Maintenance tests completed.");
518     }
519 
520     protected String[] getConfigurations()
521     {
522         return new String[]{"profiler.xml", "transaction.xml"};
523     }
524 
525     
526     
527     protected RuleCriterion addRuleCriterion(ProfilingRule rule,  
528                                    String criterionName, String criterionType, String criterionValue,int fallbackOrder, int fallbackType)
529     throws Exception
530     {
531         assertTrue("ProfilingRule is not null", (rule != null));
532 
533         
534         RuleCriterion c = profiler.createRuleCriterion();
535         assertTrue("RuleCriterion is not null", (c != null));
536         c.setFallbackOrder(fallbackOrder);
537         c.setFallbackType(fallbackType);
538         c.setName(criterionName);
539         c.setType(criterionType);
540         c.setValue(criterionValue);
541         c.setRuleId(rule.getId());
542         rule.getRuleCriteria().add(c);
543         return c;
544     }
545     
546     
547  
548     private void createStandardCriteria(ProfilingRule rule) throws Exception
549     {
550         RuleCriterion criterion;
551         assertNotNull("ProfilingRule is null", rule);
552 
553         for (int count = 0; count < 5; count++)
554         {
555             switch (count)
556             {
557             case 0:
558                 
559                 criterion = this.addRuleCriterion(rule,ProfilingRule.STANDARD_PAGE, "type-" + count, DEFAULT_PAGE, count, RuleCriterion.FALLBACK_STOP);
560                 assertTrue("criteria name " + criterion.getName(), criterion.getName().equals(
561                         ProfilingRule.STANDARD_PAGE));
562                 assertNotNull("criteria value", criterion.getValue());
563                 assertTrue("criteria value", criterion.getValue().equals(DEFAULT_PAGE));
564                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_STOP);
565                 break;
566             case 1:
567                 criterion = this.addRuleCriterion(rule,ProfilingRule.STANDARD_USER, "type-" + count, null, count, RuleCriterion.FALLBACK_STOP);
568                 assertTrue("criteria name", criterion.getName().equals(ProfilingRule.STANDARD_USER));
569                 assertNull("criteria value", criterion.getValue());
570                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_STOP);
571                 break;
572             case 2:
573                 criterion = this.addRuleCriterion(rule,ProfilingRule.STANDARD_MEDIATYPE, "type-" + count, null, count, RuleCriterion.FALLBACK_CONTINUE);
574                 assertTrue("criteria name", criterion.getName().equals(ProfilingRule.STANDARD_MEDIATYPE));
575                 assertNull("criteria value", criterion.getValue());
576                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
577                 break;
578             case 3:
579                 criterion = this.addRuleCriterion(rule,ProfilingRule.STANDARD_LANGUAGE, "type-" + count, null, count, RuleCriterion.FALLBACK_CONTINUE);
580                 assertTrue("criteria name", criterion.getName().equals(ProfilingRule.STANDARD_LANGUAGE));
581                 assertNull("criteria value", criterion.getValue());
582                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
583                 break;
584             case 4:
585                 criterion = this.addRuleCriterion(rule,ProfilingRule.STANDARD_COUNTRY, "type-" + count, null, count, RuleCriterion.FALLBACK_CONTINUE);
586                 assertTrue("criteria name", criterion.getName().equals(ProfilingRule.STANDARD_COUNTRY));
587                 assertNull("criteria value", criterion.getValue());
588                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
589                 break;
590             }
591 
592         }
593     }
594 
595 
596 
597     
598     private void createFallbackCriteria(ProfilingRule rule) throws Exception
599     {
600         RuleCriterion criterion;
601         assertNotNull("ProfilingRule is null", rule);
602 
603         for (int count = 0; count < 5; count++)
604         {
605 
606             switch (count)
607             {
608             case 0:
609                 criterion = this.addRuleCriterion(rule,ProfilingRule.STANDARD_ROLE, "type-" + count, null, count, RuleCriterion.FALLBACK_LOOP);
610                 assertTrue("fallback criteria name", criterion.getName().equals(ProfilingRule.STANDARD_ROLE));
611                 assertNull("fallback criteria value", criterion.getValue());
612                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_LOOP);
613                 break;
614             case 1:
615                 criterion = this.addRuleCriterion(rule,ProfilingRule.STANDARD_PAGE, "type-" + count, DEFAULT_PAGE, count, RuleCriterion.FALLBACK_STOP);
616                 assertTrue("fallback criteria name", criterion.getName().equals(ProfilingRule.STANDARD_PAGE));
617                 assertNotNull("fallback criteria value", criterion.getValue());
618                 assertTrue("fallback criteria value", criterion.getValue().equals(DEFAULT_PAGE));
619                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_STOP);
620                 break;
621             case 2:
622                 criterion = this.addRuleCriterion(rule,ProfilingRule.STANDARD_MEDIATYPE, "type-" + count, null, count, RuleCriterion.FALLBACK_CONTINUE);
623                 assertTrue("fallback criteria name", criterion.getName().equals(ProfilingRule.STANDARD_MEDIATYPE));
624                 assertNull("fallback criteria value", criterion.getValue());
625                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
626                 break;
627             case 3:
628                 criterion = this.addRuleCriterion(rule,ProfilingRule.STANDARD_LANGUAGE, "type-" + count, null, count, RuleCriterion.FALLBACK_CONTINUE);
629                 assertTrue("fallback criteria name", criterion.getName().equals(ProfilingRule.STANDARD_LANGUAGE));
630                 assertNull("fallback criteria value", criterion.getValue());
631                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
632                 break;
633             case 4:
634                 criterion = this.addRuleCriterion(rule,ProfilingRule.STANDARD_COUNTRY, "type-" + count, null, count, RuleCriterion.FALLBACK_CONTINUE);
635                 assertTrue("fallback criteria name", criterion.getName().equals(ProfilingRule.STANDARD_COUNTRY));
636                 assertNull("fallback criteria value", criterion.getValue());
637                 assertTrue("fallback type", criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
638                 break;
639             }
640         }
641     }
642     
643     /***
644      * Tests
645      * 
646      * @throws Exception
647      */
648     public void testNewRules() throws Exception
649     {
650         assertNotNull("profiler service is null", profiler);
651         String ruleId1 = "j1-test";
652         String ruleId2 = "j2-test";
653         
654         
655         // create org.apache.jetspeed.profiler.rules.impl.StandardProfilingRule
656         ProfilingRule rule = profiler.createProfilingRule(true);
657         assertNotNull("rule is null ", rule);
658         rule.setId(ruleId1);
659         rule.setTitle("Test Rule 1");
660         this.createStandardCriteria(rule);
661         
662         profiler.storeProfilingRule(rule);
663         //Check
664         ProfilingRule rule2 = profiler.getRule(ruleId1);
665         assertNotNull("default rule couldnt be added", rule2);
666         assertTrue("default rule id bad", rule.getId().equals(rule2.getId()));
667         
668         rule = profiler.createProfilingRule(false);
669         assertNotNull("rule is null ", rule);
670         rule.setId(ruleId2);
671         rule.setTitle("Test Rule 2");
672         
673         this.createFallbackCriteria(rule);
674 
675         profiler.storeProfilingRule(rule);
676         //Check
677         rule2 = profiler.getRule(ruleId2);
678         assertNotNull("fallback rule couldnt be added", rule2);
679         assertTrue("fallback rule id bad", rule.getId().equals(rule2.getId()));
680 
681         // Test Retrieving All Rules
682         int standardCount = 0;
683         int fallbackCount = 0;
684         Iterator rules = profiler.getRules().iterator();
685         while (rules.hasNext())
686         {
687             rule = (ProfilingRule) rules.next();
688             if (rule.getId().equals(ruleId1))
689             {
690                 assertTrue("standard rule class not mapped", rule instanceof StandardProfilingRule);
691                 checkStandardCriteria(rule);
692                 standardCount++;
693             }
694             else if (rule.getId().equals(ruleId2))
695             {
696                 assertTrue("role fallback rule class not mapped", rule instanceof RoleFallbackProfilingRule);
697                 checkFallbackCriteria(rule);
698                 fallbackCount++;
699             }
700             else
701             {
702                 // assertTrue("Unknown rule encountered: " + rule.getId(),
703                 // false);
704             }
705 
706         }
707         assertTrue("didnt find expected number of standard rules, expected = " + EXPECTED_STANDARD, standardCount == 1);
708         assertTrue("didnt find expected number of fallback rules, expected = " + EXPECTED_FALLBACK, fallbackCount == 1);
709     }
710     
711 }