001: /* ****************************************************************************
002: * Compiler_Test.java
003: * ****************************************************************************/
004:
005: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
006: * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
007: * Use is subject to license terms. *
008: * J_LZ_COPYRIGHT_END *********************************************************/
009:
010: package org.openlaszlo.compiler;
011:
012: import java.io.*;
013: import java.util.*;
014: import org.jdom.Element;
015: import junit.framework.*;
016: import org.openlaszlo.xml.internal.XMLUtils;
017:
018: /*
019: * junit.awtui.TestRunner
020: * junit.swingui.TestRunner
021: * junit.textui.TestRunner
022: * java junit.textui.TestRunner org.openlaszlo.utils.Compiler_Test
023: *
024: * @author Oliver Steele
025: */
026:
027: public class Compiler_Test extends TestCase {
028: public Compiler_Test(String name) {
029: super (name);
030: }
031:
032: public void setUp() {
033: }
034:
035: public void testAdjustRelativePath() {
036: String tests[] = {
037: // sourcedir, destdir, source, dest
038: // dest == null is an abbreviation for dest == source
039:
040: // If it's not an URL, don't change it
041: "from",
042: "to",
043: "base.ext",
044: null, // 1
045: "from",
046: "to",
047: "dir/base.ext",
048: null, // 2
049: "from",
050: "to",
051: "/dir/base.ext",
052: null, // 3
053: "from",
054: "to",
055: "c:/dir/base.ext",
056: null, // 4
057:
058: // If it's an absolute URL, don't change it
059: "from",
060: "to",
061: "http:///base.ext",
062: null, // 5
063: "from",
064: "to",
065: "http:///dir/base.ext",
066: null, // 6
067:
068: // If it's on another host, don't change it
069: "from",
070: "to",
071: "http://host.com/base.ext",
072: null, // 7
073: "from",
074: "to",
075: "http://host.com/dir/base.ext",
076: null, // 8
077:
078: // Relative URL on this host
079: "",
080: "",
081: "http:base.ext",
082: null, // 9
083: ".",
084: "",
085: "http:base.ext",
086: null, // 10
087: "",
088: ".",
089: "http:base.ext",
090: null, // 11
091: ".",
092: ".",
093: "http:base.ext",
094: null, // 12
095: "from",
096: "",
097: "http:base.ext",
098: "http:../base.ext", // 13
099: "",
100: "to",
101: "http:base.ext",
102: "http:to/base.ext", // 14
103: "from",
104: "to",
105: "http:base.ext",
106: "http:../to/base.ext", // 15
107: "/from",
108: "/to",
109: "http:base.ext",
110: "http:../to/base.ext", // 16
111: "c:/from",
112: "c:/to",
113: "http:base.ext",
114: "http:../to/base.ext", // 17
115:
116: // Preserve protocol and query
117: // https: gets "unknown protocol" under ant
118: "from",
119: "to",
120: "ftp:base.ext",
121: "ftp:../to/base.ext", // 18
122: "from",
123: "to",
124: "http:base.ext?query",
125: "http:../to/base.ext?query", // 19
126:
127: // no way to indicate port on a relative URL?
128: // doesn't preserve fragment, but the agent doesn't parse this
129: // anyway
130: //"from", "to", "http:base.ext#frag", "http:../to/base.ext#frag", // 19
131:
132: // Absolute pathnames on same root
133: "/c/from", "/c/to", "http:base.ext",
134: "http:../to/base.ext", "c:/from", "c:/to",
135: "http:base.ext", "http:../to/base.ext", };
136: int i = 0;
137: while (i < tests.length) {
138: File sourcedir = new File(tests[i++]);
139: File destdir = new File(tests[i++]);
140: String source = tests[i++];
141: String dest = tests[i++];
142: if (dest == null) {
143: dest = source;
144: }
145: String result = CompilationEnvironment.adjustRelativeURL(
146: source, sourcedir, destdir);
147: assertEquals(
148: "" + i / 4 + ": adjustRelativeURL(\"" + source
149: + "\", \"" + sourcedir + "\", \"" + destdir
150: + "\")", result, dest);
151: }
152: }
153:
154: public void testRlementRecognition() {
155: // local datasets
156: for (Iterator iter = Arrays.asList(
157: new String[] { "<dataset/>",
158: "<dataset src=\"local\"/>",
159: "<dataset src=\"c:/local\"/>", }).iterator(); iter
160: .hasNext();) {
161: String src = (String) iter.next();
162: Element elt = XMLUtils.parse(src);
163: assertTrue(src, DataCompiler.isElement(elt));
164: }
165: // non-local dataset
166: for (Iterator iter = Arrays.asList(
167: new String[] { "<dataset type=\"soap\"/>",
168: "<dataset type=\"http\"/>",
169: "<dataset src=\"http:foo\"/>",
170: "<dataset src=\"https:foo\"/>",
171: "<dataset src=\"ftp:foo\"/>", }).iterator(); iter
172: .hasNext();) {
173: String src = (String) iter.next();
174: Element elt = XMLUtils.parse(src);
175: assertTrue(src, !DataCompiler.isElement(elt));
176: }
177: }
178: }
|