1 31 package org.jruby.javasupport.test; 32 33 import java.io.IOException ; 34 import java.io.InputStreamReader ; 35 import java.io.Reader ; 36 import java.util.ArrayList ; 37 import java.util.HashMap ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 import java.util.Map ; 41 import java.util.Set ; 42 43 import org.apache.bsf.BSFException; 44 import org.apache.bsf.BSFManager; 45 46 public class TestBSF extends RubyTestCase { 47 private static final String RUBY_SCRIPT = "SimpleInterfaceImpl.rb"; 48 49 BSFManager manager = null; 50 51 public TestBSF(String name) { 52 super(name); 53 } 54 55 public void setUp() throws IOException { 56 try { 57 BSFManager.registerScriptingEngine("ruby", "org.jruby.javasupport.bsf.JRubyEngine", new String [] { "rb" }); 58 59 manager = new BSFManager(); 60 manager.exec("ruby", "(java)", 1, 1, loadScript(RUBY_SCRIPT)); 61 } catch (BSFException e) { 62 e.getTargetException().printStackTrace(); 63 fail("Unable to initialize BSF: " + e); 64 } 65 } 66 67 public void tearDown() { 68 manager = null; 69 } 70 71 72 73 public void testList() { 74 try { 75 SimpleInterface si = (SimpleInterface) manager.eval("ruby", "(java)", 1, 1, "SimpleInterfaceImpl.new"); 76 77 for (Iterator e = si.getList().iterator(); e.hasNext(); ) { 78 assertTrue(e.next().getClass() == Long .class); 79 } 80 } catch (BSFException e) { 81 fail("Problem evaluating List Test: " + e); 82 } 83 } 84 85 public void testModifyList() { 86 try { 87 SimpleInterface si = (SimpleInterface) manager.eval("ruby", "(java)", 1, 1, "MODIFY_LIST = SimpleInterfaceImpl.new"); 88 List list = si.getList(); 89 90 list.set(1, "FOO"); 91 Boolean answer = (Boolean ) manager.eval("ruby", "(java)", 1, 1, "[1, 'FOO', 3] == MODIFY_LIST.getList"); 92 assertTrue(answer.booleanValue()); 93 94 list.add(new Long (4)); 95 answer = (Boolean ) manager.eval("ruby", "(java)", 1, 1, "[1, 'FOO', 3, 4] == MODIFY_LIST.getList"); 96 assertTrue(answer.booleanValue()); 97 98 list.add(1, new Integer (2)); 99 answer = (Boolean ) manager.eval("ruby", "(java)", 1, 1, "[1, 2, 'FOO', 3, 4] == MODIFY_LIST.getList"); 100 assertTrue(answer.booleanValue()); 101 102 list.remove("FOO"); 103 answer = (Boolean ) manager.eval("ruby", "(java)", 1, 1, "[1, 2, 3, 4] == MODIFY_LIST.getList"); 104 assertTrue(answer.booleanValue()); 105 106 assertTrue(list.contains(new Long (3))); 107 assertTrue(list.indexOf(new Long (3)) == 2); 108 assertTrue(list.lastIndexOf(new Long (3)) == 2); 109 110 Object [] array = list.toArray(); 111 112 assertTrue(array.length == 4); 113 assertTrue(((Long ) array[2]).longValue() == 3); 114 115 List subList = list.subList(0, 2); 116 assertTrue(subList.size() == 3); 117 118 } catch (BSFException e) { 122 fail("Problem evaluating List Test: " + e); 123 } 124 } 125 126 public void testEmptyList() { 127 try { 128 SimpleInterface si = (SimpleInterface) manager.eval("ruby", "(java)", 1, 1, "EMPTY_LIST = SimpleInterfaceImpl.new"); 129 List list = si.getEmptyList(); 130 131 assertTrue(list.size() == 0); 132 } catch (BSFException e) { 133 fail("Problem evaluating List Test: " + e); 134 } 135 } 136 137 public void testNilList() { 138 try { 139 SimpleInterface si = (SimpleInterface) manager.eval("ruby", "(java)", 1, 1, "EMPTY_LIST = SimpleInterfaceImpl.new"); 140 List list = si.getNilList(); 141 142 assertTrue(list == null); 143 144 si.setNilList(null); 145 146 assertTrue(si.isNilListNil()); 147 } catch (BSFException e) { 148 fail("Problem evaluating List Test: " + e); 149 } 150 } 151 152 public void testNestedList() { 153 try { 154 SimpleInterface si = (SimpleInterface) manager.eval("ruby", "(java)", 1, 1, "NESTED_LIST = SimpleInterfaceImpl.new"); 155 List list = si.getNestedList(); 156 157 assertTrue(list.size() == 3); 158 List list2 = (List ) list.get(0); 159 160 assertTrue(list2.size() == 2); 161 assertTrue(list2.indexOf(new Long (1)) == 0); 162 163 si.modifyNestedList(); 164 assertTrue("FOO".equals(list.get(0))); 165 166 } catch (BSFException e) { 167 fail("Problem evaluating List Test: " + e); 168 } 169 } 170 171 189 public void testMap() { 190 try { 191 SimpleInterface si = (SimpleInterface) manager.eval("ruby", "(java)", 1, 1, "SimpleInterfaceImpl.new"); 192 Map map = si.getMap(); 193 194 List values = new ArrayList (); 195 List keys = new ArrayList (); 196 197 198 Iterator valuesIterator = map.values().iterator(); 199 assertTrue(valuesIterator.hasNext()); 200 201 for (Iterator keySetIterator = map.keySet().iterator(); keySetIterator.hasNext(); ) { 203 Object key = keySetIterator.next(); 204 205 Object value = map.get(key); 207 208 assertTrue(key.getClass() == String .class); 209 assertTrue(value.getClass() == Long .class); 210 211 Object valueViaValuesIterator = valuesIterator.next(); 213 214 assertTrue(value.equals(valueViaValuesIterator)); 216 217 220 assertTrue(map.keySet().contains(key)); 221 assertTrue(map.values().contains(value)); 222 } 223 assertFalse(valuesIterator.hasNext()); 224 225 assertTrue(map.keySet().containsAll(keys)); 226 assertTrue(map.values().containsAll(values)); 227 228 assertTrue(map.keySet().contains("A")); 229 assertTrue(map.values().contains(new Long (1))); 230 assertFalse(map.keySet().remove("-")); 231 assertTrue(map.keySet().remove("A")); 232 assertFalse(map.keySet().contains("A")); 233 assertFalse(map.values().contains(new Long (1))); 234 235 assertTrue(map.keySet().contains("B")); 236 assertTrue(map.values().contains(new Long (2))); 237 assertFalse(map.values().remove("-")); 238 assertTrue(map.values().remove(new Long (2))); 239 assertFalse(map.values().contains(new Long (2))); 240 assertFalse(map.keySet().contains("B")); 241 242 243 } catch (BSFException e) { 244 fail("Problem evaluating Map Test: " + e); 245 } 246 } 247 248 256 public void testMapEntrySetIterator() { 257 258 class TestMapValue { private int i; private String s; TestMapValue() {i = 1; s="2";} public String toString(){ return s + i; } } 259 260 try { 261 SimpleInterface si = (SimpleInterface) manager.eval("ruby", "(java)", 1, 1, "SimpleInterfaceImpl.new"); 262 Map map = si.getMap(); 263 int iteration = 1; 264 for (Iterator e = map.entrySet().iterator(); e.hasNext();) { 265 Object o = e.next(); 266 assertNotNull(o); 267 Map.Entry entry = (Map.Entry ) o; 268 assertTrue(entry.getKey().getClass() == String .class); 269 assertTrue(entry.getValue().getClass() == Long .class); 270 if (iteration++ == 1) { 271 assertEquals("A", entry.getKey()); 272 assertEquals(new Long (1L), entry.getValue()); 273 entry.setValue(new Long (3)); 275 } else { 276 assertEquals("B", entry.getKey()); 277 assertEquals(new Long (2L), entry.getValue()); 278 entry.setValue(new TestMapValue()); 280 } 281 } 282 284 iteration = 1; 285 for (Iterator e = map.entrySet().iterator(); e.hasNext();) { 286 Object o = e.next(); 287 assertNotNull(o); 288 Map.Entry entry = (Map.Entry ) o; 289 assertTrue(entry.getKey().getClass() == String .class); 290 if (iteration++ == 1) { 291 assertTrue(entry.getValue().getClass() == Long .class); 292 assertEquals("A", entry.getKey()); 293 assertEquals(new Long (3L), entry.getValue()); 294 } else { 295 assertTrue(entry.getValue().getClass() == TestMapValue.class); 296 assertEquals("B", entry.getKey()); 297 assertEquals("21", entry.getValue().toString()); 298 } 299 } 300 } catch (BSFException e) { 301 fail("Problem evaluating testMapEntrySetIterator Test: " + e); 302 } 303 } 304 305 311 public void testMapEntrySetContainsAndRemove() { 312 try { 313 SimpleInterface si = (SimpleInterface) manager.eval("ruby", "(java)", 1, 1, "SimpleInterfaceImpl.new"); 314 Map map = si.getMap(); 315 Set entrySet = map.entrySet(); 316 Iterator e = entrySet.iterator(); 317 Object next1 = e.next(); 318 Object next2 = e.next(); 319 assertFalse(e.hasNext()); 320 assertTrue(entrySet.contains(next1)); 321 assertTrue(entrySet.contains(next2)); 322 entrySet.remove(next1); 323 assertFalse(entrySet.contains(next1)); 324 entrySet.remove(next2); 325 assertFalse(entrySet.contains(next2)); 326 } catch (BSFException e) { 327 fail("Problem evaluating testMapEntrySetContainsAndRemove Test: " + e); 328 } 329 } 330 331 public void testModifyMap() { 332 try { 333 SimpleInterface si = (SimpleInterface) manager.eval("ruby", "(java)", 1, 1, "MODIFY_MAP = SimpleInterfaceImpl.new"); 334 Map map = si.getMap(); 335 336 for (Iterator e = map.keySet().iterator(); e.hasNext(); ) { 337 Object key = e.next(); 338 Object value = map.get(key); 339 assertTrue(key.getClass() == String .class); 340 assertTrue(value.getClass() == Long .class); 341 342 map.put(key, new Long (((Long ) value).longValue() + 1)); 343 } 344 345 Boolean answer = (Boolean ) manager.eval("ruby", "(java)", 1, 1, "{'A'=> 2, 'B' => 3} == MODIFY_MAP.getMap"); 346 assertTrue(answer.booleanValue()); 347 348 assertTrue(map.size() == 2); 349 350 Long value = (Long ) map.get("B"); 351 assertTrue(value.longValue() == 3); 352 353 map.remove("B"); 354 assertTrue(map.size() == 1); 355 assertTrue(map.containsKey("A")); 356 assertTrue(map.containsValue(new Long (2))); 357 assertTrue(!map.isEmpty()); 358 359 map.put("C", new Long (4)); 360 assertTrue(map.containsKey("C")); 361 362 HashMap newMap = new HashMap (); 363 newMap.put("D", "E"); 364 map.putAll(newMap); 365 366 assertTrue(map.size() == 3); 367 368 map.clear(); 369 assertTrue(map.size() == 0); 370 } catch (BSFException e) { 371 fail("Problem evaluating List Test: " + e); 372 } 373 } 374 375 public void testEmptyMap() { 376 try { 377 SimpleInterface si = (SimpleInterface) manager.eval("ruby", "(java)", 1, 1, "EMPTY_MAP = SimpleInterfaceImpl.new"); 378 Map map = si.getEmptyMap(); 379 380 assertTrue(map.size() == 0); 381 assertTrue(map.isEmpty()); 382 } catch (BSFException e) { 383 fail("Problem evaluating List Test: " + e); 384 } 385 } 386 387 public void testNilMap() { 388 try { 389 SimpleInterface si = (SimpleInterface) manager.eval("ruby", "(java)", 1, 1, "SimpleInterfaceImpl.new"); 390 Map map = si.getNilMap(); 391 392 assertTrue(map == null); 393 394 si.setNilMap(null); 395 396 assertTrue(si.isNilMapNil()); 397 } catch (BSFException e) { 398 fail("Problem evaluating List Test: " + e); 399 } 400 } 401 402 403 private String loadScript(String fileName) { 404 try { 405 Reader in = new InputStreamReader (getClass().getResourceAsStream(fileName)); 406 StringBuffer result = new StringBuffer (); 407 int length; 408 char[] buf = new char[8096]; 409 while ((length = in.read(buf, 0, buf.length)) >= 0) { 410 result.append(buf, 0, length); 411 } 412 in.close(); 413 414 return result.toString(); 415 } catch (Exception ex) {} 416 417 return null; 418 } 419 420 } 421 | Popular Tags |