001: /*
002: * Copyright (C) 2003-2007 Kepler Project.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining
005: * a copy of this software and associated documentation files (the
006: * "Software"), to deal in the Software without restriction, including
007: * without limitation the rights to use, copy, modify, merge, publish,
008: * distribute, sublicense, and/or sell copies of the Software, and to
009: * permit persons to whom the Software is furnished to do so, subject to
010: * the following conditions:
011: *
012: * The above copyright notice and this permission notice shall be
013: * included in all copies or substantial portions of the Software.
014: *
015: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
016: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
017: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
018: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
019: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
020: * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
021: * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
022: */
023:
024: package org.keplerproject.luajava.test;
025:
026: import java.util.Collection;
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.Map;
030: import java.util.Set;
031:
032: import org.keplerproject.luajava.LuaException;
033: import org.keplerproject.luajava.LuaObject;
034: import org.keplerproject.luajava.LuaState;
035: import org.keplerproject.luajava.LuaStateFactory;
036:
037: import junit.framework.TestCase;
038:
039: /**
040: * Implementation of a java.util.Map in Lua.<br>
041: * The Map is implemented inside Java, using Lua to store the data and
042: * inside Lua, used with a proxy.
043: * This test uses junit.
044: *
045: * @author thiago
046: */
047: public class TestLuaMap extends TestCase {
048: public void testMap() throws ClassNotFoundException, LuaException {
049: Map table = new HashMap();
050: table.put("testTable2-1", "testTable2Value");
051: table.put("testTable2-2", new Object());
052:
053: // test using a java accessed table.
054: Map luaMap = new LuaMap();
055:
056: luaMap.put("test", "testValue");
057: luaMap.putAll(table);
058:
059: assertTrue(luaMap.containsKey("test"));
060: assertTrue(luaMap.containsKey("testTable2-1"));
061:
062: System.out.println(luaMap.get("testTable2-2"));
063:
064: assertTrue(luaMap.containsValue("testValue"));
065:
066: assertEquals(3, luaMap.size());
067:
068: luaMap.remove("test");
069: assertNull(luaMap.get("test"));
070:
071: luaMap.clear();
072:
073: assertEquals(luaMap.size(), 0);
074:
075: // test using a lua table
076: LuaState L = LuaStateFactory.newLuaState();
077: L.openLibs();
078: int err = L.LdoFile("testMap.lua");
079: if (err != 0) {
080: switch (err) {
081: case 1:
082: System.out.println("Runtime error. " + L.toString(-1));
083: break;
084:
085: case 2:
086: System.out.println("File not found. " + L.toString(-1));
087: break;
088:
089: case 3:
090: System.out.println("Syntax error. " + L.toString(-1));
091: break;
092:
093: case 4:
094: System.out.println("Memory error. " + L.toString(-1));
095: break;
096:
097: default:
098: System.out.println("Error. " + L.toString(-1));
099: break;
100: }
101: }
102:
103: L.getGlobal("map");
104: luaMap = (Map) L.getLuaObject(-1).createProxy("java.util.Map");
105: L.pop(1);
106:
107: luaMap.put("test", "testValue");
108: luaMap.putAll(table);
109:
110: assertTrue(luaMap.containsKey("test"));
111: assertTrue(luaMap.containsKey("testTable2-1"));
112:
113: System.out.println(luaMap.get("testTable2-2"));
114:
115: assertTrue(luaMap.containsValue("testValue"));
116:
117: assertEquals(3, luaMap.size());
118:
119: luaMap.remove("test");
120: assertNull(luaMap.get("test"));
121:
122: luaMap.clear();
123:
124: assertEquals(luaMap.size(), 0);
125: }
126: }
127:
128: /**
129: * Class that implements a Map that stores the information in Lua
130: *
131: * @author thiago
132: */
133: class LuaMap implements Map {
134: private LuaState L;
135: private LuaObject table;
136:
137: /**
138: * Initializes the Luastate used and the table
139: */
140: public LuaMap() {
141: L = LuaStateFactory.newLuaState();
142: L.openLibs();
143: L.newTable();
144: table = L.getLuaObject(-1);
145: L.pop(1);
146: }
147:
148: protected void finalize() throws Throwable {
149: super .finalize();
150: L.close();
151: }
152:
153: /**
154: * @see java.util.Map#size()
155: */
156: public int size() {
157: table.push();
158: L.pushNil();
159:
160: int n;
161: for (n = 0; L.next(-2) != 0; n++)
162: L.pop(1);
163:
164: L.pop(2);
165:
166: return n;
167: }
168:
169: /**
170: * @see java.util.Map#clear()
171: */
172: public void clear() {
173: L.newTable();
174: table = L.getLuaObject(-1);
175: L.pop(1);
176: }
177:
178: /**
179: * @see java.util.Map#isEmpty()
180: */
181: public boolean isEmpty() {
182: return size() == 0;
183: }
184:
185: /**
186: * @see java.util.Map#containsKey(java.lang.Object)
187: */
188: public boolean containsKey(Object key) {
189: try {
190: L.pushObjectValue(key);
191: LuaObject obj = L.getLuaObject(-1);
192: L.pop(1);
193:
194: LuaObject temp = L.getLuaObject(table, obj);
195:
196: return !temp.isNil();
197: } catch (LuaException e) {
198: return false;
199: }
200: }
201:
202: /**
203: * @see java.util.Map#containsValue(java.lang.Object)
204: */
205: public boolean containsValue(Object value) {
206: try {
207: L.pushObjectValue(value);
208: table.push();
209: L.pushNil();
210:
211: while (L.next(-2) != 0)/* `key' is at index -2 and `value' at index -1 */
212: {
213: if (L.equal(-4, -1) != 0) {
214: L.pop(4);
215: return true;
216: }
217: L.pop(1);
218: }
219:
220: L.pop(3);
221: return false;
222: } catch (LuaException e) {
223: return false;
224: }
225: }
226:
227: /**
228: * not implemented
229: * @see java.util.Map#values()
230: */
231: public Collection values() {
232: throw new RuntimeException("not implemented");
233: }
234:
235: /**
236: * @see java.util.Map#putAll(java.util.Map)
237: */
238: public void putAll(Map t) {
239: Iterator i = t.keySet().iterator();
240: while (i.hasNext()) {
241: Object key = i.next();
242: put(key, t.get(key));
243: }
244: }
245:
246: /**
247: * @see java.util.Map#entrySet()
248: */
249: public Set entrySet() {
250: throw new RuntimeException("not implemented");
251: }
252:
253: /**
254: * @see java.util.Map#keySet()
255: */
256: public Set keySet() {
257: throw new RuntimeException("not implemented");
258: }
259:
260: /**
261: * @see java.util.Map#get(java.lang.Object)
262: */
263: public Object get(Object key) {
264: try {
265: table.push();
266: L.pushObjectValue(key);
267:
268: L.getTable(-2);
269:
270: Object ret = L.toJavaObject(-1);
271:
272: L.pop(2);
273:
274: return ret;
275: } catch (LuaException e) {
276: return null;
277: }
278: }
279:
280: /**
281: * @see java.util.Map#remove(java.lang.Object)
282: */
283: public Object remove(Object key) {
284: try {
285: Object ret = get(key);
286:
287: table.push();
288: L.pushObjectValue(key);
289: L.pushNil();
290:
291: L.setTable(-3);
292:
293: L.pop(2);
294:
295: return ret;
296: } catch (LuaException e) {
297: return null;
298: }
299: }
300:
301: /**
302: * @see java.util.Map#put(java.lang.Object, java.lang.Object)
303: */
304: public Object put(Object key, Object value) {
305: try {
306: Object ret = get(key);
307:
308: table.push();
309: L.pushObjectValue(key);
310: L.pushObjectValue(value);
311:
312: L.setTable(-3);
313:
314: L.pop(1);
315:
316: return ret;
317: } catch (LuaException e) {
318: return null;
319: }
320: }
321:
322: }
|