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.AbsoluteTranslator;
008: import com.sun.portal.rewriter.AbstractTranslator;
009: import com.sun.portal.rewriter.RewriterModule;
010: import com.sun.portal.rewriter.Translator;
011: import com.sun.portal.rewriter.test.util.BasicTestCase;
012: import com.sun.portal.rewriter.util.uri.DecoratedURI;
013: import com.sun.portal.rewriter.util.uri.PageSpec;
014: import junit.framework.TestCase;
015: import junit.framework.TestSuite;
016:
017: public class TestTranslator extends BasicTestCase {
018: public TestTranslator(String aName) {
019: super (aName);
020: }//constuctor
021:
022: public void testSimpleTranslation() throws Exception {
023: assertEquals("http://abc.com/abc.html", new AbsoluteTranslator(
024: new PageSpec("http://abc.com")).translate("abc.html"));
025: }//testSimpleTranslation()
026:
027: public void testDelegator() throws Exception {
028: final String gatewayURI = "http://gateway.sun.com/";
029: final String[][] lData = {
030: {
031: "ftp://user:password@sun.com/index.html",
032: gatewayURI
033: + "ftp://user:password@sun.com/index.html" }, //0
034: {
035: "././",
036: gatewayURI
037: + "ftp://rajanagendra.sun.com/Base/Raja/" }, //1
038: {
039: "#raj.index",
040: gatewayURI
041: + "ftp://rajanagendra.sun.com/Base/Raja/raja.html?name=raja#raj.index" }, //2
042: {
043: "./././././abc.html",
044: gatewayURI
045: + "ftp://rajanagendra.sun.com/Base/Raja/abc.html" }, //3
046: {
047: "\"/index.html\"",
048: "\""
049: + gatewayURI
050: + "ftp://rajanagendra.sun.com/index.html\"" }, //4
051: { "''\"'\"''' \"'\"' ",
052: "''\"'\"''' \"'\"' " }, //5
053: { "ftp://www.sun.com:21/abc.html",
054: gatewayURI + "ftp://www.sun.com/abc.html" }, //6
055: {
056: "index.",
057: gatewayURI
058: + "ftp://rajanagendra.sun.com/Base/Raja/index." }, //7 - Bug 4658040
059: {
060: "../../index.",
061: gatewayURI
062: + "ftp://rajanagendra.sun.com/index." }, //8 - - Bug 4658040
063: { "../",
064: gatewayURI + "ftp://rajanagendra.sun.com/Base/" }, //9
065: { "../../", gatewayURI + "ftp://rajanagendra.sun.com/" }, //10
066: { "..", gatewayURI + "ftp://rajanagendra.sun.com/Base/" }, //11
067: { "../..", gatewayURI + "ftp://rajanagendra.sun.com/" }, //12
068: {
069: "./.",
070: gatewayURI
071: + "ftp://rajanagendra.sun.com/Base/Raja/" }, //13
072: { " ", " " }, //14
073: { "", "" }, //15
074: {
075: "../../../../functions/javascript.js",
076: gatewayURI
077: + "ftp://rajanagendra.sun.com/functions/javascript.js" }, //16
078: {
079: "../../img/background.jpg",
080: gatewayURI
081: + "ftp://rajanagendra.sun.com/img/background.jpg" }, //17
082: { "ftp://www.sun.com/abc.html",
083: gatewayURI + "ftp://www.sun.com/abc.html" }, //18
084: { "ftp://www.sun.com/sh.html",
085: gatewayURI + "ftp://www.sun.com/sh.html" }, //19
086: { "ftp://www.sun.com:78/sh.html",
087: gatewayURI + "ftp://www.sun.com:78/sh.html" }, //20
088: { "ftp://www.sun.com:400/sh.html",
089: gatewayURI + "ftp://www.sun.com:400/sh.html" }, //21
090: {
091: "abc.html",
092: gatewayURI
093: + "ftp://rajanagendra.sun.com/Base/Raja/abc.html" }, //22
094: {
095: "./abc.html",
096: gatewayURI
097: + "ftp://rajanagendra.sun.com/Base/Raja/abc.html" }, //23
098: {
099: "/abc.html",
100: gatewayURI
101: + "ftp://rajanagendra.sun.com/abc.html" }, //24
102: {
103: "../../abc.html",
104: gatewayURI
105: + "ftp://rajanagendra.sun.com/abc.html" }, //25
106: {
107: "../abc.html",
108: gatewayURI + "ftp://rajanagendra.sun.com/Base/"
109: + "abc.html" }, //26
110: {
111: "../abc.html?turn=true",
112: gatewayURI
113: + "ftp://rajanagendra.sun.com/Base/abc.html?turn=true" }, //27
114: {
115: "./abc.html?turn=true",
116: gatewayURI
117: + "ftp://rajanagendra.sun.com/Base/Raja/abc.html?turn=true" }, //28
118: {
119: "ftp://www.sun.com:400/sh.html?roja=ramani",
120: gatewayURI
121: + "ftp://www.sun.com:400/sh.html?roja=ramani" }, //29
122: {
123: "./raja/mahesh/.././abc.html",
124: gatewayURI
125: + "ftp://rajanagendra.sun.com/Base/Raja/raja/abc.html" }, //30
126: {
127: "null",
128: gatewayURI
129: + "ftp://rajanagendra.sun.com/Base/Raja/null" }, //31 - Bug4627258
130: { "user:password@./index.html",
131: "user:password@./index.html" }, //32
132: { "user:password@ftp://sun.com/index.html",
133: "user:password@ftp://sun.com/index.html" }, //33
134: //{"ftp://host.com/file.shtml?view=new_cal&id=lakjsdfla&date=99", "ftp://host.com/file.shtml?view=new_cal&id=lakjsdfla&date=99"}, //34
135: //{"/file.shtml?view=new_cal&id=lakjsdfla&date=99", "ftp://rajanagendra.sun.com/file.shtml?view=new_cal&id=lakjsdfla&date=99"}, //35
136: };
137: PageSpec samplePageSpec = new PageSpec(
138: "ftp://rajanagendra.sun.com/Base/Raja/raja.html?name=raja");
139: Translator lTranslator = new AbstractTranslator(samplePageSpec) {
140: public String hook4Translate(
141: final DecoratedURI aAbsoluteURI,
142: final DecoratedURI aAbosoluteURI) {
143: return gatewayURI + aAbosoluteURI.toExternalForm();
144: }
145: };
146:
147: for (int i = 0; i < lData.length; i++) {
148: String result = lTranslator.translate(lData[i][0]);
149: assertEquals("Failed at i=" + i + ":", lData[i][1], result);
150: }
151: }//testDelegator()
152:
153: public void testISOnlyReference() throws Exception {
154: String[][] lData = { { "?", "false" }, { "#", "true" },
155: { " # ", "true" }, { "\n#", "true" },
156: { " \n # ", "true" }, { "/", "false" },
157: { ".#", "false" }, { "#?", "true" },
158: { ".#??", "false" },
159: { "https://raja.sun./com", "false" },
160: { null, "false" }, { "", "false" },
161:
162: };
163:
164: PageSpec samplePageSpec = new PageSpec(
165: "ftp://rajanagendra.sun.com/Base/Raja/raja.html?name=raja");
166: for (int i = 0; i < lData.length; i++) {
167: new ReferenceCheck(samplePageSpec, Boolean.valueOf(
168: lData[i][1]).booleanValue()).translate(lData[i][0]);
169: }
170: }//testISOnlyReference()
171:
172: public static void main(String[] args) throws Exception {
173: RewriterModule.initFile();
174: //BasicTestCase.run( TestTranslator.class );
175: TestSuite testSuite = new TestSuite();
176: testSuite.addTest(new TestTranslator("testSimpleTranslation"));
177: BasicTestCase.run(testSuite);
178: }//main()
179:
180: }//class TestTranslator
181:
182: class ReferenceCheck extends AbstractTranslator {
183: private boolean onlyReference;
184:
185: ReferenceCheck(PageSpec aPageSpec, boolean aBool) {
186: super (aPageSpec);
187: onlyReference = aBool;
188: }
189:
190: public String hook4Translate(final DecoratedURI aAbsoluteURI,
191: final DecoratedURI aAbosoluteURI) {
192: TestCase.assertEquals(aAbosoluteURI.isOnlyReference(),
193: onlyReference);
194: return aAbosoluteURI.toExternalForm();
195: }
196:
197: }//class ReferenceCheck
|