-
Notifications
You must be signed in to change notification settings - Fork 571
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HV-1774 Add a few tests to demonstrate the behavior of TokenCollector
- Loading branch information
Showing
4 changed files
with
411 additions
and
64 deletions.
There are no files selected for viewing
186 changes: 186 additions & 0 deletions
186
...rnate/validator/test/internal/engine/messageinterpolation/AbstractTokenCollectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.test.internal.engine.messageinterpolation; | ||
|
||
import org.hibernate.validator.internal.engine.messageinterpolation.InterpolationTermType; | ||
import org.hibernate.validator.internal.engine.messageinterpolation.parser.MessageDescriptorFormatException; | ||
import org.hibernate.validator.internal.engine.messageinterpolation.parser.Token; | ||
import org.hibernate.validator.internal.engine.messageinterpolation.parser.TokenCollector; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* Abstract base for {@code TokenCollector} tests. | ||
* | ||
* @author Hardy Ferentschik | ||
*/ | ||
public abstract class AbstractTokenCollectorTest { | ||
|
||
protected abstract InterpolationTermType getInterpolationTermType(); | ||
|
||
@Test | ||
public void testLiteral() { | ||
Assertions.assertThat( | ||
new TokenCollector( "foo bar", getInterpolationTermType() ) | ||
.getTokenList() | ||
) | ||
.hasSize( 1 ) | ||
.element( 0 ) | ||
.returns( "foo bar", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
} | ||
|
||
@Test(expectedExceptions = MessageDescriptorFormatException.class, expectedExceptionsMessageRegExp = "HV000169.*") | ||
public void testNestedParametersThrowException() { | ||
new TokenCollector( "#{foo {}", getInterpolationTermType() ); | ||
} | ||
|
||
@Test(expectedExceptions = MessageDescriptorFormatException.class, expectedExceptionsMessageRegExp = "HV000168.*") | ||
public void testClosingBraceWithoutOpeningBraceThrowsException() { | ||
new TokenCollector( "foo}", getInterpolationTermType() ); | ||
} | ||
|
||
@Test(expectedExceptions = MessageDescriptorFormatException.class, expectedExceptionsMessageRegExp = "HV000168.*") | ||
public void testOpeningBraceWithoutClosingBraceThrowsException() { | ||
new TokenCollector( "{foo", getInterpolationTermType() ); | ||
} | ||
|
||
@Test | ||
public void testBackslashEscapesNonMetaCharacter() { | ||
Assertions.assertThat( | ||
new TokenCollector( "foo \\bar", getInterpolationTermType() ) | ||
.getTokenList() | ||
) | ||
.hasSize( 1 ) | ||
.element( 0 ) | ||
// Backslashes are removed later, in AbstractMessageInterpolator.replaceEscapedLiterals | ||
.returns( "foo \\bar", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
} | ||
|
||
@Test | ||
public void testBackslashEscapesDollar() { | ||
Assertions.assertThat( | ||
new TokenCollector( "foo \\$ bar", getInterpolationTermType() ) | ||
.getTokenList() | ||
) | ||
.hasSize( 1 ) | ||
.element( 0 ) | ||
// Backslashes are removed later, in AbstractMessageInterpolator.replaceEscapedLiterals | ||
.returns( "foo \\$ bar", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
} | ||
|
||
@Test | ||
public void testBackslashEscapesOpeningBrace() { | ||
Assertions.assertThat( | ||
new TokenCollector( "foo \\{ bar", getInterpolationTermType() ) | ||
.getTokenList() | ||
) | ||
.hasSize( 1 ) | ||
.element( 0 ) | ||
// Backslashes are removed later, in AbstractMessageInterpolator.replaceEscapedLiterals | ||
.returns( "foo \\{ bar", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
} | ||
|
||
@Test | ||
public void testBackslashEscapesClosingBrace() { | ||
Assertions.assertThat( | ||
new TokenCollector( "foo \\} bar", getInterpolationTermType() ) | ||
.getTokenList() | ||
) | ||
.hasSize( 1 ) | ||
.element( 0 ) | ||
// Backslashes are removed later, in AbstractMessageInterpolator.replaceEscapedLiterals | ||
.returns( "foo \\} bar", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
} | ||
|
||
@Test | ||
public void testBackslashEscapesBackslash() { | ||
Assertions.assertThat( | ||
new TokenCollector( "foo \\\\ bar", getInterpolationTermType() ) | ||
.getTokenList() | ||
) | ||
.hasSize( 1 ) | ||
.element( 0 ) | ||
// Backslashes are removed later, in AbstractMessageInterpolator.replaceEscapedLiterals | ||
.returns( "foo \\\\ bar", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
} | ||
|
||
@Test | ||
public void testBackslashEscapesEL() { | ||
Assertions.assertThat( | ||
new TokenCollector( "foo \\$\\{bar\\}", getInterpolationTermType() ) | ||
.getTokenList() | ||
) | ||
.hasSize( 1 ) | ||
.element( 0 ) | ||
// Backslashes are removed later, in AbstractMessageInterpolator.replaceEscapedLiterals | ||
.returns( "foo \\$\\{bar\\}", Token::getTokenValue ) | ||
// What's important is that we did NOT detect the expression | ||
.returns( false, Token::isParameter ); | ||
} | ||
|
||
@Test | ||
public void testBackslashEscapesParameter() { | ||
Assertions.assertThat( | ||
new TokenCollector( "foo \\{bar\\}", getInterpolationTermType() ) | ||
.getTokenList() | ||
) | ||
.hasSize( 1 ) | ||
.element( 0 ) | ||
// Backslashes are removed later, in AbstractMessageInterpolator.replaceEscapedLiterals | ||
.returns( "foo \\{bar\\}", Token::getTokenValue ) | ||
// What's important is that we did NOT detect the parameter | ||
.returns( false, Token::isParameter ); | ||
} | ||
|
||
@Test(expectedExceptions = MessageDescriptorFormatException.class, expectedExceptionsMessageRegExp = "HV000168.*") | ||
public void testTrailingClosingBraceThrowsException() { | ||
new TokenCollector( "this message contains a invalid parameter start token {", getInterpolationTermType() ); | ||
} | ||
|
||
@Test | ||
public void testDollarThenNonMetaCharacterInterpretedAsLiteral() { | ||
Assertions.assertThat( | ||
new TokenCollector( "$a", getInterpolationTermType() ) | ||
.getTokenList() | ||
) | ||
.hasSize( 1 ) | ||
.element( 0 ) | ||
.returns( "$a", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
} | ||
|
||
@Test | ||
public void testTrailingDollarInterpretedAsLiteral() { | ||
Assertions.assertThat( | ||
new TokenCollector( "foo $", getInterpolationTermType() ) | ||
.getTokenList() | ||
) | ||
.hasSize( 1 ) | ||
.element( 0 ) | ||
.returns( "foo $", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
} | ||
|
||
@Test | ||
public void testTrailingBackslashInterpretedAsLiteral() { | ||
Assertions.assertThat( | ||
new TokenCollector( "foo \\", getInterpolationTermType() ) | ||
.getTokenList() | ||
) | ||
.hasSize( 1 ) | ||
.element( 0 ) | ||
.returns( "foo \\", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...idator/test/internal/engine/messageinterpolation/TokenCollectorMessageExpressionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.test.internal.engine.messageinterpolation; | ||
|
||
import org.hibernate.validator.internal.engine.messageinterpolation.InterpolationTermType; | ||
import org.hibernate.validator.internal.engine.messageinterpolation.parser.MessageDescriptorFormatException; | ||
import org.hibernate.validator.internal.engine.messageinterpolation.parser.Token; | ||
import org.hibernate.validator.internal.engine.messageinterpolation.parser.TokenCollector; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.assertj.core.api.ListAssert; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* Tests for {@code TokenCollector} in message expression mode. | ||
* | ||
* @author Hardy Ferentschik | ||
*/ | ||
public class TokenCollectorMessageExpressionTest extends AbstractTokenCollectorTest { | ||
@Override | ||
protected InterpolationTermType getInterpolationTermType() { | ||
return InterpolationTermType.EL; | ||
} | ||
|
||
// Several tests inherited from the abstract class | ||
|
||
@Test | ||
public void testMessageParameter() { | ||
ListAssert<Token> assertion = Assertions.assertThat( | ||
new TokenCollector( "foo {bar}", getInterpolationTermType() ) | ||
.getTokenList() | ||
) | ||
.hasSize( 2 ); | ||
assertion.element( 0 ) | ||
.returns( "foo ", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
assertion.element( 1 ) | ||
.returns( "{bar}", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
} | ||
|
||
@Test | ||
public void testMessageExpression() { | ||
ListAssert<Token> assertion = Assertions.assertThat( | ||
new TokenCollector( "foo ${bar}", getInterpolationTermType() ) | ||
.getTokenList() | ||
) | ||
.hasSize( 2 ); | ||
assertion.element( 0 ) | ||
.returns( "foo ", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
assertion.element( 1 ) | ||
.returns( "${bar}", Token::getTokenValue ) | ||
.returns( true, Token::isParameter ); | ||
} | ||
|
||
@Test | ||
public void testDollarThenDollarThenParameterInterpretedAsLiterals() { | ||
ListAssert<Token> assertion = Assertions.assertThat( | ||
new TokenCollector( "$${1+1}", getInterpolationTermType() ) | ||
.getTokenList() | ||
) | ||
.hasSize( 2 ); | ||
assertion.element( 0 ) | ||
.returns( "$$", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
assertion.element( 1 ) | ||
.returns( "{1+1}", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
} | ||
|
||
@Test | ||
public void testDollarThenDollarThenLiteralsInterpretedAsLiterals() { | ||
ListAssert<Token> assertion = Assertions.assertThat( | ||
new TokenCollector( "$$foo", getInterpolationTermType() ) | ||
.getTokenList() | ||
) | ||
.hasSize( 2 ); | ||
assertion.element( 0 ) | ||
.returns( "$$", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
assertion.element( 1 ) | ||
.returns( "foo", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
} | ||
|
||
@Test(expectedExceptions = MessageDescriptorFormatException.class, expectedExceptionsMessageRegExp = "HV000168.*") | ||
public void testDollarThenClosingBraceThrowsException() { | ||
new TokenCollector( "$}", getInterpolationTermType() ); | ||
} | ||
|
||
@Test | ||
public void testDollarThenEscapeInterpretedAsLiterals() { | ||
ListAssert<Token> assertion = Assertions.assertThat( | ||
new TokenCollector( "$\\A{1+1}", getInterpolationTermType() ) | ||
.getTokenList() | ||
) | ||
.hasSize( 2 ); | ||
assertion.element( 0 ) | ||
.returns( "$\\A", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
assertion.element( 1 ) | ||
.returns( "{1+1}", Token::getTokenValue ) | ||
.returns( false, Token::isParameter ); | ||
} | ||
} |
Oops, something went wrong.