-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add :format command to change format in interactive shell
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package kmql.command; | ||
|
||
import java.io.BufferedOutputStream; | ||
import java.io.PrintWriter; | ||
import java.util.List; | ||
|
||
import kmql.Command; | ||
import kmql.Engine; | ||
|
||
/** | ||
* Expire table caches. | ||
*/ | ||
public class FormatCommand implements Command { | ||
@Override | ||
public String help() { | ||
return ":format FORMAT - Set output format"; | ||
} | ||
|
||
@Override | ||
public void execute(List<String> args, Engine engine, BufferedOutputStream output) { | ||
PrintWriter pw = new PrintWriter(output); | ||
try { | ||
if (args.size() != 1) { | ||
pw.println("Error usage: :format FORMAT"); | ||
return; | ||
} | ||
String format = args.get(0); | ||
try { | ||
engine.setOutputFormat(format); | ||
pw.printf("Output format set to '%s'\n", format); | ||
} catch (IllegalArgumentException ignored) { | ||
pw.printf("Error: no such format '%s'\n", format); | ||
} | ||
} finally { | ||
pw.flush(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package kmql.command; | ||
|
||
import static java.util.Collections.emptyList; | ||
import static java.util.Collections.singletonList; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.io.BufferedOutputStream; | ||
import java.sql.SQLException; | ||
import java.util.Arrays; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnit; | ||
import org.mockito.junit.MockitoRule; | ||
|
||
import kmql.Database; | ||
import kmql.Engine; | ||
|
||
public class FormatCommandTest { | ||
@Rule | ||
public MockitoRule rule = MockitoJUnit.rule(); | ||
@Mock | ||
private Database db; | ||
@Mock | ||
private Engine engine; | ||
private final FormatCommand cmd = new FormatCommand(); | ||
|
||
@Before | ||
public void setUp() { | ||
doReturn(db).when(engine).db(); | ||
} | ||
|
||
@Test | ||
public void execute() throws SQLException { | ||
cmd.execute(singletonList("json"), engine, mock(BufferedOutputStream.class)); | ||
|
||
verify(engine, times(1)).setOutputFormat("json"); | ||
} | ||
|
||
@Test | ||
public void executeInvalidArgs() throws SQLException { | ||
cmd.execute(emptyList(), engine, mock(BufferedOutputStream.class)); | ||
cmd.execute(Arrays.asList("json", "table"), engine, mock(BufferedOutputStream.class)); | ||
|
||
verify(engine, never()).setOutputFormat(any(String.class)); | ||
} | ||
} |