// 2012-11-30 johnpfeiffer
package net.kittyandbear;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
public class PropertiesReaderTest {
// Key/Value Pairs below match the case sensitive sorting order
private static final String KEY1 = "Key1";
private static final String VALUE1 = "one";
private static final String KEY2 = "Key2";
private static final String VALUE2 = "two";
private static final String KEY3 = "KeyHost";
private static final String VALUE3 = "testHostnameValue";
private static final String KEY4 = "KeyPassword";
private static final String VALUE4 = "testPasswordValue";
private static final String KEY5 = "KeyUser";
private static final String VALUE5 = "testUsernameValue";
private static final String KEYBLANKVALUE = "KeyBlankValue";
private static final String VALUEBLANK = "";
private static PropertiesReader testerDefault;
private static File testFile;
@ClassRule
public static TemporaryFolder folder = new TemporaryFolder();
@BeforeClass
public static void setUpOnce() throws Exception {
Properties properties = new Properties();
properties.setProperty( KEY1, VALUE1 );
properties.setProperty( KEY2, VALUE2 );
properties.setProperty( KEY3, VALUE3 );
properties.setProperty( KEY4, VALUE4 );
properties.setProperty( KEYBLANKVALUE, VALUEBLANK );
properties.setProperty( KEY5, VALUE5 );
String filename = "unittest.properties";
testFile = folder.newFile( filename );
System.out.println( "DEBUG: BeforeClass getRoot(): " + folder.getRoot() );
System.out.println( "DEBUG: BeforeClass: " + testFile.getAbsolutePath() );
properties.store( new FileOutputStream( testFile ), null );
testerDefault = new PropertiesReader( testFile.getAbsolutePath() );
}
// hack to fix Junit bug: TemporaryFolder being stored in the maven project root and not being cleaned up properly
@AfterClass
public static void logout() {
if (testFile.exists()) {
testFile.delete();
testFile.deleteOnExit();
}
if (testFile.exists()) {
System.err.println( "UNABLE TO CLEAN UP TESTFILE! AfterClass: " + testFile.getAbsolutePath() );
}
}
@Test
public void testPropertiesReader() throws IOException {
assertEquals( VALUE1, testerDefault.getPropertyValue( KEY1 ) );
assertEquals( VALUE2, testerDefault.getPropertyValue( KEY2 ) );
assertEquals( VALUE3, testerDefault.getPropertyValue( KEY3 ) );
assertEquals( VALUE4, testerDefault.getPropertyValue( KEY4 ) );
assertEquals( VALUE5, testerDefault.getPropertyValue( KEY5 ) );
}
@Test
public void testGetPropertyValueBlank() {
System.out.println( testerDefault.getKeysAndValuesSortedByKeysString() );
assertEquals( "", testerDefault.getPropertyValue( KEYBLANKVALUE ) );
}
@Test
public void testGetKeysAndValues() {
ArrayList<String> result = testerDefault.getKeysAndValues();
assertEquals( 6, result.size() );
assertTrue( result.contains( KEY1 + "=" + VALUE1 ) );
assertTrue( result.contains( KEY2 + "=" + VALUE2 ) );
assertTrue( result.contains( KEY3 + "=" + VALUE3 ) );
assertTrue( result.contains( KEY4 + "=" + VALUE4 ) );
assertTrue( result.contains( KEY5 + "=" + VALUE5 ) );
assertTrue( result.contains( KEYBLANKVALUE + "=" + VALUEBLANK ) );
}
@Test
public void testGetKeysSorted() {
ArrayList<String> resultSortedKeys = testerDefault.getKeysSorted();
assertEquals( KEY1, resultSortedKeys.get( 0 ) );
assertEquals( KEY2, resultSortedKeys.get( 1 ) );
assertEquals( KEYBLANKVALUE , resultSortedKeys.get( 2 ) );
assertEquals( KEY3, resultSortedKeys.get( 3 ) );
assertEquals( KEY4, resultSortedKeys.get( 4 ) );
assertEquals( KEY5, resultSortedKeys.get( 5 ) );
}
@Test
public void testGetKeysAndValuesSortedByKeys() {
ArrayList<String> resultSortedKeys = testerDefault.getKeysAndValuesSortedByKeys();
assertEquals( KEY1 + "=" + VALUE1, resultSortedKeys.get( 0 ) );
assertEquals( KEY2 + "=" + VALUE2, resultSortedKeys.get( 1 ) );
assertEquals( KEYBLANKVALUE + "=" + VALUEBLANK, resultSortedKeys.get( 2 ) );
assertEquals( KEY3 + "=" + VALUE3, resultSortedKeys.get( 3 ) );
assertEquals( KEY4 + "=" + VALUE4, resultSortedKeys.get( 4 ) );
assertEquals( KEY5 + "=" + VALUE5, resultSortedKeys.get( 5 ) );
}
@Test
public void testGetKeysAndValuesSortedByKeysString() {
String expected = KEY1 + "=" + VALUE1 + PropertiesReader.NEWLINE + KEY2 + "=" + VALUE2 + PropertiesReader.NEWLINE +
KEYBLANKVALUE + "=" + VALUEBLANK + PropertiesReader.NEWLINE +
KEY3 + "="+ VALUE3 + PropertiesReader.NEWLINE + KEY4 + "=" + VALUE4 + PropertiesReader.NEWLINE +
KEY5 + "=" + VALUE5 + PropertiesReader.NEWLINE;
assertEquals( expected, testerDefault.getKeysAndValuesSortedByKeysString() );
}
@Test
public void testGetVersion() {
assertEquals( "0.30", PropertiesReader.CLASSVERSION );
}
@Test
public void testPropertiesReaderNull() throws IOException {
try {
new PropertiesReader( null );
}
catch( IllegalArgumentException e ) {
assertEquals( "ERROR: pathFile cannot be null", e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testPropertiesReaderEmpty() throws IOException {
try {
new PropertiesReader( "" );
}
catch( IllegalArgumentException e ) {
assertEquals( "ERROR: pathFile cannot be empty", e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testPropertiesReaderNotFound() throws IOException {
try {
new PropertiesReader( "test" );
}
catch( IllegalArgumentException e ) {
String osNameLowerCase = System.getProperty( "os.name" ).toLowerCase();
String osSpecificError = "No such file or directory";
if (osNameLowerCase.contains( "windows" )) {
osSpecificError = "The system cannot find the file specified";
}
assertEquals( "ERROR: file not found: test (" + osSpecificError + ")", e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testGetPropertyValueNull() {
try {
testerDefault.getPropertyValue( null );
}
catch( IllegalArgumentException e ) {
assertEquals( "ERROR: key cannot be null", e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
@Test
public void testGetPropertyValueEmpty() {
try {
testerDefault.getPropertyValue( "" );
}
catch( IllegalArgumentException e ) {
assertEquals( "ERROR: key cannot be empty", e.getMessage() );
return;
}
fail( "Expected IllegalArgumentException" );
}
} // end class