Object name validator component
* Moved the component from the -user package to the -naming package * Added a separate interface to the component
This commit is contained in:
parent
35d8891fe3
commit
b4903d78e4
14 changed files with 483 additions and 104 deletions
legacyworlds-server-tests/src/test/java/com/deepclone/lw/beans/naming
|
@ -0,0 +1,249 @@
|
|||
package com.deepclone.lw.beans.naming;
|
||||
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.deepclone.lw.cmd.ObjectNameError;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Tests for {@link ObjectNameValidatorBean}
|
||||
*
|
||||
* @author <a href="mailto:tseeker@legacyworlds.com">E. Benoît</a>
|
||||
*
|
||||
*/
|
||||
public class TestObjectNameValidatorBean
|
||||
{
|
||||
|
||||
/** Object name validator being tested */
|
||||
private ObjectNameValidatorBean validator;
|
||||
|
||||
|
||||
/**
|
||||
* Create the validator to run tests on
|
||||
*/
|
||||
@Before
|
||||
public void setUp( )
|
||||
{
|
||||
this.validator = new ObjectNameValidatorBean( );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A string shorter than the default minimal length will cause validation to fail with the
|
||||
* {@link ObjectNameError#INVALID} error on an unmodified validator.
|
||||
*/
|
||||
@Test
|
||||
public void testDefaultMinimalLength( )
|
||||
{
|
||||
String test = "";
|
||||
for ( int i = 0 ; i < ObjectNameValidatorBean.MIN_LENGTH - 1 ; i++ ) {
|
||||
test += "x";
|
||||
}
|
||||
|
||||
ObjectNameError error = this.validator.validate( test );
|
||||
assertNotNull( error );
|
||||
assertEquals( ObjectNameError.INVALID , error );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A string longer than the default maximal length will cause validation to fail with the
|
||||
* {@link ObjectNameError#INVALID} error on an unmodified validator.
|
||||
*/
|
||||
@Test
|
||||
public void testDefaultMaximalLength( )
|
||||
{
|
||||
String test = "";
|
||||
for ( int i = 0 ; i < ObjectNameValidatorBean.MAX_LENGTH + 1 ; i++ ) {
|
||||
test += "x";
|
||||
}
|
||||
|
||||
ObjectNameError error = this.validator.validate( test );
|
||||
assertNotNull( error );
|
||||
assertEquals( ObjectNameError.INVALID , error );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A string shorter than a modified minimal length will cause validation to fail with the
|
||||
* {@link ObjectNameError#INVALID} error.
|
||||
*/
|
||||
@Test
|
||||
public void testMinimalLength( )
|
||||
{
|
||||
String test = "";
|
||||
for ( int i = 0 ; i < 3 ; i++ ) {
|
||||
test += "x";
|
||||
}
|
||||
|
||||
this.validator.setMinLength( 4 );
|
||||
this.validator.setMaxLength( 10 );
|
||||
ObjectNameError error = this.validator.validate( test );
|
||||
assertNotNull( error );
|
||||
assertEquals( ObjectNameError.INVALID , error );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A string longer than a modified maximal length will cause validation to fail with the
|
||||
* {@link ObjectNameError#INVALID} error on an unmodified validator.
|
||||
*/
|
||||
@Test
|
||||
public void testMaximalLength( )
|
||||
{
|
||||
String test = "";
|
||||
for ( int i = 0 ; i < 11 ; i++ ) {
|
||||
test += "x";
|
||||
}
|
||||
|
||||
this.validator.setMinLength( 4 );
|
||||
this.validator.setMaxLength( 10 );
|
||||
ObjectNameError error = this.validator.validate( test );
|
||||
assertNotNull( error );
|
||||
assertEquals( ObjectNameError.INVALID , error );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A string that's empty will cause an {@link ObjectNameError#EMPTY} error.
|
||||
*/
|
||||
@Test
|
||||
public void testEmptyString( )
|
||||
{
|
||||
String test = "";
|
||||
ObjectNameError error = this.validator.validate( test );
|
||||
assertNotNull( error );
|
||||
assertEquals( ObjectNameError.EMPTY , error );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A string that's full of spaces will cause an {@link ObjectNameError#EMPTY} error.
|
||||
*/
|
||||
@Test
|
||||
public void testSpacesOnlyString( )
|
||||
{
|
||||
String test = " ";
|
||||
ObjectNameError error = this.validator.validate( test );
|
||||
assertNotNull( error );
|
||||
assertEquals( ObjectNameError.EMPTY , error );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* An otherwise valid string that starts with spaces will cause an
|
||||
* {@link ObjectNameError#INVALID} error.
|
||||
*/
|
||||
@Test
|
||||
public void testStringStartsWithSpace( )
|
||||
{
|
||||
String test = " abcde";
|
||||
this.validator.setMinLength( 1 );
|
||||
this.validator.setMaxLength( 10 );
|
||||
ObjectNameError error = this.validator.validate( test );
|
||||
assertNotNull( error );
|
||||
assertEquals( ObjectNameError.INVALID , error );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* An otherwise valid string that ends with spaces will cause an {@link ObjectNameError#INVALID}
|
||||
* error.
|
||||
*/
|
||||
@Test
|
||||
public void testStringEndsWithSpace( )
|
||||
{
|
||||
String test = "abcde ";
|
||||
this.validator.setMinLength( 1 );
|
||||
this.validator.setMaxLength( 10 );
|
||||
ObjectNameError error = this.validator.validate( test );
|
||||
assertNotNull( error );
|
||||
assertEquals( ObjectNameError.INVALID , error );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* An otherwise valid string that contains a sequence of two spaces will cause an
|
||||
* {@link ObjectNameError#INVALID} error.
|
||||
*/
|
||||
@Test
|
||||
public void testStringSpaceSequence( )
|
||||
{
|
||||
String test = "ab cde";
|
||||
this.validator.setMinLength( 1 );
|
||||
this.validator.setMaxLength( 10 );
|
||||
ObjectNameError error = this.validator.validate( test );
|
||||
assertNotNull( error );
|
||||
assertEquals( ObjectNameError.INVALID , error );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A string that contains an unsupported character will cause an {@link ObjectNameError#INVALID}
|
||||
* error.
|
||||
*/
|
||||
@Test
|
||||
public void testStringInvalidCharacter( )
|
||||
{
|
||||
String test = "abcdœ";
|
||||
this.validator.setMinLength( 1 );
|
||||
this.validator.setMaxLength( 10 );
|
||||
ObjectNameError error = this.validator.validate( test );
|
||||
assertNotNull( error );
|
||||
assertEquals( ObjectNameError.INVALID , error );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A string that does not contain any letter (but is otherwise valid) will cause an
|
||||
* {@link ObjectNameError#INVALID} error.
|
||||
*/
|
||||
@Test
|
||||
public void testStringNoLetters( )
|
||||
{
|
||||
String test = "@_@12";
|
||||
this.validator.setMinLength( 1 );
|
||||
this.validator.setMaxLength( 10 );
|
||||
ObjectNameError error = this.validator.validate( test );
|
||||
assertNotNull( error );
|
||||
assertEquals( ObjectNameError.INVALID , error );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test validation on a "good" string
|
||||
*/
|
||||
@Test
|
||||
public void testValidString( )
|
||||
{
|
||||
this.validator.setMinLength( 1 );
|
||||
this.validator.setMaxLength( 5 );
|
||||
|
||||
ObjectNameError error = this.validator.validate( "ab12@" );
|
||||
assertNull( error );
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Test validation with call-specific length constraints
|
||||
*/
|
||||
@Test
|
||||
public void testLengthConstraints( )
|
||||
{
|
||||
this.validator.setMinLength( 5 );
|
||||
this.validator.setMaxLength( 6 );
|
||||
|
||||
ObjectNameError error = this.validator.validate( "ab" , 2 , 10 );
|
||||
assertNull( error );
|
||||
|
||||
error = this.validator.validate( "a012345678" , 2 , 10 );
|
||||
assertNull( error );
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue