WICKET-6318 movign parser resolver to its package
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/6a895f00 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/6a895f00 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/6a895f00 Branch: refs/heads/WICKET-6318-configurable-property-expression-resolver Commit: 6a895f00dbf7aa7bea5ce06ebb7f937082ad6d20 Parents: 1df4300 Author: Pedro Henrique Oliveira dos Santos <[email protected]> Authored: Sat Feb 25 20:51:20 2017 -0300 Committer: Pedro Henrique Oliveira dos Santos <[email protected]> Committed: Sun Feb 26 07:18:36 2017 -0300 ---------------------------------------------------------------------- .../wicket/core/util/lang/ParserException.java | 33 --- .../core/util/lang/PropertyExpression.java | 144 ------------ .../util/lang/PropertyExpressionParser.java | 220 ------------------- .../core/util/parser/ParserException.java | 33 +++ .../core/util/parser/PropertyExpression.java | 144 ++++++++++++ .../util/parser/PropertyExpressionParser.java | 220 +++++++++++++++++++ .../util/lang/PropertyExpressionParserTest.java | 189 ---------------- .../parser/PropertyExpressionParserTest.java | 189 ++++++++++++++++ 8 files changed, 586 insertions(+), 586 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/6a895f00/wicket-core/src/main/java/org/apache/wicket/core/util/lang/ParserException.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/lang/ParserException.java b/wicket-core/src/main/java/org/apache/wicket/core/util/lang/ParserException.java deleted file mode 100644 index 62712e8..0000000 --- a/wicket-core/src/main/java/org/apache/wicket/core/util/lang/ParserException.java +++ /dev/null @@ -1,33 +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.wicket.core.util.lang; - -/** - * @author Pedro Santos - */ -public class ParserException extends RuntimeException -{ - private static final long serialVersionUID = 1L; - - /** - * @param message - */ - public ParserException(String message) - { - super(message); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/wicket/blob/6a895f00/wicket-core/src/main/java/org/apache/wicket/core/util/lang/PropertyExpression.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/lang/PropertyExpression.java b/wicket-core/src/main/java/org/apache/wicket/core/util/lang/PropertyExpression.java deleted file mode 100644 index 6596651..0000000 --- a/wicket-core/src/main/java/org/apache/wicket/core/util/lang/PropertyExpression.java +++ /dev/null @@ -1,144 +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.wicket.core.util.lang; - -/** - * Abstract syntax tree of a expression property - * - * @author Pedro Santos - */ -public class PropertyExpression -{ - JavaProperty javaProperty; - BeanProperty beanProperty; - String index; - PropertyExpression next; - - static class BeanProperty - { - String propertyName; - String index; - - public BeanProperty() - { - } - - public BeanProperty(String name, String index) - { - this.propertyName = name; - this.index = index; - } - - @Override - public int hashCode() - { - final int prime = 31; - int result = 1; - result = prime * result + ((index == null) ? 0 : index.hashCode()); - result = prime * result + ((propertyName == null) ? 0 : propertyName.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) - { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - BeanProperty other = (BeanProperty)obj; - if (index == null) - { - if (other.index != null) - return false; - } - else if (!index.equals(other.index)) - return false; - if (propertyName == null) - { - if (other.propertyName != null) - return false; - } - else if (!propertyName.equals(other.propertyName)) - return false; - return true; - } - - } - - static class JavaProperty - { - String javaIdentifier; - String index; - public boolean hasMethodSign; - - public JavaProperty() - { - } - - public JavaProperty(String javaIdentifier, String index, boolean hasMethodSign) - { - this.javaIdentifier = javaIdentifier; - this.index = index; - this.hasMethodSign = hasMethodSign; - } - - @Override - public int hashCode() - { - final int prime = 31; - int result = 1; - result = prime * result + (hasMethodSign ? 1231 : 1237); - result = prime * result + ((index == null) ? 0 : index.hashCode()); - result = prime * result + ((javaIdentifier == null) ? 0 : javaIdentifier.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) - { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - JavaProperty other = (JavaProperty)obj; - if (hasMethodSign != other.hasMethodSign) - return false; - if (index == null) - { - if (other.index != null) - return false; - } - else if (!index.equals(other.index)) - return false; - if (javaIdentifier == null) - { - if (other.javaIdentifier != null) - return false; - } - else if (!javaIdentifier.equals(other.javaIdentifier)) - return false; - return true; - } - - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/wicket/blob/6a895f00/wicket-core/src/main/java/org/apache/wicket/core/util/lang/PropertyExpressionParser.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/lang/PropertyExpressionParser.java b/wicket-core/src/main/java/org/apache/wicket/core/util/lang/PropertyExpressionParser.java deleted file mode 100644 index ef05599..0000000 --- a/wicket-core/src/main/java/org/apache/wicket/core/util/lang/PropertyExpressionParser.java +++ /dev/null @@ -1,220 +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.wicket.core.util.lang; - -import static java.lang.Character.isJavaIdentifierPart; -import static java.lang.Character.isJavaIdentifierStart; -import static java.lang.String.format; - -import org.apache.wicket.core.util.lang.PropertyExpression.BeanProperty; -import org.apache.wicket.core.util.lang.PropertyExpression.JavaProperty; - -/** - * EBNF like description of the property expression syntax <code> - * - * java letter = "_" | "$" | "A" | "a" | "B" | "b" | (...); - * java letter or digit = java letter | "0" | "1" | (...) ; - * char = java letter or digit | "." | "(" | ")" | "[" | "]" | "!" | "@" | "#" | (...); - * index char = char - "]"; - * - * empty space = { " " }; - * java identifier = java letter , {java letter or digit}; - * property name = java letter or digit , {java letter or digit}; - * method sign = "(" , empty space , ")"; - * index = "[" , index char , { index char } , "]"; - * - * bean property = property name, [ index ]; - * java property = java identifier , [ index | method sign ]; - * property expression = [ bean property | java property | index ] , { "." , property expression }; - * - * </code> - * - * @author Pedro Santos - */ -public class PropertyExpressionParser -{ - - private static final char END_OF_EXPRESSION = (char)-1; - private String text; - private int currentPosition = 0; - private int nextPosition = 1; - private char currentToken; - private char lookaheadToken; - - private char advance() - { - currentPosition = nextPosition; - currentToken = lookaheadToken; - nextPosition += 1; - if (nextPosition >= text.length()) - { - - lookaheadToken = END_OF_EXPRESSION; - } - else - { - - lookaheadToken = text.charAt(nextPosition); - } - return currentToken; - } - - PropertyExpression parse(String text) - { - this.text = text; - if (text == null || text.isEmpty()) - { - throw new ParserException("No expression was given to be parsed."); - } - currentToken = text.charAt(0); - if (text.length() == 1) - { - lookaheadToken = END_OF_EXPRESSION; - } - else - { - lookaheadToken = text.charAt(1); - } - return expression(); - - } - - private PropertyExpression expression() - { - PropertyExpression expression = new PropertyExpression(); - if (currentToken == '[') - { - expression.index = index(); - } - else if (Character.isJavaIdentifierStart(currentToken)) - { - expression.javaProperty = javaProperty(); - } - else if (Character.isJavaIdentifierPart(currentToken)) - { - expression.beanProperty = beanProperty(); - } - else - { - throw new ParserException("Expecting an expression but got: " + currentToken); - } - switch (lookaheadToken) - { - case '.' : - advance();// skips the dot - advance();// advances to the next expression - expression.next = expression(); - return expression; - case END_OF_EXPRESSION : - return expression; - case '(' : - throw new ParserException(format("Expecting a valid method name but got: '%s<--'", - text.substring(0, nextPosition + 1))); - default : - throw new ParserException(format( - "Expecting a new expression but got the invalid character '%s' at: '%s<--'", - lookaheadToken, text.substring(0, nextPosition + 1))); - } - } - - private BeanProperty beanProperty() - { - BeanProperty property = new BeanProperty(); - property.propertyName = propertyName(); - if (lookaheadToken == '[') - { - advance();// skips left bracket - property.index = index(); - } - return property; - } - - private JavaProperty javaProperty() - { - JavaProperty property = new JavaProperty(); - property.javaIdentifier = javaIdentifier(); - switch (lookaheadToken) - { - case '[' : - advance();// skips left bracket - property.index = index(); - break; - case '(' : - advance(); // skips left bracket - property.hasMethodSign = methodSign(); - break; - } - return property; - } - - private String propertyName() - { - int begin = currentPosition; - while (isJavaIdentifierPart(lookaheadToken)) - { - advance(); - } - return text.substring(begin, nextPosition); - } - - private String javaIdentifier() - { - if (!isJavaIdentifierStart(currentToken)) - { - throw new ParserException("Expeting a java identifier but got a :" + currentToken); - } - return propertyName(); - } - - private String index() - { - advance();// escape bracket - if (currentToken == ']') - { - throw new ParserException( - format("Expecting a property index but found empty brakets: '%s<--'", - text.substring(0, nextPosition))); - } - int begin = currentPosition; - while (lookaheadToken != ']') - { - advance(); - } - advance();// escape bracket - return text.substring(begin, currentPosition); - } - - private boolean methodSign() - { - emptySpace(); - if (lookaheadToken != ')') - { - throw new ParserException(format("The expression can't have method parameters: '%s<--'", - text.substring(0, nextPosition))); - } - advance();// skips right bracket - return true; - } - - private void emptySpace() - { - while (lookaheadToken == ' ') - { - advance();// skips empty spaces - } - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/6a895f00/wicket-core/src/main/java/org/apache/wicket/core/util/parser/ParserException.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/parser/ParserException.java b/wicket-core/src/main/java/org/apache/wicket/core/util/parser/ParserException.java new file mode 100644 index 0000000..67a0a58 --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/core/util/parser/ParserException.java @@ -0,0 +1,33 @@ +/* + * 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.wicket.core.util.parser; + +/** + * @author Pedro Santos + */ +public class ParserException extends RuntimeException +{ + private static final long serialVersionUID = 1L; + + /** + * @param message + */ + public ParserException(String message) + { + super(message); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/wicket/blob/6a895f00/wicket-core/src/main/java/org/apache/wicket/core/util/parser/PropertyExpression.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/parser/PropertyExpression.java b/wicket-core/src/main/java/org/apache/wicket/core/util/parser/PropertyExpression.java new file mode 100644 index 0000000..2cd5e6d --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/core/util/parser/PropertyExpression.java @@ -0,0 +1,144 @@ +/* + * 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.wicket.core.util.parser; + +/** + * Abstract syntax tree of a expression property + * + * @author Pedro Santos + */ +public class PropertyExpression +{ + JavaProperty javaProperty; + BeanProperty beanProperty; + String index; + PropertyExpression next; + + static class BeanProperty + { + String propertyName; + String index; + + public BeanProperty() + { + } + + public BeanProperty(String name, String index) + { + this.propertyName = name; + this.index = index; + } + + @Override + public int hashCode() + { + final int prime = 31; + int result = 1; + result = prime * result + ((index == null) ? 0 : index.hashCode()); + result = prime * result + ((propertyName == null) ? 0 : propertyName.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + BeanProperty other = (BeanProperty)obj; + if (index == null) + { + if (other.index != null) + return false; + } + else if (!index.equals(other.index)) + return false; + if (propertyName == null) + { + if (other.propertyName != null) + return false; + } + else if (!propertyName.equals(other.propertyName)) + return false; + return true; + } + + } + + static class JavaProperty + { + String javaIdentifier; + String index; + public boolean hasMethodSign; + + public JavaProperty() + { + } + + public JavaProperty(String javaIdentifier, String index, boolean hasMethodSign) + { + this.javaIdentifier = javaIdentifier; + this.index = index; + this.hasMethodSign = hasMethodSign; + } + + @Override + public int hashCode() + { + final int prime = 31; + int result = 1; + result = prime * result + (hasMethodSign ? 1231 : 1237); + result = prime * result + ((index == null) ? 0 : index.hashCode()); + result = prime * result + ((javaIdentifier == null) ? 0 : javaIdentifier.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + JavaProperty other = (JavaProperty)obj; + if (hasMethodSign != other.hasMethodSign) + return false; + if (index == null) + { + if (other.index != null) + return false; + } + else if (!index.equals(other.index)) + return false; + if (javaIdentifier == null) + { + if (other.javaIdentifier != null) + return false; + } + else if (!javaIdentifier.equals(other.javaIdentifier)) + return false; + return true; + } + + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/wicket/blob/6a895f00/wicket-core/src/main/java/org/apache/wicket/core/util/parser/PropertyExpressionParser.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/parser/PropertyExpressionParser.java b/wicket-core/src/main/java/org/apache/wicket/core/util/parser/PropertyExpressionParser.java new file mode 100644 index 0000000..6a9f745 --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/core/util/parser/PropertyExpressionParser.java @@ -0,0 +1,220 @@ +/* + * 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.wicket.core.util.parser; + +import static java.lang.Character.isJavaIdentifierPart; +import static java.lang.Character.isJavaIdentifierStart; +import static java.lang.String.format; + +import org.apache.wicket.core.util.parser.PropertyExpression.BeanProperty; +import org.apache.wicket.core.util.parser.PropertyExpression.JavaProperty; + +/** + * EBNF like description of the property expression syntax <code> + * + * java letter = "_" | "$" | "A" | "a" | "B" | "b" | (...); + * java letter or digit = java letter | "0" | "1" | (...) ; + * char = java letter or digit | "." | "(" | ")" | "[" | "]" | "!" | "@" | "#" | (...); + * index char = char - "]"; + * + * empty space = { " " }; + * java identifier = java letter , {java letter or digit}; + * property name = java letter or digit , {java letter or digit}; + * method sign = "(" , empty space , ")"; + * index = "[" , index char , { index char } , "]"; + * + * bean property = property name, [ index ]; + * java property = java identifier , [ index | method sign ]; + * property expression = [ bean property | java property | index ] , { "." , property expression }; + * + * </code> + * + * @author Pedro Santos + */ +public class PropertyExpressionParser +{ + + private static final char END_OF_EXPRESSION = (char)-1; + private String text; + private int currentPosition = 0; + private int nextPosition = 1; + private char currentToken; + private char lookaheadToken; + + private char advance() + { + currentPosition = nextPosition; + currentToken = lookaheadToken; + nextPosition += 1; + if (nextPosition >= text.length()) + { + + lookaheadToken = END_OF_EXPRESSION; + } + else + { + + lookaheadToken = text.charAt(nextPosition); + } + return currentToken; + } + + PropertyExpression parse(String text) + { + this.text = text; + if (text == null || text.isEmpty()) + { + throw new ParserException("No expression was given to be parsed."); + } + currentToken = text.charAt(0); + if (text.length() == 1) + { + lookaheadToken = END_OF_EXPRESSION; + } + else + { + lookaheadToken = text.charAt(1); + } + return expression(); + + } + + private PropertyExpression expression() + { + PropertyExpression expression = new PropertyExpression(); + if (currentToken == '[') + { + expression.index = index(); + } + else if (Character.isJavaIdentifierStart(currentToken)) + { + expression.javaProperty = javaProperty(); + } + else if (Character.isJavaIdentifierPart(currentToken)) + { + expression.beanProperty = beanProperty(); + } + else + { + throw new ParserException("Expecting an expression but got: " + currentToken); + } + switch (lookaheadToken) + { + case '.' : + advance();// skips the dot + advance();// advances to the next expression + expression.next = expression(); + return expression; + case END_OF_EXPRESSION : + return expression; + case '(' : + throw new ParserException(format("Expecting a valid method name but got: '%s<--'", + text.substring(0, nextPosition + 1))); + default : + throw new ParserException(format( + "Expecting a new expression but got the invalid character '%s' at: '%s<--'", + lookaheadToken, text.substring(0, nextPosition + 1))); + } + } + + private BeanProperty beanProperty() + { + BeanProperty property = new BeanProperty(); + property.propertyName = propertyName(); + if (lookaheadToken == '[') + { + advance();// skips left bracket + property.index = index(); + } + return property; + } + + private JavaProperty javaProperty() + { + JavaProperty property = new JavaProperty(); + property.javaIdentifier = javaIdentifier(); + switch (lookaheadToken) + { + case '[' : + advance();// skips left bracket + property.index = index(); + break; + case '(' : + advance(); // skips left bracket + property.hasMethodSign = methodSign(); + break; + } + return property; + } + + private String propertyName() + { + int begin = currentPosition; + while (isJavaIdentifierPart(lookaheadToken)) + { + advance(); + } + return text.substring(begin, nextPosition); + } + + private String javaIdentifier() + { + if (!isJavaIdentifierStart(currentToken)) + { + throw new ParserException("Expeting a java identifier but got a :" + currentToken); + } + return propertyName(); + } + + private String index() + { + advance();// escape bracket + if (currentToken == ']') + { + throw new ParserException( + format("Expecting a property index but found empty brakets: '%s<--'", + text.substring(0, nextPosition))); + } + int begin = currentPosition; + while (lookaheadToken != ']') + { + advance(); + } + advance();// escape bracket + return text.substring(begin, currentPosition); + } + + private boolean methodSign() + { + emptySpace(); + if (lookaheadToken != ')') + { + throw new ParserException(format("The expression can't have method parameters: '%s<--'", + text.substring(0, nextPosition))); + } + advance();// skips right bracket + return true; + } + + private void emptySpace() + { + while (lookaheadToken == ' ') + { + advance();// skips empty spaces + } + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/6a895f00/wicket-core/src/test/java/org/apache/wicket/core/util/lang/PropertyExpressionParserTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/core/util/lang/PropertyExpressionParserTest.java b/wicket-core/src/test/java/org/apache/wicket/core/util/lang/PropertyExpressionParserTest.java deleted file mode 100644 index 3651b94..0000000 --- a/wicket-core/src/test/java/org/apache/wicket/core/util/lang/PropertyExpressionParserTest.java +++ /dev/null @@ -1,189 +0,0 @@ -package org.apache.wicket.core.util.lang; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.junit.Assert.assertThat; - -import org.apache.wicket.core.util.lang.PropertyExpression.BeanProperty; -import org.apache.wicket.core.util.lang.PropertyExpression.JavaProperty; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -public class PropertyExpressionParserTest -{ - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private PropertyExpressionParser parser = new PropertyExpressionParser(); - - @Test - public void shouldParsePropertyExpressions() - { - PropertyExpression expression = parser.parse("person"); - assertThat(expression.index, nullValue()); - assertThat(expression.beanProperty, nullValue()); - assertThat(expression.javaProperty, is(new JavaProperty("person", null, false))); - assertThat(expression.next, nullValue()); - } - - @Test - public void shouldParsePropertyExpressionStartingWithDigits() - { - PropertyExpression expression = parser.parse("1person"); - assertThat(expression.index, nullValue()); - assertThat(expression.beanProperty, is(new BeanProperty("1person", null))); - assertThat(expression.javaProperty, nullValue()); - assertThat(expression.next, nullValue()); - } - - @Test - public void shouldParseShortPropertyExpressions() - { - PropertyExpression expression = parser.parse("a"); - assertThat(expression.index, nullValue()); - assertThat(expression.beanProperty, nullValue()); - assertThat(expression.javaProperty, is(new JavaProperty("a", null, false))); - assertThat(expression.next, nullValue()); - } - - @Test - public void shouldParseIndexedPropertyExpressions() - { - PropertyExpression expression = parser.parse("person[age]"); - assertThat(expression.index, nullValue()); - assertThat(expression.beanProperty, nullValue()); - assertThat(expression.javaProperty, is(new JavaProperty("person", "age", false))); - assertThat(expression.next, nullValue()); - } - - @Test - public void shouldParseMethodExpressions() - { - PropertyExpression expression = parser.parse("person()"); - assertThat(expression.index, nullValue()); - assertThat(expression.beanProperty, nullValue()); - assertThat(expression.javaProperty, is(new JavaProperty("person", null, true))); - assertThat(expression.next, nullValue()); - } - - @Test - public void shouldParsePropertyExpressionsWithSpaceInMethod() - { - final PropertyExpression parse = parser.parse("person( )"); - assertThat(parse.javaProperty, is(new JavaProperty("person", null, true))); - } - - @Test - public void shouldParseIndexExpressions() - { - PropertyExpression expression = parser.parse("[person#name]"); - assertThat(expression.index, is("person#name")); - assertThat(expression.javaProperty, nullValue()); - assertThat(expression.next, nullValue()); - } - - @Test - public void shouldParseChainedPropertyExpressions() - { - PropertyExpression expression = parser.parse("person.child"); - assertThat(expression.javaProperty, is(new JavaProperty("person", null, false))); - assertThat(expression.next.javaProperty, is(new JavaProperty("child", null, false))); - } - - @Test - public void shouldParseShortChainedPropertyExpressions() - { - PropertyExpression expression = parser.parse("a.b"); - assertThat(expression.javaProperty, is(new JavaProperty("a", null, false))); - assertThat(expression.next.javaProperty, is(new JavaProperty("b", null, false))); - } - - @Test - public void shouldParseChainedIndexedPropertyExpressions() - { - PropertyExpression expression = parser.parse("person[1].child"); - assertThat(expression.javaProperty, is(new JavaProperty("person", "1", false))); - assertThat(expression.next.javaProperty, is(new JavaProperty("child", null, false))); - } - - @Test - public void shouldParseChainedMethodExpressions() - { - PropertyExpression expression = parser.parse("person().child"); - assertThat(expression.javaProperty, is(new JavaProperty("person", null, true))); - assertThat(expression.next.javaProperty, is(new JavaProperty("child", null, false))); - } - - @Test - public void shouldParseChainedIndexExpressions() - { - PropertyExpression expression = parser.parse("[person].child"); - assertThat(expression.index, is("person")); - assertThat(expression.next.javaProperty, is(new JavaProperty("child", null, false))); - } - - @Test - public void shouldParseDeeperChainedPropertyExpressions() - { - PropertyExpression expression = parser.parse("person.child.name"); - assertThat(expression.javaProperty, is(new JavaProperty("person", null, false))); - assertThat(expression.next.javaProperty, is(new JavaProperty("child", null, false))); - assertThat(expression.next.next.javaProperty, is(new JavaProperty("name", null, false))); - } - - - @Test - public void shouldCheckExpressions() - { - expectedException.expect(ParserException.class); - expectedException.expectMessage("No expression was given to be parsed."); - parser.parse(""); - } - - @Test - public void shouldFailParsePropertyExpressionsWithSpace() - { - expectedException.expect(ParserException.class); - expectedException.expectMessage( - "Expecting a new expression but got the invalid character ' ' at: 'per <--'"); - parser.parse("per son"); - } - - @Test - public void shouldReportEmptyIndexBrackets() - { - expectedException.expect(ParserException.class); - expectedException - .expectMessage("Expecting a property index but found empty brakets: 'person[]<--'"); - parser.parse("person[]"); - } - - @Test - public void shouldReportMethodsCantHaveParameters() - { - expectedException.expect(ParserException.class); - expectedException.expectMessage( - "The expression can't have method parameters: 'repository.getPerson(<--'"); - parser.parse("repository.getPerson(filter)"); - } - - @Test - public void shouldFailParseInvalidBeanProperty() - { - expectedException.expect(ParserException.class); - expectedException.expectMessage( - "Expecting a new expression but got the invalid character '#' at: 'repository.prop#<--'"); - parser.parse("repository.prop#name"); - } - - @Test - public void shouldFailParseMethodsStartingWithInvalidCharacter() - { - expectedException.expect(ParserException.class); - expectedException - .expectMessage("Expecting a valid method name but got: 'repository.0method(<--'"); - parser.parse("repository.0method()"); - } - -} http://git-wip-us.apache.org/repos/asf/wicket/blob/6a895f00/wicket-core/src/test/java/org/apache/wicket/core/util/parser/PropertyExpressionParserTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/core/util/parser/PropertyExpressionParserTest.java b/wicket-core/src/test/java/org/apache/wicket/core/util/parser/PropertyExpressionParserTest.java new file mode 100644 index 0000000..da043f2 --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/core/util/parser/PropertyExpressionParserTest.java @@ -0,0 +1,189 @@ +package org.apache.wicket.core.util.parser; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +import org.apache.wicket.core.util.parser.PropertyExpression.BeanProperty; +import org.apache.wicket.core.util.parser.PropertyExpression.JavaProperty; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class PropertyExpressionParserTest +{ + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + private PropertyExpressionParser parser = new PropertyExpressionParser(); + + @Test + public void shouldParsePropertyExpressions() + { + PropertyExpression expression = parser.parse("person"); + assertThat(expression.index, nullValue()); + assertThat(expression.beanProperty, nullValue()); + assertThat(expression.javaProperty, is(new JavaProperty("person", null, false))); + assertThat(expression.next, nullValue()); + } + + @Test + public void shouldParsePropertyExpressionStartingWithDigits() + { + PropertyExpression expression = parser.parse("1person"); + assertThat(expression.index, nullValue()); + assertThat(expression.beanProperty, is(new BeanProperty("1person", null))); + assertThat(expression.javaProperty, nullValue()); + assertThat(expression.next, nullValue()); + } + + @Test + public void shouldParseShortPropertyExpressions() + { + PropertyExpression expression = parser.parse("a"); + assertThat(expression.index, nullValue()); + assertThat(expression.beanProperty, nullValue()); + assertThat(expression.javaProperty, is(new JavaProperty("a", null, false))); + assertThat(expression.next, nullValue()); + } + + @Test + public void shouldParseIndexedPropertyExpressions() + { + PropertyExpression expression = parser.parse("person[age]"); + assertThat(expression.index, nullValue()); + assertThat(expression.beanProperty, nullValue()); + assertThat(expression.javaProperty, is(new JavaProperty("person", "age", false))); + assertThat(expression.next, nullValue()); + } + + @Test + public void shouldParseMethodExpressions() + { + PropertyExpression expression = parser.parse("person()"); + assertThat(expression.index, nullValue()); + assertThat(expression.beanProperty, nullValue()); + assertThat(expression.javaProperty, is(new JavaProperty("person", null, true))); + assertThat(expression.next, nullValue()); + } + + @Test + public void shouldParsePropertyExpressionsWithSpaceInMethod() + { + final PropertyExpression parse = parser.parse("person( )"); + assertThat(parse.javaProperty, is(new JavaProperty("person", null, true))); + } + + @Test + public void shouldParseIndexExpressions() + { + PropertyExpression expression = parser.parse("[person#name]"); + assertThat(expression.index, is("person#name")); + assertThat(expression.javaProperty, nullValue()); + assertThat(expression.next, nullValue()); + } + + @Test + public void shouldParseChainedPropertyExpressions() + { + PropertyExpression expression = parser.parse("person.child"); + assertThat(expression.javaProperty, is(new JavaProperty("person", null, false))); + assertThat(expression.next.javaProperty, is(new JavaProperty("child", null, false))); + } + + @Test + public void shouldParseShortChainedPropertyExpressions() + { + PropertyExpression expression = parser.parse("a.b"); + assertThat(expression.javaProperty, is(new JavaProperty("a", null, false))); + assertThat(expression.next.javaProperty, is(new JavaProperty("b", null, false))); + } + + @Test + public void shouldParseChainedIndexedPropertyExpressions() + { + PropertyExpression expression = parser.parse("person[1].child"); + assertThat(expression.javaProperty, is(new JavaProperty("person", "1", false))); + assertThat(expression.next.javaProperty, is(new JavaProperty("child", null, false))); + } + + @Test + public void shouldParseChainedMethodExpressions() + { + PropertyExpression expression = parser.parse("person().child"); + assertThat(expression.javaProperty, is(new JavaProperty("person", null, true))); + assertThat(expression.next.javaProperty, is(new JavaProperty("child", null, false))); + } + + @Test + public void shouldParseChainedIndexExpressions() + { + PropertyExpression expression = parser.parse("[person].child"); + assertThat(expression.index, is("person")); + assertThat(expression.next.javaProperty, is(new JavaProperty("child", null, false))); + } + + @Test + public void shouldParseDeeperChainedPropertyExpressions() + { + PropertyExpression expression = parser.parse("person.child.name"); + assertThat(expression.javaProperty, is(new JavaProperty("person", null, false))); + assertThat(expression.next.javaProperty, is(new JavaProperty("child", null, false))); + assertThat(expression.next.next.javaProperty, is(new JavaProperty("name", null, false))); + } + + + @Test + public void shouldCheckExpressions() + { + expectedException.expect(ParserException.class); + expectedException.expectMessage("No expression was given to be parsed."); + parser.parse(""); + } + + @Test + public void shouldFailParsePropertyExpressionsWithSpace() + { + expectedException.expect(ParserException.class); + expectedException.expectMessage( + "Expecting a new expression but got the invalid character ' ' at: 'per <--'"); + parser.parse("per son"); + } + + @Test + public void shouldReportEmptyIndexBrackets() + { + expectedException.expect(ParserException.class); + expectedException + .expectMessage("Expecting a property index but found empty brakets: 'person[]<--'"); + parser.parse("person[]"); + } + + @Test + public void shouldReportMethodsCantHaveParameters() + { + expectedException.expect(ParserException.class); + expectedException.expectMessage( + "The expression can't have method parameters: 'repository.getPerson(<--'"); + parser.parse("repository.getPerson(filter)"); + } + + @Test + public void shouldFailParseInvalidBeanProperty() + { + expectedException.expect(ParserException.class); + expectedException.expectMessage( + "Expecting a new expression but got the invalid character '#' at: 'repository.prop#<--'"); + parser.parse("repository.prop#name"); + } + + @Test + public void shouldFailParseMethodsStartingWithInvalidCharacter() + { + expectedException.expect(ParserException.class); + expectedException + .expectMessage("Expecting a valid method name but got: 'repository.0method(<--'"); + parser.parse("repository.0method()"); + } + +}
