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.rproxy.rewriter.engines.js.test;
006:
007: import com.sun.portal.rewriter.Rewriter;
008: import com.sun.portal.log.common.PortalLogger;
009: import com.sun.portal.rewriter.RewriterPool;
010: import com.sun.portal.rewriter.engines.LanguageConstants;
011: import com.sun.portal.rewriter.rom.RuleSet;
012: import com.sun.portal.rewriter.test.util.BasicTestCase;
013: import com.sun.portal.rewriter.test.util.CreateRuleSet;
014: import com.sun.portal.rewriter.test.util.JSWrapperHelper;
015: import com.sun.portal.rproxy.rewriter.SRAPRewriterModule;
016: import com.sun.portal.rproxy.rewriter.test.util.SRAPJSWrapperHelper;
017: import com.sun.portal.rproxy.rewriter.test.util.SRAPTestSystemFactory;
018: import junit.framework.TestSuite;
019:
020: public class TestSRAPJSExpressionVariables extends BasicTestCase {
021: public TestSRAPJSExpressionVariables(String aName) {
022: super (aName);
023: }//constuctor
024:
025: public void testExpressionVariable0() throws Exception {
026: String lInput = "var ab = top.location + \"./abc.html\";";
027:
028: String lExpectation = "var ab ="
029: + SRAPJSWrapperHelper.CONVERT_EXPRESSION_FUNCTION_WRAP_START
030: + " top.location + \"./abc.html\");"
031: + SRAPJSWrapperHelper.SRAP_CONVERT_EXPRESSION_FUNCTION_JS;
032:
033: String lRules = "<Variable name=\"ab\" type=\"EXPRESSION\"/>";
034: RuleSet lRuleSet = CreateRuleSet.withJSVariableRules(lRules);
035: Rewriter lRewriter = RewriterPool.create(lRuleSet,
036: LanguageConstants.JS_MIME);
037: String lResult = lRewriter
038: .rewrite(
039: lInput,
040: SRAPTestSystemFactory
041: .createSRAPTranslator(
042: "https://zeus.nawab.inda.com/",
043: "http://raja.sun.com/Base/Raja/index.js?abc=10&ee=90#raju"));
044: //System.out.println( lResult );
045: assertEquals(lExpectation, lResult);
046: if (lResult.indexOf("SRAP") == -1) {
047: fail("Not Reading the function name from SRAPModule Props file");
048: }
049: }//testExpressionVariable0()
050:
051: public void testExpressionVariable1() throws Exception {
052: String lInput = "var ab = iplanet( \"javascript:top.location:\" + \"./abc.html\");";
053:
054: String lExpectation = "var ab ="
055: + SRAPJSWrapperHelper.CONVERT_EXPRESSION_FUNCTION_WRAP_START
056: + " iplanet( \"javascript:top.location:\" + \"./abc.html\"));"
057: + SRAPJSWrapperHelper.SRAP_CONVERT_EXPRESSION_FUNCTION_JS;
058:
059: String lRules = "<Variable name=\"ab\" type=\"EXPRESSION\"/>";
060: RuleSet lRuleSet = CreateRuleSet.withJSVariableRules(lRules);
061: Rewriter lRewriter = RewriterPool.create(lRuleSet,
062: LanguageConstants.JS_MIME);
063: String lResult = lRewriter
064: .rewrite(
065: lInput,
066: SRAPTestSystemFactory
067: .createSRAPTranslator(
068: "https://zeus.nawab.inda.com/",
069: "http://raja.sun.com/Base/Raja/index.js?abc=10&ee=90#raju"));
070:
071: assertEquals(lExpectation, lResult);
072: if (lResult.indexOf("SRAP") == -1) {
073: fail("Not Reading the function name from SRAPModule Props file");
074: }
075: }//testExpressionVariable1()
076:
077: public void testFunctionVariable1() throws Exception {
078: String[] lShouldMatch = { "href", //0
079: };
080:
081: String lInput = "function gotoanotherweb()\n"
082: + "{\n"
083: + " href =\"http://\"+\"iplanet.India.Sun.COM/products/index.html\";\n"
084: + "}";
085:
086: String lExpectation = "function gotoanotherweb()\n"
087: + "{\n"
088: + " href ="
089: + SRAPJSWrapperHelper.CONVERT_EXPRESSION_FUNCTION_WRAP_START
090: + "\"http://\"+\"iplanet.India.Sun.COM/products/index.html\");\n"
091: + "}" + JSWrapperHelper.CONVERT_EXPRESSION_FUNCTION_JS;
092:
093: String lRules;
094: for (int i = (lShouldMatch.length - 1); i >= 0; i--) {
095: lRules = "<Variable name=\"" + lShouldMatch[i]
096: + "\" type=\"EXPRESSION\"/>";
097: RuleSet lRuleSet = CreateRuleSet
098: .withJSVariableRules(lRules);
099: Rewriter lRewriter = RewriterPool.create(lRuleSet,
100: LanguageConstants.JS_MIME);
101: String lResult = lRewriter.rewrite(lInput,
102: getDefaultJSTranslator());
103: assertEquals("Failed Pattern = " + lShouldMatch[i] + ":"
104: + i + "\n", lExpectation, lResult);
105: }//for loop
106: }//testFunctionVariable1()
107:
108: public void testBhavani() throws Exception {
109: String lInput = "<html>\n"
110: + "<body onload=window.location.replace(\"http://raja.com:30000\")>\n"
111: + "<body onload=window.location.replace(\"telnet://localhost:30000\")>\n"
112: + "<body onload=window.location.replace(\"/index.html\")>\n"
113: + "</body></html>\n";
114:
115: String lExpectation = "<html>\n"
116: + "<body onload=window.location.replace(\"https://zeus.nawab.inda.com/http://raja.com:30000\")>\n"
117: + "<body onload=window.location.replace(\"telnet://localhost:30000\")>\n"
118: + "<body onload=window.location.replace(\"https://zeus.nawab.inda.com/http://raja.sun.com/index.html\")>\n"
119: + "</body></html>\n";
120:
121: String lJSTokenRules = "<Attribute name=\"onload\" type=\"DJS\"/>";
122: String lJSFuncRules = "<Function type=\"EXPRESSION\" name=\"*location.replace\" paramPatterns=\"y\"/>";
123: RuleSet lRuleSet = CreateRuleSet
124: .withHTMLJSTokenJSVarJSFunRules(lJSTokenRules, "",
125: lJSFuncRules);
126: Rewriter lRewriter = RewriterPool.create(lRuleSet,
127: LanguageConstants.HTML_MIME);
128: String lResult = lRewriter.rewrite(lInput,
129: SRAPTestSystemFactory.createSRAPTranslator());
130: assertEquals(lExpectation, lResult);
131: }//testBhavani()
132:
133: public static void main(String[] args) {
134: SRAPRewriterModule.initFile();
135: BasicTestCase.run(new TestSuite(
136: TestSRAPJSExpressionVariables.class));
137:
138: TestSuite testSuite = new TestSuite();
139: testSuite.addTest(new TestSRAPJSExpressionVariables(
140: "testBhavani"));
141: //BasicTestCase.run( testSuite );
142: }//main()
143: }//class TestSRAPJSExpressionVariables
|