http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7e23bf8/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriTokenizer.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriTokenizer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriTokenizer.java index 87e09ad..8051573 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriTokenizer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriTokenizer.java @@ -39,7 +39,11 @@ public class UriTokenizer { CLOSE, COMMA, SEMI, + DOT, + SLASH, EQ, + STAR, + PLUS, NULL, // variable-value tokens (convention: mixed case) @@ -63,18 +67,18 @@ public class UriTokenizer { jsonArrayOrObject } - private final String pathSegment; + private final String parseString; private int startIndex = 0; private int index = 0; - public UriTokenizer(final String pathSegment) { - this.pathSegment = pathSegment == null ? "" : pathSegment; + public UriTokenizer(final String parseString) { + this.parseString = parseString == null ? "" : parseString; } /** Returns the string value corresponding to the last successful {@link #next(TokenKind)} call. */ public String getText() { - return pathSegment.substring(startIndex, index); + return parseString.substring(startIndex, index); } /** @@ -119,14 +123,26 @@ public class UriTokenizer { case SEMI: found = nextCharacter(';'); break; + case DOT: + found = nextCharacter('.'); + break; + case SLASH: + found = nextCharacter('/'); + break; case EQ: found = nextCharacter('='); break; + case STAR: + found = nextCharacter('*'); + break; + case PLUS: + found = nextCharacter('+'); + break; case NULL: found = nextConstant("null"); break; case EOF: - found = index >= pathSegment.length(); + found = index >= parseString.length(); break; // Identifiers @@ -192,8 +208,12 @@ public class UriTokenizer { return found; } + /** + * Moves past the given string constant if found; otherwise leaves the index unchanged. + * @return whether the constant has been found at the current index + */ private boolean nextConstant(final String constant) { - if (pathSegment.startsWith(constant, index)) { + if (parseString.startsWith(constant, index)) { index += constant.length(); return true; } else { @@ -201,10 +221,14 @@ public class UriTokenizer { } } + /** + * Moves past the given string constant, ignoring case, if found; otherwise leaves the index unchanged. + * @return whether the constant has been found at the current index + */ private boolean nextConstantIgnoreCase(final String constant) { final int length = constant.length(); - if (index + length <= pathSegment.length() - && constant.equalsIgnoreCase(pathSegment.substring(index, index + length))) { + if (index + length <= parseString.length() + && constant.equalsIgnoreCase(parseString.substring(index, index + length))) { index += length; return true; } else { @@ -217,7 +241,7 @@ public class UriTokenizer { * @return whether the given character has been found at the current index */ private boolean nextCharacter(final char character) { - if (index < pathSegment.length() && pathSegment.charAt(index) == character) { + if (index < parseString.length() && parseString.charAt(index) == character) { index++; return true; } else { @@ -231,8 +255,8 @@ public class UriTokenizer { * @return whether the given character has been found at the current index */ private boolean nextCharacterRange(final char from, final char to) { - if (index < pathSegment.length()) { - final char code = pathSegment.charAt(index); + if (index < parseString.length()) { + final char code = parseString.charAt(index); if (code >= from && code <= to) { index++; return true; @@ -276,16 +300,20 @@ public class UriTokenizer { return nextCharacter('+') || nextCharacter('-'); } + /** + * Moves past an OData identifier if found; otherwise leaves the index unchanged. + * @return whether an OData identifier has been found at the current index + */ private boolean nextODataIdentifier() { int count = 0; - if (index < pathSegment.length()) { - int code = pathSegment.codePointAt(index); + if (index < parseString.length()) { + int code = parseString.codePointAt(index); if (Character.isUnicodeIdentifierStart(code) || code == '_') { count++; // Unicode characters outside of the Basic Multilingual Plane are represented as two Java characters. index += Character.isSupplementaryCodePoint(code) ? 2 : 1; - while (index < pathSegment.length() && count < 128) { - code = pathSegment.codePointAt(index); + while (index < parseString.length() && count < 128) { + code = parseString.codePointAt(index); if (Character.isUnicodeIdentifierPart(code) && !Character.isISOControl(code)) { count++; // Unicode characters outside of the Basic Multilingual Plane are represented as two Java characters. @@ -299,16 +327,30 @@ public class UriTokenizer { return count > 0; } + /** + * Moves past a qualified name if found; otherwise leaves the index unchanged. + * @return whether a qualified name has been found at the current index + */ private boolean nextQualifiedName() { - int count = 0; - do { + final int lastGoodIndex = index; + if (!nextODataIdentifier()) { + return false; + } + int count = 1; + while (nextCharacter('.')) { if (nextODataIdentifier()) { count++; } else { - return false; + index--; + break; } - } while (nextCharacter('.')); - return count >= 2; + } + if (count >= 2) { + return true; + } else { + index = lastGoodIndex; + return false; + } } private boolean nextParameterAliasName() { @@ -323,12 +365,12 @@ public class UriTokenizer { if (!nextCharacter('\'')) { return false; } - while (index < pathSegment.length()) { - if (pathSegment.charAt(index) == '\'') { + while (index < parseString.length()) { + if (parseString.charAt(index) == '\'') { // If a single quote is followed by another single quote, // it represents one single quote within the string literal, // otherwise it marks the end of the string literal. - if (index + 1 < pathSegment.length() && pathSegment.charAt(index + 1) == '\'') { + if (index + 1 < parseString.length() && parseString.charAt(index + 1) == '\'') { index++; } else { break; @@ -339,7 +381,13 @@ public class UriTokenizer { return nextCharacter('\''); } + /** + * Moves past an integer value if found; otherwise leaves the index unchanged. + * @param signed whether a sign character ('+' or '-') at the beginning is allowed + * @return whether an integer value has been found at the current index + */ private boolean nextIntegerValue(final boolean signed) { + final int lastGoodIndex = index; if (signed) { nextSign(); } @@ -347,33 +395,53 @@ public class UriTokenizer { while (nextDigit()) { hasDigits = true; } - return hasDigits; + if (hasDigits) { + return true; + } else { + index = lastGoodIndex; + return false; + } } - /** Finds and returns only decimal-number tokens with a fractional part. - * Whole numbers must be found with {@link #nextIntegerValue()}. + /** + * Moves past a decimal value with a fractional part if found; otherwise leaves the index unchanged. + * Whole numbers must be found with {@link #nextIntegerValue()}. */ private boolean nextDecimalValue() { - return nextIntegerValue(true) && nextCharacter('.') && nextIntegerValue(false); + final int lastGoodIndex = index; + if (nextIntegerValue(true) && nextCharacter('.') && nextIntegerValue(false)) { + return true; + } else { + index = lastGoodIndex; + return false; + } } /** - * Finds and returns only floating-point-number tokens with an exponential part - * and the special three constants "NaN", "-INF", and "INF". - * Whole numbers must be found with {@link #nextIntegerValue()}. - * Decimal numbers must be found with {@link #nextDecimalValue()}. + * Moves past a floating-point-number value with an exponential part + * or one of the special constants "NaN", "-INF", and "INF" + * if found; otherwise leaves the index unchanged. + * Whole numbers must be found with {@link #nextIntegerValue()}. + * Decimal numbers must be found with {@link #nextDecimalValue()}. */ private boolean nextDoubleValue() { if (nextConstant("NaN") || nextConstant("-INF") || nextConstant("INF")) { return true; } else { + final int lastGoodIndex = index; if (!nextIntegerValue(true)) { return false; } if (nextCharacter('.') && !nextIntegerValue(false)) { + index = lastGoodIndex; + return false; + } + if ((nextCharacter('E') || nextCharacter('e')) && nextIntegerValue(true)) { + return true; + } else { + index = lastGoodIndex; return false; } - return (nextCharacter('E') || nextCharacter('e')) && nextIntegerValue(true); } } @@ -533,7 +601,12 @@ public class UriTokenizer { return false; } + /** + * Moves past a JSON string if found; otherwise leaves the index unchanged. + * @return whether a JSON string has been found at the current index + */ private boolean nextJsonString() { + final int lastGoodIndex = index; if (nextCharacter('"')) { do { if (nextCharacter('\\')) { @@ -541,6 +614,7 @@ public class UriTokenizer { || nextCharacter('n') || nextCharacter('f') || nextCharacter('r') || nextCharacter('"') || nextCharacter('/') || nextCharacter('\\') || nextCharacter('u') && nextHexDigit() && nextHexDigit() && nextHexDigit() && nextHexDigit())) { + index = lastGoodIndex; return false; } } else if (nextCharacter('"')) { @@ -548,16 +622,17 @@ public class UriTokenizer { } else { index++; } - } while (index < pathSegment.length()); + } while (index < parseString.length()); + index = lastGoodIndex; return false; } + index = lastGoodIndex; return false; } private boolean nextJsonValue() { return nextConstant("null") || nextConstant("true") || nextConstant("false") - // If a double or decimal number is not found, the index must be reset; the internal methods don't do that. - || next(TokenKind.PrimitiveDoubleValue) || next(TokenKind.PrimitiveDecimalValue) || nextIntegerValue(true) + || nextDoubleValue() || nextDecimalValue() || nextIntegerValue(true) || nextJsonString() || nextJsonArrayOrObject(); } @@ -566,25 +641,42 @@ public class UriTokenizer { return nextJsonString() && nextCharacter(':') && nextJsonValue(); } + /** + * Moves past a JSON array or object if found; otherwise leaves the index unchanged. + * @return whether a JSON array or object has been found at the current index + */ private boolean nextJsonArrayOrObject() { + final int lastGoodIndex = index; if (nextCharacter('[')) { if (nextJsonValue()) { while (nextCharacter(',')) { if (!nextJsonValue()) { + index = lastGoodIndex; return false; } } } - return nextCharacter(']'); + if (nextCharacter(']')) { + return true; + } else { + index = lastGoodIndex; + return false; + } } else if (nextCharacter('{')) { if (nextJsonMember()) { while (nextCharacter(',')) { if (!nextJsonMember()) { + index = lastGoodIndex; return false; } } } - return nextCharacter('}'); + if (nextCharacter('}')) { + return true; + } else { + index = lastGoodIndex; + return false; + } } else { return false; }
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7e23bf8/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/UriInfoImplTest.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/UriInfoImplTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/UriInfoImplTest.java new file mode 100644 index 0000000..4c76e96 --- /dev/null +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/UriInfoImplTest.java @@ -0,0 +1,204 @@ +/* + * 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; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; + +import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.ex.ODataRuntimeException; +import org.apache.olingo.server.api.uri.UriInfo; +import org.apache.olingo.server.api.uri.UriInfoKind; +import org.apache.olingo.server.api.uri.UriResourceAction; +import org.apache.olingo.server.api.uri.UriResourceEntitySet; +import org.apache.olingo.server.api.uri.queryoption.AliasQueryOption; +import org.apache.olingo.server.api.uri.queryoption.QueryOption; +import org.apache.olingo.server.core.uri.UriInfoImpl; +import org.apache.olingo.server.core.uri.UriResourceActionImpl; +import org.apache.olingo.server.core.uri.UriResourceEntitySetImpl; +import org.apache.olingo.server.core.uri.queryoption.AliasQueryOptionImpl; +import org.apache.olingo.server.core.uri.queryoption.CountOptionImpl; +import org.apache.olingo.server.core.uri.queryoption.CustomQueryOptionImpl; +import org.apache.olingo.server.core.uri.queryoption.ExpandOptionImpl; +import org.apache.olingo.server.core.uri.queryoption.FilterOptionImpl; +import org.apache.olingo.server.core.uri.queryoption.FormatOptionImpl; +import org.apache.olingo.server.core.uri.queryoption.IdOptionImpl; +import org.apache.olingo.server.core.uri.queryoption.LevelsOptionImpl; +import org.apache.olingo.server.core.uri.queryoption.OrderByOptionImpl; +import org.apache.olingo.server.core.uri.queryoption.SearchOptionImpl; +import org.apache.olingo.server.core.uri.queryoption.SelectOptionImpl; +import org.apache.olingo.server.core.uri.queryoption.SkipOptionImpl; +import org.apache.olingo.server.core.uri.queryoption.SkipTokenOptionImpl; +import org.apache.olingo.server.core.uri.queryoption.TopOptionImpl; +import org.junit.Test; +import org.mockito.Mockito; + +public class UriInfoImplTest { + + @Test + public void kind() { + final UriInfo uriInfo = new UriInfoImpl().setKind(UriInfoKind.all); + assertEquals(UriInfoKind.all, uriInfo.getKind()); + } + + @Test + public void casts() { + final UriInfo uriInfo = new UriInfoImpl(); + + assertEquals(uriInfo, uriInfo.asUriInfoAll()); + assertEquals(uriInfo, uriInfo.asUriInfoBatch()); + assertEquals(uriInfo, uriInfo.asUriInfoCrossjoin()); + assertEquals(uriInfo, uriInfo.asUriInfoEntityId()); + assertEquals(uriInfo, uriInfo.asUriInfoMetadata()); + assertEquals(uriInfo, uriInfo.asUriInfoResource()); + assertEquals(uriInfo, uriInfo.asUriInfoService()); + } + + @Test + public void entityNames() { + final UriInfo uriInfo = new UriInfoImpl() + .addEntitySetName("A") + .addEntitySetName("B"); + assertArrayEquals(new String[] { "A", "B" }, uriInfo.getEntitySetNames().toArray()); + } + + @Test + public void resourceParts() { + UriInfoImpl uriInfo = new UriInfoImpl(); + + final UriResourceAction action = new UriResourceActionImpl(); + final UriResourceEntitySet entitySet0 = new UriResourceEntitySetImpl(); + final UriResourceEntitySet entitySet1 = new UriResourceEntitySetImpl(); + + uriInfo.addResourcePart(action); + uriInfo.addResourcePart(entitySet0); + + assertEquals(action, uriInfo.getUriResourceParts().get(0)); + assertEquals(entitySet0, uriInfo.getUriResourceParts().get(1)); + + assertEquals(entitySet0, uriInfo.getLastResourcePart()); + + uriInfo.addResourcePart(entitySet1); + assertEquals(entitySet1, uriInfo.getLastResourcePart()); + } + + @Test(expected = ODataRuntimeException.class) + public void doubleSystemQueryOptions() { + new UriInfoImpl() + .setSystemQueryOption(new FormatOptionImpl()) + .setSystemQueryOption(new FormatOptionImpl()); + } + + @Test + public void customQueryOption() { + final QueryOption expand = new ExpandOptionImpl().setName(""); + final QueryOption filter = new FilterOptionImpl().setName(""); + final QueryOption format = new FormatOptionImpl().setName(""); + final QueryOption id = new IdOptionImpl().setName(""); + final QueryOption inlinecount = new CountOptionImpl().setName(""); + final QueryOption orderby = new OrderByOptionImpl().setName(""); + final QueryOption search = new SearchOptionImpl().setName(""); + final QueryOption select = new SelectOptionImpl().setName(""); + final QueryOption skip = new SkipOptionImpl().setName(""); + final QueryOption skipToken = new SkipTokenOptionImpl().setName(""); + final QueryOption top = new TopOptionImpl().setName(""); + final QueryOption levels = new LevelsOptionImpl().setName(""); + + final QueryOption customOption0 = new CustomQueryOptionImpl().setName("").setText("A"); + final QueryOption customOption1 = new CustomQueryOptionImpl().setName("").setText("B"); + + final QueryOption initialQueryOption = new CustomQueryOptionImpl(); + + final QueryOption alias = new AliasQueryOptionImpl().setName("alias").setText("C"); + + final UriInfo uriInfo = new UriInfoImpl() + .setQueryOptions(Arrays.asList( + expand, + filter, + format, + id, + inlinecount, + orderby, + search, + select, + skip, + skipToken, + top, + customOption0, + customOption1, + levels, + initialQueryOption, + alias)); + + assertEquals(12, uriInfo.getSystemQueryOptions().size()); + assertEquals(expand, uriInfo.getExpandOption()); + assertEquals(filter, uriInfo.getFilterOption()); + assertEquals(format, uriInfo.getFormatOption()); + assertEquals(id, uriInfo.getIdOption()); + assertEquals(inlinecount, uriInfo.getCountOption()); + assertEquals(orderby, uriInfo.getOrderByOption()); + assertEquals(search, uriInfo.getSearchOption()); + assertEquals(select, uriInfo.getSelectOption()); + assertEquals(skip, uriInfo.getSkipOption()); + assertEquals(skipToken, uriInfo.getSkipTokenOption()); + assertEquals(top, uriInfo.getTopOption()); + + assertArrayEquals(new QueryOption[] { alias }, uriInfo.getAliases().toArray()); + assertEquals("C", uriInfo.getValueForAlias("alias")); + + assertArrayEquals(new QueryOption[] { customOption0, customOption1, initialQueryOption }, + uriInfo.getCustomQueryOptions().toArray()); + } + + @Test + public void fragment() { + final UriInfo uriInfo = new UriInfoImpl().setFragment("F"); + assertEquals("F", uriInfo.getFragment()); + } + + @Test + public void entityTypeCast() { + final EdmEntityType entityType = Mockito.mock(EdmEntityType.class); + final UriInfo uriInfo = new UriInfoImpl() + .setEntityTypeCast(entityType); + assertEquals(entityType, uriInfo.getEntityTypeCast()); + } + + @Test + public void alias() { + final UriInfo uriInfo = new UriInfoImpl() + .addAlias((AliasQueryOption) new AliasQueryOptionImpl().setName("A").setText("notUsed")) + .addAlias((AliasQueryOption) new AliasQueryOptionImpl().setName("A").setText("X")) + .addAlias((AliasQueryOption) new AliasQueryOptionImpl().setName("B").setText("Y")) + .addAlias((AliasQueryOption) new AliasQueryOptionImpl().setName("C").setText("Z")); + + assertEquals(3, uriInfo.getAliases().size()); + assertEquals("X", uriInfo.getValueForAlias("A")); + assertEquals("Y", uriInfo.getValueForAlias("B")); + assertEquals("Z", uriInfo.getValueForAlias("C")); + assertNull(uriInfo.getValueForAlias("D")); + + assertTrue(uriInfo.getSystemQueryOptions().isEmpty()); + assertTrue(uriInfo.getCustomQueryOptions().isEmpty()); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7e23bf8/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/UriDecoderTest.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/UriDecoderTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/UriDecoderTest.java new file mode 100644 index 0000000..20ab94f --- /dev/null +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/UriDecoderTest.java @@ -0,0 +1,94 @@ +/* + * 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; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Arrays; +import java.util.List; + +import org.apache.olingo.server.api.uri.queryoption.QueryOption; +import org.junit.Test; + +public class UriDecoderTest { + + @Test + public void split() throws Exception { + assertTrue(UriDecoder.splitAndDecodePath("").isEmpty()); + assertTrue(UriDecoder.splitAndDecodePath("/").isEmpty()); + assertEquals(Arrays.asList("a"), UriDecoder.splitAndDecodePath("a")); + assertEquals(Arrays.asList("a"), UriDecoder.splitAndDecodePath("a/")); + assertEquals(Arrays.asList("a"), UriDecoder.splitAndDecodePath("/a")); + assertEquals(Arrays.asList("a", "a"), UriDecoder.splitAndDecodePath("a/a")); + assertEquals(Arrays.asList("a", "a"), UriDecoder.splitAndDecodePath("/a/a")); + } + + @Test + public void path() throws Exception { + assertEquals(Arrays.asList("a", "entitySet('/')", "bcd"), + UriDecoder.splitAndDecodePath("a/entitySet('%2F')/b%63d")); + } + + @Test + public void options() throws Exception { + assertTrue(UriDecoder.splitAndDecodeOptions("").isEmpty()); + + checkOption("a", "a", ""); + checkOption("a=b", "a", "b"); + checkOption("=", "", ""); + checkOption("=b", "", "b"); + + checkOption("a&c", "a", ""); + checkOption("a&c", "c", ""); + + checkOption("a=b&c", "a", "b"); + checkOption("a=b&c", "c", ""); + + checkOption("a=b&c=d", "a", "b"); + checkOption("a=b&c=d", "c", "d"); + + checkOption("=&=", "", ""); + assertEquals(2, UriDecoder.splitAndDecodeOptions("=&=").size()); + + checkOption("=&c=d", "", ""); + checkOption("=&c=d", "c", "d"); + + checkOption("a%62c=d%65f", "abc", "def"); + checkOption("a='%26%3D'", "a", "'&='"); + } + + @Test(expected = UriParserSyntaxException.class) + public void wrongPercentEncoding() throws Exception { + UriDecoder.splitAndDecodePath("%wrong"); + } + + private void checkOption(final String query, final String name, final String value) + throws UriParserSyntaxException { + final List<QueryOption> options = UriDecoder.splitAndDecodeOptions(query); + for (final QueryOption option : options) { + if (option.getName().equals(name)) { + assertEquals(value, option.getText()); + return; + } + } + fail("Option " + name + " not found!"); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7e23bf8/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/UriTokenizerTest.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/UriTokenizerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/UriTokenizerTest.java index a9e97ce..177a396 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/UriTokenizerTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/uri/parser/UriTokenizerTest.java @@ -51,7 +51,7 @@ public class UriTokenizerTest { @Test public void sequence() { - final UriTokenizer tokenizer = new UriTokenizer("(A=1,B=2);"); + final UriTokenizer tokenizer = new UriTokenizer("(A=1,B=2);.*/+"); assertTrue(tokenizer.next(TokenKind.OPEN)); assertFalse(tokenizer.next(TokenKind.OPEN)); assertTrue(tokenizer.next(TokenKind.ODataIdentifier)); @@ -68,6 +68,10 @@ public class UriTokenizerTest { assertFalse(tokenizer.next(TokenKind.EOF)); assertTrue(tokenizer.next(TokenKind.CLOSE)); assertTrue(tokenizer.next(TokenKind.SEMI)); + assertTrue(tokenizer.next(TokenKind.DOT)); + assertTrue(tokenizer.next(TokenKind.STAR)); + assertTrue(tokenizer.next(TokenKind.SLASH)); + assertTrue(tokenizer.next(TokenKind.PLUS)); assertTrue(tokenizer.next(TokenKind.EOF)); } @@ -100,8 +104,10 @@ public class UriTokenizerTest { public void qualifiedName() { assertTrue(new UriTokenizer("namespace.name").next(TokenKind.QualifiedName)); - final UriTokenizer tokenizer = new UriTokenizer("multi.part.namespace.name"); + final UriTokenizer tokenizer = new UriTokenizer("multi.part.namespace.name.1"); assertTrue(tokenizer.next(TokenKind.QualifiedName)); + assertTrue(tokenizer.next(TokenKind.DOT)); + assertTrue(tokenizer.next(TokenKind.PrimitiveIntegerValue)); assertTrue(tokenizer.next(TokenKind.EOF)); assertFalse(new UriTokenizer("name").next(TokenKind.QualifiedName)); @@ -334,6 +340,7 @@ public class UriTokenizerTest { assertFalse(new UriTokenizer("[,1]").next(TokenKind.jsonArrayOrObject)); assertFalse(new UriTokenizer("[1,,2]").next(TokenKind.jsonArrayOrObject)); assertFalse(new UriTokenizer("[1,x]").next(TokenKind.jsonArrayOrObject)); + assertFalse(new UriTokenizer("[+\"x\"]").next(TokenKind.jsonArrayOrObject)); assertFalse(new UriTokenizer("{\"name\":1,}").next(TokenKind.jsonArrayOrObject)); assertFalse(new UriTokenizer("{,\"name\":1}").next(TokenKind.jsonArrayOrObject)); assertFalse(new UriTokenizer("{\"name\":1,,\"name2\":2}").next(TokenKind.jsonArrayOrObject)); @@ -350,6 +357,7 @@ public class UriTokenizerTest { assertFalse(new UriTokenizer("[\"\\u1\"]").next(TokenKind.jsonArrayOrObject)); assertFalse(new UriTokenizer("[\"\\u12x\"]").next(TokenKind.jsonArrayOrObject)); assertFalse(new UriTokenizer("[\"\\u123x\"]").next(TokenKind.jsonArrayOrObject)); + wrongToken(TokenKind.jsonArrayOrObject, "[{\"name\":+123.456},null]", '\\'); } private void wrongToken(final TokenKind kind, final String value, final char disturbCharacter) { @@ -358,6 +366,7 @@ public class UriTokenizerTest { final UriTokenizer tokenizer = new UriTokenizer(value + disturbCharacter); assertTrue(tokenizer.next(kind)); assertEquals(value, tokenizer.getText()); + assertFalse(tokenizer.next(TokenKind.EOF)); // Place the disturbing character at every position in the value string // and check that this leads to a failed token recognition. http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7e23bf8/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/UriInfoImplTest.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/UriInfoImplTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/UriInfoImplTest.java deleted file mode 100644 index 20bdade..0000000 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/UriInfoImplTest.java +++ /dev/null @@ -1,212 +0,0 @@ -/* - * 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; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.util.Arrays; -import java.util.Collections; - -import org.apache.olingo.commons.api.edm.Edm; -import org.apache.olingo.commons.api.edm.EdmEntityType; -import org.apache.olingo.commons.api.ex.ODataRuntimeException; -import org.apache.olingo.server.api.OData; -import org.apache.olingo.server.api.edmx.EdmxReference; -import org.apache.olingo.server.api.uri.UriInfo; -import org.apache.olingo.server.api.uri.UriInfoKind; -import org.apache.olingo.server.api.uri.UriResourceAction; -import org.apache.olingo.server.api.uri.UriResourceEntitySet; -import org.apache.olingo.server.api.uri.queryoption.AliasQueryOption; -import org.apache.olingo.server.api.uri.queryoption.QueryOption; -import org.apache.olingo.server.core.uri.queryoption.AliasQueryOptionImpl; -import org.apache.olingo.server.core.uri.queryoption.CountOptionImpl; -import org.apache.olingo.server.core.uri.queryoption.CustomQueryOptionImpl; -import org.apache.olingo.server.core.uri.queryoption.ExpandOptionImpl; -import org.apache.olingo.server.core.uri.queryoption.FilterOptionImpl; -import org.apache.olingo.server.core.uri.queryoption.FormatOptionImpl; -import org.apache.olingo.server.core.uri.queryoption.IdOptionImpl; -import org.apache.olingo.server.core.uri.queryoption.LevelsOptionImpl; -import org.apache.olingo.server.core.uri.queryoption.OrderByOptionImpl; -import org.apache.olingo.server.core.uri.queryoption.SearchOptionImpl; -import org.apache.olingo.server.core.uri.queryoption.SelectOptionImpl; -import org.apache.olingo.server.core.uri.queryoption.SkipOptionImpl; -import org.apache.olingo.server.core.uri.queryoption.SkipTokenOptionImpl; -import org.apache.olingo.server.core.uri.queryoption.TopOptionImpl; -import org.apache.olingo.server.tecsvc.provider.EdmTechProvider; -import org.apache.olingo.server.tecsvc.provider.EntityTypeProvider; -import org.junit.Test; - -public class UriInfoImplTest { - - private static final Edm edm = OData.newInstance().createServiceMetadata( - new EdmTechProvider(), Collections.<EdmxReference> emptyList()).getEdm(); - - @Test - public void kind() { - final UriInfo uriInfo = new UriInfoImpl().setKind(UriInfoKind.all); - assertEquals(UriInfoKind.all, uriInfo.getKind()); - } - - @Test - public void casts() { - final UriInfo uriInfo = new UriInfoImpl(); - - assertEquals(uriInfo, uriInfo.asUriInfoAll()); - assertEquals(uriInfo, uriInfo.asUriInfoBatch()); - assertEquals(uriInfo, uriInfo.asUriInfoCrossjoin()); - assertEquals(uriInfo, uriInfo.asUriInfoEntityId()); - assertEquals(uriInfo, uriInfo.asUriInfoMetadata()); - assertEquals(uriInfo, uriInfo.asUriInfoResource()); - assertEquals(uriInfo, uriInfo.asUriInfoService()); - } - - @Test - public void entityNames() { - final UriInfo uriInfo = new UriInfoImpl() - .addEntitySetName("A") - .addEntitySetName("B"); - assertArrayEquals(new String[] { "A", "B" }, uriInfo.getEntitySetNames().toArray()); - } - - @Test - public void resourceParts() { - UriInfoImpl uriInfo = new UriInfoImpl(); - - final UriResourceAction action = new UriResourceActionImpl(); - final UriResourceEntitySet entitySet0 = new UriResourceEntitySetImpl(); - final UriResourceEntitySet entitySet1 = new UriResourceEntitySetImpl(); - - uriInfo.addResourcePart(action); - uriInfo.addResourcePart(entitySet0); - - assertEquals(action, uriInfo.getUriResourceParts().get(0)); - assertEquals(entitySet0, uriInfo.getUriResourceParts().get(1)); - - assertEquals(entitySet0, uriInfo.getLastResourcePart()); - - uriInfo.addResourcePart(entitySet1); - assertEquals(entitySet1, uriInfo.getLastResourcePart()); - } - - @Test(expected = ODataRuntimeException.class) - public void doubleSystemQueryOptions() { - new UriInfoImpl() - .setSystemQueryOption(new FormatOptionImpl()) - .setSystemQueryOption(new FormatOptionImpl()); - } - - @Test - public void customQueryOption() { - final QueryOption expand = new ExpandOptionImpl().setName(""); - final QueryOption filter = new FilterOptionImpl().setName(""); - final QueryOption format = new FormatOptionImpl().setName(""); - final QueryOption id = new IdOptionImpl().setName(""); - final QueryOption inlinecount = new CountOptionImpl().setName(""); - final QueryOption orderby = new OrderByOptionImpl().setName(""); - final QueryOption search = new SearchOptionImpl().setName(""); - final QueryOption select = new SelectOptionImpl().setName(""); - final QueryOption skip = new SkipOptionImpl().setName(""); - final QueryOption skipToken = new SkipTokenOptionImpl().setName(""); - final QueryOption top = new TopOptionImpl().setName(""); - final QueryOption levels = new LevelsOptionImpl().setName(""); - - final QueryOption customOption0 = new CustomQueryOptionImpl().setName("").setText("A"); - final QueryOption customOption1 = new CustomQueryOptionImpl().setName("").setText("B"); - - final QueryOption initialQueryOption = new CustomQueryOptionImpl(); - - final QueryOption alias = new AliasQueryOptionImpl().setName("alias").setText("C"); - - final UriInfo uriInfo = new UriInfoImpl() - .setQueryOptions(Arrays.asList( - expand, - filter, - format, - id, - inlinecount, - orderby, - search, - select, - skip, - skipToken, - top, - customOption0, - customOption1, - levels, - initialQueryOption, - alias)); - - assertEquals(12, uriInfo.getSystemQueryOptions().size()); - assertEquals(expand, uriInfo.getExpandOption()); - assertEquals(filter, uriInfo.getFilterOption()); - assertEquals(format, uriInfo.getFormatOption()); - assertEquals(id, uriInfo.getIdOption()); - assertEquals(inlinecount, uriInfo.getCountOption()); - assertEquals(orderby, uriInfo.getOrderByOption()); - assertEquals(search, uriInfo.getSearchOption()); - assertEquals(select, uriInfo.getSelectOption()); - assertEquals(skip, uriInfo.getSkipOption()); - assertEquals(skipToken, uriInfo.getSkipTokenOption()); - assertEquals(top, uriInfo.getTopOption()); - - assertArrayEquals(new QueryOption[] { alias }, uriInfo.getAliases().toArray()); - assertEquals("C", uriInfo.getValueForAlias("alias")); - - assertArrayEquals(new QueryOption[] { customOption0, customOption1, initialQueryOption }, - uriInfo.getCustomQueryOptions().toArray()); - } - - @Test - public void fragment() { - final UriInfo uriInfo = new UriInfoImpl().setFragment("F"); - assertEquals("F", uriInfo.getFragment()); - } - - @Test - public void entityTypeCast() { - final EdmEntityType entityType = edm.getEntityType(EntityTypeProvider.nameETKeyNav); - assertNotNull(entityType); - - final UriInfo uriInfo = new UriInfoImpl() - .setEntityTypeCast(entityType); - assertEquals(entityType, uriInfo.getEntityTypeCast()); - } - - @Test - public void alias() { - final UriInfo uriInfo = new UriInfoImpl() - .addAlias((AliasQueryOption) new AliasQueryOptionImpl().setName("A").setText("notUsed")) - .addAlias((AliasQueryOption) new AliasQueryOptionImpl().setName("A").setText("X")) - .addAlias((AliasQueryOption) new AliasQueryOptionImpl().setName("B").setText("Y")) - .addAlias((AliasQueryOption) new AliasQueryOptionImpl().setName("C").setText("Z")); - - assertEquals(3, uriInfo.getAliases().size()); - assertEquals("X", uriInfo.getValueForAlias("A")); - assertEquals("Y", uriInfo.getValueForAlias("B")); - assertEquals("Z", uriInfo.getValueForAlias("C")); - assertNull(uriInfo.getValueForAlias("D")); - - assertTrue(uriInfo.getSystemQueryOptions().isEmpty()); - assertTrue(uriInfo.getCustomQueryOptions().isEmpty()); - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7e23bf8/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java index 11acede..e9f1c07 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java @@ -40,18 +40,19 @@ import org.apache.olingo.server.api.uri.UriResourceKind; import org.apache.olingo.server.api.uri.queryoption.expression.BinaryOperatorKind; import org.apache.olingo.server.api.uri.queryoption.expression.MethodKind; import org.apache.olingo.server.core.uri.parser.UriParserException; -import org.apache.olingo.server.core.uri.parser.UriParserSemanticException; import org.apache.olingo.server.core.uri.parser.UriParserSemanticException.MessageKeys; import org.apache.olingo.server.core.uri.parser.UriParserSyntaxException; import org.apache.olingo.server.core.uri.parser.search.SearchParserException; import org.apache.olingo.server.core.uri.testutil.FilterValidator; import org.apache.olingo.server.core.uri.testutil.TestUriValidator; import org.apache.olingo.server.core.uri.validator.UriValidationException; +import org.apache.olingo.server.tecsvc.provider.ActionProvider; import org.apache.olingo.server.tecsvc.provider.ComplexTypeProvider; import org.apache.olingo.server.tecsvc.provider.ContainerProvider; import org.apache.olingo.server.tecsvc.provider.EdmTechProvider; import org.apache.olingo.server.tecsvc.provider.EntityTypeProvider; import org.apache.olingo.server.tecsvc.provider.EnumTypeProvider; +import org.apache.olingo.server.tecsvc.provider.FunctionProvider; import org.apache.olingo.server.tecsvc.provider.PropertyProvider; import org.apache.olingo.server.tecsvc.provider.TypeDefinitionProvider; import org.junit.Ignore; @@ -2731,17 +2732,6 @@ public class TestFullResourcePath { .isNavProperty("NavPropertyETKeyNavOne", EntityTypeProvider.nameETKeyNav, false) .isType(EntityTypeProvider.nameETKeyNav); - testUri.run("ESTwoKeyNav(PropertyInt16=1,PropertyString='2')", "$select=olingo.odata.test1.ETBaseTwoKeyNav" - + "/PropertyInt16") - .isKind(UriInfoKind.resource).goPath() - .first() - .isKeyPredicate(0, "PropertyInt16", "1") - .isKeyPredicate(1, "PropertyString", "'2'") - .isSelectStartType(0, EntityTypeProvider.nameETBaseTwoKeyNav) - .goSelectItem(0) - .first() - .isPrimitiveProperty("PropertyInt16", PropertyProvider.nameInt16, false); - testUri.run("ESKeyNav", "$expand=NavPropertyETKeyNavOne($select=PropertyInt16)") .isKind(UriInfoKind.resource) .goPath().first() @@ -2763,17 +2753,6 @@ public class TestFullResourcePath { .goUpExpandValidator() .isSelectText("PropertyCompNav/PropertyInt16"); - testUri.run("ESMixEnumDefCollComp", - "$select=PropertyEnumString,PropertyDefString,CollPropertyEnumString,CollPropertyDefString") - .isKind(UriInfoKind.resource) - .goSelectItemPath(0).isPrimitiveProperty("PropertyEnumString", EnumTypeProvider.nameENString, false) - .goUpUriValidator() - .goSelectItemPath(1).isPrimitiveProperty("PropertyDefString", TypeDefinitionProvider.nameTDString, false) - .goUpUriValidator() - .goSelectItemPath(2).isPrimitiveProperty("CollPropertyEnumString", EnumTypeProvider.nameENString, true) - .goUpUriValidator() - .goSelectItemPath(3).isPrimitiveProperty("CollPropertyDefString", TypeDefinitionProvider.nameTDString, true); - testUri.runEx("ESKeyNav", "$expand=undefined") .isExSemantic(MessageKeys.EXPRESSION_PROPERTY_NOT_IN_TYPE); testUri.runEx("ESTwoKeyNav", "$expand=PropertyCompNav/undefined") @@ -2815,6 +2794,134 @@ public class TestFullResourcePath { } @Test + public void select() throws Exception { + testUri.run("ESTwoKeyNav", "$select=*") + .isSelectItemStar(0); + + testUri.run("ESTwoKeyNav", "$select=olingo.odata.test1.*") + .isSelectItemAllOp(0, new FullQualifiedName("olingo.odata.test1", "*")); + testUri.run("ESTwoKeyNav", "$select=Namespace1_Alias.*") + .isSelectItemAllOp(0, new FullQualifiedName("Namespace1_Alias", "*")); + + testUri.run("ESTwoKeyNav", "$select=PropertyString") + .goSelectItemPath(0).isPrimitiveProperty("PropertyString", PropertyProvider.nameString, false); + + testUri.run("ESTwoKeyNav", "$select=PropertyComp") + .goSelectItemPath(0).isComplexProperty("PropertyComp", ComplexTypeProvider.nameCTPrimComp, false); + + testUri.run("ESAllPrim", "$select=PropertyTimeOfDay,PropertyDate,NavPropertyETTwoPrimOne") + .isKind(UriInfoKind.resource) + .goSelectItemPath(0).first().isPrimitiveProperty("PropertyTimeOfDay", PropertyProvider.nameTimeOfDay, false) + .goUpUriValidator() + .goSelectItemPath(1).first().isPrimitiveProperty("PropertyDate", PropertyProvider.nameDate, false) + .goUpUriValidator() + .goSelectItemPath(2).first().isNavProperty("NavPropertyETTwoPrimOne", EntityTypeProvider.nameETTwoPrim, false); + + testUri.run("ESMixEnumDefCollComp", + "$select=PropertyEnumString,PropertyDefString,CollPropertyEnumString,CollPropertyDefString") + .isKind(UriInfoKind.resource) + .goSelectItemPath(0).isPrimitiveProperty("PropertyEnumString", EnumTypeProvider.nameENString, false) + .goUpUriValidator() + .goSelectItemPath(1).isPrimitiveProperty("PropertyDefString", TypeDefinitionProvider.nameTDString, false) + .goUpUriValidator() + .goSelectItemPath(2).isPrimitiveProperty("CollPropertyEnumString", EnumTypeProvider.nameENString, true) + .goUpUriValidator() + .goSelectItemPath(3).isPrimitiveProperty("CollPropertyDefString", TypeDefinitionProvider.nameTDString, true); + + testUri.run("ESTwoKeyNav", "$select=PropertyComp/PropertyInt16") + .goSelectItemPath(0) + .first() + .isComplexProperty("PropertyComp", ComplexTypeProvider.nameCTPrimComp, false) + .n() + .isPrimitiveProperty("PropertyInt16", PropertyProvider.nameInt16, false); + + testUri.run("ESTwoKeyNav", "$select=PropertyComp/PropertyComp") + .goSelectItemPath(0) + .first() + .isComplexProperty("PropertyComp", ComplexTypeProvider.nameCTPrimComp, false) + .n() + .isComplexProperty("PropertyComp", ComplexTypeProvider.nameCTAllPrim, false); + + testUri.run("ESTwoKeyNav", "$select=olingo.odata.test1.ETBaseTwoKeyNav") + .isSelectStartType(0, EntityTypeProvider.nameETBaseTwoKeyNav); + + testUri.run("ESTwoKeyNav(PropertyInt16=1,PropertyString='2')", + "$select=olingo.odata.test1.ETBaseTwoKeyNav/PropertyInt16") + .isKind(UriInfoKind.resource).goPath() + .first() + .isKeyPredicate(0, "PropertyInt16", "1") + .isKeyPredicate(1, "PropertyString", "'2'") + .isSelectStartType(0, EntityTypeProvider.nameETBaseTwoKeyNav) + .goSelectItem(0) + .first() + .isPrimitiveProperty("PropertyInt16", PropertyProvider.nameInt16, false); + + testUri.run("ESTwoKeyNav(PropertyInt16=1,PropertyString='1')/PropertyCompNav", + "$select=olingo.odata.test1.CTTwoBasePrimCompNav") + .isSelectStartType(0, ComplexTypeProvider.nameCTTwoBasePrimCompNav); + + testUri.run("ESTwoKeyNav", "$select=PropertyCompNav/olingo.odata.test1.CTTwoBasePrimCompNav") + .goSelectItemPath(0) + .first() + .isComplexProperty("PropertyCompNav", ComplexTypeProvider.nameCTBasePrimCompNav, false) + .isTypeFilter(ComplexTypeProvider.nameCTTwoBasePrimCompNav); + + testUri.run("ESTwoKeyNav", "$select=PropertyCompNav/Namespace1_Alias.CTTwoBasePrimCompNav/PropertyInt16") + .goSelectItemPath(0) + .first() + .isComplexProperty("PropertyCompNav", ComplexTypeProvider.nameCTBasePrimCompNav, false) + .isTypeFilter(ComplexTypeProvider.nameCTTwoBasePrimCompNav) + .n() + .isPrimitiveProperty("PropertyInt16", PropertyProvider.nameInt16, false); + + testUri.run("ESAllPrim", "$select=olingo.odata.test1.BAESAllPrimRTETAllPrim") + .goSelectItemPath(0) + .first() + .isAction(ActionProvider.nameBAESAllPrimRTETAllPrim.getName()); + testUri.run("ESTwoKeyNav", "$select=Namespace1_Alias.BFCESTwoKeyNavRTString") + .goSelectItemPath(0) + .first() + .isFunction(FunctionProvider.nameBFCESTwoKeyNavRTString.getName()); + testUri.run("ESTwoKeyNav", "$select=olingo.odata.test1.BFCESTwoKeyNavRTStringParam(ParameterComp)") + .goSelectItemPath(0) + .first() + .isFunction(FunctionProvider.nameBFCESTwoKeyNavRTStringParam.getName()); + + testUri.runEx("ESMixPrimCollComp", "$select=wrong") + .isExSemantic(MessageKeys.EXPRESSION_PROPERTY_NOT_IN_TYPE); + testUri.runEx("ESMixPrimCollComp", "$select=PropertyComp/wrong") + .isExSemantic(MessageKeys.EXPRESSION_PROPERTY_NOT_IN_TYPE); + testUri.runEx("ESMixPrimCollComp", "$select=PropertyComp///PropertyInt16") + .isExSyntax(UriParserSyntaxException.MessageKeys.SYNTAX); + testUri.runEx("ESMixPrimCollComp", "$select=/PropertyInt16") + .isExSyntax(UriParserSyntaxException.MessageKeys.SYNTAX); + testUri.runEx("ESMixPrimCollComp", "$select=PropertyInt16+") + .isExSyntax(UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION); + testUri.runEx("ESTwoKeyNav", "$select=olingo.odata.test1.1") + .isExSemantic(MessageKeys.UNKNOWN_PART); + testUri.runEx("ESTwoKeyNav", "$select=olingo.odata.test1.ETKeyNav") + .isExSemantic(MessageKeys.INCOMPATIBLE_TYPE_FILTER); + testUri.runEx("ESTwoKeyNav", "$select=PropertyCompNav/olingo.odata.test1.CTTwoPrim") + .isExSemantic(MessageKeys.INCOMPATIBLE_TYPE_FILTER); + testUri.runEx("ESTwoKeyNav", "$select=PropertyCompNav/olingo.odata.test1.CTwrong") + .isExSemantic(MessageKeys.UNKNOWN_TYPE); + testUri.runEx("ESTwoKeyNav", "$select=PropertyCompNav/.") + .isExSemantic(MessageKeys.UNKNOWN_PART); + testUri.runEx("ESTwoKeyNav", "$select=PropertyCompNav/olingo.odata.test1.CTTwoBasePrimCompNav/.") + .isExSemantic(MessageKeys.UNKNOWN_PART); + testUri.runEx("AIRT", "$select=wrong") + .isExSemantic(MessageKeys.ONLY_FOR_TYPED_PARTS); + testUri.runEx("AIRT", "$select=olingo.odata.test1.BAESAllPrimRT") + .isExSemantic(MessageKeys.ONLY_FOR_TYPED_PARTS); + testUri.runEx("ESTwoKeyNav", "$select=olingo.odata.test1.BFwrong") + .isExSemantic(MessageKeys.UNKNOWN_PART); + testUri.runEx("ESTwoKeyNav", "$select=olingo.odata.test1.BFCESTwoKeyNavRTStringParam()") + .isExSyntax(UriParserSyntaxException.MessageKeys.SYNTAX); + testUri.runEx("ESTwoKeyNav", "$select=Namespace1_Alias.BFCESTwoKeyNavRTStringParam(ParameterComp,...)") + .isExSyntax(UriParserSyntaxException.MessageKeys.SYNTAX); + } + + @Test public void runTop() throws Exception { // top testUri.run("ESKeyNav", "$top=1") @@ -5950,7 +6057,7 @@ public class TestFullResourcePath { testUri.runEx("ESTwoKeyNav/olingo.odata.test1.BFCESTwoKeyNavRTStringParam" + "(ParameterComp={\"PropertyInt16\":1,\"PropertyString\":\"Test\"})") - .isExSemantic(UriParserSemanticException.MessageKeys.INVALID_KEY_VALUE); + .isExSemantic(MessageKeys.INVALID_KEY_VALUE); testUri.runEx("FICRTCTTwoPrimTwoParam(ParameterInt16=1,ParameterString=null)") .isExValidation(UriValidationException.MessageKeys.MISSING_PARAMETER); @@ -5964,7 +6071,7 @@ public class TestFullResourcePath { testUri.run("FICRTCTTwoPrimTwoParam(ParameterInt16=1,ParameterString=@test)", "@test='null'"); testUri.runEx("FICRTCTTwoPrimTwoParam(ParameterInt16=1,ParameterString=@test,UnknownParam=1)", "@test='null'") - .isExSemantic(UriParserSemanticException.MessageKeys.FUNCTION_NOT_FOUND); + .isExSemantic(MessageKeys.FUNCTION_NOT_FOUND); testUri.run("FICRTCollCTTwoPrimTwoParam(ParameterInt16=1,ParameterString=@test)", "@test='null'"); testUri.run("FICRTCollCTTwoPrimTwoParam(ParameterInt16=1,ParameterString=@test)", "@test=null"); @@ -5975,7 +6082,7 @@ public class TestFullResourcePath { .isExSyntax(UriParserSyntaxException.MessageKeys.DUPLICATED_ALIAS); testUri.runEx("ESAllPrim", "$filter=FINRTInt16() eq 0") - .isExSemantic(UriParserSemanticException.MessageKeys.FUNCTION_IMPORT_NOT_ALLOWED); + .isExSemantic(MessageKeys.FUNCTION_IMPORT_NOT_ALLOWED); testUri.runEx("ESTwoKeyNav", "$filter=olingo.odata.test1.BFCESTwoKeyNavRTStringParam" + "(ParameterComp=@p1) eq 0&@p1={\"PropertyInt16\":1,\"PropertyString\":\"1\"") http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7e23bf8/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java index e6612ff..19f5946 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestUriParserImpl.java @@ -22,13 +22,11 @@ import java.util.Arrays; import java.util.Collections; import org.apache.olingo.commons.api.edm.Edm; -import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.server.api.OData; import org.apache.olingo.server.api.edmx.EdmxReference; import org.apache.olingo.server.api.uri.UriInfoKind; import org.apache.olingo.server.api.uri.UriResourceKind; import org.apache.olingo.server.api.uri.queryoption.expression.MethodKind; -import org.apache.olingo.server.core.uri.parser.UriParserSemanticException; import org.apache.olingo.server.core.uri.parser.UriParserSyntaxException; import org.apache.olingo.server.core.uri.testutil.FilterValidator; import org.apache.olingo.server.core.uri.testutil.ResourceValidator; @@ -1112,64 +1110,6 @@ public class TestUriParserImpl { .isMethod(MethodKind.GEOINTERSECTS, 2); } - @Test - public void testSelect() throws Exception { - testUri.run("ESTwoKeyNav", "$select=*") - .isSelectItemStar(0); - - testUri.run("ESTwoKeyNav", "$select=olingo.odata.test1.*") - .isSelectItemAllOp(0, new FullQualifiedName("olingo.odata.test1", "*")); - - testUri.run("ESTwoKeyNav", "$select=PropertyString") - .goSelectItemPath(0).isPrimitiveProperty("PropertyString", PropertyProvider.nameString, false); - - testUri.run("ESTwoKeyNav", "$select=PropertyComp") - .goSelectItemPath(0).isComplexProperty("PropertyComp", ComplexTypeProvider.nameCTPrimComp, false); - - testUri.run("ESTwoKeyNav", "$select=PropertyComp/PropertyInt16") - .goSelectItemPath(0) - .first() - .isComplexProperty("PropertyComp", ComplexTypeProvider.nameCTPrimComp, false) - .n() - .isPrimitiveProperty("PropertyInt16", PropertyProvider.nameInt16, false); - - testUri.run("ESTwoKeyNav", "$select=PropertyComp/PropertyComp") - .goSelectItemPath(0) - .first() - .isComplexProperty("PropertyComp", ComplexTypeProvider.nameCTPrimComp, false) - .n() - .isComplexProperty("PropertyComp", ComplexTypeProvider.nameCTAllPrim, false); - - testUri.run("ESTwoKeyNav", "$select=olingo.odata.test1.ETBaseTwoKeyNav") - .isSelectStartType(0, EntityTypeProvider.nameETBaseTwoKeyNav); - - testUri.run("ESTwoKeyNav(PropertyInt16=1,PropertyString='1')/PropertyCompNav", - "$select=olingo.odata.test1.CTTwoBasePrimCompNav") - .isSelectStartType(0, ComplexTypeProvider.nameCTTwoBasePrimCompNav); - - testUri.run("ESTwoKeyNav", "$select=PropertyCompNav/olingo.odata.test1.CTTwoBasePrimCompNav") - .goSelectItemPath(0) - .first() - .isComplexProperty("PropertyCompNav", ComplexTypeProvider.nameCTBasePrimCompNav, false) - .n() - .isTypeFilterOnCollection(ComplexTypeProvider.nameCTTwoBasePrimCompNav); - - testUri.run("ESAllPrim", "$select=PropertyTimeOfDay,PropertyDate,PropertyTimeOfDay") - .isKind(UriInfoKind.resource) - .goSelectItemPath(0).first().isPrimitiveProperty("PropertyTimeOfDay", PropertyProvider.nameTimeOfDay, false) - .goUpUriValidator() - .goSelectItemPath(1).first().isPrimitiveProperty("PropertyDate", PropertyProvider.nameDate, false); - - testUri.runEx("ESMixPrimCollComp", "$select=wrong") - .isExSemantic(UriParserSemanticException.MessageKeys.EXPRESSION_PROPERTY_NOT_IN_TYPE); - testUri.runEx("ESMixPrimCollComp", "$select=PropertyComp/wrong") - .isExSemantic(UriParserSemanticException.MessageKeys.EXPRESSION_PROPERTY_NOT_IN_TYPE); - testUri.runEx("ESMixPrimCollComp", "$select=PropertyComp///PropertyInt16") - .isExSyntax(UriParserSyntaxException.MessageKeys.SYNTAX); - testUri.runEx("ESMixPrimCollComp", "$select=/PropertyInt16") - .isExSyntax(UriParserSyntaxException.MessageKeys.SYNTAX); - } - private final String encode(final String uriPart) { return uriPart.replaceAll(":", "%3A"); } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7e23bf8/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/parser/RawUriTest.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/parser/RawUriTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/parser/RawUriTest.java deleted file mode 100644 index c897400..0000000 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/parser/RawUriTest.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * 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; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import org.junit.Test; - -public class RawUriTest { - - private RawUri runRawParser(final String path, final String query, final int skipSegments) - throws UriParserSyntaxException { - return UriDecoder.decodeUri(path, query, null, skipSegments); - } - - @Test - public void testOption() throws Exception { - RawUri rawUri; - rawUri = runRawParser("", "", 0); - checkOptionCount(rawUri, 0); - - rawUri = runRawParser("", "a", 0); - checkOption(rawUri, 0, "a", ""); - - rawUri = runRawParser("", "a=b", 0); - checkOption(rawUri, 0, "a", "b"); - - rawUri = runRawParser("", "=", 0); - checkOption(rawUri, 0, "", ""); - - rawUri = runRawParser("", "=b", 0); - checkOption(rawUri, 0, "", "b"); - - rawUri = runRawParser("", "a&c", 0); - checkOption(rawUri, 0, "a", ""); - checkOption(rawUri, 1, "c", ""); - - rawUri = runRawParser("", "a=b&c", 0); - checkOption(rawUri, 0, "a", "b"); - checkOption(rawUri, 1, "c", ""); - - rawUri = runRawParser("", "a=b&c=d", 0); - checkOption(rawUri, 0, "a", "b"); - checkOption(rawUri, 1, "c", "d"); - - rawUri = runRawParser("", "=&=", 0); - checkOption(rawUri, 0, "", ""); - checkOption(rawUri, 1, "", ""); - - rawUri = runRawParser("", "=&c=d", 0); - checkOption(rawUri, 0, "", ""); - checkOption(rawUri, 1, "c", "d"); - } - - private void checkOption(final RawUri rawUri, final int index, final String name, final String value) { - RawUri.QueryOption option = rawUri.queryOptionListDecoded.get(index); - - assertEquals(name, option.name); - assertEquals(value, option.value); - } - - private void checkOptionCount(final RawUri rawUri, final int count) { - assertEquals(count, rawUri.queryOptionListDecoded.size()); - } - - @Test - public void testPath() throws Exception { - RawUri rawUri; - - rawUri = runRawParser("", null, 0); - checkPath(rawUri, "", Collections.<String> emptyList()); - - rawUri = runRawParser("/", null, 0); - checkPath(rawUri, "/", Collections.<String> emptyList()); - - rawUri = runRawParser("/entitySet", null, 0); - checkPath(rawUri, "/entitySet", Arrays.asList("entitySet")); - - rawUri = runRawParser("//entitySet", null, 0); - checkPath(rawUri, "//entitySet", Arrays.asList("entitySet")); - - rawUri = runRawParser("entitySet", null, 0); - checkPath(rawUri, "entitySet", Arrays.asList("entitySet")); - - rawUri = runRawParser("/nonServiceSegment/entitySet", null, 0); - checkPath(rawUri, "/nonServiceSegment/entitySet", Arrays.asList("nonServiceSegment", "entitySet")); - - rawUri = runRawParser("/nonServiceSegment/entitySet", null, 1); - checkPath(rawUri, "/nonServiceSegment/entitySet", Arrays.asList("entitySet")); - - rawUri = runRawParser("nonServiceSegment/entitySet", null, 0); - checkPath(rawUri, "nonServiceSegment/entitySet", Arrays.asList("nonServiceSegment", "entitySet")); - - rawUri = runRawParser("nonServiceSegment/entitySet", null, 1); - checkPath(rawUri, "nonServiceSegment/entitySet", Arrays.asList("entitySet")); - - rawUri = runRawParser("non//Service/Segment///entitySet/", null, 3); - checkPath(rawUri, "non//Service/Segment///entitySet/", Arrays.asList("entitySet")); - - rawUri = runRawParser("/a", "abc=xx+yz", 0); - checkPath(rawUri, "/a", Arrays.asList("a")); - } - - @Test - public void testSplit() { - assertTrue(UriDecoder.splitSkipEmpty("", '/').isEmpty()); - assertTrue(UriDecoder.splitSkipEmpty("/", '/').isEmpty()); - assertEquals(Arrays.asList("a"), UriDecoder.splitSkipEmpty("a", '/')); - assertEquals(Arrays.asList("a"), UriDecoder.splitSkipEmpty("a/", '/')); - assertEquals(Arrays.asList("a"), UriDecoder.splitSkipEmpty("/a", '/')); - assertEquals(Arrays.asList("a", "a"), UriDecoder.splitSkipEmpty("a/a", '/')); - assertEquals(Arrays.asList("a", "a"), UriDecoder.splitSkipEmpty("/a/a", '/')); - } - - private void checkPath(final RawUri rawUri, final String path, final List<String> list) { - assertEquals(path, rawUri.path); - - assertEquals(list.size(), rawUri.pathSegmentListDecoded.size()); - - for (int i = 0; i < list.size(); i++) { - assertEquals(list.get(i), rawUri.pathSegmentListDecoded.get(i)); - } - } - - @Test(expected = UriParserSyntaxException.class) - public void wrongPercentEncoding() throws Exception { - runRawParser("%wrong", null, 0); - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7e23bf8/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ParserWithLogging.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ParserWithLogging.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ParserWithLogging.java deleted file mode 100644 index 5ebc57e..0000000 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ParserWithLogging.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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.testutil; - -import org.antlr.v4.runtime.DefaultErrorStrategy; -import org.antlr.v4.runtime.DiagnosticErrorListener; -import org.apache.olingo.commons.api.edm.Edm; -import org.apache.olingo.server.api.OData; -import org.apache.olingo.server.core.uri.antlr.UriParserParser; -import org.apache.olingo.server.core.uri.parser.Parser; - -public class ParserWithLogging extends Parser { - TestErrorLogger errorCollector1; - TestErrorLogger errorCollector2; - - public ParserWithLogging(final Edm edm, final OData odata) { - super(edm, odata); - errorCollector1 = new TestErrorLogger("Stage 1", 1); - errorCollector2 = new TestErrorLogger("Stage 2", 1); - } - - @Override - protected void addStage2ErrorStategy(final UriParserParser parser) { - // Don't throw an at first syntax error, so the error listener will be called - parser.setErrorHandler(new DefaultErrorStrategy()); - } - - @Override - protected void addStage1ErrorListener(final UriParserParser parser) { - // Log error to console - parser.removeErrorListeners(); - parser.addErrorListener(errorCollector1); - parser.addErrorListener(new DiagnosticErrorListener()); - } - - @Override - protected void addStage2ErrorListener(final UriParserParser parser) { - // Log error to console - parser.removeErrorListeners(); - parser.addErrorListener(errorCollector2); - parser.addErrorListener(new DiagnosticErrorListener()); - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7e23bf8/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ResourceValidator.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ResourceValidator.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ResourceValidator.java index d70b204..a9d25be 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ResourceValidator.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ResourceValidator.java @@ -52,6 +52,7 @@ import org.apache.olingo.server.api.uri.queryoption.ExpandOption; import org.apache.olingo.server.api.uri.queryoption.SelectItem; import org.apache.olingo.server.api.uri.queryoption.SelectOption; import org.apache.olingo.server.core.uri.UriResourceWithKeysImpl; +import org.apache.olingo.server.core.uri.parser.Parser; import org.apache.olingo.server.core.uri.validator.UriValidationException; import org.apache.olingo.server.core.uri.validator.UriValidator; @@ -85,7 +86,7 @@ public class ResourceValidator implements TestValidator { // --- Execution --- public ResourceValidator run(final String path) { - ParserWithLogging testParser = new ParserWithLogging(edm, odata); + Parser testParser = new Parser(edm, odata); UriInfo uriInfoTmp = null; uriPathInfo = null; http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7e23bf8/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestErrorLogger.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestErrorLogger.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestErrorLogger.java deleted file mode 100644 index 76b0d38..0000000 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestErrorLogger.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * 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.testutil; - -import java.util.BitSet; -import java.util.Collections; -import java.util.List; - -import org.antlr.v4.runtime.ANTLRErrorListener; -import org.antlr.v4.runtime.Parser; -import org.antlr.v4.runtime.RecognitionException; -import org.antlr.v4.runtime.Recognizer; -import org.antlr.v4.runtime.atn.ATNConfigSet; -import org.antlr.v4.runtime.dfa.DFA; -import org.apache.olingo.server.core.uri.antlr.UriLexer; - -class TestErrorLogger implements ANTLRErrorListener { - - private String prefix; - private int logLevel = 0; - - public TestErrorLogger(final String prefix, final int logLevel) { - this.prefix = prefix; - this.logLevel = logLevel; - } - - @Override - public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line, - final int charPositionInLine, - final String msg, final RecognitionException e) { - - if (logLevel > 0) { - System.out.println("\n" + prefix + " -- SyntaxError"); - trace(recognizer, offendingSymbol, line, charPositionInLine, msg, e); - } - - } - - @Override - public void reportAmbiguity(final Parser recognizer, final DFA dfa, final int startIndex, final int stopIndex, - final boolean exact, - final BitSet ambigAlts, final ATNConfigSet configs) { - // Test - } - - @Override - public void reportAttemptingFullContext(final Parser recognizer, final DFA dfa, final int startIndex, - final int stopIndex, - final BitSet conflictingAlts, final ATNConfigSet configs) { - // Test - } - - @Override - public void reportContextSensitivity(final Parser recognizer, final DFA dfa, final int startIndex, - final int stopIndex, final int prediction, - final ATNConfigSet configs) { - // Test - } - - private void printStack(final Recognizer<?, ?> recognizer) { - List<String> stack = ((Parser) recognizer).getRuleInvocationStack(); - Collections.reverse(stack); - System.out.println(" rule stack: " + stack); - } - - public void trace(final Recognizer<?, ?> recognizer, final Object offendingSymbol, - final int line, final int charPositionInLine, final String msg, final RecognitionException e) { - - System.out.println("Error message: " + msg); - - printStack(recognizer); - - System.out.println(" line/char :" + line + " / " + charPositionInLine); - System.out.println(" sym :" + offendingSymbol); - if (e != null && e.getOffendingToken() != null) { - - String lexerTokenName = ""; - try { - lexerTokenName = UriLexer.VOCABULARY.getDisplayName(e.getOffendingToken().getType()); - } catch (ArrayIndexOutOfBoundsException es) { - lexerTokenName = "token error"; - } - - System.out.println(" tokenname:" + lexerTokenName); - } - - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7e23bf8/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TokenValidator.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TokenValidator.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TokenValidator.java index 3ec6229..547c2ea 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TokenValidator.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TokenValidator.java @@ -29,35 +29,18 @@ import org.apache.olingo.server.core.uri.antlr.UriLexer; public class TokenValidator { private String input = null; - private List<? extends Token> tokens = null; private Token curToken = null; private Exception curException = null; private int startMode; - private int logLevel = 0; - - // --- Setup --- - - public TokenValidator log(final int logLevel) { - this.logLevel = logLevel; - return this; - } // --- Execution --- public TokenValidator run(final String uri) { input = uri; - tokens = parseInput(uri); - if (logLevel > 0) { - showTokens(); - } - first(); - exFirst(); - logLevel = 0; - return this; } @@ -87,31 +70,6 @@ public class TokenValidator { return this; } - public TokenValidator exLast() { - // curException = exceptions.get(exceptions.size() - 1); - return this; - } - - // navigate within the exception list - public TokenValidator exFirst() { - try { - // curException = exceptions.get(0); - } catch (IndexOutOfBoundsException ex) { - curException = null; - } - return this; - - } - - public TokenValidator exAt(final int index) { - try { - // curException = exceptions.get(index); - } catch (IndexOutOfBoundsException ex) { - curException = null; - } - return this; - } - // --- Validation --- public TokenValidator isText(final String expected) { @@ -162,32 +120,8 @@ public class TokenValidator { private List<? extends Token> parseInput(final String input) { ANTLRInputStream inputStream = new ANTLRInputStream(input); - - UriLexer lexer = new UriLexerWithTrace(inputStream, logLevel, startMode); - // lexer.addErrorListener(new ErrorCollector(this)); + UriLexer lexer = new UriLexer(inputStream); + lexer.mode(startMode); return lexer.getAllTokens(); } - - public TokenValidator showTokens() { - boolean first = true; - System.out.println("input: " + input); - String nL = "\n"; - String out = "[" + nL; - for (Token token : tokens) { - if (!first) { - out += ","; - first = false; - } - int index = token.getType(); - if (index != -1) { - out += "\"" + token.getText() + "\"" + " " + UriLexer.VOCABULARY.getDisplayName(index) + nL; - } else { - out += "\"" + token.getText() + "\"" + " " + index + nL; - } - } - out += ']'; - System.out.println("tokens: " + out); - return this; - } - } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7e23bf8/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/UriLexerWithTrace.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/UriLexerWithTrace.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/UriLexerWithTrace.java deleted file mode 100644 index c067394..0000000 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/UriLexerWithTrace.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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.testutil; - -import org.antlr.v4.runtime.ANTLRInputStream; -import org.antlr.v4.runtime.Token; -import org.apache.olingo.server.core.uri.antlr.UriLexer; - -public class UriLexerWithTrace extends UriLexer { - int logLevel = 0; - - public UriLexerWithTrace(final ANTLRInputStream antlrInputStream, final int logLevel) { - super(antlrInputStream); - this.logLevel = logLevel; - } - - public UriLexerWithTrace(final ANTLRInputStream antlrInputStream, final int logLevel, final int mode) { - super(antlrInputStream); - super.mode(mode); - this.logLevel = logLevel; - } - - @Override - public void emit(final Token token) { - if (logLevel > 1) { - String out = String.format("%1$-" + 20 + "s", token.getText()); - - int tokenType = token.getType(); - if (tokenType == -1) { - out += "-1/EOF"; - } else { - out += UriLexer.VOCABULARY.getDisplayName(tokenType); - } - System.out.println("Lexer.emit(...):" + out); - } - - super.emit(token); - } - - @Override - public void pushMode(final int m) { - - String out = UriLexer.modeNames[_mode] + "-->"; - - super.pushMode(m); - - out += UriLexer.modeNames[_mode]; - - if (logLevel > 1) { - System.out.println(out + " "); - } - } - - @Override - public int popMode() { - - String out = UriLexer.modeNames[_mode] + "-->"; - - int m = super.popMode(); - - out += UriLexer.modeNames[_mode]; - - if (logLevel > 1) { - System.out.println(out + " "); - } - - return m; - } -}