| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | (1) | |
| 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 | | | package org.jcoderz.phoenix.sqlparser; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.FileInputStream; |
| 37 | | | import java.io.FileReader; |
| 38 | | | import java.io.FilenameFilter; |
| 39 | | | import java.io.LineNumberReader; |
| 40 | | | import java.io.StringReader; |
| 41 | | | import java.io.StringWriter; |
| 42 | | | import java.util.ArrayList; |
| 43 | | | import java.util.Iterator; |
| 44 | | | import java.util.List; |
| 45 | | | import junit.framework.Test; |
| 46 | | | import junit.framework.TestCase; |
| 47 | | | import junit.framework.TestSuite; |
| 48 | | | |
| 49 | | | |
| 50 | | | {@link } |
| 51 | | | |
| 52 | | | @author |
| 53 | | | |
| 54 | 0 | | public class SqlScannerTest |
| 55 | | | extends TestCase |
| 56 | | | { |
| 57 | | | private static final String SQL_DIRECTORY = "test/data"; |
| 58 | | | |
| 59 | | | |
| 60 | | | @return |
| 61 | | | |
| 62 | | | public static Test suite () |
| 63 | | | { |
| 64 | 100 | | final File dir = new File(getSqlDirectory()); |
| 65 | 100 | | final String[] sqlFiles = dir.list(new SqlFilenameFilter()); |
| 66 | 100 | | if (sqlFiles == null || sqlFiles.length == 0) |
| 67 | | | { |
| 68 | 0 | | throw new RuntimeException("[Error] " |
| 69 | | | + "No SQL files found at '" + dir + "'!"); |
| 70 | | | } |
| 71 | | | |
| 72 | 100 | | final TestSuite suite = new TestSuite(); |
| 73 | 100 | | for (int i = 0; i < sqlFiles.length; i++) |
| 74 | | | { |
| 75 | 100 | | suite.addTest(new XmlFileValidation(dir, sqlFiles[i])); |
| 76 | | | } |
| 77 | | | |
| 78 | 100 | | return suite; |
| 79 | | | } |
| 80 | | | |
| 81 | | | private static String getSqlDirectory () |
| 82 | | | { |
| 83 | 100 | | return System.getProperty("basedir", ".") |
| 84 | | | + File.separator + SQL_DIRECTORY; |
| 85 | | | } |
| 86 | | | |
| 87 | 100 | | static class SqlFilenameFilter |
| 88 | | | implements FilenameFilter |
| 89 | | | { |
| 90 | | | {@inheritDoc} |
| 91 | | | public boolean accept (File dir, String name) |
| 92 | | | { |
| 93 | 100 | | return name.endsWith(".sql"); |
| 94 | | | } |
| 95 | | | } |
| 96 | | | |
| 97 | | | |
| 98 | 0 | | static class XmlFileValidation |
| 99 | | | extends TestCase |
| 100 | | | { |
| 101 | | | private final String mSqlFileName; |
| 102 | | | private final File mDir; |
| 103 | | | |
| 104 | | | public XmlFileValidation (File dir, String sqlFileName) |
| 105 | | | { |
| 106 | 100 | | super(sqlFileName); |
| 107 | 100 | | mSqlFileName = sqlFileName; |
| 108 | 100 | | mDir = dir; |
| 109 | 100 | | } |
| 110 | | | |
| 111 | | | public void runTest () |
| 112 | | | throws Throwable |
| 113 | | | { |
| 114 | 100 | | testSqlFile(); |
| 115 | 100 | | } |
| 116 | | | |
| 117 | | | public void testSqlFile () |
| 118 | | | throws Exception |
| 119 | | | { |
| 120 | 100 | | final File file = new File(mDir, mSqlFileName); |
| 121 | 100 | | final StringWriter writer = new StringWriter(); |
| 122 | | | try |
| 123 | | | { |
| 124 | 100 | | final SqlScanner scanner |
| 125 | | | = new SqlScanner(new FileInputStream(file)); |
| 126 | | | |
| 127 | 100 | | final List tokens = new ArrayList(); |
| 128 | | | |
| 129 | | | for (;;) |
| 130 | | | { |
| 131 | 100 | | final Token t = scanner.nextToken(); |
| 132 | 100 | | if (t.getType() == TokenType.EOF) |
| 133 | | | { |
| 134 | 100 | | break; |
| 135 | | | } |
| 136 | 100 | | tokens.add(t); |
| 137 | 100 | | } |
| 138 | | | |
| 139 | 100 | | for (final Iterator iterator = tokens.iterator(); |
| 140 | 100 | | iterator.hasNext();) |
| 141 | | | { |
| 142 | 100 | | final Token t = (Token) iterator.next(); |
| 143 | 100 | | writer.write(t.getValue()); |
| 144 | 100 | | } |
| 145 | 100 | | writer.close(); |
| 146 | | | } |
| 147 | 0 | | catch (Exception e) |
| 148 | | | { |
| 149 | 0 | (2) | e.printStackTrace(); |
| 150 | 0 | | fail("Expected well-formed SQL file " + file + " : " + e); |
| 151 | 100 | | } |
| 152 | 100 | | diff(file, new LineNumberReader(new StringReader(writer.toString()))); |
| 153 | 100 | | } |
| 154 | | | |
| 155 | | | private void diff (File file, LineNumberReader reader) |
| 156 | | | throws Exception |
| 157 | | | { |
| 158 | 100 | (3) | final LineNumberReader org |
| 159 | | | = new LineNumberReader(new FileReader(file)); |
| 160 | | | for (;;) |
| 161 | | | { |
| 162 | 100 | | final String line1 = org.readLine(); |
| 163 | 100 | | if (line1 == null) |
| 164 | | | { |
| 165 | 100 | | break; |
| 166 | | | } |
| 167 | 100 | | final String line2 = reader.readLine(); |
| 168 | 100 | | if (line2 == null) |
| 169 | | | { |
| 170 | 0 | | fail("Unexpected end of file"); |
| 171 | | | } |
| 172 | 100 | | assertEquals("File: " + file + " Line " + org.getLineNumber() |
| 173 | | | + " should be equals", line1, line2); |
| 174 | | | |
| 175 | 100 | | } |
| 176 | 100 | | } |
| 177 | | | |
| 178 | | | } |
| 179 | | | } |