Skip to content

Commit

Permalink
Improve Test Coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
manticore-projects committed Jul 11, 2021
1 parent a7ad671 commit 57ed8f9
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 101 deletions.
12 changes: 9 additions & 3 deletions src/main/java/net/sf/jsqlparser/expression/JsonFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@
public class JsonFunction extends ASTNodeAccessImpl implements Expression {
private JsonFunctionType functionType;
private final ArrayList<JsonKeyValuePair> keyValuePairs = new ArrayList<>();

private ArrayList<JsonFunctionExpression> expressions = new ArrayList<>();

private final ArrayList<JsonFunctionExpression> expressions = new ArrayList<>();
private JsonAggregateOnNullType onNullType;
private JsonAggregateUniqueKeysType uniqueKeysType;

public ArrayList<JsonKeyValuePair> getKeyValuePairs() {
return keyValuePairs;
}

public ArrayList<JsonFunctionExpression> getExpressions() {
return expressions;
}

public JsonKeyValuePair getKeyValuePair(int i) {
return keyValuePairs.get(i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ public JsonFunctionExpression withUsingFormatJson(boolean usingFormatJson) {
}

public StringBuilder append(StringBuilder builder) {
return builder.append(expression.toString()).append(usingFormatJson ? " FORMAT JSON" : "");
return builder.append(getExpression()).append(isUsingFormatJson() ? " FORMAT JSON" : "");
}

@Override
public String toString() {
return append(new StringBuilder()).toString();
}

}
194 changes: 104 additions & 90 deletions src/main/java/net/sf/jsqlparser/expression/JsonKeyValuePair.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,95 +18,109 @@
*/

public class JsonKeyValuePair {
private final String key;
private boolean usingKeyKeyword = false;
private final Object value;
private boolean usingValueKeyword = false;
private boolean usingFormatJson = false;

public JsonKeyValuePair(String key, Object value, boolean usingKeyKeyword, boolean usingValueKeyword) {
this.key = Objects.requireNonNull(key, "The KEY of the Pair must not be null");
this.value = Objects.requireNonNull(value, "The VALUE of the Pair must not be null");
this.usingKeyKeyword = usingKeyKeyword;
this.usingValueKeyword = usingValueKeyword;
}

public boolean isUsingKeyKeyword() {
return usingKeyKeyword;
}

public void setUsingKeyKeyword(boolean usingKeyKeyword) {
this.usingKeyKeyword = usingKeyKeyword;
}

public JsonKeyValuePair withUsingKeyKeyword(boolean usingKeyKeyword) {
this.setUsingKeyKeyword(usingKeyKeyword);
return this;
}

public boolean isUsingValueKeyword() {
return usingValueKeyword;
}

public void setUsingValueKeyword(boolean usingValueKeyword) {
this.usingValueKeyword = usingValueKeyword;
}

public JsonKeyValuePair withUsingValueKeyword(boolean usingValueKeyword) {
this.setUsingValueKeyword(usingValueKeyword);
return this;
}

public boolean isUsingFormatJson() {
return usingFormatJson;
}
private final String key;
private boolean usingKeyKeyword = false;
private final Object value;
private boolean usingValueKeyword = false;
private boolean usingFormatJson = false;

public JsonKeyValuePair(String key, Object value, boolean usingKeyKeyword,
boolean usingValueKeyword) {
this.key = Objects.requireNonNull(key, "The KEY of the Pair must not be null");
this.value = Objects.requireNonNull(value, "The VALUE of the Pair must not be null");
this.usingKeyKeyword = usingKeyKeyword;
this.usingValueKeyword = usingValueKeyword;
}

public boolean isUsingKeyKeyword() {
return usingKeyKeyword;
}

public void setUsingKeyKeyword(boolean usingKeyKeyword) {
this.usingKeyKeyword = usingKeyKeyword;
}

public JsonKeyValuePair withUsingKeyKeyword(boolean usingKeyKeyword) {
this.setUsingKeyKeyword(usingKeyKeyword);
return this;
}

public boolean isUsingValueKeyword() {
return usingValueKeyword;
}

public void setUsingValueKeyword(boolean usingValueKeyword) {
this.usingValueKeyword = usingValueKeyword;
}

public JsonKeyValuePair withUsingValueKeyword(boolean usingValueKeyword) {
this.setUsingValueKeyword(usingValueKeyword);
return this;
}

public boolean isUsingFormatJson() {
return usingFormatJson;
}

public void setUsingFormatJson(boolean usingFormatJson) {
this.usingFormatJson = usingFormatJson;
}

public JsonKeyValuePair withUsingFormatJson(boolean usingFormatJson) {
this.setUsingFormatJson(usingFormatJson);
return this;
}

@Override
public int hashCode() {
int hash = 7;
hash = 83 * hash + Objects.hashCode(this.key);
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final JsonKeyValuePair other = (JsonKeyValuePair) obj;
return Objects.equals(this.key, other.key);
}

public String getKey() {
return key;
}

public Object getValue() {
return value;
}

public StringBuilder append(StringBuilder builder) {
if (isUsingValueKeyword()) {
if (isUsingKeyKeyword()) {
builder.append("KEY ");
}
builder.append(getKey()).append(" VALUE ").append(getValue());
} else {
builder.append(getKey()).append(":").append(getValue());
}

if (isUsingFormatJson()) {
builder.append(" FORMAT JSON");
}

return builder;
}

@Override
public String toString() {
return append(new StringBuilder()).toString();
}

public void setUsingFormatJson(boolean usingFormatJson) {
this.usingFormatJson = usingFormatJson;
}

public JsonKeyValuePair withUsingFormatJson(boolean usingFormatJson) {
this.setUsingFormatJson(usingFormatJson);
return this;
}

@Override
public int hashCode() {
int hash = 7;
hash = 83 * hash + Objects.hashCode(this.key);
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final JsonKeyValuePair other = (JsonKeyValuePair) obj;
return Objects.equals(this.key, other.key);
}

public String getKey() {
return key;
}

public Object getValue() {
return value;
}

public StringBuilder append(StringBuilder builder) {
return builder;
}

@Override
public String toString() {
return append(new StringBuilder()).toString();
}

}
15 changes: 13 additions & 2 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.sf.jsqlparser.expression.JsonAggregateFunction;
import net.sf.jsqlparser.expression.JsonExpression;
import net.sf.jsqlparser.expression.JsonFunction;
import net.sf.jsqlparser.expression.JsonFunctionExpression;
import net.sf.jsqlparser.expression.KeepExpression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.MySQLGroupConcat;
Expand Down Expand Up @@ -1012,11 +1013,21 @@ public void visit(AlterSession alterSession) {

@Override
public void visit(JsonAggregateFunction expression) {
expression.accept(this);
Expression expr = expression.getExpression();
if (expr!=null) {
expr.accept(this);
}

expr = expression.getFilterExpression();
if (expr!=null) {
expr.accept(this);
}
}

@Override
public void visit(JsonFunction expression) {
expression.accept(this);
for (JsonFunctionExpression expr: expression.getExpressions()) {
expr.getExpression().accept(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@
*/
@SuppressWarnings({"PMD.CyclomaticComplexity"})
public class ExpressionValidator extends AbstractValidator<Expression> implements ExpressionVisitor {


@Override
public void visit(Addition addition) {
visitBinaryExpression(addition, " + ");
Expand Down Expand Up @@ -591,12 +589,12 @@ public void visit(XMLSerializeExpr xml) {

@Override
public void visit(JsonAggregateFunction expression) {
expression.accept(this);
// no idea what this is good for
}

@Override
public void visit(JsonFunction expression) {
expression.accept(this);
// no idea what this is good for
}

}
40 changes: 40 additions & 0 deletions src/test/java/net/sf/jsqlparser/expression/JsonFunctionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

package net.sf.jsqlparser.expression;

import java.util.Objects;
import junit.framework.Assert;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.test.TestUtils;
import org.junit.Test;
Expand Down Expand Up @@ -48,6 +50,44 @@ public void testObjectAgg() throws JSQLParserException {
"SELECT JSON_OBJECTAGG( KEY foo VALUE bar NULL ON NULL WITH UNIQUE KEYS ) FILTER( WHERE name = 'Raj' ) OVER( PARTITION BY name ) FROM dual ",
true);
}

@Test
public void testObjectBuilder() throws JSQLParserException {
JsonFunction f = new JsonFunction();
f.setType(JsonFunctionType.OBJECT);

JsonKeyValuePair keyValuePair1 = new JsonKeyValuePair("foo", "bar", false, false);
keyValuePair1.setUsingKeyKeyword(true);
keyValuePair1.setUsingValueKeyword(true);
f.add(keyValuePair1.withUsingFormatJson(true));

JsonKeyValuePair keyValuePair2 = new JsonKeyValuePair("foo", "bar", false, false).withUsingKeyKeyword(true).withUsingValueKeyword(true).withUsingFormatJson(false);

// this should work because we compare based on KEY only
Assert.assertEquals(keyValuePair1, keyValuePair2);

// this must fail because all the properties are considered
Assert.assertFalse(Objects.equals(keyValuePair1.toString(), keyValuePair2.toString()));

f.add(keyValuePair2);
}

@Test
public void testArrayBuilder() throws JSQLParserException {
JsonFunction f = new JsonFunction();
f.setType(JsonFunctionType.ARRAY);

JsonFunctionExpression expression1 = new JsonFunctionExpression(new NullValue());
expression1.setUsingFormatJson(true);

JsonFunctionExpression expression2 = new JsonFunctionExpression(new NullValue()).withUsingFormatJson(
true);

Assert.assertTrue(Objects.equals(expression1.toString(), expression2.toString()));

f.add(expression1);
f.add(expression2);
}

@Test
public void testArrayAgg() throws JSQLParserException {
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -677,5 +677,26 @@ public void testUsing() throws JSQLParserException {
assertTrue(tableList.contains("A"));
assertTrue(tableList.contains("B.C"));
}

@Test
public void testJsonFunction() throws JSQLParserException {
String sql = "SELECT JSON_ARRAY( 1, 2, 3 ) FROM mytbl";
Statement stmt = CCJSqlParserUtil.parse(sql);
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(stmt);
assertEquals(1, tableList.size());
assertTrue(tableList.contains("mytbl"));
}

@Test
public void testJsonAggregateFunction() throws JSQLParserException {
String sql = "SELECT JSON_ARRAYAGG( (SELECT * from dual) FORMAT JSON) FROM mytbl";
Statement stmt = CCJSqlParserUtil.parse(sql);
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(stmt);
assertEquals(2, tableList.size());
assertTrue(tableList.contains("dual"));
assertTrue(tableList.contains("mytbl"));
}

}

0 comments on commit 57ed8f9

Please sign in to comment.