001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rewriter.test;
006:
007: import com.sun.portal.rewriter.RewriterModule;
008: import com.sun.portal.rewriter.RewriterPool;
009: import com.sun.portal.rewriter.engines.LanguageConstants;
010: import com.sun.portal.rewriter.rom.RuleSetManager;
011: import com.sun.portal.rewriter.test.util.BasicTestCase;
012: import com.sun.portal.rewriter.test.util.Timer;
013: import com.sun.portal.rewriter.util.Resource;
014: import com.sun.portal.rewriter.util.StringHelper;
015: import com.sun.portal.rewriter.util.uri.PageSpec;
016:
017: public class TestRewriterCache extends BasicTestCase {
018: private static final Timer timer = new Timer();
019: private static PageSpec samplePageSpec;
020:
021: static {
022: try {
023: samplePageSpec = new PageSpec(
024: "http://rajanagendra.sun.com/Base/Raja/raja.html?name=raja",
025: LanguageConstants.JS_MIME);
026: } catch (Exception e) {
027: e.printStackTrace();
028: }
029: }
030:
031: public TestRewriterCache(String aName) {
032: super (aName);
033: }//constructor
034:
035: public void testWithSimpleTimer() throws Exception {
036: String ruleSetID = storeRandomRuleSet("test_rewriter_cache");
037:
038: Timer timer = new Timer();
039:
040: //first time
041: timer.start();
042: RewriterPool.getDefault()
043: .getRewriter(ruleSetID, samplePageSpec);
044: timer.stop();
045: long first = timer.getTimeTaken();
046:
047: //second time
048: timer.start();
049: RewriterPool.getDefault()
050: .getRewriter(ruleSetID, samplePageSpec);
051: timer.stop();
052:
053: long second = timer.getTimeTaken();
054: RuleSetManager.getDefault().delete(ruleSetID);
055:
056: String msg = "Rewriter Cache: First it took : " + first
057: + "(s) and Second time it took: " + second + "(s)";
058: assertTrue(msg, ((second < first) && (second < 5))); //second time it should take zero time..
059: RuleSetManager.getDefault().delete(ruleSetID);
060: }//testWithSimpleTimer()
061:
062: public void testEventFiring() throws Exception {
063: String ruleSetID = storeRandomRuleSet("test_rewriter_cache");
064:
065: String lRuleSetString = Resource
066: .read(RewriterModule.RESOURCE_GENERIC_RULESET_LOCATION);
067: lRuleSetString = StringHelper.searchAndReplace(lRuleSetString,
068: RuleSetManager.create(lRuleSetString).getID(),
069: ruleSetID);
070: RuleSetManager.getDefault().store(lRuleSetString);
071:
072: //first time
073: timer.start();
074: RewriterPool.getDefault()
075: .getRewriter(ruleSetID, samplePageSpec);
076: timer.stop();
077: long first = timer.getTimeTaken();
078:
079: //store so that id is removed from cache
080: RuleSetManager.getDefault().store(lRuleSetString);
081:
082: //second time
083: timer.start();
084: RewriterPool.getDefault()
085: .getRewriter(ruleSetID, samplePageSpec);
086: timer.stop();
087:
088: long second = timer.getTimeTaken();
089:
090: RuleSetManager.getDefault().delete(ruleSetID);
091:
092: String msg = "Rewriter Cache: First it took : " + first
093: + "(s) and Second time it took: " + second + "(s)";
094: assertTrue(msg, ((second == 0) || ((first > 5)
095: && (Math.abs(second - first) >= 0) && (Math.abs(second
096: - first) < 20)))); //both times it should take almost the same time..
097: }//testEventFiring()
098:
099: public static void main(String[] args) {
100: RewriterModule.initFile();
101: BasicTestCase.run(TestRewriterCache.class);
102: }//main()
103: }//class TestRuleSetCache
|