|
| 1 | + |
| 2 | +package com.activeandroid.test.parser; |
| 3 | + |
| 4 | +import com.activeandroid.test.ActiveAndroidTestCase; |
| 5 | +import com.activeandroid.test.R; |
| 6 | +import com.activeandroid.util.SqlParser; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | + |
| 13 | +public class ParserTest extends ActiveAndroidTestCase { |
| 14 | + |
| 15 | + private final String sql1 = "CREATE TABLE Entity1 ( Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL, Column1 INTEGER )"; |
| 16 | + private final String sql2 = "CREATE TABLE Entity2 ( Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL, Column1 INTEGER )"; |
| 17 | + |
| 18 | + private final String invalid = "CREATE TABLE Entity1 ( Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL, */ Column1 INTEGER )"; |
| 19 | + |
| 20 | + private InputStream getStream(int id) { |
| 21 | + return this.getContext().getResources().openRawResource(id); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Should be able to parse a script with two multi-line statments, even if the last statement |
| 26 | + * is not terminated by a semicolon. |
| 27 | + * @throws IOException |
| 28 | + */ |
| 29 | + public void testTwoStatements() throws IOException { |
| 30 | + |
| 31 | + final InputStream stream = this.getStream(R.raw.two_statements); |
| 32 | + List<String> commands = SqlParser.parse(stream); |
| 33 | + |
| 34 | + assertEquals(2, commands.size()); |
| 35 | + assertEquals(sql1, commands.get(0)); |
| 36 | + assertEquals(sql2, commands.get(1)); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Should be able to parse a multi-line statement that has an embedded line comment. |
| 41 | + * @throws IOException |
| 42 | + */ |
| 43 | + public void testLineComment() throws IOException { |
| 44 | + |
| 45 | + final InputStream stream = this.getStream(R.raw.line_comment); |
| 46 | + List<String> commands = SqlParser.parse(stream); |
| 47 | + |
| 48 | + assertEquals(1, commands.size()); |
| 49 | + assertEquals(sql1, commands.get(0)); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Should be able to handle a line comment that contains string tokens. |
| 54 | + * @throws IOException |
| 55 | + */ |
| 56 | + public void testLineCommentWithString() throws IOException { |
| 57 | + |
| 58 | + final InputStream stream = this.getStream(R.raw.line_comment_with_string); |
| 59 | + List<String> commands = SqlParser.parse(stream); |
| 60 | + |
| 61 | + assertEquals(1, commands.size()); |
| 62 | + assertEquals(sql1, commands.get(0)); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Should ignore a block comment end token inside a line comment. |
| 67 | + * @throws IOException |
| 68 | + */ |
| 69 | + public void testLineAndBlockEndComment() throws IOException { |
| 70 | + |
| 71 | + final InputStream stream = this.getStream(R.raw.line_comment_and_block_end); |
| 72 | + List<String> commands = SqlParser.parse(stream); |
| 73 | + |
| 74 | + assertEquals(1, commands.size()); |
| 75 | + assertEquals(sql1, commands.get(0)); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Should be able to handle a block comment. |
| 80 | + * @throws IOException |
| 81 | + */ |
| 82 | + public void testBlockComment() throws IOException { |
| 83 | + |
| 84 | + final InputStream stream = this.getStream(R.raw.block_comment); |
| 85 | + List<String> commands = SqlParser.parse(stream); |
| 86 | + |
| 87 | + assertEquals(1, commands.size()); |
| 88 | + assertEquals(sql1, commands.get(0)); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Should be able to handle a block comment that contains string tokens. |
| 93 | + * @throws IOException |
| 94 | + */ |
| 95 | + public void testBlockCommentWithString() throws IOException { |
| 96 | + |
| 97 | + final InputStream stream = this.getStream(R.raw.block_comment_with_string); |
| 98 | + List<String> commands = SqlParser.parse(stream); |
| 99 | + |
| 100 | + assertEquals(1, commands.size()); |
| 101 | + assertEquals(sql1, commands.get(0)); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Should ignore a line comment token inside a block comment. |
| 106 | + * @throws IOException |
| 107 | + */ |
| 108 | + public void testBlockAndLineComment() throws IOException { |
| 109 | + |
| 110 | + final InputStream stream = this.getStream(R.raw.block_with_line_comment); |
| 111 | + List<String> commands = SqlParser.parse(stream); |
| 112 | + |
| 113 | + assertEquals(1, commands.size()); |
| 114 | + assertEquals(sql1, commands.get(0)); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Should be able to parse a script that incorrectly closes a block comment twice. The |
| 119 | + * resulting script is not expected to run, but the parser shouldn't choke on it. |
| 120 | + * @throws IOException |
| 121 | + */ |
| 122 | + public void testInvalidBlockComment() throws IOException { |
| 123 | + |
| 124 | + final InputStream stream = this.getStream(R.raw.invalid_block_comment); |
| 125 | + List<String> commands = SqlParser.parse(stream); |
| 126 | + |
| 127 | + assertEquals(1, commands.size()); |
| 128 | + assertEquals(invalid, commands.get(0)); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Should ignore a line comment token inside a string. |
| 133 | + * @throws IOException |
| 134 | + */ |
| 135 | + public void testStringWithLineComment() throws IOException { |
| 136 | + final String sql = "INSERT INTO Entity ( Id, Column1, Column2 ) VALUES ( 1, '-- some text', 'some text' )"; |
| 137 | + |
| 138 | + final InputStream stream = this.getStream(R.raw.string_with_line_comment); |
| 139 | + List<String> commands = SqlParser.parse(stream); |
| 140 | + |
| 141 | + assertEquals(1, commands.size()); |
| 142 | + assertEquals(sql, commands.get(0)); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Should ignore block comment tokens inside strings. |
| 147 | + * @throws IOException |
| 148 | + */ |
| 149 | + public void testStringWithBlockComment() throws IOException { |
| 150 | + final String sql = "INSERT INTO Entity ( Id, Column1, Column2 ) VALUES ( 1, '/* some text', 'some text */' )"; |
| 151 | + |
| 152 | + final InputStream stream = this.getStream(R.raw.string_with_block_comment); |
| 153 | + List<String> commands = SqlParser.parse(stream); |
| 154 | + |
| 155 | + assertEquals(1, commands.size()); |
| 156 | + assertEquals(sql, commands.get(0)); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Should be able to handle a script that contains anything nasty I can thing of right now. |
| 161 | + * @throws IOException |
| 162 | + */ |
| 163 | + public void testComplex() throws IOException { |
| 164 | + final String sql1 = "CREATE TABLE Entity2 ( Id INTEGER AUTO_INCREMENT PRIMARY KEY, Column TEXT NOT NULL, Column2 INTEGER NULL )"; |
| 165 | + final String sql2 = "INSERT INTO Entity2 ( Id, Column, Column2 ) SELECT Id, Column, 0 FROM Entity"; |
| 166 | + final String sql3 = "DROP TABLE Entity"; |
| 167 | + final String sql4 = "ALTER TABLE Entity2 RENAME TO Entity"; |
| 168 | + final String sql5 = "INSERT INTO Entity2 ( Id, Column, Column2) VALUES ( 9001 , 42, 'string /* string */ -- string' )"; |
| 169 | + |
| 170 | + final InputStream stream = this.getStream(R.raw.complex); |
| 171 | + List<String> commands = SqlParser.parse(stream); |
| 172 | + |
| 173 | + assertEquals(5, commands.size()); |
| 174 | + assertEquals(sql1, commands.get(0)); |
| 175 | + assertEquals(sql2, commands.get(1)); |
| 176 | + assertEquals(sql3, commands.get(2)); |
| 177 | + assertEquals(sql4, commands.get(3)); |
| 178 | + assertEquals(sql5, commands.get(4)); |
| 179 | + } |
| 180 | +} |
0 commit comments