001: package com.sun.portal.rewriter.engines.js.test;
002:
003: import com.sun.portal.rewriter.Rewriter;
004: import com.sun.portal.rewriter.RewriterModule;
005: import com.sun.portal.rewriter.RewriterPool;
006: import com.sun.portal.rewriter.engines.LanguageConstants;
007: import com.sun.portal.rewriter.rom.RuleSet;
008: import com.sun.portal.rewriter.test.util.BasicTestCase;
009: import com.sun.portal.rewriter.test.util.CreateRuleSet;
010: import junit.framework.TestSuite;
011:
012: public class TestJSURLFunctions extends BasicTestCase {
013: public TestJSURLFunctions(String aName) {
014: super (aName);
015: }//constuctor
016:
017: public void testURLParams() throws Exception {
018: final String lInput = "addMenu.addItem(new NavBarMenuItem(\"name\",\"javascript:top.location + './abc.html'\");";
019: final String lExpectation = "addMenu.addItem(new NavBarMenuItem(\"name\",\"javascript:top.location + './abc.html'\");";
020: String[] lRules = {
021: "<Function type=\"URL\" source=\"hWWWWWTT\" name=\"NavBarMenuItem\" paramPatterns=\",y\" />",
022: "<Function type=\"URL\" source=\"\" name=\"NavBarMenuItem\" paramPatterns=\",y\" />",
023: "<Function type=\"URL\" source=\"*\" name=\"NavBarMenuItem\" paramPatterns=\",y\" />",
024: "<Function type=\"URL\" source='hWWWWWTT' name=\"NavBarMenuItem\" paramPatterns=\",y\" />",
025: "<Function type=\"URL\" name=\"\" paramPatterns=\",y\" />",
026: "<Function type=\"URL\" name=\"*\" paramPatterns=\",y\" />", };
027: for (int i = 0; i < lRules.length; i++) {
028: RuleSet lRuleSet = CreateRuleSet
029: .withJSFunctionRules(lRules[i]);
030: Rewriter lRewriter = RewriterPool.create(lRuleSet,
031: LanguageConstants.JS_MIME);
032: String lResult = lRewriter.rewrite(lInput,
033: getDefaultTranslator());
034: assertEquals("Failed:\n", lExpectation, lResult);
035: }
036: }//testURLParams()
037:
038: public void testURLParams1() throws Exception {
039: final String lInput = "addMenu.addItem(new NavBarMenuItem(\"name\",\"./abc.html\"));";
040: final String lExpectation = "addMenu.addItem(new NavBarMenuItem(\"name\",\"http://rajanagendra.sun.com/Base/Raja/abc.html\"));";
041:
042: String lRules = "<Function type=\"URL\" source = \"http*\" name=\"NavBarMenuItem\" paramPatterns=\" ,y\" />";
043: RuleSet lRuleSet = CreateRuleSet.withJSFunctionRules(lRules);
044: Rewriter lRewriter = RewriterPool.create(lRuleSet,
045: LanguageConstants.JS_MIME);
046: String lResult = lRewriter.rewrite(lInput,
047: getDefaultTranslator());
048: assertEquals("Failed:\n", lExpectation, lResult);
049: }//testURLParams1()
050:
051: public void testURLParamsRelated2BugNo4809850() throws Exception {
052: final String lInput = "getTopFrame().main.location.href= './abc.html';"
053: + "addMenu.addItem(new NavBarMenuItem(\"name\",\"getTopFrame().main.location.href= './abc.html'\"));"
054: + "getTopFrame().main.location.href= './abc.html';";
055: final String lExpectation = "getTopFrame().main.location.href= 'http://rajanagendra.sun.com/Base/Raja/abc.html';"
056: + "addMenu.addItem(new NavBarMenuItem(\"name\",\"getTopFrame().main.location.href= 'http://rajanagendra.sun.com/Base/Raja/abc.html'\"));"
057: + "getTopFrame().main.location.href= 'http://rajanagendra.sun.com/Base/Raja/abc.html';";
058:
059: String lJSVarRules[] = {
060: "<Variable name=\"getTopFrame.main.location.href\" type=\"URL\"/>",
061: "<Variable name=\"*.main.location.href\" type=\"URL\"/>",
062: "<Variable name=\"*main.location.href\" type=\"URL\"/>",
063: "<Variable name=\"*.location.href\" type=\"URL\"/>",
064: "<Variable name=\"*location.href\" type=\"URL\"/>",
065: "<Variable name=\"*.href\" type=\"URL\"/>",
066: "<Variable name=\"*href\" type=\"URL\"/>", };
067:
068: String lJSFuncRules = "<Function type=\"DJS\" source = \"http*\" name=\"NavBarMenuItem\" paramPatterns=\" ,y\" />";
069: for (int i = 0; i < lJSVarRules.length; i++) {
070: RuleSet lRuleSet = CreateRuleSet
071: .withHTMLAttJSVarJSFunRules("", lJSVarRules[i],
072: lJSFuncRules);
073: Rewriter lRewriter = RewriterPool.create(lRuleSet,
074: LanguageConstants.JS_MIME);
075: String lResult = lRewriter.rewrite(lInput,
076: getDefaultTranslator());
077: assertEquals("Failed for i=" + i, lExpectation, lResult);
078: }
079: }//testURLParamsRelated2BugNo4809850()
080:
081: public void testURLParams2() throws Exception {
082: final String lInput = "addMenu.addItem(new NavBarMenuItem(\"name\",\"./abc.html\"), \"abc.html\");";
083: final String lExpectation = "addMenu.addItem(new NavBarMenuItem(\"name\",\"http://rajanagendra.sun.com/Base/Raja/abc.html\"), \"http://rajanagendra.sun.com/Base/Raja/abc.html\");";
084: String lRules = "<Function type=\"URL\" name=\"NavBarMenuItem\" source = \"hTtp*\" paramPatterns=\" ,y\" /> <Function type=\"URL\" name=\"addMenu.addItem\" paramPatterns=\" ,y\" />";
085:
086: RuleSet lRuleSet = CreateRuleSet.withJSFunctionRules(lRules);
087: Rewriter lRewriter = RewriterPool.create(lRuleSet,
088: LanguageConstants.JS_MIME);
089: String lResult = lRewriter.rewrite(lInput,
090: getDefaultTranslator());
091: assertEquals("Failed:\n", lExpectation, lResult);
092: }//testURLParams2()
093:
094: public void testSimpleURLParams() throws Exception {
095: String lInput = "urlWindow('abc.html')";
096: String lExpectation = "urlWindow('http://rajanagendra.sun.com/Base/Raja/abc.html')";
097: String lRules = "<Function type=\"URL\" name=\"url*w\" paramPatterns=\"y\" />";
098:
099: RuleSet lRuleSet = CreateRuleSet.withJSFunctionRules(lRules);
100: Rewriter lRewriter = RewriterPool.create(lRuleSet,
101: LanguageConstants.JS_MIME);
102: String lResult = lRewriter.rewrite(lInput,
103: getDefaultTranslator());
104: assertEquals("Failed: \n", lExpectation, lResult);
105: }//testSimpleURLParams()
106:
107: public void testSimpleURLParams1() throws Exception {
108: String lInput = "urlWindow('false','abc.html', 'true', 'true', '/xyz.js ')";
109: String lExpectation = "urlWindow('false','http://rajanagendra.sun.com/Base/Raja/abc.html', 'true', 'true', 'http://rajanagendra.sun.com/xyz.js ')";
110: String lRules = "<Function type=\"URL\" name=\"url*w\" paramPatterns=\",y,,,y\" />";
111:
112: RuleSet lRuleSet = CreateRuleSet.withJSFunctionRules(lRules);
113: Rewriter lRewriter = RewriterPool.create(lRuleSet,
114: LanguageConstants.JS_MIME);
115: String lResult = lRewriter.rewrite(lInput,
116: getDefaultTranslator());
117: assertEquals("Failed: \n", lExpectation, lResult);
118: }//testSimpleURLParams()
119:
120: /**
121: * null as a function param value, should not get rewritten
122: */
123: public void testBug4627258() throws Exception {
124: String lInput = "function Action(href,oc,t)\n"
125: + "{\n"
126: + " this.href = href;\n"
127: + " this.onClick = oc\n"
128: + " this.target = t;\n"
129: + "}//Action()\n"
130: + "\n"
131: + "dominoActions[8] = new Action('/mail/pshanks.nsf/a7fd7540d0bfb3e58825643c0002d673/$searchForm?SearchView',null,'top');\n"
132: + "dominoActions[8] = new Action(null,null,'top');\n";
133:
134: String lExpectation = "function Action(href,oc,t)\n"
135: + "{\n"
136: + " this.href = href;\n"
137: + " this.onClick = oc\n"
138: + " this.target = t;\n"
139: + "}//Action()\n"
140: + "\n"
141: + "dominoActions[8] = new Action('http://rajanagendra.sun.com/mail/pshanks.nsf/a7fd7540d0bfb3e58825643c0002d673/$searchForm?SearchView',null,'top');\n"
142: + "dominoActions[8] = new Action(null,null,'top');\n";
143:
144: String lRules = "<Function type=\"URL\" name=\"*ctio*\" paramPatterns=\"y,,\" />";
145:
146: RuleSet lRuleSet = CreateRuleSet.withJSFunctionRules(lRules);
147: Rewriter lRewriter = RewriterPool.create(lRuleSet,
148: LanguageConstants.JS_MIME);
149: String lResult = lRewriter.rewrite(lInput,
150: getDefaultTranslator());
151: assertEquals("Failed: \n", lExpectation, lResult);
152: }//testBug4627258()
153:
154: public void testBug4627258Varient() throws Exception {
155: String lInput =
156:
157: "dominoActions[8] = new Action('/mail/pshanks.nsf/a7fd7540d0bfb3e58825643c0002d673/$searchForm?SearchView',null,'top');\n"
158: + "dominoActions[9] = new Action(null,null,'top');\n"
159: + "dominoActions[10] = new Action('mail/pshanks.nsf/a7fd7540d0bfb3e58825643c0002d673/$searchForm?SearchView',null,'top');\n"
160: + "\n"
161: + "function Action(href,oc,t)\n"
162: + "{\n"
163: + " this.href = href;\n"
164: + " this.onClick = oc\n"
165: + " this.target = t;\n" + "}//Action()\n";
166:
167: String lExpectation = "dominoActions[8] = new Action('http://rajanagendra.sun.com/mail/pshanks.nsf/a7fd7540d0bfb3e58825643c0002d673/$searchForm?SearchView',null,'top');\n"
168: + "dominoActions[9] = new Action(null,null,'top');\n"
169: + "dominoActions[10] = new Action('http://rajanagendra.sun.com/Base/Raja/mail/pshanks.nsf/a7fd7540d0bfb3e58825643c0002d673/$searchForm?SearchView',null,'top');\n"
170: + "\n"
171: + "function Action(href,oc,t)\n"
172: + "{\n"
173: + " this.href = href;\n"
174: + " this.onClick = oc\n"
175: + " this.target = t;\n" + "}//Action()\n";
176:
177: String lRules = "<Function type=\"URL\" name=\"*ctio*\" paramPatterns=\"y\" source = \"http*html\"/>";
178:
179: RuleSet lRuleSet = CreateRuleSet.withJSFunctionRules(lRules);
180: Rewriter lRewriter = RewriterPool.create(lRuleSet,
181: LanguageConstants.JS_MIME);
182: String lResult = lRewriter.rewrite(lInput,
183: getDefaultTranslator());
184: assertEquals("Failed: \n", lExpectation, lResult);
185: }//testBug4627258Varient()
186:
187: /**
188: *FIXME:Bug-4638498
189: */
190: public void atestPolyMorphicFunctions() throws Exception {
191: String lInput = "dominoActions[10] = new Action('mail.html',null,'top');\n"
192: + "\n"
193: + "function Action(href,oc,t)\n"
194: + "{\n"
195: + " this.href = href;\n"
196: + " this.onClick = oc\n"
197: + " this.target = t;\n"
198: + "}//Action()\n"
199: + "\n"
200: + "function Action(oc,href)\n"
201: + "{\n"
202: + " this.href = href;\n"
203: + " this.onClick = oc\n"
204: + "}//Action()\n"
205: + "dominoActions[11] = new Action('top', '/second/pshanks.html');\n";
206:
207: String lExpectation = "dominoActions[10] = new Action('http://rajanagendra.sun.com/Base/Raja/mail.html',null,'top');\n"
208: + "\n"
209: + "function Action(href,oc,t)\n"
210: + "{\n"
211: + " this.href = href;\n"
212: + " this.onClick = oc\n"
213: + " this.target = t;\n"
214: + "}//Action()\n"
215: + "\n"
216: + "function Action(oc,href)\n"
217: + "{\n"
218: + " this.href = href;\n"
219: + " this.onClick = oc\n"
220: + "}//Action()\n"
221: + "dominoActions[11] = new Action('top', 'http://rajanagendra.sun.com/second/pshanks.html');\n";
222:
223: String lRules = "<Function type=\"URL\" name=\"*ctio*\" paramPatterns=\"y,,\" source = \"http*html\" />";
224:
225: RuleSet lRuleSet = CreateRuleSet.withJSFunctionRules(lRules);
226: Rewriter lRewriter = RewriterPool.create(lRuleSet,
227: LanguageConstants.JS_MIME);
228: String lResult = lRewriter.rewrite(lInput,
229: getDefaultTranslator());
230: assertEquals("Failed: \n", lExpectation, lResult);
231: }//testPolyMorphicFunctions()
232:
233: public void testFunctionCallFun() throws Exception {
234: String lInput = "function someFunction() \n"
235: + "{\n"
236: + " ab(getTopFrame().main.location.href=\"/someURI?someParameter\",\"test.html\");\n"
237: + "}\n";
238:
239: String lExpectation = "function someFunction() \n"
240: + "{\n"
241: + " ab(getTopFrame().main.location.href=\"http://rajanagendra.sun.com/someURI?someParameter\",\"http://rajanagendra.sun.com/Base/Raja/test.html\");\n"
242: + "}\n";
243:
244: String lJSVarRules[] = {
245: "<Variable name=\"*href\" type=\"URL\"/>",
246: "<Variable name=\"getTopFrame*.main.location.href\" type=\"URL\"/>",
247: "<Variable name=\"*.main.location.href\" type=\"URL\"/>",
248: "<Variable name=\"*main.location.href\" type=\"URL\"/>",
249: "<Variable name=\"*location.href\" type=\"URL\"/>", };
250:
251: String lJSFunRules[] = {
252: "<Function type=\"URL\" name=\"ab\" paramPatterns=\"y,y\"/>",
253: "<Function type=\"URL\" name=\"ab\" paramPatterns=\",y\"/>", };
254:
255: for (int i = 0; i < lJSVarRules.length; i++) {
256: for (int j = 0; j < lJSFunRules.length; j++) {
257: RuleSet lRuleSet = CreateRuleSet
258: .withHTMLAttJSVarJSFunRules("", lJSVarRules[i],
259: lJSFunRules[j]);
260: Rewriter lRewriter = RewriterPool.create(lRuleSet,
261: LanguageConstants.JS_MIME);
262: String lResult = lRewriter.rewrite(lInput,
263: getDefaultJSTranslator());
264: assertEquals(lExpectation, lResult);
265: }
266: }
267: }//testFunctionCallFun()
268:
269: //FixMe:&39;&39; 3rd param should not be rewritten
270: public void testEntityReferenceURLFunc() throws Exception {
271: String lInput = "iplanet( '/portal/document1.pdf','AB', '', 'RAJA.HTML')";
272: String lExpectation = "iplanet( 'http://rajanagendra.sun.com/portal/document1.pdf','AB', 'http://rajanagendra.sun.com/Base/Raja/', 'http://rajanagendra.sun.com/Base/Raja/RAJA.HTML')";
273:
274: String lJSFunRules = "<Function type=\"URL\" name=\"iplanet\" paramPatterns=\"y,,y,y\"/>";
275: RuleSet lRuleSet = CreateRuleSet
276: .withJSFunctionRules(lJSFunRules);
277: Rewriter lRewriter = RewriterPool.create(lRuleSet,
278: LanguageConstants.JS_MIME);
279: String lResult = lRewriter.rewrite(lInput,
280: getDefaultJSTranslator());
281: assertEquals(lExpectation, lResult);
282: }//testEntityReferenceURLFunc()
283:
284: public void testBugNo4742026VW_NavBar20_JS() throws Exception {
285: String lInput = "function window.document.oncontextmenu()\n"
286: + "{\n"
287: + " if(null != event.returnValue)\n"
288: + " {\n"
289: + " if(\"object\" == typeof(event.returnValue))\n"
290: + " {\n"
291: + " idTreeMenu.menuShow(true,event.clientX, event.clientY+15);\n"
292: + " }\n" + " }\n"
293: + " var testVar='/Base/Raja/abc.html';\n" + "}\n";
294:
295: String lExpectation = "function window.document.oncontextmenu()\n"
296: + "{\n"
297: + " if(null != event.returnValue)\n"
298: + " {\n"
299: + " if(\"object\" == typeof(event.returnValue))\n"
300: + " {\n"
301: + " idTreeMenu.menuShow(true,event.clientX, event.clientY+15);\n"
302: + " }\n"
303: + " }\n"
304: + " var testVar='http://rajanagendra.sun.com/Base/Raja/abc.html';\n"
305: + "}\n";
306:
307: String lVarRules = "<Variable name=\"testVar\"/>";
308:
309: RuleSet lRuleSet = CreateRuleSet.withJSVariableRules(lVarRules);
310:
311: Rewriter lRewriter = RewriterPool.create(lRuleSet,
312: LanguageConstants.JS_MIME);
313: String lResult = lRewriter.rewrite(lInput,
314: getDefaultJSTranslator());
315: assertEquals(lExpectation, lResult);
316: }//testBugNo4742026VW_NavBar20_JS()
317:
318: public void testBugNo4742026VW_NavBar20_JSVarient()
319: throws Exception {
320: String lInput = "function window.document.oncontextmenu.ab.c.d.e.f.g.h.i.j.k.l.m.n.op.q.r.s.t()\n"
321: + "{\n"
322: + " if(null != event.returnValue)\n"
323: + " {\n"
324: + " if(\"object\" == typeof(event.returnValue))\n"
325: + " {\n"
326: + " idTreeMenu.menuShow(true,event.clientX, event.clientY+15);\n"
327: + " }\n"
328: + " }\n"
329: + " var testVar='/Base/Raja/abc.html';\n" + "}\n";
330:
331: String lExpectation = "function window.document.oncontextmenu.ab.c.d.e.f.g.h.i.j.k.l.m.n.op.q.r.s.t()\n"
332: + "{\n"
333: + " if(null != event.returnValue)\n"
334: + " {\n"
335: + " if(\"object\" == typeof(event.returnValue))\n"
336: + " {\n"
337: + " idTreeMenu.menuShow(true,event.clientX, event.clientY+15);\n"
338: + " }\n"
339: + " }\n"
340: + " var testVar='http://rajanagendra.sun.com/Base/Raja/abc.html';\n"
341: + "}\n";
342:
343: String lVarRules = "<Variable name=\"testVar\"/>";
344:
345: RuleSet lRuleSet = CreateRuleSet.withJSVariableRules(lVarRules);
346:
347: Rewriter lRewriter = RewriterPool.create(lRuleSet,
348: LanguageConstants.JS_MIME);
349: String lResult = lRewriter.rewrite(lInput,
350: getDefaultJSTranslator());
351: assertEquals(lExpectation, lResult);
352: }//testBugNo4742026VW_NavBar20_JSVarient()
353:
354: public void testCaseSensitivityOfFunctionRules() throws Exception {
355: String lInput = "funcCALL('abc.html', 'xyz.html');\n"
356: + "funcCall('abc.html', 'xyz.html');\n"
357: + "funcCaLL(raja='abc.html', rAja= 'xyz.html');\n";
358:
359: String lExpectation = "funcCALL('abc.html', 'xyz.html');\n"
360: + "funcCall('http://rajanagendra.sun.com/Base/Raja/abc.html', 'http://rajanagendra.sun.com/Base/Raja/xyz.html');\n"
361: + "funcCaLL(raja='abc.html', rAja= 'xyz.html');\n";
362:
363: String[] lJSFunRules = {
364: "<Function type=\"URL\" name=\"funcCall\" paramPatterns=\"y,y\"/>",
365: "<Function type=\"EXPRESSION\" name=\"funcCall\" paramPatterns=\"y,y\"/>",
366: "<Function name=\"funcCall\" paramPatterns=\"y,y\"/>",
367: "<Function type=\"URL\" name=\"funcCall\" paramPatterns=\"y,y\" source='*'/>", };
368:
369: for (int i = 0; i < lJSFunRules.length; i++) {
370: RuleSet lRuleSet = CreateRuleSet
371: .withJSFunctionRules(lJSFunRules[i]);
372: Rewriter lRewriter = RewriterPool.create(lRuleSet,
373: LanguageConstants.JS_MIME);
374: String lResult = lRewriter.rewrite(lInput,
375: getDefaultJSTranslator());
376: assertEquals(lExpectation, lResult);
377: }
378: }//testCaseSensitivityOfFunctionRules()
379:
380: public void testBugNo4908891() throws Exception {
381: String lInput = "<HTML>\n"
382: + "<HEAD>\n"
383: + "\n"
384: + "<SCRIPT LANGUAGE=\"JavaScript\">\n"
385: + "\n"
386: + " var width, height = 22;\n"
387: + "\n"
388: + " l4 = new List(false, width, height, bgColor);\n"
389: + " l4.addItem(\"<a class=subMenu href='javascript:select(l4,\\\"Monatsliste.asp\\\",\\\n"
390: + "3)'>Monatsliste</a>\",false);\n" + "\n" + "\n"
391: + "</SCRIPT>\n" + "\n" + "A line of text\n" + "\n"
392: + "</BODY>\n" + "</HTML>\n";
393:
394: String lExpectation = "<HTML>\n"
395: + "<HEAD>\n"
396: + "\n"
397: + "<SCRIPT LANGUAGE=\"JavaScript\">\n"
398: + "\n"
399: + " var width, height = 22;\n"
400: + "\n"
401: + " l4 = new List(false, width, height, bgColor);\n"
402: + " l4.addItem(\"<a class=subMenu href='javascript:select(l4,\\\"http://rajanagendra.sun.com/Base/Raja/Monatsliste.asp\\\",3)'>Monatsliste</a>\",false);\n"
403: + "\n" + "\n" + "</SCRIPT>\n" + "\n"
404: + "A line of text\n" + "\n" + "</BODY>\n" + "</HTML>\n";
405:
406: String lHTMLAttRules = "<Attribute name=\"href\" />";
407: String lJSFunRules = "<Function type=\"URL\" name=\"select\" paramPatterns=\",y,,\"/>"
408: + "<Function type=\"DHTML\" name=\"*addItem\" paramPatterns=\"y\"/>";
409:
410: RuleSet lRuleSet = CreateRuleSet.withHTMLAttJSFunRules(
411: lHTMLAttRules, lJSFunRules);
412: Rewriter lRewriter = RewriterPool.create(lRuleSet,
413: LanguageConstants.HTML_MIME);
414: String lResult = lRewriter.rewrite(lInput,
415: getDefaultTranslator());
416: assertEquals(lExpectation, lResult);
417: }//testBugNo4908891()
418:
419: public static void main(String[] args) {
420: RewriterModule.initFile();
421: //BasicTestCase.run( TestJSURLFunctions.class );
422: TestSuite testSuite = new TestSuite();
423: testSuite.addTest(new TestJSURLFunctions("testBugNo4908891"));
424: BasicTestCase.run(testSuite);
425: }//main()
426:
427: }//class TestJSURLFunctions
|