Skip to content

Commit 053521d

Browse files
authored
Rework SqlLineTest so that it actually interacts with the database (#3273)
The previous code called `SqlLine.main` which would call `System.exit` once SqlLine finished processing, which would mean that JUnit wouldn't get any result back, and the test would be marked as ignored. This doesn't seem like great design from the gradle perspective. During the rework, I also discovered that: 1. If you create the `SqlLine` object, you can change the `OutputStream` it uses (and error), so you don't need to change system out, or err. 2. The driver provided was wrong (it is `JDBCRelationalDriver`, not `JDBCEmbedDriver`) 3. the url was wrong (it's `jdbc:relational` not `jdbc:embed`, and you need three / before the database, not 1) 4. it missed the `-` before the `-e` in the argument list Note: I thought about adding an interactive test, where it runs multiple statements, but that started to get complicated, and didn't seem worthwhile.
1 parent 47e289e commit 053521d

File tree

1 file changed

+27
-26
lines changed
  • fdb-relational-cli/src/test/java/com/apple/foundationdb/relational/cli

1 file changed

+27
-26
lines changed

fdb-relational-cli/src/test/java/com/apple/foundationdb/relational/cli/SQLLineTest.java

+27-26
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020

2121
package com.apple.foundationdb.relational.cli;
2222

23-
import com.apple.foundationdb.relational.recordlayer.RelationalKeyspaceProvider;
24-
23+
import com.google.common.base.Charsets;
2524
import org.junit.jupiter.api.Test;
2625
import sqlline.SqlLine;
2726

28-
import java.io.BufferedOutputStream;
27+
import java.io.ByteArrayInputStream;
2928
import java.io.ByteArrayOutputStream;
3029
import java.io.IOException;
31-
import java.io.PrintStream;
30+
import java.nio.charset.StandardCharsets;
3231

33-
import static org.junit.jupiter.api.Assertions.assertTrue;
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.junit.jupiter.api.Assertions.assertEquals;
3434

3535
/**
3636
* SQLline doesn't do the intellij terminal very well.
@@ -45,32 +45,33 @@ public class SQLLineTest {
4545
*/
4646
@Test
4747
public void runSqlline() throws IOException {
48-
PrintStream oldOut = System.out;
49-
try (PrintStream ps = new PrintStream(new BufferedOutputStream(new ByteArrayOutputStream()));) {
50-
System.setOut(ps);
51-
SqlLine.main(new String[]{
48+
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
49+
ByteArrayOutputStream err = new ByteArrayOutputStream();
50+
ByteArrayInputStream in = new ByteArrayInputStream(new byte[0])) {
51+
final SqlLine sqlLine = new SqlLine();
52+
sqlLine.setOutputStream(out);
53+
sqlLine.setErrorStream(err);
54+
final SqlLine.Status status = sqlLine.begin(new String[] {
5255
"-ac", "com.apple.foundationdb.relational.cli.sqlline.Customize",
5356
// We don't need the below because we add our command over in our
5457
// sqlline Customize class.
5558
// "-ch", "sqlline.PlannerDebuggerCommandHandler",
56-
"-u", "jdbc:embed:/__SYS?schema=CATALOG",
57-
"-d", "com.apple.foundationdb.relational.jdbc.JDBCEmbedDriver",
59+
"-u", "jdbc:relational:///__SYS?schema=CATALOG",
60+
"-d", "com.apple.foundationdb.relational.jdbc.JDBCRelationalDriver",
5861
"--maxWidth=257",
59-
"e", "select * from databases;"
60-
});
61-
ps.flush();
62-
// Output should contain something like:
63-
// +-------------+
64-
// | DATABASE_ID |
65-
// +-------------+
66-
// | /__SYS |
67-
// +-------------+
68-
// Do primitive check tht output string has above.
69-
assertTrue(ps.toString().contains("DATABASE_ID"));
70-
assertTrue(ps.toString().contains(RelationalKeyspaceProvider.SYS));
71-
} finally {
72-
// Restore old sysout.
73-
System.setOut(oldOut);
62+
"-e", "select * from databases;"
63+
}, in, false);
64+
try {
65+
assertEquals(status, SqlLine.Status.OK);
66+
67+
final String output = out.toString(Charsets.UTF_8);
68+
assertThat(output).contains("DATABASE_ID")
69+
.contains("/__SYS");
70+
} catch (AssertionError e) {
71+
System.out.println(err.toString(StandardCharsets.UTF_8));
72+
System.out.println(out.toString(StandardCharsets.UTF_8));
73+
throw e;
74+
}
7475
}
7576
}
7677
}

0 commit comments

Comments
 (0)