john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

PropertiesDuplicateCheckerTest

// 2012-07-05 johnpfeiffer

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class PropertiesDuplicateCheckerTest
{

    private static final String CLASSVERSION = "0.2";
    private static final String NEWLINE = System.getProperty( "line.separator" );
    private static final String PROPERTY1 = "property1=value1";
    private static final String PROPERTY2 = "property2=value2";

    private String testFilePath;
    private File testFile;
    private PropertiesDuplicateChecker testerDefault;

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    @Before
    public void setUp() throws Exception
    {
        testFile = folder.newFile( "defaultTestFile.txt" );
        testFilePath = testFile.getAbsolutePath();
        testerDefault = new PropertiesDuplicateChecker( testFilePath );
    }

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

    @Test
    public void testPropertiesDuplicateCheckerNull() throws IOException
    {
        try
        {
            testerDefault = new PropertiesDuplicateChecker( null );
        }catch( IllegalArgumentException e )
        {
            assertEquals( "ERROR: Path and File cannot be null" , e.getMessage() );
        }
    }

    @Test
    public void testPropertiesDuplicateCheckerEmpty() throws IOException
    {
        try
        {
            testerDefault = new PropertiesDuplicateChecker( "" );
        }catch( IllegalArgumentException e )
        {
            assertEquals( "ERROR: Path and File cannot be empty" , e.getMessage() );
        }
    }

    @Test
    public void testPropertiesDuplicateCheckerSingleLine() throws IOException
    {
        writeToTestFile( PROPERTY1 );
        PropertiesDuplicateChecker test = new PropertiesDuplicateChecker( testFile.getAbsolutePath() );
        assertFalse( test.containsDuplicateKeys() );
    }

    @Test
    public void testPropertiesDuplicateCheckerSingleLineTrailingNewline() throws IOException
    {
        writeToTestFile( PROPERTY1 + NEWLINE );
        PropertiesDuplicateChecker test = new PropertiesDuplicateChecker( testFile.getAbsolutePath() );
        assertFalse( test.containsDuplicateKeys() );
    }

    @Test
    public void testPropertiesDuplicateCheckerMultiLine() throws IOException
    {
        writeToTestFile( PROPERTY1 + NEWLINE + PROPERTY1 );
        PropertiesDuplicateChecker test = new PropertiesDuplicateChecker( testFile.getAbsolutePath() );
        assertTrue( test.containsDuplicateKeys() );
    }

    @Test
    public void testPropertiesDuplicateCheckerMultiLineTrailingNewline() throws IOException
    {
        writeToTestFile( PROPERTY1 + NEWLINE + PROPERTY1 + NEWLINE );
        PropertiesDuplicateChecker test = new PropertiesDuplicateChecker( testFile.getAbsolutePath() );
        assertTrue( test.containsDuplicateKeys() );
        String array[] = PROPERTY1.split( "=" );
        String expected = array[0];
        ArrayList <String> result = test.getDuplicateKeys();
        assertEquals( expected , result.get( 0 ) );
    }

    @Test
    public void testPropertiesDuplicateCheckerMultiLineTrailingNewlineNoDuplicate() throws IOException
    {
        writeToTestFile( PROPERTY1 + NEWLINE + PROPERTY2 + NEWLINE );
        PropertiesDuplicateChecker test = new PropertiesDuplicateChecker( testFile.getAbsolutePath() );
        assertFalse( test.containsDuplicateKeys() );
    }

    @Test
    public void testPropertiesDuplicateCheckerDoubleDuplicates() throws IOException
    {
        writeToTestFile( PROPERTY1 + NEWLINE + PROPERTY1 + NEWLINE + PROPERTY2 + NEWLINE + PROPERTY2 + NEWLINE  );
        PropertiesDuplicateChecker test = new PropertiesDuplicateChecker( testFile.getAbsolutePath() );
        assertTrue( test.containsDuplicateKeys() );
        String array1[] = PROPERTY1.split( "=" );
        String expected1 = array1[0];
        String array2[] = PROPERTY2.split( "=" );
        String expected2 = array2[0];
        ArrayList <String> result = test.getDuplicateKeys();
        assertEquals( expected1 , result.get( 0 ) );
        assertEquals( expected2 , result.get( 1 ) );
    }



    private void writeToTestFile( String fileContent )
    {
        testFile = null;
        try
        {
            testFile = folder.newFile( "testFile.txt" );
            FileWriter outFile = new FileWriter( testFile );
            PrintWriter out = new PrintWriter( outFile );
            out.print( fileContent );
            out.close();
        }catch( Exception e )
        {
            e.printStackTrace();
        }
    }

} // end class

  • « javascript validate html form
  • PropertiesDuplicateChecker »

Published

Jul 6, 2012

Category

java-classes

~313 words

Tags

  • classes 92
  • java 252
  • propertiesduplicatecheckertest 1