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.Rewriter;
008: import com.sun.portal.rewriter.RewriterModule;
009: import com.sun.portal.rewriter.RewriterPool;
010: import com.sun.portal.rewriter.engines.AbstractRewriter;
011: import com.sun.portal.rewriter.engines.LanguageConstants;
012: import com.sun.portal.rewriter.rom.RuleSet;
013: import com.sun.portal.rewriter.rom.RuleSetManager;
014: import com.sun.portal.rewriter.test.util.BasicTestCase;
015: import com.sun.portal.rewriter.test.util.CreateRuleSet;
016: import com.sun.portal.rewriter.util.Resource;
017: import com.sun.portal.rewriter.util.uri.PageSpec;
018:
019: public class TestRewriterPool extends BasicTestCase {
020: public TestRewriterPool(String aName) {
021: super (aName);
022: }//constructor
023:
024: public void testCreateRewriter() throws Exception {
025: Rewriter lRewriter = RewriterPool
026: .create(
027: Resource
028: .read(RewriterModule.RESOURCE_GENERIC_RULESET_LOCATION),
029: "aad");
030: assertNull(lRewriter);
031: }//testCreateRewriter()
032:
033: public void testRewriterInstance() throws Exception {
034: //store some known id ruleset
035: RuleSet testRuleSet = CreateRuleSet.emptyRuleSet();
036: String xmlString = testRuleSet.toXML();
037: RuleSetManager.getDefault().store(xmlString);
038: assertTrue(RuleSetManager.getDefault().matchesWithID(xmlString) != null);
039:
040: //then call the get instance with that id
041: PageSpec lSpec = new PageSpec("http://raja/",
042: LanguageConstants.MARKUP_MIME);
043: Rewriter lRewriter = RewriterPool.getDefault().getRewriter(
044: testRuleSet.getID(), lSpec);
045: assertNotNull(lRewriter);
046: assertEquals(LanguageConstants.MARKUP_MIME,
047: ((AbstractRewriter) lRewriter).getMIME());
048:
049: lSpec = new PageSpec("http://raja/",
050: LanguageConstants.HTML_MIME);
051: lRewriter = RewriterPool.getDefault().getRewriter(
052: testRuleSet.getID(), lSpec);
053: assertNotNull(lRewriter);
054: assertEquals(LanguageConstants.HTML_MIME,
055: ((AbstractRewriter) lRewriter).getMIME());
056:
057: lSpec = new PageSpec("http://raja/", LanguageConstants.JS_MIME);
058: lRewriter = RewriterPool.getDefault().getRewriter(
059: testRuleSet.getID(), lSpec);
060: assertNotNull(lRewriter);
061: assertEquals(LanguageConstants.JS_MIME,
062: ((AbstractRewriter) lRewriter).getMIME());
063:
064: lSpec = new PageSpec("http://raja/", LanguageConstants.XML_MIME);
065: lRewriter = RewriterPool.getDefault().getRewriter(
066: testRuleSet.getID(), lSpec);
067: assertNotNull(lRewriter);
068: assertEquals(LanguageConstants.XML_MIME,
069: ((AbstractRewriter) lRewriter).getMIME());
070:
071: lSpec = new PageSpec("http://raja/", LanguageConstants.CSS_MIME);
072: lRewriter = RewriterPool.getDefault().getRewriter(
073: testRuleSet.getID(), lSpec);
074: assertNotNull(lRewriter);
075: assertEquals(LanguageConstants.CSS_MIME,
076: ((AbstractRewriter) lRewriter).getMIME());
077:
078: //WML rewriter returns HTML rewriter - internal implemantation
079: lSpec = new PageSpec("http://raja/", "text/wml");
080: lRewriter = RewriterPool.getDefault().getRewriter(
081: testRuleSet.getID(), lSpec);
082: assertNull(lRewriter);
083:
084: //WML rewriter returns HTML rewriter - internal implemantation
085: lSpec = new PageSpec("http://raja/", "text/vnd.wap.wml");
086: lRewriter = RewriterPool.getDefault().getRewriter(
087: testRuleSet.getID(), lSpec);
088: assertNull(lRewriter);
089:
090: //no rewriter exits for this mime type
091: lSpec = new PageSpec("http://raja/", "zip");
092: lRewriter = RewriterPool.getDefault().getRewriter(
093: testRuleSet.getID(), lSpec);
094: assertNull(lRewriter);
095:
096: lSpec = new PageSpec("http://raja/", "xyZ");
097: lRewriter = RewriterPool.getDefault().getRewriter(
098: testRuleSet.getID(), lSpec);
099: assertNull(lRewriter);
100:
101: //guess mime from file extenstion
102: lSpec = new PageSpec("http://raja/ABC.HTML", "");
103: lRewriter = RewriterPool.getDefault().getRewriter(
104: testRuleSet.getID(), lSpec);
105: assertNotNull("Derived Mime is : " + lSpec.getMIME(), lRewriter);
106: assertEquals(LanguageConstants.HTML_MIME,
107: ((AbstractRewriter) lRewriter).getMIME());
108:
109: lSpec = new PageSpec("http://raja/ABC.htm", "");
110: lRewriter = RewriterPool.getDefault().getRewriter(
111: testRuleSet.getID(), lSpec);
112: assertNotNull("Derived Mime is : " + lSpec.getMIME(), lRewriter);
113: assertEquals(LanguageConstants.HTML_MIME,
114: ((AbstractRewriter) lRewriter).getMIME());
115:
116: lSpec = new PageSpec("http://raja/ABC.jS", "");
117: lRewriter = RewriterPool.getDefault().getRewriter(
118: testRuleSet.getID(), lSpec);
119: assertNotNull("Derived Mime is : " + lSpec.getMIME(), lRewriter);
120: assertEquals(LanguageConstants.JS_MIME,
121: ((AbstractRewriter) lRewriter).getMIME());
122:
123: lSpec = new PageSpec("http://raja/ABC.wMl", "");
124: lRewriter = RewriterPool.getDefault().getRewriter(
125: testRuleSet.getID(), lSpec);
126: assertNull("Derived Mime is : " + lSpec.getMIME(), lRewriter);
127:
128: lSpec = new PageSpec("http://raja/ABC.CsS", "");
129: lRewriter = RewriterPool.getDefault().getRewriter(
130: testRuleSet.getID(), lSpec);
131: assertNotNull("Derived Mime is : " + lSpec.getMIME(), lRewriter);
132: assertEquals(LanguageConstants.CSS_MIME,
133: ((AbstractRewriter) lRewriter).getMIME());
134:
135: lSpec = new PageSpec("http://raja/ABC.xml", "");
136: lRewriter = RewriterPool.getDefault().getRewriter(
137: testRuleSet.getID(), lSpec);
138: assertNotNull("Derived Mime is : " + lSpec.getMIME(), lRewriter);
139: assertEquals(LanguageConstants.XML_MIME,
140: ((AbstractRewriter) lRewriter).getMIME());
141:
142: //clean the stored ruleset
143: RuleSetManager.getDefault().delete(testRuleSet.getID());
144: assertTrue(RuleSetManager.getDefault().matchesWithID(xmlString) == null);
145: }//testRewriterInstance()
146:
147: public static void main(String[] args) {
148: BasicTestCase.run(TestRewriterPool.class);
149: }//main()
150:
151: }//class TestRewriterPool
|