first step to bring debugging code to j2me

This commit is contained in:
Shu Lei
2007-10-12 01:36:28 +00:00
parent 6970b30c7a
commit 341a5588b0
35 changed files with 1294 additions and 220 deletions

View File

@@ -0,0 +1,33 @@
package lua.debug;
import java.io.IOException;
import junit.framework.TestCase;
public class DebugEventTest extends TestCase {
public void testDebugEventSerialization() {
try {
DebugEvent event = new DebugEvent(DebugEventType.started);
byte[] data = SerializationHelper.serialize(event);
DebugEvent eventOut = (DebugEvent)SerializationHelper.deserialize(data);
assertNotNull(eventOut);
assertEquals(event.getType(), eventOut.getType());
} catch (IOException e) {
fail(e.getMessage());
}
}
public void testDebugEventBreakpointSerialization() {
try {
DebugEventBreakpoint event = new DebugEventBreakpoint("test.lua", 100);
byte[] data = SerializationHelper.serialize(event);
DebugEventBreakpoint eventOut
= (DebugEventBreakpoint) SerializationHelper.deserialize(data);
assertNotNull(eventOut);
assertEquals(event.getSource(), eventOut.getSource());
assertEquals(event.getLineNumber(), eventOut.getLineNumber());
} catch (IOException e) {
fail(e.getMessage());
}
}
}

View File

@@ -0,0 +1,56 @@
package lua.debug;
import java.io.IOException;
import junit.framework.TestCase;
public class DebugRequestTest extends TestCase {
public void testDebugRequestSerialization() {
try {
DebugRequest request = new DebugRequest(DebugRequestType.resume);
byte[] data = SerializationHelper.serialize(request);
DebugRequest requestOut
= (DebugRequest)SerializationHelper.deserialize(data);
assertNotNull(requestOut);
assertEquals(request.getType(), requestOut.getType());
} catch (IOException e) {
fail(e.getMessage());
}
}
public void testDebugRequestStackSerialization() {
try {
DebugRequestStack request = new DebugRequestStack(1);
byte[] data = SerializationHelper.serialize(request);
DebugRequestStack requestOut
= (DebugRequestStack) SerializationHelper.deserialize(data);
assertNotNull(requestOut);
assertEquals(requestOut.getIndex(), 1);
} catch (IOException e) {
fail(e.getMessage());
}
}
public void testDebugRequestLineBreakpointToggleSerialization() {
try
{
doTestDebugRequestLineBreakpointToggleSerialization(DebugRequestType.lineBreakpointSet, "test.lua", 100);
doTestDebugRequestLineBreakpointToggleSerialization(DebugRequestType.lineBreakpointClear, "test.lua", 50);
} catch (IOException e) {
fail(e.getMessage());
}
}
private void doTestDebugRequestLineBreakpointToggleSerialization(
DebugRequestType type, String source, int lineNo) throws IOException {
DebugRequestLineBreakpointToggle request
= new DebugRequestLineBreakpointToggle(DebugRequestType.lineBreakpointSet, "test.lua", 100);
byte[] data = SerializationHelper.serialize(request);
DebugRequestLineBreakpointToggle requestOut =
(DebugRequestLineBreakpointToggle) SerializationHelper.deserialize(data);
assertNotNull(requestOut);
assertEquals(request.getType(), requestOut.getType());
assertEquals(request.getSource(), requestOut.getSource());
assertEquals(request.getLineNumber(), requestOut.getLineNumber());
}
}

View File

