KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > javasupport > test > TestBSF


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2002 Don Schwartz <schwardo@users.sourceforge.net>
15  * Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
16  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
17  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
18  *
19  * Alternatively, the contents of this file may be used under the terms of
20  * either of the GNU General Public License Version 2 or later (the "GPL"),
21  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
22  * in which case the provisions of the GPL or the LGPL are applicable instead
23  * of those above. If you wish to allow use of your version of this file only
24  * under the terms of either the GPL or the LGPL, and not to allow others to
25  * use your version of this file under the terms of the CPL, indicate your
26  * decision by deleting the provisions above and replace them with the notice
27  * and other provisions required by the GPL or the LGPL. If you do not delete
28  * the provisions above, a recipient may use your version of this file under
29  * the terms of any one of the CPL, the GPL or the LGPL.
30  ***** END LICENSE BLOCK *****/

31 package org.jruby.javasupport.test;
32
33 import java.io.IOException JavaDoc;
34 import java.io.InputStreamReader JavaDoc;
35 import java.io.Reader JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Map JavaDoc;
41 import java.util.Set JavaDoc;
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 JavaDoc RUBY_SCRIPT = "SimpleInterfaceImpl.rb";
48
49     BSFManager manager = null;
50     
51     public TestBSF(String JavaDoc name) {
52         super(name);
53     }
54     
55     public void setUp() throws IOException JavaDoc {
56         try {
57             BSFManager.registerScriptingEngine("ruby", "org.jruby.javasupport.bsf.JRubyEngine", new String JavaDoc[] { "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 JavaDoc e = si.getList().iterator(); e.hasNext(); ) {
78                 assertTrue(e.next().getClass() == Long JavaDoc.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 JavaDoc list = si.getList();
89
90             list.set(1, "FOO");
91             Boolean JavaDoc answer = (Boolean JavaDoc) manager.eval("ruby", "(java)", 1, 1, "[1, 'FOO', 3] == MODIFY_LIST.getList");
92             assertTrue(answer.booleanValue());
93             
94             list.add(new Long JavaDoc(4));
95             answer = (Boolean JavaDoc) manager.eval("ruby", "(java)", 1, 1, "[1, 'FOO', 3, 4] == MODIFY_LIST.getList");
96             assertTrue(answer.booleanValue());
97             
98             list.add(1, new Integer JavaDoc(2));
99             answer = (Boolean JavaDoc) 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 JavaDoc) manager.eval("ruby", "(java)", 1, 1, "[1, 2, 3, 4] == MODIFY_LIST.getList");
104             assertTrue(answer.booleanValue());
105             
106             assertTrue(list.contains(new Long JavaDoc(3)));
107             assertTrue(list.indexOf(new Long JavaDoc(3)) == 2);
108             assertTrue(list.lastIndexOf(new Long JavaDoc(3)) == 2);
109             
110             Object JavaDoc[] array = list.toArray();
111             
112             assertTrue(array.length == 4);
113             assertTrue(((Long JavaDoc) array[2]).longValue() == 3);
114             
115             List JavaDoc subList = list.subList(0, 2);
116             assertTrue(subList.size() == 3);
117             
118             //subList.clear();
119
// Sublist is supposed to share same backing store as list...TODO in RubyArray.
120
//assertTrue(list.size() == 1);
121
} 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 JavaDoc 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 JavaDoc 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 JavaDoc list = si.getNestedList();
156             
157             assertTrue(list.size() == 3);
158             List JavaDoc list2 = (List JavaDoc) list.get(0);
159             
160             assertTrue(list2.size() == 2);
161             assertTrue(list2.indexOf(new Long JavaDoc(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     /**
172      * Tests the use of RubyHash when used from java.
173      * Tests:
174      * RubyHash#keySet()
175      * RubyHash#get()
176      * RubyHash#keySet()#iterator()#hasNext()
177      * RubyHash#keySet()#iterator()#next()
178      * RubyHash#keySet()#remove()
179      * RubyHash#keySet()#contains()
180      * RubyHash#keySet()#containsAll()
181      * RubyHash#values()
182      * RubyHash#values()#iterator()
183      * RubyHash#values()#iterator()#hasNext()
184      * RubyHash#values()#iterator()#next()
185      * RubyHash#values()#contains()
186      * RubyHash#values()#containsAll()
187      * RubyHash#values()#remove()
188      */

189     public void testMap() {
190         try {
191             SimpleInterface si = (SimpleInterface) manager.eval("ruby", "(java)", 1, 1, "SimpleInterfaceImpl.new");
192             Map JavaDoc map = si.getMap();
193             
194             List JavaDoc values = new ArrayList JavaDoc();
195             List JavaDoc keys = new ArrayList JavaDoc();
196             
197
198             Iterator JavaDoc valuesIterator = map.values().iterator();
199             assertTrue(valuesIterator.hasNext());
200
201             // Iterate over the RubyHash keySet, simultaneously iterating over the values()
202
for (Iterator JavaDoc keySetIterator = map.keySet().iterator(); keySetIterator.hasNext(); ) {
203                 Object JavaDoc key = keySetIterator.next();
204                 
205                 // Get the value from the map via the key
206
Object JavaDoc value = map.get(key);
207                 
208                 assertTrue(key.getClass() == String JavaDoc.class);
209                 assertTrue(value.getClass() == Long JavaDoc.class);
210                 
211                 // Get the value from the map via the values iterator
212
Object JavaDoc valueViaValuesIterator = valuesIterator.next();
213                 
214                 // Check the 2 values obtained via different means
215
assertTrue(value.equals(valueViaValuesIterator));
216                 
217                 // Note that WE CAN'T say the following, because of the on-the-fly conversion of Fixnum to Long
218
// assertTrue(value == valueViaValuesIterator);
219

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 JavaDoc(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 JavaDoc(1)));
234             
235             assertTrue(map.keySet().contains("B"));
236             assertTrue(map.values().contains(new Long JavaDoc(2)));
237             assertFalse(map.values().remove("-"));
238             assertTrue(map.values().remove(new Long JavaDoc(2)));
239             assertFalse(map.values().contains(new Long JavaDoc(2)));
240             assertFalse(map.keySet().contains("B"));
241         
242         
243         } catch (BSFException e) {
244             fail("Problem evaluating Map Test: " + e);
245         }
246     }
247
248     /**
249      * Tests the use of RubyHash when used from java.
250      * Tests:
251      * RubyHash#entrySet()
252      * RubyHash#entrySet()#iterator()#hasNext()
253      * RubyHash#entrySet()#iterator()#next()
254      * RubyHash#entrySet()#iterator()#next()#setValue()
255      */

256     public void testMapEntrySetIterator() {
257         
258         class TestMapValue { private int i; private String JavaDoc s; TestMapValue() {i = 1; s="2";} public String JavaDoc toString(){ return s + i; } }
259         
260         try {
261             SimpleInterface si = (SimpleInterface) manager.eval("ruby", "(java)", 1, 1, "SimpleInterfaceImpl.new");
262             Map JavaDoc map = si.getMap();
263             int iteration = 1;
264             for (Iterator JavaDoc e = map.entrySet().iterator(); e.hasNext();) {
265                 Object JavaDoc o = e.next();
266                 assertNotNull(o);
267                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) o;
268                 assertTrue(entry.getKey().getClass() == String JavaDoc.class);
269                 assertTrue(entry.getValue().getClass() == Long JavaDoc.class);
270                 if (iteration++ == 1) {
271                     assertEquals("A", entry.getKey());
272                     assertEquals(new Long JavaDoc(1L), entry.getValue());
273                     // Set a value in the RubyHash
274
entry.setValue(new Long JavaDoc(3));
275                 } else {
276                     assertEquals("B", entry.getKey());
277                     assertEquals(new Long JavaDoc(2L), entry.getValue());
278                     // Set a value in the RubyHash
279
entry.setValue(new TestMapValue());
280                 }
281             }
282             // Check the entry.setValue values come back out ok
283

284             iteration = 1;
285             for (Iterator JavaDoc e = map.entrySet().iterator(); e.hasNext();) {
286                 Object JavaDoc o = e.next();
287                 assertNotNull(o);
288                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) o;
289                 assertTrue(entry.getKey().getClass() == String JavaDoc.class);
290                 if (iteration++ == 1) {
291                     assertTrue(entry.getValue().getClass() == Long JavaDoc.class);
292                     assertEquals("A", entry.getKey());
293                     assertEquals(new Long JavaDoc(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     /**
306      * Tests the use of RubyHash when used from java.
307      * Tests:
308      * RubyHash#entrySet()#contains()
309      * RubyHash#entrySet()#remove()
310      */

311     public void testMapEntrySetContainsAndRemove() {
312         try {
313             SimpleInterface si = (SimpleInterface) manager.eval("ruby", "(java)", 1, 1, "SimpleInterfaceImpl.new");
314             Map JavaDoc map = si.getMap();
315             Set JavaDoc entrySet = map.entrySet();
316             Iterator JavaDoc e = entrySet.iterator();
317             Object JavaDoc next1 = e.next();
318             Object JavaDoc 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 JavaDoc map = si.getMap();
335
336             for (Iterator JavaDoc e = map.keySet().iterator(); e.hasNext(); ) {
337                 Object JavaDoc key = e.next();
338                 Object JavaDoc value = map.get(key);
339                 assertTrue(key.getClass() == String JavaDoc.class);
340                 assertTrue(value.getClass() == Long JavaDoc.class);
341                 
342                 map.put(key, new Long JavaDoc(((Long JavaDoc) value).longValue() + 1));
343             }
344             
345             Boolean JavaDoc answer = (Boolean JavaDoc) 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 JavaDoc value = (Long JavaDoc) 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 JavaDoc(2)));
357             assertTrue(!map.isEmpty());
358             
359             map.put("C", new Long JavaDoc(4));
360             assertTrue(map.containsKey("C"));
361             
362             HashMap JavaDoc newMap = new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc loadScript(String JavaDoc fileName) {
404         try {
405             Reader JavaDoc in = new InputStreamReader JavaDoc(getClass().getResourceAsStream(fileName));
406             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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 JavaDoc ex) {}
416         
417         return null;
418     }
419
420 }
421
Popular Tags