Skip to content

Commit a78ab94

Browse files
committed
signed expressions tests improved
1 parent 9b8d727 commit a78ab94

File tree

4 files changed

+103
-23
lines changed

4 files changed

+103
-23
lines changed

src/main/java/net/sf/jsqlparser/expression/SignedExpression.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,26 @@
2626
*/
2727
public class SignedExpression implements Expression {
2828

29-
private char sign;
30-
private Expression expression;
29+
private char sign;
30+
private Expression expression;
3131

3232
public SignedExpression(char sign, Expression expression) {
33-
this.sign = sign;
34-
setExpression(expression);
33+
setSign(sign);
34+
setExpression(expression);
3535
}
3636

37-
public char getSign() {
38-
return sign;
39-
}
37+
public char getSign() {
38+
return sign;
39+
}
4040

41-
public void setSign(char sign) {
42-
this.sign = sign;
43-
}
41+
public final void setSign(char sign) {
42+
this.sign = sign;
43+
if (sign != '+' && sign != '-') {
44+
throw new IllegalArgumentException("illegal sign character, only + - allowed");
45+
}
46+
}
4447

45-
public Expression getExpression() {
48+
public Expression getExpression() {
4649
return expression;
4750
}
4851

@@ -55,8 +58,8 @@ public void accept(ExpressionVisitor expressionVisitor) {
5558
expressionVisitor.visit(this);
5659
}
5760

58-
@Override
59-
public String toString() {
60-
return getSign() + expression.toString();
61-
}
61+
@Override
62+
public String toString() {
63+
return getSign() + expression.toString();
64+
}
6265
}

src/main/java/net/sf/jsqlparser/statement/create/index/CreateIndex.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
* Copyright (C) 2004 - 2013 JSQLParser
66
* %%
77
* This program is free software: you can redistribute it and/or modify
8-
* it under the terms of the GNU Lesser General Public License as
9-
* published by the Free Software Foundation, either version 2.1 of the
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
1010
* License, or (at your option) any later version.
11-
*
11+
*
1212
* This program is distributed in the hope that it will be useful,
1313
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1515
* GNU General Lesser Public License for more details.
16-
*
17-
* You should have received a copy of the GNU General Lesser Public
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
1818
* License along with this program. If not, see
1919
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
2020
* #L%
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (C) 2014 JSQLParser.
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+
* MA 02110-1301 USA
18+
*/
19+
20+
package net.sf.jsqlparser.expression;
21+
22+
import net.sf.jsqlparser.JSQLParserException;
23+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
24+
import org.junit.After;
25+
import org.junit.AfterClass;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
import static org.junit.Assert.*;
30+
31+
/**
32+
*
33+
* @author toben
34+
*/
35+
public class SignedExpressionTest {
36+
37+
public SignedExpressionTest() {
38+
}
39+
40+
@BeforeClass
41+
public static void setUpClass() {
42+
}
43+
44+
@AfterClass
45+
public static void tearDownClass() {
46+
}
47+
48+
@Before
49+
public void setUp() {
50+
}
51+
52+
@After
53+
public void tearDown() {
54+
}
55+
56+
/**
57+
* Test of getSign method, of class SignedExpression.
58+
*/
59+
@Test(expected = IllegalArgumentException.class)
60+
public void testGetSign() throws JSQLParserException {
61+
new SignedExpression('*', CCJSqlParserUtil.parseExpression("a"));
62+
fail("must not work");
63+
}
64+
}

src/test/java/net/sf/jsqlparser/test/select/SelectTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,34 @@ public void testAllColumnsFromTable() throws Exception {
138138
assertStatementCanBeDeparsedAs(select, statement);
139139
}
140140

141-
public void testSimpleSigns() throws Exception {
141+
public void testSimpleSigns() throws JSQLParserException {
142142
final String statement = "SELECT +1, -1 FROM tableName";
143143
Select select = (Select) parserManager.parse(new StringReader(statement));
144144

145145
assertStatementCanBeDeparsedAs(select, statement);
146146
}
147147

148-
public void testSimpleAdditionsAndSubtractions() throws Exception {
148+
public void testSimpleAdditionsAndSubtractionsWithSigns() throws JSQLParserException {
149149
final String statement = "SELECT 1 - 1, 1 + 1, -1 - 1, -1 + 1, +1 + 1, +1 - 1 FROM tableName";
150150
Select select = (Select) parserManager.parse(new StringReader(statement));
151151

152152
assertStatementCanBeDeparsedAs(select, statement);
153153
}
154+
155+
public void testOperationsWithSigns() throws JSQLParserException {
156+
Expression expr = CCJSqlParserUtil.parseExpression("1 - -1");
157+
assertEquals("1 - -1", expr.toString());
158+
assertTrue(expr instanceof Subtraction);
159+
Subtraction sub = (Subtraction) expr;
160+
assertTrue(sub.getLeftExpression() instanceof LongValue);
161+
assertTrue(sub.getRightExpression() instanceof SignedExpression);
162+
163+
SignedExpression sexpr = (SignedExpression) sub.getRightExpression();
164+
assertEquals("-", sexpr.getSign());
165+
assertEquals("1", sexpr.getExpression().toString());
166+
}
154167

155-
public void testSignedColumns() throws Exception {
168+
public void testSignedColumns() throws JSQLParserException {
156169
final String statement = "SELECT -columnName, +columnName, +(columnName), -(columnName) FROM tableName";
157170
Select select = (Select) parserManager.parse(new StringReader(statement));
158171

0 commit comments

Comments
 (0)