@@ -0,0 +1,88 @@
package lua.debug;
import java.io.IOException;
import junit.framework.TestCase;
import lua.Lua;
public class DebugResponseTest extends TestCase {
public void testDebugResponseSimpleSerialization() {
try {
DebugResponseSimple responseIn = DebugResponseSimple.SUCCESS;
byte[] data = SerializationHelper.serialize(responseIn);
DebugResponseSimple responseOut
= (DebugResponseSimple)SerializationHelper.deserialize(data);
assertEquals(responseIn, responseOut);
} catch (IOException e) {
fail(e.getMessage());
}
}
public void testDebugResponseStackSerialization() {
try {
doTestDebugResponseStackSerialization(null);
doTestDebugResponseStackSerialization(new Variable[0]);
Variable[] variables = new Variable[5];
variables[0] = new Variable(0, "variable1", Lua.LUA_TSTRING, "value1");
variables[1] = new Variable(1, "variable2", Lua.LUA_TNIL, "nil");
variables[2] = new Variable(2, "variable3", Lua.LUA_TBOOLEAN, "false");
TableVariable childTable = new TableVariable(0, "child", Lua.LUA_TTABLE, new String[0], new Object[0]);
String[] keys = new String[] {"key1", "key2"};
Object[] values = new Object[] {"value1", childTable};
variables[3] = new TableVariable(2, "variable4", Lua.LUA_TTABLE, keys, values);
variables[4] = new Variable(2, "variable3", Lua.LUA_TNUMBER, "10");
doTestDebugResponseStackSerialization(variables);
} catch (IOException e) {
fail(e.getMessage());
}
}
private void doTestDebugResponseStackSerialization(Variable[] variables)
throws IOException {
DebugResponseStack stackIn = new DebugResponseStack(variables);
byte[] data = SerializationHelper.serialize(stackIn);
DebugResponseStack stackOut
= (DebugResponseStack) SerializationHelper.deserialize(data);
Variable[] variablesIn = stackIn.getVariables();
Variable[] variablesOut = stackOut.getVariables();
assertNotNull(variablesIn);
assertNotNull(variablesOut);
assertEquals(variablesIn.length, variablesOut.length);
for (int i = 0; i < variablesIn.length; i++) {
assertEquals(variablesIn[i], variablesOut[i]);
}
}
public void testDebugResponseCallgraphSerialization() {
try {
doTestDebugResponseCallgraphSerialization(null);
doTestDebugResponseCallgraphSerialization(new StackFrame[0]);
StackFrame[] frames = new StackFrame[1];
frames[0] = new StackFrame(100, "test.lua");
doTestDebugResponseCallgraphSerialization(frames);
} catch (IOException e) {
fail(e.getMessage());
}
}
private void doTestDebugResponseCallgraphSerialization(StackFrame[] frames)
throws IOException {
DebugResponseCallgraph responseIn = new DebugResponseCallgraph(frames);
byte[] data = SerializationHelper.serialize(responseIn);
DebugResponseCallgraph responseOut =
(DebugResponseCallgraph) SerializationHelper.deserialize(data);
assertNotNull(responseOut);
StackFrame[] inFrames = responseIn.getCallgraph();
StackFrame[] outFrames = responseOut.getCallgraph();
assertNotNull(outFrames);
assertEquals(inFrames.length, outFrames.length);
for (int i = 0; i < inFrames.length; i++) {
assertEquals(inFrames[i], outFrames[i]);
}
}
}

View File

@@ -0,0 +1,41 @@
package lua.debug;
import junit.framework.TestCase;
public class EnumTypeTest extends TestCase {
public void testDebugSupportStateSerialization() {
try {
DebugSupport.State stateIn = DebugSupport.State.RUNNING;
byte[] data = SerializationHelper.serialize(stateIn);
DebugSupport.State stateOut
= (DebugSupport.State) SerializationHelper.deserialize(data);
assertEquals(stateIn, stateOut);
} catch (Exception e) {
fail(e.getMessage());
}
}
public void testDebugRequestTypeSerialization() {
try {
DebugRequestType type = DebugRequestType.lineBreakpointClear;
byte[] data = SerializationHelper.serialize(type);
DebugRequestType typeOut
= (DebugRequestType) SerializationHelper.deserialize(data);
assertEquals(type, typeOut);
} catch (Exception e) {
fail(e.getMessage());
}
}
public void testDebugEventTypeSerialization() {
try {
DebugEventType type = DebugEventType.error;
byte[] data = SerializationHelper.serialize(type);
DebugEventType typeOut = (DebugEventType) SerializationHelper.deserialize(data);
assertNotNull(typeOut);
assertEquals(type, typeOut);
} catch (Exception e) {
fail(e.getMessage());
}
}
}

View File

