1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.lib.uno.helper;
29 public class UnoUrlTest  {
30 
31 	private UnoUrlTest() {
32 	}
33 
34 
35 	private void fail(String msg) {
36 		System.err.println(msg);
37 		System.exit(1);
38 	}
39 
40 	private static void log(String msg) {
41 		System.out.println(msg);
42 	}
43 
44 	private void assertTrue(boolean b) {
45 		if (!b)
46 			fail("boolean assertion failed");
47 	}
48 
49 	private void assertEquals(String expected, String actual) {
50 		if (!expected.equals(actual)) {
51 			fail("Expected: '"+ expected + "' but was: '"+actual+"'");
52 		}
53 	}
54 
55 	private void assertEquals(int expected, int actual) {
56 		if (expected != actual) {
57 			fail("Expected: "+ expected + " but was: "+actual);
58 		}
59 	}
60 
61 	public void testStart1() {
62 		try {
63 			UnoUrl url = UnoUrl.parseUnoUrl("uno:x;y;z");
64 			assertTrue((url != null));
65 			assertEquals("x", url.getConnection());
66 		} catch (com.sun.star.lang.IllegalArgumentException e) {
67 			fail("Caught exception:" + e.getMessage());
68 		}
69 	}
70 
71 	public void testStart2() {
72 		try {
73 			UnoUrl url = UnoUrl.parseUnoUrl("uno1:x;y;z");
74 			fail("Should throw an exception");
75 		} catch (com.sun.star.lang.IllegalArgumentException e) {
76 		}
77 	}
78 
79 	public void testStart3() {
80 		try {
81 			UnoUrl url = UnoUrl.parseUnoUrl("un:x;y;z");
82 			fail("Should throw an exception");
83 		} catch (com.sun.star.lang.IllegalArgumentException e) {
84 		}
85 	}
86 
87 	public void testStart4() {
88 		try {
89 			UnoUrl url = UnoUrl.parseUnoUrl("x;y;z");
90 			assertTrue((url != null));
91 			assertEquals("y", url.getProtocol());
92 		} catch (com.sun.star.lang.IllegalArgumentException e) {
93 			fail("Caught exception:" + e.getMessage());
94 		}
95 	}
96 
97 	public void testParam1() {
98 		try {
99 			UnoUrl url = UnoUrl.parseUnoUrl("uno:");
100 			fail("Should throw an exception");
101 		} catch (com.sun.star.lang.IllegalArgumentException e) {
102 		}
103 	}
104 
105 	public void testParam2() {
106 		try {
107 			UnoUrl url = UnoUrl.parseUnoUrl("uno:a;");
108 			fail("Should throw an exception");
109 		} catch (com.sun.star.lang.IllegalArgumentException e) {
110 		}
111 	}
112 
113 	public void testPartName1() {
114 		try {
115 			UnoUrl url = UnoUrl.parseUnoUrl("uno:abc!abc;b;c");
116 			fail("Should throw an exception");
117 		} catch (com.sun.star.lang.IllegalArgumentException e) {
118 		}
119 	}
120 
121 	public void testOID1() {
122 		try {
123 			UnoUrl url = UnoUrl.parseUnoUrl("uno:x;y;ABC<ABC");
124 			fail("Should throw an exception");
125 		} catch (com.sun.star.lang.IllegalArgumentException e) {
126 		}
127 	}
128 
129 	public void testOIDandParams1() {
130 		try {
131 			UnoUrl url = UnoUrl.parseUnoUrl("uno:x,key9=val9;y;ABC");
132 			assertTrue((url != null));
133 			assertEquals("ABC", url.getRootOid());
134 			assertEquals(1, url.getConnectionParameters().size());
135 			assertEquals("val9", (String)url.getConnectionParameters().get("key9"));
136 		} catch (com.sun.star.lang.IllegalArgumentException e) {
137 			fail(e.getMessage());
138 		}
139 	}
140 
141 	public void testOIDandParams2() {
142 		try {
143 			UnoUrl url = UnoUrl.parseUnoUrl("uno:x,key1=val1,k2=v2;y,k3=v3;ABC()!/");
144 			assertTrue((url != null));
145 			assertEquals("ABC()!/", url.getRootOid());
146 			assertEquals(2, url.getConnectionParameters().size());
147 			assertEquals(1, url.getProtocolParameters().size());
148 		} catch (com.sun.star.lang.IllegalArgumentException e) {
149 			fail("Caught exception:" + e.getMessage());
150 		}
151 	}
152 
153 	public void testParams1() {
154 		try {
155 			UnoUrl url = UnoUrl.parseUnoUrl("uno:x,abc!abc=val;y;ABC");
156 			fail("Should throw an exception");
157 		} catch (com.sun.star.lang.IllegalArgumentException e) {
158 		}
159 	}
160 
161 	public void testParams2() {
162 		try {
163 			UnoUrl url = UnoUrl.parseUnoUrl("uno:x,abc=val<val;y;ABC");
164 			fail("Should throw an exception");
165 		} catch (com.sun.star.lang.IllegalArgumentException e) {
166 		}
167 	}
168 
169 	public void testParams3() {
170 		try {
171 			UnoUrl url = UnoUrl.parseUnoUrl("uno:x,abc=val!()val;y;ABC");
172 			assertTrue((url != null));
173 			assertEquals(1, url.getConnectionParameters().size());
174 		} catch (com.sun.star.lang.IllegalArgumentException e) {
175 			fail("Caught exception:" + e.getMessage());
176 		}
177 	}
178 
179 	public void testCommon() {
180 		try {
181 			UnoUrl url =
182 				UnoUrl.parseUnoUrl(
183 					"socket,host=localhost,port=2002;urp;StarOffice.ServiceManager");
184 			assertTrue((url != null));
185 			assertEquals("StarOffice.ServiceManager", url.getRootOid());
186 			assertEquals("socket", url.getConnection());
187 			assertEquals("urp", url.getProtocol());
188 			assertEquals("2002", (String)url.getConnectionParameters().get("port"));
189 		} catch (com.sun.star.lang.IllegalArgumentException e) {
190 			fail("Caught exception:" + e.getMessage());
191 		}
192 	}
193 
194 	public void testUTF() {
195 		try {
196 			UnoUrl url =
197 				UnoUrl.parseUnoUrl(
198 					"socket,host=localhost,horst=abc%c3%9c%c3%a4ABC%41%2c%2C,port=2002;urp;StarOffice.ServiceManager");
199 			assertEquals("abc��ABCA,,", (String)url.getConnectionParameters().get("horst"));
200 			assertEquals(
201 				"host=localhost,horst=abc%c3%9c%c3%a4ABC%41%2c%2C,port=2002",
202 				url.getConnectionParametersAsString());
203 		} catch (com.sun.star.lang.IllegalArgumentException e) {
204 			fail("Caught exception:" + e.getMessage());
205 		}
206 
207 	}
208 
209 	public void testUTF1() {
210 		try {
211 			UnoUrl url = UnoUrl.parseUnoUrl("uno:x,abc=val%4t;y;ABC");
212 			fail("Should throw an exception");
213 		} catch (com.sun.star.lang.IllegalArgumentException e) {
214 		}
215 	}
216 
217 
218 	public static void main(String args[]) {
219 		UnoUrlTest t = new UnoUrlTest();
220 
221 		log("Running test case 1");
222 		t.testStart1();
223 		log("Running test case 2");
224 		t.testStart2();
225 		log("Running test case 3");
226 		t.testStart3();
227 		log("Running test case 4");
228 		t.testStart4();
229 
230 		log("Running test case 5");
231 		t.testParam1();
232 		log("Running test case 6");
233 		t.testParam2();
234 
235 		log("Running test case 7");
236 		t.testPartName1();
237 
238 		log("Running test case 8");
239 		t.testOID1();
240 
241 		log("Running test case 9");
242 		t.testOIDandParams1();
243 		log("Running test case 10");
244 		t.testOIDandParams2();
245 
246 		log("Running test case 11");
247 		t.testParams1();
248 		log("Running test case 12");
249 		t.testParams2();
250 		log("Running test case 13");
251 		t.testParams3();
252 
253 		log("Running test case 14");
254 		t.testCommon();
255 
256 		log("Running test case 15");
257 		t.testUTF();
258 		log("Running test case 16");
259 		t.testUTF1();
260 	}
261 }
262