You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2015/11/17 16:04:22 UTC
[12/23] olingo-odata4 git commit: [OLINGO-568] search parser tests
refactoring
[OLINGO-568] search parser tests refactoring
Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/22d152fc
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/22d152fc
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/22d152fc
Branch: refs/heads/master
Commit: 22d152fc4c1681f91b2b4e545c005873e655290c
Parents: ba5220a
Author: Christian Amend <ch...@sap.com>
Authored: Thu Nov 12 13:58:11 2015 +0100
Committer: Christian Amend <ch...@sap.com>
Committed: Thu Nov 12 13:58:38 2015 +0100
----------------------------------------------------------------------
.../queryoption/search/SearchExpression.java | 14 +-
.../uri/parser/search/SearchBinaryImpl.java | 2 +-
.../uri/parser/search/SearchExpressionImpl.java | 58 +++++
.../core/uri/parser/search/SearchParser.java | 10 +-
.../core/uri/parser/search/SearchTermImpl.java | 4 +-
.../core/uri/parser/search/SearchUnaryImpl.java | 2 +-
.../search/SearchParserAndTokenizerTest.java | 171 ++++++++++++
.../uri/parser/search/SearchParserTest.java | 260 ++++++++++---------
.../uri/parser/search/SearchTokenizerTest.java | 2 +-
9 files changed, 387 insertions(+), 136 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22d152fc/lib/server-api/src/main/java/org/apache/olingo/server/api/uri/queryoption/search/SearchExpression.java
----------------------------------------------------------------------
diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/uri/queryoption/search/SearchExpression.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/uri/queryoption/search/SearchExpression.java
index ed66f5f..983919c 100644
--- a/lib/server-api/src/main/java/org/apache/olingo/server/api/uri/queryoption/search/SearchExpression.java
+++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/uri/queryoption/search/SearchExpression.java
@@ -19,5 +19,17 @@
package org.apache.olingo.server.api.uri.queryoption.search;
public interface SearchExpression {
- //No additional methods needed for now.
+
+ boolean isSearchTerm();
+
+ SearchTerm asSearchTerm();
+
+ boolean isSearchBinary();
+
+ SearchBinary asSearchBinary();
+
+ boolean isSearchUnary();
+
+ SearchUnary asSearchUnary();
+
}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22d152fc/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchBinaryImpl.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchBinaryImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchBinaryImpl.java
index 37b03c0..418d9e7 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchBinaryImpl.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchBinaryImpl.java
@@ -22,7 +22,7 @@ import org.apache.olingo.server.api.uri.queryoption.search.SearchBinary;
import org.apache.olingo.server.api.uri.queryoption.search.SearchBinaryOperatorKind;
import org.apache.olingo.server.api.uri.queryoption.search.SearchExpression;
-public class SearchBinaryImpl implements SearchBinary {
+public class SearchBinaryImpl extends SearchExpressionImpl implements SearchBinary {
private final SearchBinaryOperatorKind operator;
private final SearchExpression left;
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22d152fc/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchExpressionImpl.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchExpressionImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchExpressionImpl.java
new file mode 100644
index 0000000..ee5a197
--- /dev/null
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchExpressionImpl.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.server.core.uri.parser.search;
+
+import org.apache.olingo.server.api.uri.queryoption.search.SearchBinary;
+import org.apache.olingo.server.api.uri.queryoption.search.SearchExpression;
+import org.apache.olingo.server.api.uri.queryoption.search.SearchTerm;
+import org.apache.olingo.server.api.uri.queryoption.search.SearchUnary;
+
+public abstract class SearchExpressionImpl implements SearchExpression {
+
+ @Override
+ public boolean isSearchTerm() {
+ return this instanceof SearchTerm;
+ }
+
+ @Override
+ public SearchTerm asSearchTerm() {
+ return isSearchTerm() ? (SearchTerm) this : null;
+ }
+
+ @Override
+ public boolean isSearchBinary() {
+ return this instanceof SearchBinary;
+ }
+
+ @Override
+ public SearchBinary asSearchBinary() {
+ return isSearchBinary() ? (SearchBinary) this : null;
+ }
+
+ @Override
+ public boolean isSearchUnary() {
+ return this instanceof SearchUnary;
+ }
+
+ @Override
+ public SearchUnary asSearchUnary() {
+ return isSearchUnary() ? (SearchUnary) this : null;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22d152fc/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchParser.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchParser.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchParser.java
index 60fcd4b..5e26c35 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchParser.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchParser.java
@@ -28,8 +28,8 @@ import java.util.Iterator;
public class SearchParser {
- private Iterator<SearchQueryToken> tokens;
- private SearchExpression root;
+ protected Iterator<SearchQueryToken> tokens;
+ protected SearchExpression root;
// private SearchQueryToken currentToken;
public SearchOption parse(String path, String value) {
@@ -37,7 +37,7 @@ public class SearchParser {
try {
tokens = tokenizer.tokenize(value).iterator();
// currentToken = tokens.next();
- root = processSearchExpression();
+ root = processTokens();
} catch (SearchTokenizerException e) {
return null;
}
@@ -46,8 +46,10 @@ public class SearchParser {
return searchOption;
}
- private SearchExpression processSearchExpression() {
+ protected SearchExpression processTokens() {
SearchQueryToken token = nextToken();
+
+
if(token.getToken() == SearchQueryToken.Token.OPEN) {
throw illegalState();
} else if(token.getToken() == SearchQueryToken.Token.NOT) {
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22d152fc/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchTermImpl.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchTermImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchTermImpl.java
index 24b1291..efd7280 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchTermImpl.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchTermImpl.java
@@ -20,7 +20,7 @@ package org.apache.olingo.server.core.uri.parser.search;
import org.apache.olingo.server.api.uri.queryoption.search.SearchTerm;
-public class SearchTermImpl implements SearchTerm {
+public class SearchTermImpl extends SearchExpressionImpl implements SearchTerm {
private final String term;
public SearchTermImpl(String term) {
@@ -34,6 +34,6 @@ public class SearchTermImpl implements SearchTerm {
@Override
public String toString() {
- return "{'" + term + "'}";
+ return "'" + term + "'";
}
}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22d152fc/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchUnaryImpl.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchUnaryImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchUnaryImpl.java
index 639540d..51e3a24 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchUnaryImpl.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/search/SearchUnaryImpl.java
@@ -22,7 +22,7 @@ import org.apache.olingo.server.api.uri.queryoption.search.SearchTerm;
import org.apache.olingo.server.api.uri.queryoption.search.SearchUnary;
import org.apache.olingo.server.api.uri.queryoption.search.SearchUnaryOperatorKind;
-public class SearchUnaryImpl implements SearchUnary {
+public class SearchUnaryImpl extends SearchExpressionImpl implements SearchUnary {
private final SearchTerm operand;
public SearchUnaryImpl(SearchTerm operand) {
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22d152fc/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchParserAndTokenizerTest.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchParserAndTokenizerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchParserAndTokenizerTest.java
new file mode 100644
index 0000000..ca9d80b
--- /dev/null
+++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchParserAndTokenizerTest.java
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.server.core.uri.parser.search;
+
+import static org.apache.olingo.server.api.uri.queryoption.search.SearchBinaryOperatorKind.AND;
+import static org.apache.olingo.server.api.uri.queryoption.search.SearchBinaryOperatorKind.OR;
+
+import java.lang.reflect.Field;
+
+import org.apache.olingo.server.api.uri.queryoption.SearchOption;
+import org.apache.olingo.server.api.uri.queryoption.search.SearchExpression;
+import org.apache.olingo.server.api.uri.queryoption.search.SearchUnary;
+import org.junit.Assert;
+import org.junit.Ignore;
+import org.junit.Test;
+
+public class SearchParserAndTokenizerTest {
+
+ @Test
+ public void basicParsing() throws SearchTokenizerException {
+ SearchExpressionValidator.init("a")
+ .validate(with("a"));
+ SearchExpressionValidator.init("a AND b")
+ .validate(with("a", and("b")));
+ SearchExpressionValidator.init("a AND b AND c")
+ .validate(with("a", and("b", and("c"))));
+ SearchExpressionValidator.init("a OR b")
+ .validate(with("a", or("b")));
+ SearchExpressionValidator.init("a OR b OR c")
+ .validate(with("a", or("b", or("c"))));
+ }
+
+ @Test
+ @Ignore("Currently not working")
+ public void mixedParsing() throws Exception {
+ SearchExpressionValidator.init("a AND b OR c")
+ .validate(with("c", or("a", and("b"))));
+ }
+
+ @Ignore
+ @Test
+ public void sebuilder() {
+ System.out.println(with("c", or("a", and("b"))).toString());
+ System.out.println(with("a", and("b", and("c"))).toString());
+ System.out.println(with("a").toString());
+ System.out.println(with(not("a")).toString());
+ System.out.println(with("a", and("b")).toString());
+ System.out.println(with("a", or("b")).toString());
+ System.out.println(with("a", and(not("b"))).toString());
+ }
+
+ private static SearchExpression with(String term) {
+ return new SearchTermImpl(term);
+ }
+
+ private static SearchExpression with(String left, SearchExpression right) {
+ setLeftField(left, right);
+ return right;
+ }
+
+ private static SearchUnary with(SearchUnary unary) {
+ return unary;
+ }
+
+ private static SearchExpression or(String left, SearchExpression right) {
+ SearchExpression or = or(right);
+ setLeftField(left, right);
+ return or;
+ }
+
+ private static SearchExpression and(String left, SearchExpression right) {
+ SearchExpression and = and(right);
+ setLeftField(left, right);
+ return and;
+ }
+
+ private static SearchExpression or(SearchExpression right) {
+ return new SearchBinaryImpl(null, OR, right);
+ }
+
+ private static SearchExpression and(SearchExpression right) {
+ return new SearchBinaryImpl(null, AND, right);
+ }
+
+ private static SearchExpression and(String right) {
+ return and(new SearchTermImpl(right));
+ }
+
+ private static SearchExpression or(String right) {
+ return or(new SearchTermImpl(right));
+ }
+
+ private static SearchUnary not(String term) {
+ return new SearchUnaryImpl(new SearchTermImpl(term));
+ }
+
+ private static void setLeftField(String left, SearchExpression se) {
+ try {
+ Field field = null;
+ if (se instanceof SearchUnaryImpl) {
+ field = SearchBinaryImpl.class.getDeclaredField("operand");
+ } else if (se instanceof SearchBinaryImpl) {
+ field = SearchBinaryImpl.class.getDeclaredField("left");
+ } else {
+ Assert.fail("Unexpected exception: " + se.getClass());
+ }
+ field.setAccessible(true);
+ field.set(se, new SearchTermImpl(left));
+ } catch (Exception e) {
+ Assert.fail("Unexpected exception: " + e.getClass());
+ }
+ }
+
+ private static class SearchExpressionValidator {
+ private boolean log;
+ private final String searchQuery;
+
+ private SearchExpressionValidator(String searchQuery) {
+ this.searchQuery = searchQuery;
+ }
+
+ private static SearchExpressionValidator init(String searchQuery) {
+ return new SearchExpressionValidator(searchQuery);
+ }
+
+ private SearchExpressionValidator enableLogging() {
+ log = true;
+ return this;
+ }
+
+ private void validate(Class<? extends Exception> exception) throws SearchTokenizerException {
+ try {
+ new SearchTokenizer().tokenize(searchQuery);
+ } catch (Exception e) {
+ Assert.assertEquals(exception, e.getClass());
+ return;
+ }
+ Assert.fail("Expected exception " + exception.getClass().getSimpleName() + " was not thrown.");
+ }
+
+ private void validate(SearchExpression expectedSearchExpression) throws SearchTokenizerException {
+ SearchParser tokenizer = new SearchParser();
+ SearchOption result = tokenizer.parse(null, searchQuery);
+ Assert.assertNotNull(result);
+ final SearchExpression searchExpression = result.getSearchExpression();
+ Assert.assertNotNull(searchExpression);
+ if (log) {
+ System.out.println(expectedSearchExpression);
+ }
+ Assert.assertEquals(expectedSearchExpression.toString(), searchExpression.toString());
+ }
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22d152fc/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchParserTest.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchParserTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchParserTest.java
index fa42293..961663c 100644
--- a/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchParserTest.java
+++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchParserTest.java
@@ -6,9 +6,9 @@
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -18,151 +18,159 @@
*/
package org.apache.olingo.server.core.uri.parser.search;
-import org.apache.olingo.server.api.uri.queryoption.SearchOption;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.olingo.server.api.uri.queryoption.search.SearchBinaryOperatorKind;
import org.apache.olingo.server.api.uri.queryoption.search.SearchExpression;
-import org.apache.olingo.server.api.uri.queryoption.search.SearchUnary;
-import org.junit.Assert;
+import org.apache.olingo.server.core.uri.parser.search.SearchQueryToken.Token;
import org.junit.Ignore;
import org.junit.Test;
-import java.lang.reflect.Field;
-
-import static org.apache.olingo.server.api.uri.queryoption.search.SearchBinaryOperatorKind.*;
-
-public class SearchParserTest {
+public class SearchParserTest extends SearchParser {
@Test
- public void basicParsing() throws SearchTokenizerException {
- SearchExpressionValidator.init("a")
- .validate(with("a"));
- SearchExpressionValidator.init("a AND b")
- .validate(with("a", and("b")));
- SearchExpressionValidator.init("a AND b AND c")
- .validate(with("a", and("b", and("c"))));
- SearchExpressionValidator.init("a OR b")
- .validate(with("a", or("b")));
- SearchExpressionValidator.init("a OR b OR c")
- .validate(with("a", or("b", or("c"))));
+ public void simple() {
+ SearchExpression se = run(Token.WORD);
+ assertEquals("'word1'", se.toString());
+ assertTrue(se.isSearchTerm());
+ assertEquals("word1", se.asSearchTerm().getSearchTerm());
+
+ se = run(Token.PHRASE);
+ assertEquals("'phrase1'", se.toString());
+ assertTrue(se.isSearchTerm());
+ //TODO: Check if quotation marks should be part of the string we deliver
+ assertEquals("phrase1", se.asSearchTerm().getSearchTerm());
}
@Test
- @Ignore("Currently not working")
- public void mixedParsing() throws Exception{
- SearchExpressionValidator.init("a AND b OR c")
- .validate(with("c", or("a", and("b"))));
+ public void simpleAnd() {
+ SearchExpression se = run(Token.WORD, Token.AND, Token.WORD);
+ assertEquals("{'word1' AND 'word2'}", se.toString());
+ assertTrue(se.isSearchBinary());
+ assertEquals(SearchBinaryOperatorKind.AND, se.asSearchBinary().getOperator());
+ assertEquals("word1", se.asSearchBinary().getLeftOperand().asSearchTerm().getSearchTerm());
+ assertEquals("word2", se.asSearchBinary().getRightOperand().asSearchTerm().getSearchTerm());
+
+ se = run(Token.PHRASE, Token.AND, Token.PHRASE);
+ assertEquals("{'phrase1' AND 'phrase2'}", se.toString());
+ assertTrue(se.isSearchBinary());
+ assertEquals(SearchBinaryOperatorKind.AND, se.asSearchBinary().getOperator());
+ assertEquals("phrase1", se.asSearchBinary().getLeftOperand().asSearchTerm().getSearchTerm());
+ assertEquals("phrase2", se.asSearchBinary().getRightOperand().asSearchTerm().getSearchTerm());
}
-
+
@Test
- @Ignore("Internal test, to be deleted")
- public void sebuilder() {
- System.out.println(with("c", or("a", and("b"))).toString());
- System.out.println(with("a", and("b", and("c"))).toString());
- System.out.println(with("a").toString());
- System.out.println(with(not("a")).toString());
- System.out.println(with("a", and("b")).toString());
- System.out.println(with("a", or("b")).toString());
- System.out.println(with("a", and(not("b"))).toString());
- }
-
- private static SearchExpression with(String term) {
- return new SearchTermImpl(term);
- }
-
-
- private static SearchExpression with(String left, SearchExpression right) {
- setLeftField(left, right);
- return right;
+ public void simpleOr() {
+ SearchExpression se = run(Token.WORD, Token.OR, Token.WORD);
+ assertEquals("{'word1' OR 'word2'}", se.toString());
+ assertTrue(se.isSearchBinary());
+ assertEquals(SearchBinaryOperatorKind.OR, se.asSearchBinary().getOperator());
+ assertEquals("word1", se.asSearchBinary().getLeftOperand().asSearchTerm().getSearchTerm());
+ assertEquals("word2", se.asSearchBinary().getRightOperand().asSearchTerm().getSearchTerm());
+
+ se = run(Token.PHRASE, Token.OR, Token.PHRASE);
+ assertEquals("{'phrase1' OR 'phrase2'}", se.toString());
+ assertTrue(se.isSearchBinary());
+ assertEquals(SearchBinaryOperatorKind.OR, se.asSearchBinary().getOperator());
+ assertEquals("phrase1", se.asSearchBinary().getLeftOperand().asSearchTerm().getSearchTerm());
+ assertEquals("phrase2", se.asSearchBinary().getRightOperand().asSearchTerm().getSearchTerm());
}
-
- private static SearchUnary with(SearchUnary unary) {
- return unary;
- }
-
- private static SearchExpression or(String left, SearchExpression right) {
- SearchExpression or = or(right);
- setLeftField(left, right);
- return or;
- }
-
- private static SearchExpression and(String left, SearchExpression right) {
- SearchExpression and = and(right);
- setLeftField(left, right);
- return and;
- }
-
- private static SearchExpression or(SearchExpression right) {
- return new SearchBinaryImpl(null, OR, right);
+
+ @Ignore
+ @Test
+ public void simpleImplicitAnd() {
+ SearchExpression se = run(Token.WORD, Token.WORD);
+ assertEquals("{'word1' AND 'word2'}", se.toString());
+ assertTrue(se.isSearchBinary());
+ assertEquals(SearchBinaryOperatorKind.AND, se.asSearchBinary().getOperator());
+ assertEquals("word1", se.asSearchBinary().getLeftOperand().asSearchTerm().getSearchTerm());
+ assertEquals("word2", se.asSearchBinary().getRightOperand().asSearchTerm().getSearchTerm());
+
+ se = run(Token.PHRASE, Token.PHRASE);
+ assertEquals("{'phrase1' AND 'phrase2'}", se.toString());
+ assertTrue(se.isSearchBinary());
+ assertEquals(SearchBinaryOperatorKind.AND, se.asSearchBinary().getOperator());
+ assertEquals("phrase1", se.asSearchBinary().getLeftOperand().asSearchTerm().getSearchTerm());
+ assertEquals("phrase2", se.asSearchBinary().getRightOperand().asSearchTerm().getSearchTerm());
}
-
- private static SearchExpression and(SearchExpression right) {
- return new SearchBinaryImpl(null, AND, right);
+
+ @Ignore
+ @Test
+ public void simpleBrackets() {
+ SearchExpression se = run(Token.OPEN, Token.WORD, Token.CLOSE);
+ assertEquals("'word1'", se.toString());
+ assertTrue(se.isSearchTerm());
+ assertEquals("word1", se.asSearchTerm().getSearchTerm());
+
+ se = run(Token.OPEN, Token.PHRASE, Token.CLOSE);
+ assertEquals("'phrase1'", se.toString());
+ assertTrue(se.isSearchTerm());
+ assertEquals("phrase1", se.asSearchTerm().getSearchTerm());
}
-
- private static SearchExpression and(String right) {
- return and(new SearchTermImpl(right));
+
+ @Ignore
+ @Test
+ public void simpleNot() {
+ SearchExpression se = run(Token.NOT, Token.WORD);
+ assertEquals("{NOT 'word1'}", se.toString());
+ assertTrue(se.isSearchUnary());
+ assertEquals("word1", se.asSearchUnary().getOperand().asSearchTerm().getSearchTerm());
+
+ se = run(Token.NOT, Token.WORD);
+ assertEquals("'phrase1'", se.toString());
+ assertTrue(se.isSearchUnary());
+ assertEquals("phrase1", se.asSearchUnary().getOperand().asSearchTerm().getSearchTerm());
}
-
- private static SearchExpression or(String right) {
- return or(new SearchTermImpl(right));
+
+ @Ignore
+ @Test
+ public void precedenceLast() {
+ //word1 AND (word2 AND word3)
+ SearchExpression se = run(Token.WORD, Token.AND, Token.OPEN, Token.WORD, Token.AND, Token.WORD, Token.CLOSE);
+ assertEquals("{'word1' AND {'word2' AND 'word3'}}", se.toString());
}
-
- private static SearchUnary not(String term) {
- return new SearchUnaryImpl(new SearchTermImpl(term));
+
+ @Ignore
+ @Test
+ public void precedenceFirst() {
+ //(word1 AND word2) AND word3
+ SearchExpression se = run(Token.OPEN, Token.WORD, Token.AND, Token.WORD, Token.CLOSE, Token.AND, Token.WORD);
+ assertEquals("{{'word1' AND 'word2'} AND 'word3'}", se.toString());
}
-
- private static void setLeftField(String left, SearchExpression se) {
- try {
- Field field = null;
- if(se instanceof SearchUnaryImpl) {
- field = SearchBinaryImpl.class.getDeclaredField("operand");
- } else if(se instanceof SearchBinaryImpl) {
- field = SearchBinaryImpl.class.getDeclaredField("left");
- } else {
- Assert.fail("Unexpected exception: " + se.getClass());
- }
- field.setAccessible(true);
- field.set(se, new SearchTermImpl(left));
- } catch (Exception e) {
- Assert.fail("Unexpected exception: " + e.getClass());
- }
+
+
+ private SearchExpression run(SearchQueryToken.Token... tokenArray) {
+ List<SearchQueryToken> tokenList = prepareTokens(tokenArray);
+ tokens = tokenList.iterator();
+ SearchExpression se = processTokens();
+ assertNotNull(se);
+ return se;
}
- private static class SearchExpressionValidator {
- private boolean log;
- private final String searchQuery;
-
- private SearchExpressionValidator(String searchQuery) {
- this.searchQuery = searchQuery;
- }
-
- private static SearchExpressionValidator init(String searchQuery) {
- return new SearchExpressionValidator(searchQuery);
- }
- private SearchExpressionValidator enableLogging() {
- log = true;
- return this;
- }
- private void validate(Class<? extends Exception> exception) throws SearchTokenizerException {
- try {
- new SearchTokenizer().tokenize(searchQuery);
- } catch (Exception e) {
- Assert.assertEquals(exception, e.getClass());
- return;
- }
- Assert.fail("Expected exception " + exception.getClass().getSimpleName() + " was not thrown.");
- }
-
- private void validate(SearchExpression expectedSearchExpression) throws SearchTokenizerException {
- SearchParser tokenizer = new SearchParser();
- SearchOption result = tokenizer.parse(null, searchQuery);
- Assert.assertNotNull(result);
- final SearchExpression searchExpression = result.getSearchExpression();
- Assert.assertNotNull(searchExpression);
- if(log) {
- System.out.println(expectedSearchExpression);
+ public List<SearchQueryToken> prepareTokens(SearchQueryToken.Token... tokenArray) {
+ ArrayList<SearchQueryToken> tokenList = new ArrayList<SearchQueryToken>();
+ int wordNumber = 1;
+ int phraseNumber = 1;
+ for (int i = 0; i < tokenArray.length; i++) {
+ SearchQueryToken token = mock(SearchQueryToken.class);
+ when(token.getToken()).thenReturn(tokenArray[i]);
+ if (tokenArray[i] == Token.WORD) {
+ when(token.getLiteral()).thenReturn("word" + wordNumber);
+ wordNumber++;
+ } else if (tokenArray[i] == Token.PHRASE) {
+ when(token.getLiteral()).thenReturn("phrase" + phraseNumber);
+ phraseNumber++;
}
- Assert.assertEquals(expectedSearchExpression.toString(), searchExpression.toString());
+ tokenList.add(token);
}
+ return tokenList;
}
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22d152fc/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchTokenizerTest.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchTokenizerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchTokenizerTest.java
index 828b4c4..904bd3f 100644
--- a/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchTokenizerTest.java
+++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/search/SearchTokenizerTest.java
@@ -33,7 +33,7 @@ public class SearchTokenizerTest {
public void parseBasics() throws Exception {
SearchTokenizer tokenizer = new SearchTokenizer();
List<SearchQueryToken> result;
-
+
//
result = tokenizer.tokenize("abc");
Assert.assertNotNull(result);