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

[SQLLINE-260] Validate given values for enum type properties #261

Closed
wants to merge 3 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
7 changes: 0 additions & 7 deletions src/main/java/sqlline/SqlLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -1526,14 +1526,12 @@ String scanForDriver(String url) {
return driver.getClass().getCanonicalName();
}

System.out.println("before");
scanDrivers();

if ((driver = findRegisteredDriver(url)) != null) {
return driver.getClass().getCanonicalName();
}

System.out.println("return null");
return null;
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -1603,11 +1601,6 @@ int print(ResultSet rs, DispatchCallback callback) throws SQLException {
}
}

if (f == null) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check becomes useless as after this commit the check for valid output format is going to be done before property setting

error(loc("unknown-format", format, getOutputFormats().keySet()));
f = new TableOutputFormat(this);
}

Rows rows;
if (getOpts().getIncremental()) {
rows = new IncrementalRows(this, rs, callback);
Expand Down
43 changes: 35 additions & 8 deletions src/main/java/sqlline/SqlLineOpts.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public class SqlLineOpts implements Completer {
put(MAX_HISTORY_ROWS, SqlLineOpts.this::setMaxHistoryRows);
put(MODE, SqlLineOpts.this::setMode);
put(NUMBER_FORMAT, SqlLineOpts.this::setNumberFormat);
put(OUTPUT_FORMAT, SqlLineOpts.this::setOutputFormat);
put(TIME_FORMAT, SqlLineOpts.this::setTimeFormat);
put(TIMESTAMP_FORMAT, SqlLineOpts.this::setTimestampFormat);
}
Expand Down Expand Up @@ -411,6 +412,13 @@ public void set(SqlLineProperty key, Object value) {
? (String) value : String.valueOf(value);
valueToSet = DEFAULT.equalsIgnoreCase(strValue)
? key.defaultValue() : value;
if (!key.getAvailableValues().isEmpty()
&& !key.getAvailableValues().contains(valueToSet.toString())) {
sqlLine.error(
sqlLine.loc("unknown-value",
key.propertyName(), value, key.getAvailableValues()));
return;
}
break;
case INTEGER:
try {
Expand Down Expand Up @@ -484,7 +492,7 @@ public void setNumberFormat(String numberFormat) {
DecimalFormatSymbols.getInstance(Locale.ROOT));
nf.format(Integer.MAX_VALUE);
} catch (Exception e) {
throw new IllegalArgumentException(e.getMessage());
sqlLine.handleException(e);
}
propertiesMap.put(NUMBER_FORMAT, numberFormat);
}
Expand Down Expand Up @@ -572,9 +580,8 @@ public void setColorScheme(String colorScheme) {
propertiesMap.put(COLOR_SCHEME, colorScheme);
return;
}
throw new IllegalArgumentException(
sqlLine.loc("unknown-colorscheme", colorScheme,
new TreeSet<>(BuiltInHighlightStyle.BY_NAME.keySet())));
sqlLine.error(sqlLine.loc("unknown-value", COLOR_SCHEME.propertyName(),
colorScheme, COLOR_SCHEME.getAvailableValues()));
}

public String getColorScheme() {
Expand Down Expand Up @@ -653,7 +660,7 @@ public void setCsvQuoteCharacter(String csvQuoteCharacter) {
return;
}
}
throw new IllegalArgumentException("CsvQuoteCharacter is '"
sqlLine.error("CsvQuoteCharacter is '"
+ csvQuoteCharacter + "'; it must be a character of default");
}

Expand Down Expand Up @@ -744,9 +751,29 @@ public void setMode(String mode) {
keyMaps.put(LineReader.MAIN, keyMaps.get(LineReader.VIINS));
break;
default:
throw new IllegalArgumentException(
sqlLine.loc(
"unknown-mode", mode, Arrays.asList(LineReader.EMACS, "vi")));
sqlLine.error(
sqlLine.loc("unknown-value", MODE.propertyName(),
mode, Arrays.asList(LineReader.EMACS, "vi")));
}
}

