1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package com.sun.star.lib.uno.helper; 25 public class UnoUrlTest { 26 UnoUrlTest()27 private UnoUrlTest() { 28 } 29 30 fail(String msg)31 private void fail(String msg) { 32 System.err.println(msg); 33 System.exit(1); 34 } 35 log(String msg)36 private static void log(String msg) { 37 System.out.println(msg); 38 } 39 assertTrue(boolean b)40 private void assertTrue(boolean b) { 41 if (!b) 42 fail("boolean assertion failed"); 43 } 44 assertEquals(String expected, String actual)45 private void assertEquals(String expected, String actual) { 46 if (!expected.equals(actual)) { 47 fail("Expected: '"+ expected + "' but was: '"+actual+"'"); 48 } 49 } 50 assertEquals(int expected, int actual)51 private void assertEquals(int expected, int actual) { 52 if (expected != actual) { 53 fail("Expected: "+ expected + " but was: "+actual); 54 } 55 } 56 testStart1()57 public void testStart1() { 58 try { 59 UnoUrl url = UnoUrl.parseUnoUrl("uno:x;y;z"); 60 assertTrue((url != null)); 61 assertEquals("x", url.getConnection()); 62 } catch (com.sun.star.lang.IllegalArgumentException e) { 63 fail("Caught exception:" + e.getMessage()); 64 } 65 } 66 testStart2()67 public void testStart2() { 68 try { 69 UnoUrl url = UnoUrl.parseUnoUrl("uno1:x;y;z"); 70 fail("Should throw an exception"); 71 } catch (com.sun.star.lang.IllegalArgumentException e) { 72 } 73 } 74 testStart3()75 public void testStart3() { 76 try { 77 UnoUrl url = UnoUrl.parseUnoUrl("un:x;y;z"); 78 fail("Should throw an exception"); 79 } catch (com.sun.star.lang.IllegalArgumentException e) { 80 } 81 } 82 testStart4()83 public void testStart4() { 84 try { 85 UnoUrl url = UnoUrl.parseUnoUrl("x;y;z"); 86 assertTrue((url != null)); 87 assertEquals("y", url.getProtocol()); 88 } catch (com.sun.star.lang.IllegalArgumentException e) { 89 fail("Caught exception:" + e.getMessage()); 90 } 91 } 92 testParam1()93 public void testParam1() { 94 try { 95 UnoUrl url = UnoUrl.parseUnoUrl("uno:"); 96 fail("Should throw an exception"); 97 } catch (com.sun.star.lang.IllegalArgumentException e) { 98 } 99 } 100 testParam2()101 public void testParam2() { 102 try { 103 UnoUrl url = UnoUrl.parseUnoUrl("uno:a;"); 104 fail("Should throw an exception"); 105 } catch (com.sun.star.lang.IllegalArgumentException e) { 106 } 107 } 108 testPartName1()109 public void testPartName1() { 110 try { 111 UnoUrl url = UnoUrl.parseUnoUrl("uno:abc!abc;b;c"); 112 fail("Should throw an exception"); 113 } catch (com.sun.star.lang.IllegalArgumentException e) { 114 } 115 } 116 testOID1()117 public void testOID1() { 118 try { 119 UnoUrl url = UnoUrl.parseUnoUrl("uno:x;y;ABC<ABC"); 120 fail("Should throw an exception"); 121 } catch (com.sun.star.lang.IllegalArgumentException e) { 122 } 123 } 124 testOIDandParams1()125 public void testOIDandParams1() { 126 try { 127 UnoUrl url = UnoUrl.parseUnoUrl("uno:x,key9=val9;y;ABC"); 128 assertTrue((url != null)); 129 assertEquals("ABC", url.getRootOid()); 130 assertEquals(1, url.getConnectionParameters().size()); 131 assertEquals("val9", (String)url.getConnectionParameters().get("key9")); 132 } catch (com.sun.star.lang.IllegalArgumentException e) { 133 fail(e.getMessage()); 134 } 135 } 136 testOIDandParams2()137 public void testOIDandParams2() { 138 try { 139 UnoUrl url = UnoUrl.parseUnoUrl("uno:x,key1=val1,k2=v2;y,k3=v3;ABC()!/"); 140 assertTrue((url != null)); 141 assertEquals("ABC()!/", url.getRootOid()); 142 assertEquals(2, url.getConnectionParameters().size()); 143 assertEquals(1, url.getProtocolParameters().size()); 144 } catch (com.sun.star.lang.IllegalArgumentException e) { 145 fail("Caught exception:" + e.getMessage()); 146 } 147 } 148 testParams1()149 public void testParams1() { 150 try { 151 UnoUrl url = UnoUrl.parseUnoUrl("uno:x,abc!abc=val;y;ABC"); 152 fail("Should throw an exception"); 153 } catch (com.sun.star.lang.IllegalArgumentException e) { 154 } 155 } 156 testParams2()157 public void testParams2() { 158 try { 159 UnoUrl url = UnoUrl.parseUnoUrl("uno:x,abc=val<val;y;ABC"); 160 fail("Should throw an exception"); 161 } catch (com.sun.star.lang.IllegalArgumentException e) { 162 } 163 } 164 testParams3()165 public void testParams3() { 166 try { 167 UnoUrl url = UnoUrl.parseUnoUrl("uno:x,abc=val!()val;y;ABC"); 168 assertTrue((url != null)); 169 assertEquals(1, url.getConnectionParameters().size()); 170 } catch (com.sun.star.lang.IllegalArgumentException e) { 171 fail("Caught exception:" + e.getMessage()); 172 } 173 } 174 testCommon()175 public void testCommon() { 176 try { 177 UnoUrl url = 178 UnoUrl.parseUnoUrl( 179 "socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"); 180 assertTrue((url != null)); 181 assertEquals("StarOffice.ServiceManager", url.getRootOid()); 182 assertEquals("socket", url.getConnection()); 183 assertEquals("urp", url.getProtocol()); 184 assertEquals("2002", (String)url.getConnectionParameters().get("port")); 185 } catch (com.sun.star.lang.IllegalArgumentException e) { 186 fail("Caught exception:" + e.getMessage()); 187 } 188 } 189 testUTF()190 public void testUTF() { 191 try { 192 UnoUrl url = 193 UnoUrl.parseUnoUrl( 194 "socket,host=localhost,horst=abc%c3%9c%c3%a4ABC%41%2c%2C,port=2002;urp;StarOffice.ServiceManager"); 195 assertEquals("abc��ABCA,,", (String)url.getConnectionParameters().get("horst")); 196 assertEquals( 197 "host=localhost,horst=abc%c3%9c%c3%a4ABC%41%2c%2C,port=2002", 198 url.getConnectionParametersAsString()); 199 } catch (com.sun.star.lang.IllegalArgumentException e) { 200 fail("Caught exception:" + e.getMessage()); 201 } 202 203 } 204 testUTF1()205 public void testUTF1() { 206 try { 207 UnoUrl url = UnoUrl.parseUnoUrl("uno:x,abc=val%4t;y;ABC"); 208 fail("Should throw an exception"); 209 } catch (com.sun.star.lang.IllegalArgumentException e) { 210 } 211 } 212 213 main(String args[])214 public static void main(String args[]) { 215 UnoUrlTest t = new UnoUrlTest(); 216 217 log("Running test case 1"); 218 t.testStart1(); 219 log("Running test case 2"); 220 t.testStart2(); 221 log("Running test case 3"); 222 t.testStart3(); 223 log("Running test case 4"); 224 t.testStart4(); 225 226 log("Running test case 5"); 227 t.testParam1(); 228 log("Running test case 6"); 229 t.testParam2(); 230 231 log("Running test case 7"); 232 t.testPartName1(); 233 234 log("Running test case 8"); 235 t.testOID1(); 236 237 log("Running test case 9"); 238 t.testOIDandParams1(); 239 log("Running test case 10"); 240 t.testOIDandParams2(); 241 242 log("Running test case 11"); 243 t.testParams1(); 244 log("Running test case 12"); 245 t.testParams2(); 246 log("Running test case 13"); 247 t.testParams3(); 248 249 log("Running test case 14"); 250 t.testCommon(); 251 252 log("Running test case 15"); 253 t.testUTF(); 254 log("Running test case 16"); 255 t.testUTF1(); 256 } 257 } 258