@@ -21,29 +21,99 @@
******************************************************************************/
package lua.debug;
import java.io.IOException;
import java.net.URL;
import junit.framework.TestCase;
import lua.debug.StandardLuaJVM.ParseException;
/**
* Sanity test for StandardLuaJVM.
*/
public class LuaJVMTest extends TestCase {
protected void doTestRun(String testName) {
String[] args = new String[2];
args[0] = "-file";
URL filePath = getClass().getResource("/"+ testName);
if (filePath != null) {
args[1] = filePath.getPath();
try {
StandardLuaJVM.main(args);
} catch (Exception e) {
e.printStackTrace();
fail("Test " + testName + " failed due to " + e.getMessage());
}
}
}
public void testCommandLineParse() {
// null arguments
String[] args = null;
StandardLuaJVM vm = new StandardLuaJVM();
try {
vm.parse(args);
fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {}
// empty arguments
args = new String[] {};
try {
vm.parse(args);
fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {}
// incomplete arguments
args = new String[] { "-debug" };
try {
vm.parse(args);
fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {}
// incomplete arguments
args = new String[] { "-debug", "1046" };
try {
vm.parse(args);
fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {}
// missing script name
args = new String[] { "-debug", "1046", "1047"};
try {
vm.parse(args);
fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {}
// malformed request port
args = new String[] { "-debug", "104x", "1046", "dummy.lua"};
try {
vm.parse(args);
fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {}
// malformed event port
args = new String[] { "-debug", "1046", "104x", "dummy.lua"};
try {
vm.parse(args);
fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {}
// event port == request port
args = new String[] { "-debug", "1046", "1046", "dummy.lua"};
try {
vm.parse(args);
fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {}
// lua script cannot be found
args = new String[] { "-debug", "1046", "1047", "dummy.lua"};
try {
vm.parse(args);
vm.run();
fail("Should never reach this line.");
} catch (ParseException e) {
fail("Should never reach this line.");
} catch (IOException e) {
//expected
}
// lua script cannot be found
args = new String[] {"dummy.lua"};
try {
vm.parse(args);
vm.run();
fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {
fail("Should never reach this line.");
} catch (IOException e) {
//expected
}
}
public void testRun() {
String[] tests = new String[] {
"autoload",
@@ -78,4 +148,18 @@ public class LuaJVMTest extends TestCase {
System.out.println();
}
}
protected void doTestRun(String testName) {
String[] args = new String[1];
URL filePath = getClass().getResource("/"+ testName);
if (filePath != null) {
args[0] = filePath.getPath();
try {
StandardLuaJVM.main(args);
} catch (Exception e) {
e.printStackTrace();
fail("Test " + testName + " failed due to " + e.getMessage());
}
}
}
}

View File

@@ -0,0 +1,19 @@
package lua.debug;
import java.io.IOException;
import junit.framework.TestCase;
public class StackFrameTest extends TestCase {
public void testSerialization() {
try {
StackFrame stackIn = new StackFrame(10, "test.lua");
byte[] data = SerializationHelper.serialize(stackIn);
StackFrame stackOut = (StackFrame) SerializationHelper.deserialize(data);
assertNotNull(stackOut);
assertEquals(stackIn, stackOut);
} catch (IOException e) {
fail(e.getMessage());
}
}
}

View File

@@ -0,0 +1,134 @@
package lua.debug;
import java.io.IOException;
import junit.framework.TestCase;
import lua.Lua;
import lua.value.LBoolean;
import lua.value.LDouble;
import lua.value.LInteger;
import lua.value.LNil;
import lua.value.LString;
import lua.value.LTable;
public class TableVariableTest extends TestCase {
public void testCreate() {
LTable table = new LTable();
table.put("key1", new LString("value1"));
table.put("key2", new LNil());
table.put("key3", LBoolean.TRUE);
table.put("key4", LInteger.valueOf(10));
table.put("key5", new LDouble(0.5));
LTable childTable = new LTable();
childTable.put("childKey1", new LString("childValue1"));
table.put("key6", childTable);
TableVariable tableVariable = new TableVariable(0, "tableVar", Lua.LUA_TTABLE, table);
String[] keys = tableVariable.getKeys();
assertNotNull(keys);
assertEquals(keys.length, 6);
for (int i = 0; i < 6; i++) {
assertTrue(keys[i].startsWith("key"));
}
Object[] values = tableVariable.getValues();
assertNotNull(values);
assertEquals(values.length, 6);
for (int i = 0; i < 6; i++) {
if (values[i] instanceof String) {
assertTrue(values[i].equals("value1") ||
values[i].equals("nil") ||
values[i].equals("true") ||
values[i].equals("10") ||
values[i].equals("0.5"));
} else if (values[i] instanceof TableVariable) {
TableVariable child = (TableVariable) values[i];
String[] childKeys = child.getKeys();
assertNotNull(childKeys);
assertEquals(childKeys.length, 1);
assertEquals(childKeys[0], "childKey1");
Object[] childValues = child.getValues();
assertNotNull(childValues);
assertEquals(childValues.length, 1);
assertEquals(childValues[0], "childValue1");
} else {
fail("bad value type");
}
}
}
public void testSerialization() {
String[] keys = null;
Object[] values = null;
try {
doTestSerialization(0, "name", Lua.LUA_TTABLE, keys, values);
} catch (IOException e) {
fail(e.getMessage());
}
keys = new String[0];
values = new String[0];
try {
doTestSerialization(0, "name", Lua.LUA_TTABLE, keys, values);
} catch (IOException e) {
fail(e.getMessage());
}
keys = new String[] {"key1", "key2"};
values = new String[] {"value1", "value2"};
try {
doTestSerialization(0, "name", Lua.LUA_TTABLE, keys, values);
} catch (IOException e) {
fail(e.getMessage());
}
TableVariable grandchild = new TableVariable(1, "child", Lua.LUA_TTABLE, keys, values);
keys = new String[] {"grandchild1", "grandchild2", "grandchild3"};
values = new Object[] {"value1", "value2", grandchild};
TableVariable child = new TableVariable(1, "child", Lua.LUA_TTABLE, keys, values);
keys = new String[] {"child1", "child2"};
values = new Object[] {"value1", child};
try {
doTestSerialization(0, "name", Lua.LUA_TTABLE, keys, values);
} catch (IOException e) {
fail(e.getMessage());
}
}
protected void doTestSerialization(int index, String name, int type,
String[] keys, Object[] values)
throws IOException {
TableVariable in = new TableVariable(index, name, type, keys, values);
byte[] data = SerializationHelper.serialize(in);
TableVariable out = (TableVariable) SerializationHelper.deserialize(data);
assertTableVariable(in, out);
}
private void assertTableVariable(TableVariable in, TableVariable out) {
assertEquals(in.getIndex(), out.getIndex());
assertEquals(in.getType(), out.getType());
assertEquals(in.getName(), out.getName());
String[] inKeys = in.getKeys();
String[] outKeys = out.getKeys();
assertEquals(inKeys == null ? 0 : inKeys.length, outKeys == null ? 0 : outKeys.length);
for (int i = 0; inKeys != null && i < inKeys.length; i++) {
assertEquals(inKeys[i], outKeys[i]);
}
Object[] inValues = in.getValues();
Object[] outValues = out.getValues();
assertEquals(inValues == null ? 0 : inValues.length, outValues == null ? 0 : outValues.length);
for (int i = 0; inValues != null && i < inValues.length; i++) {
if (inValues[i] instanceof String && outValues[i] instanceof String) {
assertEquals(inValues[i], outValues[i]);
} else if (inValues[i] instanceof TableVariable && outValues[i] instanceof TableVariable) {
assertTableVariable((TableVariable)inValues[i], (TableVariable)outValues[i]);
} else {
fail("bad serialization");
}
}
}
}

View File

@@ -0,0 +1,26 @@
package lua.debug;
import java.io.IOException;
import junit.framework.TestCase;
import lua.Lua;
public class VariableTest extends TestCase {
public void testSerialization() {
doTestSerialization(0, "varName", Lua.LUA_TSTRING, null);
doTestSerialization(0, "varName", Lua.LUA_TSTRING, "varValue");
}
protected void doTestSerialization(int index, String varName, int type, String varValue) {
Variable varIn = new Variable(index, varName, type, varValue);
Variable varOut = null;
try {
byte[] data = SerializationHelper.serialize(varIn);
varOut = (Variable) SerializationHelper.deserialize(data);
} catch (IOException e) {
fail(e.getMessage());
}
assertNotNull(varOut);
assertEquals(varIn, varOut);
}
}