1 /*
2 * Copyright (C) 2003-2007 Kepler Project.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 
24 package org.keplerproject.luajava.test;
25 
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.Map;
30 import java.util.Set;
31 
32 import org.keplerproject.luajava.LuaException;
33 import org.keplerproject.luajava.LuaObject;
34 import org.keplerproject.luajava.LuaState;
35 import org.keplerproject.luajava.LuaStateFactory;
36 
37 import junit.framework.TestCase;
38 
39 
40 /**
41  * Implementation of a java.util.Map in Lua.<br>
42  * The Map is implemented inside Java, using Lua to store the data and
43  * inside Lua, used with a proxy.
44  * This test uses junit.
45  *
46  * @author thiago
47  */
48 public class TestLuaMap extends TestCase
49 {
testMap()50   public void testMap() throws ClassNotFoundException, LuaException
51   {
52     Map table  = new HashMap();
53     table.put("testTable2-1", "testTable2Value");
54     table.put("testTable2-2", new Object());
55 
56     // test using a java accessed table.
57     Map luaMap = new LuaMap();
58 
59     luaMap.put("test", "testValue");
60     luaMap.putAll(table);
61 
62     assertTrue(luaMap.containsKey("test"));
63     assertTrue(luaMap.containsKey("testTable2-1"));
64 
65     System.out.println(luaMap.get("testTable2-2"));
66 
67     assertTrue(luaMap.containsValue("testValue"));
68 
69     assertEquals(3, luaMap.size());
70 
71     luaMap.remove("test");
72     assertNull(luaMap.get("test"));
73 
74     luaMap.clear();
75 
76     assertEquals(luaMap.size(), 0);
77 
78 
79     // test using a lua table
80     LuaState L = LuaStateFactory.newLuaState();
81     L.openLibs();
82     int err = L.LdoFile("testMap.lua");
83     if(err != 0)
84     {
85       switch (err)
86       {
87         case 1 :
88           System.out.println("Runtime error. " + L.toString(-1));
89           break;
90 
91         case 2 :
92           System.out.println("File not found. " + L.toString(-1));
93           break;
94 
95         case 3 :
96           System.out.println("Syntax error. " + L.toString(-1));
97           break;
98 
99         case 4 :
100           System.out.println("Memory error. " + L.toString(-1));
101           break;
102 
103         default :
104           System.out.println("Error. " + L.toString(-1));
105           break;
106       }
107     }
108 
109     L.getGlobal("map");
110     luaMap = (Map) L.getLuaObject(-1).createProxy("java.util.Map");
111     L.pop(1);
112 
113     luaMap.put("test", "testValue");
114     luaMap.putAll(table);
115 
116     assertTrue(luaMap.containsKey("test"));
117     assertTrue(luaMap.containsKey("testTable2-1"));
118 
119     System.out.println(luaMap.get("testTable2-2"));
120 
121     assertTrue(luaMap.containsValue("testValue"));
122 
123     assertEquals(3, luaMap.size());
124 
125     luaMap.remove("test");
126     assertNull(luaMap.get("test"));
127 
128     luaMap.clear();
129 
130     assertEquals(luaMap.size(), 0);
131   }
132 }
133 
134 /**
135  * Class that implements a Map that stores the information in Lua
136  *
137  * @author thiago
138  */
139 class LuaMap implements Map
140 {
141   private LuaState L;
142   private LuaObject table;
143 
144   /**
145    * Initializes the Luastate used and the table
146    */
LuaMap()147   public LuaMap()
148   {
149     L = LuaStateFactory.newLuaState();
150     L.openLibs();
151     L.newTable();
152     table = L.getLuaObject(-1);
153     L.pop(1);
154   }
155 
finalize()156   protected void finalize() throws Throwable
157   {
158     super.finalize();
159     L.close();
160   }
161 
162   /**
163    * @see java.util.Map#size()
164    */
size()165   public int size()
166   {
167     table.push();
168     L.pushNil();
169 
170     int n;
171     for (n = 0; L.next(-2) != 0; n++)L.pop(1);
172 
173     L.pop(2);
174 
175     return n;
176   }
177 
178   /**
179    * @see java.util.Map#clear()
180    */
clear()181   public void clear()
182   {
183     L.newTable();
184     table = L.getLuaObject(-1);
185     L.pop(1);
186   }
187 
188   /**
189    * @see java.util.Map#isEmpty()
190    */
isEmpty()191   public boolean isEmpty()
192   {
193     return size() == 0;
194   }
195 
196   /**
197    * @see java.util.Map#containsKey(java.lang.Object)
198    */
containsKey(Object key)199   public boolean containsKey(Object key)
200   {
201     try
202     {
203       L.pushObjectValue(key);
204 	    LuaObject obj = L.getLuaObject(-1);
205 	    L.pop(1);
206 
207 	    LuaObject temp = L.getLuaObject(table, obj);
208 
209 	    return !temp.isNil();
210     }
211     catch (LuaException e)
212     {
213       return false;
214     }
215   }
216 
217   /**
218    * @see java.util.Map#containsValue(java.lang.Object)
219    */
containsValue(Object value)220   public boolean containsValue(Object value)
221   {
222     try
223     {
224       L.pushObjectValue(value);
225 	    table.push();
226 	    L.pushNil();
227 
228 	    while(L.next(-2) != 0)/* `key' is at index -2 and `value' at index -1 */
229 	    {
230 	      if (L.equal(-4, -1) != 0)
231 	      {
232 	        L.pop(4);
233 	        return true;
234 	      }
235 	      L.pop(1);
236 	    }
237 
238 	    L.pop(3);
239 	    return false;
240     }
241     catch (LuaException e)
242     {
243       return false;
244     }
245   }
246 
247 
248   /**
249    * not implemented
250    * @see java.util.Map#values()
251    */
values()252   public Collection values()
253   {
254     throw new RuntimeException("not implemented");
255   }
256 
257   /**
258    * @see java.util.Map#putAll(java.util.Map)
259    */
putAll(Map t)260   public void putAll(Map t)
261   {
262     Iterator i = t.keySet().iterator();
263     while(i.hasNext())
264     {
265       Object key = i.next();
266       put(key, t.get(key));
267     }
268   }
269 
270   /**
271    * @see java.util.Map#entrySet()
272    */
entrySet()273   public Set entrySet()
274   {
275     throw new RuntimeException("not implemented");
276   }
277 
278   /**
279    * @see java.util.Map#keySet()
280    */
keySet()281   public Set keySet()
282   {
283     throw new RuntimeException("not implemented");
284   }
285 
286   /**
287    * @see java.util.Map#get(java.lang.Object)
288    */
get(Object key)289   public Object get(Object key)
290   {
291     try
292     {
293 	    table.push();
294 	    L.pushObjectValue(key);
295 
296 	    L.getTable(-2);
297 
298 	    Object ret = L.toJavaObject(-1);
299 
300 	    L.pop(2);
301 
302 	    return ret;
303     }
304     catch(LuaException e)
305     {
306       return null;
307     }
308   }
309 
310   /**
311    * @see java.util.Map#remove(java.lang.Object)
312    */
remove(Object key)313   public Object remove(Object key)
314   {
315     try
316     {
317 	    Object ret = get(key);
318 
319 	    table.push();
320 	    L.pushObjectValue(key);
321 	    L.pushNil();
322 
323 	    L.setTable(-3);
324 
325 	    L.pop(2);
326 
327 	    return ret;
328     }
329     catch(LuaException e)
330     {
331       return null;
332     }
333   }
334 
335   /**
336    * @see java.util.Map#put(java.lang.Object, java.lang.Object)
337    */
put(Object key, Object value)338   public Object put(Object key, Object value)
339   {
340     try
341     {
342 	    Object ret = get(key);
343 
344 	    table.push();
345 	    L.pushObjectValue(key);
346 	    L.pushObjectValue(value);
347 
348 	    L.setTable(-3);
349 
350 	    L.pop(1);
351 
352 	    return ret;
353     }
354     catch(LuaException e)
355     {
356       return null;
357     }
358   }
359 
360 }