1 /** 2 * 3 */ 4 package test.crispy.util; 5 6 import java.util.HashMap; 7 import java.util.Iterator; 8 import java.util.Map; 9 import java.util.Properties; 10 import java.util.Vector; 11 12 import javax.swing.tree.DefaultMutableTreeNode; 13 import javax.xml.namespace.QName; 14 15 import junit.framework.TestCase; 16 import net.sf.crispy.InvocationException; 17 import net.sf.crispy.Property; 18 import net.sf.crispy.impl.MiniServer; 19 import net.sf.crispy.impl.rest.MiniRestServer; 20 import net.sf.crispy.interceptor.StopWatchInterceptor; 21 import net.sf.crispy.util.Util; 22 import test.crispy.JUnitTestException; 23 import test.crispy.example.interceptor.WaitInterceptor; 24 import test.crispy.example.model.Adresse; 25 import test.crispy.example.model.Kunde; 26 import test.crispy.example.service.Calculator; 27 import test.crispy.example.service.CalculatorImpl; 28 import test.crispy.example.service.Echo; 29 import test.crispy.example.service.EchoImpl; 30 31 /** 32 * @author Linke 33 * 34 */ 35 public class UtilTest extends TestCase { 36 37 public void testClassNameWithOutPackage() throws Exception { 38 String lvResult = Util.getClassNameWithOutPackage((Class) null); 39 assertNull(lvResult); 40 41 lvResult = Util.getClassNameWithOutPackage((String) null); 42 assertNull(lvResult); 43 44 lvResult = Util.getClassNameWithOutPackage(Echo.class); 45 assertNotNull(lvResult); 46 assertEquals(lvResult, "Echo"); 47 48 lvResult = Util.getClassNameWithOutPackage(Echo.class.getName()); 49 assertNotNull(lvResult); 50 assertEquals(lvResult, "Echo"); 51 52 lvResult = Util.getClassNameWithOutPackage("Dummy"); 53 assertNotNull(lvResult); 54 assertEquals(lvResult, "Dummy"); 55 56 lvResult = Util.getClassNameWithOutPackage(""); 57 assertNotNull(lvResult); 58 assertEquals(lvResult, ""); 59 } 60 61 public void testCutString() throws Exception { 62 String lvCutString = Util.cutString(null, null); 63 assertNull(lvCutString); 64 65 lvCutString = Util.cutString(null, "/"); 66 assertNull(lvCutString); 67 68 lvCutString = Util.cutString("rmi://localhost:1099", null); 69 assertEquals(lvCutString, "rmi://localhost:1099"); 70 71 lvCutString = Util.cutString("rmi://localhost:1099", "-"); 72 assertEquals(lvCutString, "rmi://localhost:1099"); 73 74 lvCutString = Util.cutString("rmi://localhost:1099/", "/"); 75 assertEquals(lvCutString, "rmi://localhost:1099"); 76 } 77 78 public void testReverse() throws Exception { 79 String lvReverse = Util.reverse(Kunde.class); 80 assertEquals(lvReverse, "model.example.crispy.test"); 81 82 lvReverse = Util.reverse(null); 83 assertNull(lvReverse); 84 } 85 86 public void testCreateQNameByClass() throws Exception { 87 QName lvQName = Util.createQNameByClass(Kunde.class); 88 assertEquals(lvQName.getLocalPart(), Kunde.class.getName()); 89 assertEquals(lvQName.getNamespaceURI(), "model.example.crispy.test"); 90 91 lvQName = Util.createQNameByClass(null); 92 assertNull(lvQName); 93 } 94 95 public void testAllPropertiesByPrefixAndSort() throws Exception { 96 Properties properties = new Properties(); 97 properties.put(Echo.class.getName(), EchoImpl.class.getName()); 98 properties.put(Calculator.class.getName(), CalculatorImpl.class.getName()); 99 properties.put(Property.INTERCEPTOR_CLASS, WaitInterceptor.class.getName()); 100 properties.put(Property.INTERCEPTOR_CLASS_2, StopWatchInterceptor.class.getName()); 101 Map lvSortedMap = Util.getAllPropertiesByPrefixAndSort(properties, Property.INTERCEPTOR_CLASS); 102 assertTrue(lvSortedMap.size() == 2); 103 Iterator it = lvSortedMap.values().iterator(); 104 Object o = it.next(); 105 assertEquals(o.toString(), WaitInterceptor.class.getName()); 106 o = it.next(); 107 assertEquals(o.toString(), StopWatchInterceptor.class.getName()); 108 } 109 110 public void testAllPropertiesByPrefixAndSortWithNotValidValue() throws Exception { 111 Properties properties = new Properties(); 112 properties.put(Echo.class, EchoImpl.class.getName()); 113 try { 114 Util.getAllPropertiesByPrefixAndSort(properties, Property.INTERCEPTOR_CLASS); 115 fail("The properties must be a String (not Class)"); 116 } catch (Exception e) { 117 assertTrue(true); 118 } 119 } 120 121 public void testIsClassInArray() throws Exception { 122 boolean isInArray = Util.isClassInArray(null, null); 123 assertEquals(isInArray, false); 124 125 isInArray = Util.isClassInArray(null, UtilTest.class); 126 assertEquals(isInArray, false); 127 128 isInArray = Util.isClassInArray(new Class[0], null); 129 assertEquals(isInArray, false); 130 131 isInArray = Util.isClassInArray(new Class[] {ConverterTest.class, UtilTest.class}, this.getClass()); 132 assertEquals(isInArray, true); 133 134 isInArray = Util.isClassInArray(new Class[] {ConverterTest.class, UtilTest.class}, InvokerTest.class); 135 assertEquals(isInArray, false); 136 137 } 138 139 public void testPrintArray() throws Exception { 140 String lvResult = Util.printArray(null); 141 assertEquals("Array is NULL", lvResult); 142 143 lvResult = Util.printArray(new Object[] {new Integer(4711)}); 144 assertEquals("4711 -> java.lang.Integer\n", lvResult); 145 } 146 147 public void testArray2String() throws Exception { 148 String lvResult = Util.array2String(null); 149 assertNull(lvResult); 150 151 lvResult = Util.array2String(new Object [] { new Integer (4711), new Integer (911) }); 152 assertEquals("4711 ; 911 ; ", lvResult); 153 } 154 155 public void testMap2String() throws Exception { 156 String lvResult = Util.map2String(null, null); 157 assertNull(lvResult); 158 159 Map lvMap = new HashMap(); 160 lvMap.put(new Integer(4711), new Integer(911)); 161 lvResult = Util.map2String(lvMap, null); 162 assertEquals("[4711 = 911] ", lvResult); 163 164 lvMap.put(new Integer(2), new Integer(3)); 165 lvResult = Util.map2String(lvMap, null); 166 assertEquals("[2 = 3] [4711 = 911] ", lvResult); 167 168 lvMap.put("class", this.getClass()); 169 lvResult = Util.map2String(lvMap, null); 170 assertEquals("[2 = 3] [4711 = 911] -> (class = class test.crispy.util.UtilTest)", lvResult); 171 172 lvMap.put("vector", new Vector()); 173 lvResult = Util.map2String(lvMap, null); 174 assertEquals("[2 = 3] [4711 = 911] -> (class = class test.crispy.util.UtilTest)", lvResult); 175 176 } 177 178 public void testPrintMapInfo() throws Exception { 179 Util.printMapInfo(null); 180 181 Map lvMap = new HashMap(); 182 lvMap.put(new Integer(4711), new Integer(911)); 183 Util.printMapInfo(lvMap); 184 } 185 186 public void testIsPortFree() throws Exception { 187 boolean isPortFree = Util.isPortFree(7007); 188 assertTrue(isPortFree); 189 190 MiniServer lvMiniServer = new MiniRestServer(7007); 191 lvMiniServer.start(); 192 isPortFree = Util.isPortFree(7007); 193 assertFalse(isPortFree); 194 lvMiniServer.stop(); 195 } 196 197 public void testCreateTree() throws Exception { 198 Util.createTree(null, new DefaultMutableTreeNode()); 199 Util.createTree(new Integer(7), new DefaultMutableTreeNode()); 200 Util.createTree(new Vector(), new DefaultMutableTreeNode()); 201 202 Kunde lvKunde = new Kunde("Test"); 203 Adresse lvAdresse1 = new Adresse(); 204 lvAdresse1.setOrt("London"); 205 Adresse lvAdresse2 = new Adresse(); 206 lvAdresse2.setOrt("New York"); 207 lvKunde.getAdressen().add(lvAdresse1); 208 lvKunde.getAdressen().add(lvAdresse2); 209 Util.createTree(lvKunde, new DefaultMutableTreeNode()); 210 211 Util.createTreeByCollection(lvKunde.getAdressen(), new DefaultMutableTreeNode(), null); 212 Util.createTreeByCollection(null, new DefaultMutableTreeNode(), null); 213 214 Util.createTreeByMap(null, new DefaultMutableTreeNode(), null); 215 Map lvMap = new HashMap(); 216 lvMap.put(new Integer(4711), new Integer(911)); 217 Util.createTree(lvMap, new DefaultMutableTreeNode()); 218 lvMap.put(new Vector(), new Vector()); 219 Util.createTree(lvMap, new DefaultMutableTreeNode()); 220 lvMap.put(new HashMap(), new HashMap()); 221 Util.createTree(lvMap, new DefaultMutableTreeNode()); 222 223 Vector v = new Vector(); 224 v.add(lvKunde.getAdressen()); 225 v.add(lvMap); 226 Util.createTreeByCollection(v, new DefaultMutableTreeNode(), null); 227 228 } 229 230 public void testcreateObject() throws Exception { 231 Object o = Util.createObject(null); 232 assertNull(o); 233 try { 234 o = Util.createObject("not.valid.Calss"); 235 fail("Is not a valid Class String"); 236 } catch (Exception e) { 237 assertTrue(true); 238 } 239 o = Util.createObject(EchoImpl.class.getName()); 240 assertNotNull(o); 241 assertTrue(o instanceof EchoImpl); 242 } 243 244 public void testSeparateUrlAndPortWithNullValue() throws Exception { 245 String lvUrlAndPort[] = Util.separateHostAndPort(null); 246 assertNull(lvUrlAndPort); 247 } 248 249 public void testSeparateUrlAndPort() throws Exception { 250 String lvUrlAndPort[] = Util.separateHostAndPort("myhost.com:8182"); 251 assertNotNull(lvUrlAndPort); 252 assertEquals(lvUrlAndPort.length, 2); 253 assertEquals(lvUrlAndPort[0], "myhost.com"); 254 assertEquals(lvUrlAndPort[1], "8182"); 255 } 256 257 public void testSeparateUrlAndPortInvalideUrl() throws Exception { 258 try { 259 String lvUrlAndPort[] = Util.separateHostAndPort("myhost.com"); 260 fail("Illegal Url and Port must throw Exception"); 261 } catch (IllegalArgumentException e) { 262 assertTrue(true); 263 } 264 265 try { 266 String lvUrlAndPort[] = Util.separateHostAndPort("myhost.com:abc"); 267 fail("Illegal Port must throw Exception"); 268 } catch (IllegalArgumentException e) { 269 assertTrue(true); 270 } 271 } 272 273 public void testFindDeepestThrowable() throws Exception { 274 String lvMessage = "JUnit-Test-Message"; 275 InvocationException lvException = new InvocationException(lvMessage); 276 Throwable lvFindThrowable = Util.findDeepestThrowable(lvException); 277 assertNotNull(lvFindThrowable); 278 assertEquals(lvFindThrowable.getMessage(), lvMessage); 279 } 280 281 public void testFindDeepestThrowable2() throws Exception { 282 String lvMessage = "JUnit-Test-Message"; 283 JUnitTestException lvUnitTestException = new JUnitTestException(lvMessage); 284 InvocationException lvException = new InvocationException("No-Message", lvUnitTestException); 285 286 Throwable lvFindThrowable = Util.findDeepestThrowable(lvException); 287 assertNotNull(lvFindThrowable); 288 assertEquals(lvFindThrowable.getMessage(), lvMessage); 289 } 290 291 public void testFindDeepestThrowable3() throws Exception { 292 String lvMessage = "JUnit-Test-Message"; 293 JUnitTestException lvUnitTestException = new JUnitTestException(lvMessage); 294 InvocationException lvException = new InvocationException("No-Message-1", lvUnitTestException); 295 InvocationException lvException2 = new InvocationException("No-Message-2", lvException); 296 297 Throwable lvFindThrowable = Util.findDeepestThrowable(lvException2); 298 assertNotNull(lvFindThrowable); 299 assertEquals(lvFindThrowable.getMessage(), lvMessage); 300 301 lvFindThrowable = Util.findDeepestThrowable(lvException); 302 assertNotNull(lvFindThrowable); 303 assertEquals(lvFindThrowable.getMessage(), lvMessage); 304 } 305 306 307 }