| 1 | package felix.test; |
| 2 | |
| 3 | import static org.junit.Assert.*; |
| 4 | |
| 5 | import java.io.BufferedReader; |
| 6 | import java.io.FileNotFoundException; |
| 7 | import java.io.FileReader; |
| 8 | import java.sql.ResultSet; |
| 9 | import java.sql.SQLException; |
| 10 | import java.util.ArrayList; |
| 11 | import java.util.regex.Pattern; |
| 12 | |
| 13 | import org.junit.BeforeClass; |
| 14 | import org.junit.Test; |
| 15 | |
| 16 | import tuffy.db.RDB; |
| 17 | import tuffy.util.Config; |
| 18 | import tuffy.util.FileMan; |
| 19 | import tuffy.util.UIMan; |
| 20 | |
| 21 | import felix.main.Felix; |
| 22 | import felix.parser.FelixCommandOptions; |
| 23 | import felix.util.FelixConfig; |
| 24 | import felix.util.FelixUIMan; |
| 25 | |
| 26 | /** |
| 27 | * Test using evidence supplied in files. |
| 28 | * @author Josh Slauson |
| 29 | * |
| 30 | */ |
| 31 | public class IOFileTest { |
| 32 | |
| 33 | public static RDB db; |
| 34 | |
| 35 | /** |
| 36 | * Perform required setup for this test. |
| 37 | */ |
| 38 | @BeforeClass |
| 39 | public static final void setUp() { |
| 40 | // delete existing output files |
| 41 | FileMan.removeFile("test/felix/output/out.txt_IOFileTest_simpleTest"); |
| 42 | FileMan.removeFile("test/felix/output/out.txt_IOFileTest_missingEvidence"); |
| 43 | |
| 44 | FelixConfig.overrideID(); |
| 45 | |
| 46 | UIMan.parseConfigFile(Config.path_conf); |
| 47 | |
| 48 | if(FelixConfig.evidDBSchema != null){ |
| 49 | FelixConfig.db_schema = FelixConfig.evidDBSchema; |
| 50 | } |
| 51 | |
| 52 | db = RDB.getRDBbyConfig(); |
| 53 | db.schema = FelixConfig.db_schema; |
| 54 | db.resetSchema(FelixConfig.db_schema); |
| 55 | |
| 56 | if(!db.isSchemaExists("felix_test")) { |
| 57 | String query = "CREATE SCHEMA felix_test"; |
| 58 | db.execute(query); |
| 59 | } |
| 60 | |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Check if evidence was correctly loaded into a database table. |
| 65 | * @param expect |
| 66 | */ |
| 67 | public void checkEvidence(String[] expect) { |
| 68 | // Check if evidence table exists |
| 69 | db = RDB.getRDBbyConfig(FelixConfig.db_schema); |
| 70 | long count = db.countTuples("pred_evid"); |
| 71 | System.out.println("Checking evidence count: " + count + " = " + expect.length/2 + "?"); |
| 72 | assertTrue(count == expect.length/2); |
| 73 | |
| 74 | // Check evidence table contents |
| 75 | String query = "SELECT c1.string AS a1, c2.string AS b2 FROM pred_evid e, constants c1, constants c2 WHERE e.a1 = c1.id AND e.b2 = c2.id"; |
| 76 | ResultSet rs = db.query(query); |
| 77 | |
| 78 | try { |
| 79 | while(rs != null && rs.next()) { |
| 80 | String a = rs.getString("a1"); |
| 81 | String b = rs.getString("b2"); |
| 82 | |
| 83 | System.out.println("Checking evidence contents: " + a + ", " + b); |
| 84 | |
| 85 | boolean match = false; |
| 86 | for(int i = 0; i + 1 < expect.length; i+=2) { |
| 87 | if(a.equals(expect[i])) { |
| 88 | assertTrue(b.equals(expect[i+1])); |
| 89 | match = true; |
| 90 | } |
| 91 | } |
| 92 | if(!match) { |
| 93 | System.out.println("\tNo match"); |
| 94 | } |
| 95 | assertTrue(match); |
| 96 | } |
| 97 | |
| 98 | db.close(); |
| 99 | } catch (SQLException e) { |
| 100 | // TODO Auto-generated catch block |
| 101 | e.printStackTrace(); |
| 102 | assertTrue(false); |
| 103 | } |
| 104 | |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Checks if output file contains expected results. |
| 109 | * @param output |
| 110 | * @param expect |
| 111 | */ |
| 112 | public void checkOutput(String output, String[] expect) { |
| 113 | ArrayList<String> lines = FileMan.getLines(output); |
| 114 | System.out.println("Checking output size: " + lines.size() + " = " + expect.length/2 + "?"); |
| 115 | assertTrue(lines.size() == expect.length/2); |
| 116 | |
| 117 | for(int i = 0; i < lines.size(); i++) { |
| 118 | System.out.println("Checking output: " + lines.get(i)); |
| 119 | |
| 120 | boolean match = false; |
| 121 | for(int j = 0; j + 1 < expect.length; j+=2) { |
| 122 | if(lines.get(i).equals("fact(\"" + expect[j] + "\", \"" + expect[j+1] + "\")")) { |
| 123 | match = true; |
| 124 | } |
| 125 | } |
| 126 | if(!match) { |
| 127 | System.out.println("\tNo match"); |
| 128 | } |
| 129 | assertTrue(match); |
| 130 | } |
| 131 | |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Simple smoke test. |
| 136 | */ |
| 137 | @Test |
| 138 | public void simpleTest() { |
| 139 | try { |
| 140 | String output = "test/felix/output/out.txt_IOFileTest_simpleTest"; |
| 141 | String[] args = {"-e", "test/felix/io_test/evid.db", "-i", "test/felix/io_test/prog.mln", |
| 142 | "-o", output, "-queryFile", "test/felix/io_test/query.db", "-keepData"}; |
| 143 | |
| 144 | FelixConfig.overrideID(); |
| 145 | FelixCommandOptions options = FelixUIMan.parseCommand(args); |
| 146 | new Felix().run(options); |
| 147 | |
| 148 | String[] expect = {"1", "2", "4", "5"}; |
| 149 | checkEvidence(expect); |
| 150 | checkOutput(output, expect); |
| 151 | |
| 152 | } catch(Exception e) { |
| 153 | e.printStackTrace(); |
| 154 | assertTrue(false); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | // NOTE: correctly handles error |
| 160 | // @Test |
| 161 | // public void extraEvidence() { |
| 162 | // try { |
| 163 | // String output = "test/felix/output/out.txt_IOFileTest_extraEvidence"; |
| 164 | // String[] args = {"-e", "test/felix/io_test/evid2.db", "-i", "test/felix/io_test/prog.mln", |
| 165 | // "-o", output, "-queryFile", "test/felix/io_test/query.db", "-keepData"}; |
| 166 | // |
| 167 | // FelixConfig.overrideID(); |
| 168 | // FelixCommandOptions options = FelixUIMan.parseCommand(args); |
| 169 | // new Felix().run(options); |
| 170 | // |
| 171 | // String[] expect = {"1", "2", "4", "5"}; |
| 172 | // checkEvidence(expect); |
| 173 | // checkOutput(output, expect); |
| 174 | // |
| 175 | // } catch(Exception e) { |
| 176 | // e.printStackTrace(); |
| 177 | // assertTrue(false); |
| 178 | // } |
| 179 | // } |
| 180 | |
| 181 | // NOTE: correctly handles error |
| 182 | // @Test |
| 183 | // public void noEvidence() { |
| 184 | // try { |
| 185 | // String output = "test/felix/output/out.txt_IOFileTest_noEvidence"; |
| 186 | // String[] args = {"-e", "test/felix/io_test/evid.db", "-i", "test/felix/io_test/prog3.mln", |
| 187 | // "-o", output, "-queryFile", "test/felix/io_test/query.db", "-keepData"}; |
| 188 | // |
| 189 | // FelixConfig.overrideID(); |
| 190 | // FelixCommandOptions options = FelixUIMan.parseCommand(args); |
| 191 | // new Felix().run(options); |
| 192 | // |
| 193 | // String[] expect = {}; |
| 194 | // checkEvidence(expect); |
| 195 | // checkOutput(output, expect); |
| 196 | // |
| 197 | // } catch(Exception e) { |
| 198 | // e.printStackTrace(); |
| 199 | // assertTrue(false); |
| 200 | // } |
| 201 | // } |
| 202 | |
| 203 | /** |
| 204 | * Test using a evidence file that doesn't exist. |
| 205 | */ |
| 206 | @Test |
| 207 | public void missingEvid() { |
| 208 | try { |
| 209 | |
| 210 | |
| 211 | String output = "test/felix/output/out.txt_IOFileTest_missingEvidence"; |
| 212 | String[] args = {"-e", "test/felix/io_test/evid.db-missing", "-i", "test/felix/io_test/prog.mln", |
| 213 | "-o", output, "-queryFile", "test/felix/io_test/query.db", "-keepData"}; |
| 214 | |
| 215 | FelixConfig.overrideID(); |
| 216 | FelixCommandOptions options = FelixUIMan.parseCommand(args); |
| 217 | |
| 218 | new Felix().run(options); |
| 219 | |
| 220 | // just assume all goes well if we don't throw an exception |
| 221 | } catch (Exception e) { |
| 222 | e.printStackTrace(); |
| 223 | assertTrue(false); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // @Test |
| 228 | // public void incorrectEvidence() { |
| 229 | // try { |
| 230 | // String output = "test/felix/output/out.txt_IOFileTest_incorrectEvidence"; |
| 231 | // String[] args = {"-e", "test/felix/io_test/evid.db-incorrect", "-i", "test/felix/io_test/prog.mln", |
| 232 | // "-o", output, "-queryFile", "test/felix/io_test/query.db", "-keepData"}; |
| 233 | // |
| 234 | // FelixConfig.overrideID(); |
| 235 | // FelixCommandOptions options = FelixUIMan.parseCommand(args); |
| 236 | // |
| 237 | // new Felix().run(options); |
| 238 | // } catch (Exception e) { |
| 239 | // e.printStackTrace(); |
| 240 | // assertTrue(false); |
| 241 | // } |
| 242 | // } |
| 243 | } |