john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

PasswordTest

// 2012-09-12 johnpfeiffer
package net.kittyandbear.util;

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.Before;

public class PasswordTest
{
    Password p;
    private static final String CLASSVERSION = "0.35";
    private static String SYMBOLS = "aAbBcCdDeEfFGHJKLMnNPrRStTVWXYy023456789";

    @Before
    public void setUp() throws Exception
    {
    }

    @Test
    public void testGetVersion()
    {
        assertEquals( CLASSVERSION , Password.getVersion() );
    }


    @Test
    public void testPasswordLength()
    {
        String password;
        p = new Password( Password.MINLENGTH );
        password = p.get();
        assertEquals( Password.MINLENGTH , password.length() );

        p = new Password( Password.MAXLENGTH );
        password = p.get();
        assertEquals( Password.MAXLENGTH , password.length() );
    }

    @Test
    public void testPasswordLengthNegative()
    {
        try
        {
            new Password( -1 );
        }catch( IllegalArgumentException e )
        {
            return;
        }
        fail( "Expected IllegalArgumentException" );
    }

    @Test
    public void testPasswordZero()
    {
        try
        {
            new Password( 0 );
        }catch( IllegalArgumentException e )
        {
            return;
        }
        fail( "Expected IllegalArgumentException" );
    }

    @Test
    public void testPasswordLengthGreaterThanMax()
    {
        try
        {
            p = new Password( 100 );
        }catch( IllegalArgumentException e )
        {
            return;
        }
        fail( "Expected IllegalArgumentException" );

    }

    // TODO: improve validation with mock objects?
    @Test
    public void testGet()
    {
        p = new Password( 1 );
        String password = p.get();
        char pc = password.charAt( 0 );
        int location = -1;
        char c;
        for( int i = 0 ; i < SYMBOLS.length() ; i++ )
        {
            c = SYMBOLS.charAt( i );
            if( c == pc )
            {
                location = i;
            }
        }
        if( location == -1 )
        {
            fail( "password contains an illegal character" );
        }
    }

    @Test
    public void testIsValid()
    {
        p = new Password( 1 );
        String password = p.get();
        assertTrue( p.isValid( password ) );
    }

    @Test
    public void testIsValidNull()
    {
        p = new Password( 1 );
        assertFalse( p.isValid( null ) );
    }

    @Test
    public void testIsValidEmptyPassword()
    {
        p = new Password( 1 );
        assertFalse( p.isValid( "" ) );
    }

} // end class

  • « Password
  • Password 0.35.pom.xml »

Published

Sep 12, 2012

Category

java-classes

~198 words

Tags

  • classes 92
  • java 252
  • passwordtest 1