Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix beeline test KyuubiBeeLineTest.testKyuubiBeelineComment #6160

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public class BeeLine implements Closeable {
private final BeeLineOpts opts = new BeeLineOpts(this, System.getProperties());
private String lastProgress = null;
private final Map<SQLWarning, Date> seenWarnings = new HashMap<SQLWarning, Date>();
private final Commands commands = new Commands(this);
private Commands commands = new Commands(this);
private OutputFile scriptOutputFile = null;
private OutputFile recordOutputFile = null;
private PrintStream outputStream = new PrintStream(System.out, true);
Expand Down Expand Up @@ -2382,4 +2382,8 @@ void setIsTestMode(boolean isTestMode) {
boolean isTestMode() {
return isTestMode;
}

protected void setCommands(Commands commands) {
this.commands = commands;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public class KyuubiBeeLine extends BeeLine {

public static final String KYUUBI_BEELINE_DEFAULT_JDBC_DRIVER =
"org.apache.kyuubi.jdbc.KyuubiHiveDriver";
protected KyuubiCommands commands = new KyuubiCommands(this);
private Driver defaultDriver;

// copied from org.apache.hive.beeline.BeeLine
Expand All @@ -66,11 +65,7 @@ public KyuubiBeeLine() {
@SuppressWarnings("deprecation")
public KyuubiBeeLine(boolean isBeeLine) {
super(isBeeLine);
try {
DynFields.builder().hiddenImpl(BeeLine.class, "commands").buildChecked(this).set(commands);
} catch (Throwable t) {
throw new ExceptionInInitializerError("Failed to inject kyuubi commands");
}
setCommands(new KyuubiCommands(this));
try {
defaultDriver =
DynConstructors.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@
import java.io.PrintStream;
import org.apache.kyuubi.util.reflect.DynFields;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class KyuubiBeeLineTest {

private static final Logger LOG = LoggerFactory.getLogger(KyuubiBeeLineTest.class);

@Test
public void testKyuubiBeelineWithoutArgs() {
KyuubiBeeLine kyuubiBeeLine = new KyuubiBeeLine();
Expand Down Expand Up @@ -90,15 +95,46 @@ public void testKyuubiBeeLinePythonMode() {

@Test
public void testKyuubiBeelineComment() {
KyuubiBeeLine kyuubiBeeLine = new KyuubiBeeLine();
int result = kyuubiBeeLine.initArgsFromCliVars(new String[] {"-e", "--comment show database;"});
assertEquals(0, result);
result = kyuubiBeeLine.initArgsFromCliVars(new String[] {"-e", "--comment\n show database;"});
assertEquals(1, result);
result =
kyuubiBeeLine.initArgsFromCliVars(
new String[] {"-e", "--comment line 1 \n --comment line 2 \n show database;"});
assertEquals(1, result);
KyuubiBeeLine interceptedKyuubiBeeLine =
new KyuubiBeeLine() {
@Override
boolean dispatch(String line) {
if (line != null && line.startsWith("!connect")) {
LOG.info("Return true for command: {} to pretend connection is established.", line);
return true;
}
return super.dispatch(line);
}
};

String[] cmd = new String[] {""};
KyuubiCommands interceptedCommands =
new KyuubiCommands(interceptedKyuubiBeeLine) {
@Override
public boolean sql(String line, boolean entireLineAsCommand) {
LOG.info("Return true for sql {} to pretend sql is executed successfully.", line);
cmd[0] = line;
return true;
}
};
interceptedKyuubiBeeLine.setCommands(interceptedCommands);

interceptedKyuubiBeeLine.initArgs(
new String[] {"-u", "dummy_url", "-e", "--comment show database;"});
assertEquals(0, cmd[0].length());

// Beeline#exit must be false to execute sql
interceptedKyuubiBeeLine.setExit(false);
interceptedKyuubiBeeLine.initArgs(
new String[] {"-u", "dummy_url", "-e", "--comment\n show database;"});
assertEquals("show database;", cmd[0]);

interceptedKyuubiBeeLine.setExit(false);
interceptedKyuubiBeeLine.initArgs(
new String[] {
"-u", "dummy_url", "-e", "--comment line 1 \n --comment line 2 \n show database;"
});
assertEquals("show database;", cmd[0]);
}

static class BufferPrintStream extends PrintStream {
Expand Down
Loading