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.profiler;
018:
019: import java.util.Collection;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.Locale;
023: import java.util.Map;
024: import java.util.Properties;
025: import java.util.Set;
026:
027: import javax.security.auth.Subject;
028:
029: import junit.framework.Test;
030: import junit.framework.TestSuite;
031:
032: import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
033: import org.apache.jetspeed.mockobjects.request.MockRequestContext;
034: import org.apache.jetspeed.profiler.impl.JetspeedProfilerImpl;
035: import org.apache.jetspeed.profiler.rules.ProfileResolvers;
036: import org.apache.jetspeed.profiler.rules.ProfilingRule;
037: import org.apache.jetspeed.profiler.rules.RuleCriterion;
038: import org.apache.jetspeed.profiler.rules.impl.RoleFallbackProfilingRule;
039: import org.apache.jetspeed.profiler.rules.impl.StandardProfilingRule;
040: import org.apache.jetspeed.request.RequestContext;
041: import org.apache.jetspeed.security.SecurityHelper;
042: import org.apache.jetspeed.security.impl.PrincipalsSet;
043: import org.apache.jetspeed.security.impl.RolePrincipalImpl;
044: import org.apache.jetspeed.security.impl.UserPrincipalImpl;
045:
046: /**
047: * TestProfiler
048: *
049: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
050: * @version $Id: TestProfiler.java 517719 2007-03-13 15:05:48Z ate $
051: */
052: public class TestProfiler extends DatasourceEnabledSpringTestCase {
053: private Profiler profiler = null;
054: private ProfileResolvers resolvers = null;
055:
056: protected static final Properties TEST_PROPS = new Properties();
057:
058: static {
059: TEST_PROPS.put("defaultRule", "j1");
060: TEST_PROPS.put("anonymousUser", "anon");
061: TEST_PROPS
062: .put("locator.impl",
063: "org.apache.jetspeed.profiler.impl.JetspeedProfileLocator");
064: TEST_PROPS
065: .put("principalRule.impl",
066: "org.apache.jetspeed.profiler.rules.impl.PrincipalRuleImpl");
067: TEST_PROPS
068: .put("profilingRule.impl",
069: "org.apache.jetspeed.profiler.rules.impl.AbstractProfilingRule");
070: }
071:
072: /*
073: * (non-Javadoc)
074: *
075: * @see junit.framework.TestCase#tearDown()
076: */
077: protected void tearDown() throws Exception {
078: super .tearDown();
079: }
080:
081: /**
082: * Start the tests.
083: *
084: * @param args
085: * the arguments. Not used
086: */
087: public static void main(String args[]) {
088: junit.awtui.TestRunner.main(new String[] { TestProfiler.class
089: .getName() });
090: }
091:
092: protected void setUp() throws Exception {
093: super .setUp();
094: this .profiler = (Profiler) ctx.getBean("profiler");
095: JetspeedProfilerImpl profilerImpl = (JetspeedProfilerImpl) ctx
096: .getBean("profilerImpl");
097: assertNotNull("profiler not found ", profiler);
098: ProfileResolvers resolvers = (ProfileResolvers) ctx
099: .getBean("ProfileResolvers");
100: assertNotNull("resolvers not found ", resolvers);
101: profilerImpl.setDefaultRule(JetspeedProfilerImpl.DEFAULT_RULE);
102: }
103:
104: public static Test suite() {
105: // All methods starting with "test" will be executed in the test suite.
106: return new TestSuite(TestProfiler.class);
107: }
108:
109: private static final String DEFAULT_RULE = "j1";
110:
111: private static final String FALLBACK_RULE = "role-fallback";
112:
113: private static final int EXPECTED_STANDARD = 1;
114:
115: private static final int EXPECTED_FALLBACK = 1;
116:
117: private static final String DEFAULT_PAGE = "default-page";
118:
119: private static final String URF_CRITERIA[] = { "user",
120: "navigation", "role", "path.session" };
121:
122: private static final String URCF_CRITERIA[] = { "user",
123: "navigation", "rolecombo", "path.session" };
124:
125: public void testUserRoleFallback() throws Exception {
126: assertNotNull("profiler service is null", profiler);
127: System.out.println("START: running test user role fallback...");
128:
129: // make sure rule is set correctly
130: ProfilingRule rule = profiler.getRule("user-role-fallback");
131: assertNotNull("rule is null ", rule);
132: Iterator c = rule.getRuleCriteria().iterator();
133: int ix = 0;
134: while (c.hasNext()) {
135: RuleCriterion rc = (RuleCriterion) c.next();
136: assertTrue("criterion type check " + rc.getType(), rc
137: .getType().equals(URF_CRITERIA[ix]));
138: System.out.println(rc.getType());
139: ix++;
140: }
141:
142: // test applying it
143: RequestContext context = new MockRequestContext();
144: Subject subject = createSubject();
145: context.setPath("/homepage.psml");
146: context.setSubject(subject);
147: ProfileLocator locator = rule.apply(context, profiler);
148: System.out.println("locator = " + locator);
149: assertTrue(
150: "locator string " + locator.toString(),
151: locator
152: .toString()
153: .equals(
154: "/homepage.psml:user:david:navigation:/:role:ATP:role:NB:role:ATP-NB:page:/homepage.psml"));
155:
156: System.out
157: .println("COMPLETED: running test user role fallback.");
158: }
159:
160: public void testUserRoleComboFallback() throws Exception {
161: assertNotNull("profiler service is null", profiler);
162: System.out
163: .println("START: running test user rolecombo fallback...");
164:
165: // make sure rule is set correctly
166: ProfilingRule rule = profiler
167: .getRule("user-rolecombo-fallback");
168: assertNotNull("rule is null ", rule);
169: Iterator c = rule.getRuleCriteria().iterator();
170: int ix = 0;
171: while (c.hasNext()) {
172: RuleCriterion rc = (RuleCriterion) c.next();
173: assertTrue("criterion type check " + rc.getType(), rc
174: .getType().equals(URCF_CRITERIA[ix]));
175: System.out.println(rc.getType());
176: ix++;
177: }
178:
179: // test applying it
180: RequestContext context = new MockRequestContext();
181: Subject subject = createSubject2();
182: context.setPath("/homepage.psml");
183: context.setSubject(subject);
184: ProfileLocator locator = rule.apply(context, profiler);
185: System.out.println("locator = " + locator);
186: assertTrue(
187: "locator string " + locator.toString(),
188: locator
189: .toString()
190: .equals(
191: "/homepage.psml:user:david:navigation:/:role:ATP-NB:page:/homepage.psml"));
192:
193: System.out
194: .println("COMPLETED: running test user role fallback.");
195: }
196:
197: protected Subject createSubject() {
198: Set principals = new PrincipalsSet();
199: Set publicCredentials = new HashSet();
200: Set privateCredentials = new HashSet();
201:
202: principals.add(new UserPrincipalImpl("david"));
203: principals.add(new RolePrincipalImpl("ATP"));
204: principals.add(new RolePrincipalImpl("NB"));
205: principals.add(new RolePrincipalImpl("ATP-NB"));
206: Subject subject = new Subject(true, principals,
207: publicCredentials, privateCredentials);
208: return subject;
209: }
210:
211: protected Subject createSubject2() {
212: Set principals = new PrincipalsSet();
213: Set publicCredentials = new HashSet();
214: Set privateCredentials = new HashSet();
215:
216: principals.add(new UserPrincipalImpl("david"));
217: principals.add(new RolePrincipalImpl("ATP"));
218: principals.add(new RolePrincipalImpl("NB"));
219: Subject subject = new Subject(true, principals,
220: publicCredentials, privateCredentials);
221: return subject;
222: }
223:
224: /**
225: * Tests
226: *
227: * @throws Exception
228: */
229: public void testRules() throws Exception {
230: assertNotNull("profiler service is null", profiler);
231:
232: // Test Default Rule
233: ProfilingRule rule = profiler.getDefaultRule();
234: assertNotNull("Default profiling rule is null", rule);
235: assertTrue("default rule unexpected, = " + rule.getId(), rule
236: .getId().equals(DEFAULT_RULE));
237: assertTrue("default rule class not mapped",
238: rule instanceof StandardProfilingRule);
239:
240: // Test anonymous principal-rule
241: ProfilingRule anonRule = profiler.getRuleForPrincipal(
242: new UserPrincipalImpl("anon"),
243: ProfileLocator.PAGE_LOCATOR);
244: assertNotNull("anonymous rule is null", anonRule);
245: assertTrue("anonymous rule is j1", anonRule.getId().equals(
246: DEFAULT_RULE));
247:
248: // Test Retrieving All Rules
249: int standardCount = 0;
250: int fallbackCount = 0;
251: Iterator rules = profiler.getRules().iterator();
252: while (rules.hasNext()) {
253: rule = (ProfilingRule) rules.next();
254: if (rule.getId().equals(DEFAULT_RULE)) {
255: assertTrue("standard rule class not mapped",
256: rule instanceof StandardProfilingRule);
257: checkStandardCriteria(rule);
258: standardCount++;
259: } else if (rule.getId().equals(FALLBACK_RULE)) {
260: assertTrue("role fallback rule class not mapped",
261: rule instanceof RoleFallbackProfilingRule);
262: checkFallbackCriteria(rule);
263: fallbackCount++;
264: } else {
265: // assertTrue("Unknown rule encountered: " + rule.getId(),
266: // false);
267: }
268:
269: }
270: assertTrue(
271: "didnt find expected number of standard rules, expected = "
272: + EXPECTED_STANDARD, standardCount == 1);
273: assertTrue(
274: "didnt find expected number of fallback rules, expected = "
275: + EXPECTED_FALLBACK, fallbackCount == 1);
276:
277: }
278:
279: private void checkStandardCriteria(ProfilingRule rule) {
280: Collection criteriaCollection = rule.getRuleCriteria();
281: assertNotNull("Criteria is null", criteriaCollection);
282: Iterator criteria = criteriaCollection.iterator();
283: int count = 0;
284: while (criteria.hasNext()) {
285: RuleCriterion criterion = (RuleCriterion) criteria.next();
286: assertNotNull("criteria type ", criterion.getType());
287: System.out
288: .println("criteria name = " + criterion.getName());
289: switch (count) {
290: case 0:
291: assertTrue("criteria name " + criterion.getName(),
292: criterion.getName().equals(
293: ProfilingRule.STANDARD_PAGE));
294: assertNotNull("criteria value", criterion.getValue());
295: assertTrue("criteria value", criterion.getValue()
296: .equals(DEFAULT_PAGE));
297: assertTrue(
298: "fallback type",
299: criterion.getFallbackType() == RuleCriterion.FALLBACK_STOP);
300: break;
301: case 1:
302: assertTrue("criteria name", criterion.getName().equals(
303: ProfilingRule.STANDARD_USER));
304: assertNull("criteria value", criterion.getValue());
305: assertTrue(
306: "fallback type",
307: criterion.getFallbackType() == RuleCriterion.FALLBACK_STOP);
308: break;
309: case 2:
310: assertTrue("criteria name", criterion.getName().equals(
311: ProfilingRule.STANDARD_MEDIATYPE));
312: assertNull("criteria value", criterion.getValue());
313: assertTrue(
314: "fallback type",
315: criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
316: break;
317: case 3:
318: assertTrue("criteria name", criterion.getName().equals(
319: ProfilingRule.STANDARD_LANGUAGE));
320: assertNull("criteria value", criterion.getValue());
321: assertTrue(
322: "fallback type",
323: criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
324: break;
325: case 4:
326: assertTrue("criteria name", criterion.getName().equals(
327: ProfilingRule.STANDARD_COUNTRY));
328: assertNull("criteria value", criterion.getValue());
329: assertTrue(
330: "fallback type",
331: criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
332: break;
333: }
334: count++;
335: }
336: }
337:
338: private void checkFallbackCriteria(ProfilingRule rule) {
339: Collection criteriaCollection = rule.getRuleCriteria();
340: assertNotNull("Criteria is null", criteriaCollection);
341: Iterator criteria = criteriaCollection.iterator();
342: int count = 0;
343: while (criteria.hasNext()) {
344: RuleCriterion criterion = (RuleCriterion) criteria.next();
345: assertNotNull("fallback criteria type", criterion.getType());
346:
347: switch (count) {
348: case 0:
349: assertTrue("fallback criteria name", criterion
350: .getName().equals(ProfilingRule.STANDARD_ROLE));
351: assertNull("fallback criteria value", criterion
352: .getValue());
353: assertTrue(
354: "fallback type",
355: criterion.getFallbackType() == RuleCriterion.FALLBACK_LOOP);
356: break;
357: case 1:
358: assertTrue("fallback criteria name", criterion
359: .getName().equals(ProfilingRule.STANDARD_PAGE));
360: assertNotNull("fallback criteria value", criterion
361: .getValue());
362: assertTrue("fallback criteria value", criterion
363: .getValue().equals(DEFAULT_PAGE));
364: assertTrue(
365: "fallback type",
366: criterion.getFallbackType() == RuleCriterion.FALLBACK_STOP);
367: break;
368: case 2:
369: assertTrue("fallback criteria name", criterion
370: .getName().equals(
371: ProfilingRule.STANDARD_MEDIATYPE));
372: assertNull("fallback criteria value", criterion
373: .getValue());
374: assertTrue(
375: "fallback type",
376: criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
377: break;
378: case 3:
379: assertTrue("fallback criteria name", criterion
380: .getName().equals(
381: ProfilingRule.STANDARD_LANGUAGE));
382: assertNull("fallback criteria value", criterion
383: .getValue());
384: assertTrue(
385: "fallback type",
386: criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
387: break;
388: case 4:
389: assertTrue("fallback criteria name", criterion
390: .getName().equals(
391: ProfilingRule.STANDARD_COUNTRY));
392: assertNull("fallback criteria value", criterion
393: .getValue());
394: assertTrue(
395: "fallback type",
396: criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
397: break;
398: }
399: count++;
400: }
401: }
402:
403: public void testStandardRule() throws Exception {
404: assertNotNull("profiler service is null", profiler);
405:
406: RequestContext request = new MockRequestContext("default-other");
407:
408: request.setSubject(SecurityHelper.createSubject("anon"));
409: request.setLocale(new Locale("en", "US"));
410: request.setMediaType("HTML");
411: request.setMimeType("text/html");
412: Map params = request.getParameterMap();
413: params.put("page", "default-other");
414: params.put("path", "/sports/football/nfl/chiefs");
415:
416: ProfileLocator locator = profiler.getProfile(request,
417: ProfileLocator.PAGE_LOCATOR);
418: assertNotNull("rule test on getProfile returned null", locator);
419: String path = locator.getLocatorPath();
420: System.out.println("locator = " + path);
421: assertTrue(
422: "locator key value unexpected: " + path,
423: path
424: .equals("page:default-other:user:anon:mediatype:HTML:language:en:country:US"));
425:
426: // test fallback
427: Iterator fallback = locator.iterator();
428: int count = 0;
429: while (fallback.hasNext()) {
430: ProfileLocatorProperty[] locatorProperties = (ProfileLocatorProperty[]) fallback
431: .next();
432: assertTrue("locatorProperties is not null",
433: (locatorProperties != null));
434: String locatorPath = locator
435: .getLocatorPath(locatorProperties);
436: switch (count) {
437: case 0:
438: assertTrue(
439: "locatorPath[0]: " + locatorPath,
440: locatorPath
441: .equals("page:default-other:user:anon:mediatype:HTML:language:en:country:US"));
442: break;
443: case 1:
444: assertTrue(
445: "locatorPath[1]: " + locatorPath,
446: locatorPath
447: .equals("page:default-other:user:anon:mediatype:HTML:language:en"));
448: break;
449: case 2:
450: assertTrue(
451: "locatorPath[2]: " + locatorPath,
452: locatorPath
453: .equals("page:default-other:user:anon:mediatype:HTML"));
454: break;
455: case 3:
456: assertTrue("locatorPath[3]: " + locatorPath,
457: locatorPath
458: .equals("page:default-other:user:anon"));
459: break;
460:
461: }
462: count++;
463: System.out.println("path = " + locatorPath);
464: }
465: assertTrue("fallback count = 4, " + count, count == 4);
466:
467: // create a Simple locator
468: RequestContext request2 = new MockRequestContext("/test");
469: ProfileLocator locator2 = profiler.createLocator(request2);
470: locator2.add("page", "test");
471: fallback = locator2.iterator();
472: count = 0;
473: while (fallback.hasNext()) {
474: ProfileLocatorProperty[] locatorProperties = (ProfileLocatorProperty[]) fallback
475: .next();
476: assertTrue("locatorProperties is not null",
477: (locatorProperties != null));
478: String locatorPath = locator
479: .getLocatorPath(locatorProperties);
480: assertTrue("locatorPath: " + locatorPath, locatorPath
481: .equals("page:test"));
482:
483: System.out.println("Simple Test: path = " + locatorPath);
484: count++;
485: }
486: assertTrue("fallback count = 1, " + count, count == 1);
487:
488: // create an empty locator
489: RequestContext request3 = new MockRequestContext("/");
490: ProfileLocator locator3 = profiler.createLocator(request3);
491: fallback = locator3.iterator();
492: count = 0;
493: while (fallback.hasNext()) {
494: fallback.next();
495: count++;
496: }
497: assertTrue("fallback count = 0, " + count, count == 0);
498:
499: }
500:
501: public void testPage() throws Exception {
502: assertNotNull("profiler service is null", profiler);
503:
504: RequestContext request = new MockRequestContext();
505:
506: request.setSubject(SecurityHelper.createSubject("anon"));
507: request.setLocale(new Locale("en", "US"));
508: request.setMediaType("HTML");
509: request.setMimeType("text/html");
510:
511: ProfileLocator locator = profiler.getProfile(request,
512: ProfileLocator.PAGE_LOCATOR);
513: assertNotNull("rule test on getProfile returned null", locator);
514: System.out.println("page = " + locator.getValue("page"));
515: }
516:
517: public void testPath() throws Exception {
518: assertNotNull("profiler service is null", profiler);
519:
520: RequestContext request = new MockRequestContext(
521: "/football/nfl/chiefs");
522: ProfilingRule rule = profiler.getRule("path");
523: ProfileLocator locator = profiler.getProfile(request, rule);
524: assertNotNull("rule test on getProfile returned null", locator);
525: String path = locator.getLocatorPath();
526: System.out.println("locator = " + path);
527: assertTrue("locator path: " + path, path
528: .equals("path:/football/nfl/chiefs"));
529: }
530:
531: public void testGetLocatorNames() throws Exception {
532: assertNotNull("profiler service is null", profiler);
533: String[] result = profiler
534: .getLocatorNamesForPrincipal(new UserPrincipalImpl(
535: "guest"));
536: for (int ix = 0; ix < result.length; ix++) {
537: System.out.println("$$$ result = " + result[ix]);
538: assertTrue("locator name = " + result[ix], result[ix]
539: .equals("page"));
540: }
541: }
542:
543: public void testMaintenance() throws Exception {
544: System.out.println("Maintenance tests commencing....");
545: assertNotNull("profiler service is null", profiler);
546: ProfilingRule rule = new StandardProfilingRule(resolvers);
547: rule
548: .setClassname("org.apache.jetspeed.profiler.rules.impl.StandardProfilingRule");
549: rule.setId("testmo");
550: rule.setTitle("The Grand Title");
551: profiler.storeProfilingRule(rule);
552: ProfilingRule rule2 = profiler.getRule("testmo");
553: assertNotNull("rule couldnt be added", rule2);
554: assertTrue("rule id bad", rule.getId().equals(rule2.getId()));
555:
556: rule2.setTitle("The New Title");
557: profiler.storeProfilingRule(rule2);
558:
559: ProfilingRule rule3 = profiler.getRule("testmo");
560: assertNotNull("rule couldnt be retrieved", rule3);
561: assertTrue("rule title is bad", rule3.getTitle().equals(
562: rule2.getTitle()));
563:
564: profiler.deleteProfilingRule(rule);
565: ProfilingRule rule4 = profiler.getRule("testmo");
566: assertNull("rule couldnt be deleted", rule4);
567:
568: System.out.println("Maintenance tests completed.");
569: }
570:
571: protected String[] getConfigurations() {
572: return new String[] { "profiler.xml", "transaction.xml" };
573: }
574:
575: protected RuleCriterion addRuleCriterion(ProfilingRule rule,
576: String criterionName, String criterionType,
577: String criterionValue, int fallbackOrder, int fallbackType)
578: throws Exception {
579: assertTrue("ProfilingRule is not null", (rule != null));
580:
581: RuleCriterion c = profiler.createRuleCriterion();
582: assertTrue("RuleCriterion is not null", (c != null));
583: c.setFallbackOrder(fallbackOrder);
584: c.setFallbackType(fallbackType);
585: c.setName(criterionName);
586: c.setType(criterionType);
587: c.setValue(criterionValue);
588: c.setRuleId(rule.getId());
589: rule.getRuleCriteria().add(c);
590: return c;
591: }
592:
593: private void createStandardCriteria(ProfilingRule rule)
594: throws Exception {
595: RuleCriterion criterion;
596: assertNotNull("ProfilingRule is null", rule);
597:
598: for (int count = 0; count < 5; count++) {
599: switch (count) {
600: case 0:
601:
602: criterion = this .addRuleCriterion(rule,
603: ProfilingRule.STANDARD_PAGE, "type-" + count,
604: DEFAULT_PAGE, count,
605: RuleCriterion.FALLBACK_STOP);
606: assertTrue("criteria name " + criterion.getName(),
607: criterion.getName().equals(
608: ProfilingRule.STANDARD_PAGE));
609: assertNotNull("criteria value", criterion.getValue());
610: assertTrue("criteria value", criterion.getValue()
611: .equals(DEFAULT_PAGE));
612: assertTrue(
613: "fallback type",
614: criterion.getFallbackType() == RuleCriterion.FALLBACK_STOP);
615: break;
616: case 1:
617: criterion = this .addRuleCriterion(rule,
618: ProfilingRule.STANDARD_USER, "type-" + count,
619: null, count, RuleCriterion.FALLBACK_STOP);
620: assertTrue("criteria name", criterion.getName().equals(
621: ProfilingRule.STANDARD_USER));
622: assertNull("criteria value", criterion.getValue());
623: assertTrue(
624: "fallback type",
625: criterion.getFallbackType() == RuleCriterion.FALLBACK_STOP);
626: break;
627: case 2:
628: criterion = this .addRuleCriterion(rule,
629: ProfilingRule.STANDARD_MEDIATYPE, "type-"
630: + count, null, count,
631: RuleCriterion.FALLBACK_CONTINUE);
632: assertTrue("criteria name", criterion.getName().equals(
633: ProfilingRule.STANDARD_MEDIATYPE));
634: assertNull("criteria value", criterion.getValue());
635: assertTrue(
636: "fallback type",
637: criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
638: break;
639: case 3:
640: criterion = this .addRuleCriterion(rule,
641: ProfilingRule.STANDARD_LANGUAGE, "type-"
642: + count, null, count,
643: RuleCriterion.FALLBACK_CONTINUE);
644: assertTrue("criteria name", criterion.getName().equals(
645: ProfilingRule.STANDARD_LANGUAGE));
646: assertNull("criteria value", criterion.getValue());
647: assertTrue(
648: "fallback type",
649: criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
650: break;
651: case 4:
652: criterion = this .addRuleCriterion(rule,
653: ProfilingRule.STANDARD_COUNTRY,
654: "type-" + count, null, count,
655: RuleCriterion.FALLBACK_CONTINUE);
656: assertTrue("criteria name", criterion.getName().equals(
657: ProfilingRule.STANDARD_COUNTRY));
658: assertNull("criteria value", criterion.getValue());
659: assertTrue(
660: "fallback type",
661: criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
662: break;
663: }
664:
665: }
666: }
667:
668: private void createFallbackCriteria(ProfilingRule rule)
669: throws Exception {
670: RuleCriterion criterion;
671: assertNotNull("ProfilingRule is null", rule);
672:
673: for (int count = 0; count < 5; count++) {
674:
675: switch (count) {
676: case 0:
677: criterion = this .addRuleCriterion(rule,
678: ProfilingRule.STANDARD_ROLE, "type-" + count,
679: null, count, RuleCriterion.FALLBACK_LOOP);
680: assertTrue("fallback criteria name", criterion
681: .getName().equals(ProfilingRule.STANDARD_ROLE));
682: assertNull("fallback criteria value", criterion
683: .getValue());
684: assertTrue(
685: "fallback type",
686: criterion.getFallbackType() == RuleCriterion.FALLBACK_LOOP);
687: break;
688: case 1:
689: criterion = this .addRuleCriterion(rule,
690: ProfilingRule.STANDARD_PAGE, "type-" + count,
691: DEFAULT_PAGE, count,
692: RuleCriterion.FALLBACK_STOP);
693: assertTrue("fallback criteria name", criterion
694: .getName().equals(ProfilingRule.STANDARD_PAGE));
695: assertNotNull("fallback criteria value", criterion
696: .getValue());
697: assertTrue("fallback criteria value", criterion
698: .getValue().equals(DEFAULT_PAGE));
699: assertTrue(
700: "fallback type",
701: criterion.getFallbackType() == RuleCriterion.FALLBACK_STOP);
702: break;
703: case 2:
704: criterion = this .addRuleCriterion(rule,
705: ProfilingRule.STANDARD_MEDIATYPE, "type-"
706: + count, null, count,
707: RuleCriterion.FALLBACK_CONTINUE);
708: assertTrue("fallback criteria name", criterion
709: .getName().equals(
710: ProfilingRule.STANDARD_MEDIATYPE));
711: assertNull("fallback criteria value", criterion
712: .getValue());
713: assertTrue(
714: "fallback type",
715: criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
716: break;
717: case 3:
718: criterion = this .addRuleCriterion(rule,
719: ProfilingRule.STANDARD_LANGUAGE, "type-"
720: + count, null, count,
721: RuleCriterion.FALLBACK_CONTINUE);
722: assertTrue("fallback criteria name", criterion
723: .getName().equals(
724: ProfilingRule.STANDARD_LANGUAGE));
725: assertNull("fallback criteria value", criterion
726: .getValue());
727: assertTrue(
728: "fallback type",
729: criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
730: break;
731: case 4:
732: criterion = this .addRuleCriterion(rule,
733: ProfilingRule.STANDARD_COUNTRY,
734: "type-" + count, null, count,
735: RuleCriterion.FALLBACK_CONTINUE);
736: assertTrue("fallback criteria name", criterion
737: .getName().equals(
738: ProfilingRule.STANDARD_COUNTRY));
739: assertNull("fallback criteria value", criterion
740: .getValue());
741: assertTrue(
742: "fallback type",
743: criterion.getFallbackType() == RuleCriterion.FALLBACK_CONTINUE);
744: break;
745: }
746: }
747: }
748:
749: /**
750: * Tests
751: *
752: * @throws Exception
753: */
754: public void testNewRules() throws Exception {
755: assertNotNull("profiler service is null", profiler);
756: String ruleId1 = "j1-test";
757: String ruleId2 = "j2-test";
758:
759: // create org.apache.jetspeed.profiler.rules.impl.StandardProfilingRule
760: ProfilingRule rule = profiler.createProfilingRule(true);
761: assertNotNull("rule is null ", rule);
762: rule.setId(ruleId1);
763: rule.setTitle("Test Rule 1");
764: this .createStandardCriteria(rule);
765:
766: profiler.storeProfilingRule(rule);
767: //Check
768: ProfilingRule rule2 = profiler.getRule(ruleId1);
769: assertNotNull("default rule couldnt be added", rule2);
770: assertTrue("default rule id bad", rule.getId().equals(
771: rule2.getId()));
772:
773: rule = profiler.createProfilingRule(false);
774: assertNotNull("rule is null ", rule);
775: rule.setId(ruleId2);
776: rule.setTitle("Test Rule 2");
777:
778: this .createFallbackCriteria(rule);
779:
780: profiler.storeProfilingRule(rule);
781: //Check
782: rule2 = profiler.getRule(ruleId2);
783: assertNotNull("fallback rule couldnt be added", rule2);
784: assertTrue("fallback rule id bad", rule.getId().equals(
785: rule2.getId()));
786:
787: // Test Retrieving All Rules
788: int standardCount = 0;
789: int fallbackCount = 0;
790: Iterator rules = profiler.getRules().iterator();
791: while (rules.hasNext()) {
792: rule = (ProfilingRule) rules.next();
793: if (rule.getId().equals(ruleId1)) {
794: assertTrue("standard rule class not mapped",
795: rule instanceof StandardProfilingRule);
796: checkStandardCriteria(rule);
797: standardCount++;
798: } else if (rule.getId().equals(ruleId2)) {
799: assertTrue("role fallback rule class not mapped",
800: rule instanceof RoleFallbackProfilingRule);
801: checkFallbackCriteria(rule);
802: fallbackCount++;
803: } else {
804: // assertTrue("Unknown rule encountered: " + rule.getId(),
805: // false);
806: }
807:
808: }
809: assertTrue(
810: "didnt find expected number of standard rules, expected = "
811: + EXPECTED_STANDARD, standardCount == 1);
812: assertTrue(
813: "didnt find expected number of fallback rules, expected = "
814: + EXPECTED_FALLBACK, fallbackCount == 1);
815: }
816:
817: }
|