public void setOutputFormat(String outputFormat) {
if (DEFAULT.equalsIgnoreCase(outputFormat)) {
set(OUTPUT_FORMAT, OUTPUT_FORMAT.defaultValue());
return;
}

Set<String> availableFormats =
sqlLine.getOutputFormats().keySet().stream()
.map(t -> t.toUpperCase(Locale.ROOT)).collect(Collectors.toSet());
if (availableFormats.contains(outputFormat.toUpperCase(Locale.ROOT))) {
set(OUTPUT_FORMAT, outputFormat);
} else {
sqlLine.error(
sqlLine.loc("unknown-value",
OUTPUT_FORMAT.propertyName(),
outputFormat,
sqlLine.getOutputFormats().keySet()));
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/main/resources/sqlline/SqlLine.properties
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,7 @@ driver: Driver: {0} (version {1})
autocommit-status: Autocommit status: {0}
isolation-status: Transaction isolation: {0}
isolation-level-not-supported: Transaction isolation level {0} is not supported. Default ({1}) will be used instead.
unknown-format: Unknown output format "{0}". Possible values: {1}
unknown-colorscheme: Unknown color scheme "{0}". Possible values: {1}
unknown-mode: Unknown mode "{0}". Possible values: {1}
unknown-value: Unknown {0} "{1}". Possible values: {2}

closed: closed
open: open
Expand Down
65 changes: 19 additions & 46 deletions src/test/java/sqlline/SqlLineArgsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;
Expand All @@ -32,7 +33,6 @@
import org.hsqldb.jdbc.JDBCResultSet;
import org.hsqldb.jdbc.JDBCResultSetMetaData;
import org.jline.builtins.Commands;
import org.jline.reader.LineReader;
import org.jline.terminal.Terminal;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand Down Expand Up @@ -1731,7 +1731,7 @@ public void testCustomOutputFormats() {
+ "!set outputformat json\n"
+ "values 1;";
checkScriptFile(script, true, equalTo(SqlLine.Status.OK),
containsString("Unknown output format \"json\""));
containsString("Unknown outputFormat \"json\""));
}

@Test
Expand Down Expand Up @@ -1858,6 +1858,23 @@ public void testSetUsage() {
containsString("Usage: set [all | <property name> [<value>]]"));
}

@Test
public void testSetWrongValueToEnumTypeProperties() {
Collection<BuiltInProperty> propertiesWithAvailableValues =
Arrays.stream(BuiltInProperty.values())
.filter(t -> !t.getAvailableValues().isEmpty())
.collect(Collectors.toSet());
final String wrongValue = "wrong_value";
for (BuiltInProperty property: propertiesWithAvailableValues) {
final String script =
"!set " + property.propertyName() + " " + wrongValue + "\n";
checkScriptFile(script, true, equalTo(SqlLine.Status.OK),
allOf(containsString("Unknown "
+ property.propertyName() + " \"" + wrongValue + "\""),
not(containsString("Exception"))));
}
}

@Test
public void testResetSuccess() {
final String script = "!set timeout 200\n"
Expand Down Expand Up @@ -2134,50 +2151,6 @@ public void testSave() {
}
}

@Test
public void testSetWrongColorScheme() {
final SqlLine sqlLine = new SqlLine();
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
SqlLine.Status status =
begin(sqlLine, os, false, "-e", "!set maxwidth 80");
assertThat(status, equalTo(SqlLine.Status.OK));
final DispatchCallback dc = new DispatchCallback();
final String invalidColorScheme = "invalid";
sqlLine.runCommands(dc, "!set colorscheme " + invalidColorScheme);
assertThat(os.toString("UTF8"),
containsString(
sqlLine.loc("unknown-colorscheme", invalidColorScheme,
new TreeSet<>(BuiltInHighlightStyle.BY_NAME.keySet()))));
os.reset();
} catch (Exception e) {
// fail
throw new RuntimeException(e);
}
}

@Test
public void testSetWrongEditingMode() {
final SqlLine sqlLine = new SqlLine();
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
SqlLine.Status status =
begin(sqlLine, os, false, "-e", "!set maxwidth 80");
assertThat(status, equalTo(SqlLine.Status.OK));
DispatchCallback dc = new DispatchCallback();
final String invalidEditingMode = "invalid";
sqlLine.runCommands(dc, "!set mode " + invalidEditingMode);
assertThat(os.toString("UTF8"),
containsString(
sqlLine.loc("unknown-mode", invalidEditingMode,
Arrays.asList(LineReader.EMACS, "vi"))));
os.reset();
} catch (Exception e) {
// fail
throw new RuntimeException(e);
}
}

@Test
public void testScript() {
final File file = createTempFile("sqlline", ".script");
Expand Down