// 2012-06-25 johnpfeiffer
// TODO: easymock
// TODO: Write = fstab missing or readonly
// TODO: WRITETEST if( value.contains( CIFSService.PARAMCIFSSTORAGEFOLDER ) || value.contains( NFSService.PARAMNFSSTORAGEFOLDER ) && !value.startsWith( "#" ) )
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertEquals;
public class CIFSServiceDAOTest
{
private CIFSServiceDAO testerDefault;
private PrintWriter fileOut;
private static final String TESTCIFSSHARE = "//testserver/testshare";
private static final String TESTUSER = "testuser";
private static final String TESTPASSWORD = "testpassword";
@Before
public void setUp() throws IOException , InstantiationException
{
fileOut = new PrintWriter( CIFSServiceDAO.CIFSSERVERFILE );
fileOut.println( CIFSService.PARAMCIFSSHARE + "=" + TESTCIFSSHARE );
fileOut.close();
fileOut = new PrintWriter( CIFSServiceDAO.CIFSCREDENTIALSFILE );
fileOut.println( CIFSService.PARAMCIFSUSER + "=" + TESTUSER );
fileOut.println( CIFSService.PARAMCIFSPASSWORD + "=" + TESTPASSWORD );
fileOut.close();
testerDefault = new CIFSServiceDAO.Builder().build();
}
@Test
public void testCIFSServiceDAO()
{
String expectedHTML = expectedHTMLForm( TESTCIFSSHARE , TESTUSER );
assertEquals( expectedHTML , testerDefault.getHTMLForm() );
}
@Test
public void testCIFSServiceDAOServerFileMissing() throws IOException
{
File f = new File( CIFSServiceDAO.CIFSSERVERFILE );
f.delete();
try
{
new CIFSServiceDAO.Builder().build();
}catch( InstantiationException e )
{
return;
}
fail( "Expected InstantiationException" );
}
@Test
public void testCIFSServiceDAOServerFileHostEmpty() throws IOException
{
fileOut = new PrintWriter( CIFSServiceDAO.CIFSSERVERFILE );
fileOut.println();
fileOut.close();
try
{
new CIFSServiceDAO.Builder().build();
}catch( InstantiationException e )
{
return;
}
fail( "Expected InstantiationException" );
}
@Test
public void testCIFSServiceDAOServerFileTooManyLines() throws IOException
{
fileOut = new PrintWriter( CIFSServiceDAO.CIFSSERVERFILE );
fileOut.println( "line" );
fileOut.println( "line" );
fileOut.close();
try
{
new CIFSServiceDAO.Builder().build();
}catch( InstantiationException e )
{
return;
}
fail( "Expected InstantiationException" );
}
@Test
public void testCIFSServiceDAOServerFileCIFSShareContainsWhitespace() throws IOException
{
fileOut = new PrintWriter( CIFSServiceDAO.CIFSSERVERFILE );
fileOut.println( CIFSService.PARAMCIFSSHARE + "=" + "contains whitespace" );
fileOut.close();
try
{
new CIFSServiceDAO.Builder().build();
}catch( InstantiationException e )
{
return;
}
fail( "Expected InstantiationException" );
}
@Test
public void testCIFSServiceDAOCredentialsFileMissing() throws IOException
{
File f = new File( CIFSServiceDAO.CIFSCREDENTIALSFILE );
f.delete();
try
{
new CIFSServiceDAO.Builder().build();
}catch( InstantiationException e )
{
return;
}
fail( "Expected InstantiationException" );
}
@Test
public void testCIFSServiceDAOCredentialsFileSingleLineInvalid() throws IOException
{
fileOut = new PrintWriter( CIFSServiceDAO.CIFSCREDENTIALSFILE );
fileOut.println( CIFSService.PARAMCIFSUSER + "=" + TESTUSER );
fileOut.close();
try
{
new CIFSServiceDAO.Builder().build();
}catch( InstantiationException e )
{
return;
}
fail( "Expected InstantiationException" );
}
@Test
public void testCIFSServiceDAOCredentialsFileTooManyLines() throws IOException
{
fileOut = new PrintWriter( CIFSServiceDAO.CIFSCREDENTIALSFILE );
fileOut.println( "line" );
fileOut.println( "line" );
fileOut.println( "line" );
fileOut.close();
try
{
new CIFSServiceDAO.Builder().build();
}catch( InstantiationException e )
{
return;
}
fail( "Expected InstantiationException" );
}
@Test
public void testCIFSServiceDAOConstructor() throws InstantiationException
{
String constructorCIFSShare = "//constructorTesthost/share";
String constructorUser = "constructorTestuser";
String constructorPassword = "constructorTestpassword";
CIFSService newCifs = new CIFSService.Builder().cifsshare( constructorCIFSShare ).user( constructorUser ).password( constructorPassword ).build();
CIFSServiceDAO newCIFSDAO = new CIFSServiceDAO.Builder().cifs( newCifs ).build();
String expectedHTML = expectedHTMLForm( constructorCIFSShare , constructorUser );
assertEquals( expectedHTML , newCIFSDAO.getHTMLForm() );
assertEquals( constructorCIFSShare , newCIFSDAO.getCIFSService().getCIFSShare() );
assertEquals( constructorUser , newCIFSDAO.getCIFSService().getUser() );
assertEquals( constructorPassword , newCIFSDAO.getCIFSService().getPassword() );
}
@Test
public void testCIFSServiceDAOConstructorCIFSNull() throws InstantiationException
{
try
{
new CIFSServiceDAO.Builder().cifs( null ).build();
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Should create IllegalArgumentException" );
}
@Test
public void testGetHTMLForm()
{
assertEquals( expectedHTMLForm( TESTCIFSSHARE , TESTUSER ) , testerDefault.getHTMLForm() );
}
private String expectedHTMLForm( String host , String user )
{
StringBuilder strb = new StringBuilder();
String newline = "<br /><br />" + System.getProperty( "line.separator" );
String space = " ";
strb.append( "<label>CIFS Server Address: </label>" + space + space + "<input type='text' name='" + CIFSService.PARAMCIFSSHARE + "' size='40' value='"
+ StringUtils.safeHTML( host ) + "' /> " + newline );
strb.append( "<label>CIFS Username: </label>" + space + space + "<input type='text' name='" + CIFSService.PARAMCIFSUSER + "' size='40' value='"
+ StringUtils.safeHTML( user ) + "' /> " + newline );
strb.append( "<label>CIFS Password: </label>" + space + space + "<input type='password' name='" + CIFSService.PARAMCIFSPASSWORD + "' size='40' /> "
+ newline );
return strb.toString();
}
@Test
public void testCIFSServiceDAOWriteToFile() throws IOException, InstantiationException
{
String writeCIFSShare = "//writeTesthost/share";
String writeUser = "writeTestuser";
String writePassword = "writeTestpassword";
CIFSService cifsTemp = new CIFSService.Builder().cifsshare( writeCIFSShare ).user( writeUser ).password( writePassword ).build();
CIFSServiceDAO newCIFSDAO = new CIFSServiceDAO.Builder().cifs( cifsTemp ).build();
newCIFSDAO.writeToFile();
FileSystem fs = new FileSystem();
String expected = null;
String cifsshare = fs.stringFromFile( CIFSServiceDAO.CIFSSERVERFILE );
expected = CIFSService.PARAMCIFSSHARE + "=" + writeCIFSShare + System.getProperty( "line.separator" );
assertEquals( expected , cifsshare );
String credentials = fs.stringFromFile( CIFSServiceDAO.CIFSCREDENTIALSFILE );
expected = CIFSService.PARAMCIFSUSER + "=" + writeUser + System.getProperty( "line.separator" );
expected = expected + CIFSService.PARAMCIFSPASSWORD + "=" + writePassword + System.getProperty( "line.separator" );
assertEquals( expected , credentials );
}
} // end class