forked from hflicka/arden2bytecode
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathJDBCQueryTest.java
More file actions
158 lines (140 loc) · 5.36 KB
/
JDBCQueryTest.java
File metadata and controls
158 lines (140 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package arden.tests.implementation;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Assert;
import org.junit.Test;
import com.lexicalscope.jewel.cli.CliFactory;
import arden.CommandLineOptions;
import arden.compiler.CompiledMlm;
import arden.compiler.Compiler;
import arden.compiler.CompilerException;
import arden.runtime.ArdenList;
import arden.runtime.ArdenNumber;
import arden.runtime.ArdenString;
import arden.runtime.ArdenValue;
import arden.runtime.ExecutionContext;
import arden.runtime.MedicalLogicModule;
import arden.runtime.evoke.CallTrigger;
import arden.runtime.jdbc.DriverHelper;
import arden.runtime.jdbc.JDBCExecutionContext;
import arden.runtime.jdbc.JDBCQuery;
public class JDBCQueryTest extends ImplementationTest {
private static boolean SQLiteLoaded = false;
private static final String SQLITE_PATH = "./sqlite-jdbc-3.7.2.jar";
private static CompiledMlm parseTemplate(String dataCode, String logicCode, String actionCode)
throws CompilerException {
try {
InputStream s = JDBCQueryTest.class.getResourceAsStream("ActionTemplate.mlm");
String fullCode = inputStreamToString(s).replace("$ACTION", actionCode).replace("$DATA", dataCode).replace(
"$LOGIC", logicCode);
Compiler c = new Compiler();
return c.compileMlm(new StringReader(fullCode));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Driver loadSQLite() throws
MalformedURLException,
InstantiationException,
IllegalAccessException,
SQLException {
if (SQLiteLoaded) {
return DriverManager.getDriver("jdbc:sqlite:");
}
URL urlA =
new File(
SQLITE_PATH
).toURI().toURL();
URL[] urls = { urlA };
URLClassLoader ulc = new URLClassLoader(urls);
Driver driver;
try {
driver = (Driver)Class.forName("org.sqlite.JDBC", true, ulc).newInstance();
} catch (ClassNotFoundException e) {
System.err.println("SQLite JDBC driver not found. Skipping associated test in " + this.getClass().getName());
return null;
}
DriverManager.registerDriver(new DriverHelper(driver));
SQLiteLoaded = true;
return driver;
}
private Statement initDb() {
Connection connection = null;
Statement statement = null;
try {
connection = DriverManager.getConnection("jdbc:sqlite:");
statement = connection.createStatement();
statement.executeUpdate("drop table if exists person");
statement.executeUpdate("create table person (id integer, name string)");
statement.executeUpdate("insert into person values(1, 'A')");
statement.executeUpdate("insert into person values(2, 'B')");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return statement;
}
@Test
public void testObjectToArdenValue() throws Exception {
Assert.assertEquals(new ArdenNumber(1.0),
JDBCQuery.objectToArdenValue(new Integer(1)));
Assert.assertEquals(new ArdenNumber(1.0),
JDBCQuery.objectToArdenValue(new Double(1.0)));
Assert.assertEquals(new ArdenString("hey"),
JDBCQuery.objectToArdenValue(new String("hey")));
}
@Test
public void testResultSetToArdenValues() throws Exception {
if (loadSQLite() == null) {
return;
}
Statement stmt = initDb();
ResultSet results = stmt.executeQuery("select * from person");
ArdenValue[] ardenValues = JDBCQuery.resultSetToArdenValues(results);
ArdenValue[] expectedA = {new ArdenNumber(1), new ArdenNumber(2)};
ArdenValue[] expectedB = {new ArdenString("A"), new ArdenString("B")};
ArdenList[] expectedArrA = {new ArdenList(expectedA), new ArdenList(expectedB)};
Assert.assertArrayEquals(expectedArrA, ardenValues);
ArdenValue[] expectedC = {new ArdenString("A"), new ArdenString("X")};
ArdenList[] expectedArrB = {new ArdenList(expectedA), new ArdenList(expectedC)};
assertArrayNotEquals(expectedArrB, ardenValues);
ArdenValue[] expectedD = {new ArdenString("1"), new ArdenNumber(2)};
ArdenList[] expectedArrC = {new ArdenList(expectedD), new ArdenList(expectedB)};
assertArrayNotEquals(expectedArrC, ardenValues);
}
@Test
public void testJDBCExecutionContextRead() throws Exception {
if (loadSQLite() == null) {
return;
}
String[] args = new String[]{"--env", "jdbc:sqlite:"};
CommandLineOptions options =
CliFactory.parseArguments(CommandLineOptions.class, args);
ExecutionContext testContext = new JDBCExecutionContext(options);
MedicalLogicModule mlm = parseTemplate(
"varA := read {drop table if exists person};\n" +
"varB := read {create table person (id integer, name string)};\n" +
"varC := read {insert into person values (1, 'A')};\n" +
"varD := read {insert into person values (2, 'B')};\n" +
"(varE, varF) := read {select * from person};\n",
"conclude true;",
"return (varE, varF);");
ArdenValue[] result = mlm.run(testContext, null, new CallTrigger());
Assert.assertEquals(1, result.length);
ArdenValue[] expected = {new ArdenNumber(1), new ArdenNumber(2),
new ArdenString("A"), new ArdenString("B")};
ArdenValue[] resultList = ((ArdenList)(result[0])).values;
Assert.assertArrayEquals(expected, resultList);
}
}