import javax.validation.ConstraintViolationException;
throws ConstraintViolationException
@NotNull
These concepts are documented in http://docs.oracle.com/javaee/6/api/javax/validation/ConstraintViolationException.html
BUT in order to have Eclipse recognize the above I had to download the API abstract contract
http://repo1.maven.org/maven2/javax/validation/validation-api/1.0.0.GA/
AND the reference implenetation http://www.hibernate.org/subprojects/validator/download.html
(but you could also use Apache Bval http://bval.apache.org/downloads.html )
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
NOTE: assertions are useful but for Debugging/Testing, they incur a performance overhead and are turned off by default
It MAY be more useful then to use dedicated Unit Tests (i.e. external framework) rather than littering your code with things
that don't run (unless you're basically treating them as comments)
http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html_single/
http://docs.oracle.com/javaee/6/api/javax/validation/constraints/package-tree.html
// 2012-06-19 johnpfeiffer requires outbound internet connection for
// InetAddress.getByName(String)
import java.net.InetAddress;
import java.util.HashSet;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class AtmosService
{
protected static final String ATMOSSTORAGETYPE = "EMCstorageAdaptor";
protected static final String PARAMEMCIPADDRESS = "emcIpAddress";
protected static final String PARAMEMCPORT = "emcPortNumber";
protected static final String PARAMEMCUID = "emcUid";
protected static final String PARAMEMCSHAREDSECRET = "emcSharedSecret";
protected static final String PARAMEMCSTORAGEFOLDER = "emcStorageFolder";
protected static final String DEFAULTEMCIPADDRESS = "storage.synaptic.att.com";
protected static final int DEFAULTEMCPORT = 443;
protected static final String DEFAULTUID = "subtenant/uid";
protected static final String DEFAULTSHAREDSECRET = "sharedsecret=";
protected static final String DEFAULTEMCFOLDER = "oxygencloud-storage";
@NotNull( message = "ERROR: Atmos hostname cannot be null" )
private String emcIpAddress = DEFAULTEMCIPADDRESS;
@Min( 0 )
private int emcPortNumber = DEFAULTEMCPORT;
@NotNull( message = "ERROR: Atmos UID cannot be null" )
private String emcUid = DEFAULTUID;
@NotNull( message = "ERROR: Atmos SharedSecret cannot be null" )
private String emcSharedSecret = DEFAULTSHAREDSECRET;
@NotNull( message = "ERROR: Atmos storageFolder cannot be null" )
private String emcStorageFolder = DEFAULTEMCFOLDER;
public static class Builder
{
private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
@NotNull( message = "ERROR: Atmos hostname cannot be null" )
private String emcIpAddress = DEFAULTEMCIPADDRESS;
@Size( min = 0 , max = 65535 , message = "ERROR: port number is out of range" )
private int emcPortNumber = DEFAULTEMCPORT;
@NotNull( message = "ERROR: Atmos UID cannot be null" )
private String emcUid = DEFAULTUID;
@NotNull( message = "ERROR: Atmos SharedSecret cannot be null" )
private String emcSharedSecret = DEFAULTSHAREDSECRET;
@NotNull( message = "ERROR: Atmos storageFolder cannot be null" )
private String emcStorageFolder = DEFAULTEMCFOLDER;
public Builder hostname( String value )
{
this.emcIpAddress = value;
return this;
}
public Builder port( int value )
{
this.emcPortNumber = value;
return this;
}
public Builder uid( String value )
{
this.emcUid = value;
return this;
}
public Builder sharedSecret( String value )
{
this.emcSharedSecret = value;
return this;
}
public AtmosService build() throws ConstraintViolationException, IllegalArgumentException
{
AtmosService atmos = new AtmosService( this );
Set <ConstraintViolation <AtmosService>> violations = validator.validate( atmos );
if( !violations.isEmpty() )
{
throw new ConstraintViolationException( new HashSet <ConstraintViolation <?>>( violations ) );
}
return new AtmosService( this );
}
}
private AtmosService( Builder builder ) throws IllegalArgumentException
{
if( emcIpAddress.isEmpty() )
{
throw new IllegalArgumentException( "ERROR: host cannot be empty" );
}
try
{
InetAddress.getByName( emcIpAddress );
}catch( Exception e )
{
throw new IllegalArgumentException( "ERROR: hostname " + e.getMessage() );
}
if( StringUtils.containsWhiteSpace( emcUid ) )
{
throw new IllegalArgumentException( "ERROR: uid cannot contain whitespace" );
}
if( StringUtils.containsWhiteSpace( emcSharedSecret ) )
{
throw new IllegalArgumentException( "ERROR: sharedSecret cannot contain whitespace" );
}
if( !emcSharedSecret.trim().endsWith( "=" ) )
{
throw new IllegalArgumentException( "ERROR: sharedSecret must end with an =" );
}
this.emcIpAddress = builder.emcIpAddress;
this.emcPortNumber = builder.emcPortNumber;
this.emcUid = builder.emcUid;
this.emcSharedSecret = builder.emcSharedSecret;
this.emcStorageFolder = builder.emcStorageFolder;
}
protected String getString( String header )
{
StringBuilder strb = new StringBuilder();
String newline = System.getProperty( "line.separator" );
strb.append( header + PARAMEMCIPADDRESS + "=" + emcIpAddress + newline );
strb.append( header + PARAMEMCPORT + "=" + emcPortNumber + newline );
strb.append( header + PARAMEMCUID + "=" + emcUid + newline );
strb.append( header + PARAMEMCSHAREDSECRET + "=" + emcSharedSecret + newline );
strb.append( header + PARAMEMCSTORAGEFOLDER + "=" + emcStorageFolder + newline );
return strb.toString();
}
protected String getHTMLForm()
{
StringBuilder strb = new StringBuilder();
String newline = "<br /><br />" + System.getProperty( "line.separator" );
String space = " ";
strb.append( "<label>Atmos Hostname or IP Address:</label>" + space + space + "<input type='text' name='" + PARAMEMCIPADDRESS + "' size='40' value='"
+ StringUtils.safeHTML( emcIpAddress ) + "' /> " + newline );
strb.append( "<label>Port Number: </label> " );
strb.append( "<input type='radio' name='" + PARAMEMCPORT + "' value='80' " );
if( emcPortNumber == 80 )
{
strb.append( "checked='checked'" );
}
strb.append( " /> <label>80</label>" );
strb.append( space + space );
strb.append( "<input type='radio' name='" + PARAMEMCPORT + "' value='443' " );
if( emcPortNumber == 443 )
{
strb.append( "checked='checked'" );
}
strb.append( " /> <label> 443 </label> " + newline );
strb.append( "<label>Atmos Subtenant/UID:</label>" + space + space + "<input type='text' name='" + PARAMEMCUID + "' size='60' value='"
+ StringUtils.safeHTML( emcUid ) + "' /> " + newline );
strb.append( "<label>Atmos Shared Secret:</label>" + space + space + "<input type='text' name='" + PARAMEMCSHAREDSECRET + "' size='40' value='"
+ StringUtils.safeHTML( emcSharedSecret ) + "' /> " + newline );
return strb.toString();
}
} // end class
// 2012-06-19 johnpfeiffer requires outbound internet connection when testing InetAddress.getByName(String)
// TODO: enhanced validation of inputs (port must be digits in range, ip address or hostname in format)
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.junit.Before;
import org.junit.Test;
public class AtmosServiceTest
{
private AtmosService defaultTester;
private static final String constructorHostname = "storage.synaptic.att.com";
private static final int constructorPort = 80;
private static final String constructorUid = "constructor/UID";
private static final String constructorSharedSecret = "constructorSecret=";
@Before
public void setUp() throws Exception
{
defaultTester = new AtmosService.Builder().build();
}
@Test
public void testAtmosService()
{
String expected = expectedAtmosServiceString( "" , AtmosService.DEFAULTEMCIPADDRESS , AtmosService.DEFAULTEMCPORT , AtmosService.DEFAULTUID ,
AtmosService.DEFAULTSHAREDSECRET );
assertEquals( expected , defaultTester.getString( "" ) );
String expectedHTML = expectedAtmosHTMLForm( AtmosService.DEFAULTEMCIPADDRESS , AtmosService.DEFAULTEMCPORT , AtmosService.DEFAULTUID ,
AtmosService.DEFAULTSHAREDSECRET );
assertEquals( expectedHTML , defaultTester.getHTMLForm() );
}
@Test
public void testAtmosServiceStringIntStringString()
{
AtmosService constructor = new AtmosService.Builder().hostname( constructorHostname ).port( constructorPort ).uid( constructorUid )
.sharedSecret( constructorSharedSecret ).build();
assertEquals( expectedAtmosServiceString( "" , constructorHostname , constructorPort , constructorUid , constructorSharedSecret ) ,
constructor.getString( "" ) );
assertEquals( expectedAtmosHTMLForm( constructorHostname , constructorPort , constructorUid , constructorSharedSecret ) , constructor.getHTMLForm() );
}
@Test
public void testAtmosServiceHostnameNull()
{
try
{
new AtmosService.Builder().hostname( null ).build();
}catch( ConstraintViolationException cve )
{
Set <ConstraintViolation <?>> violations = cve.getConstraintViolations();
for( ConstraintViolation <?> v : violations )
{
System.out.println( v.getMessage() );
}
return;
}
fail( "Expected ConstraintViolationException" );
}
@Test
public void testGetString()
{
assertEquals(
expectedAtmosServiceString( "" , AtmosService.DEFAULTEMCIPADDRESS , AtmosService.DEFAULTEMCPORT , AtmosService.DEFAULTUID ,
AtmosService.DEFAULTSHAREDSECRET ) , defaultTester.getString( "" ) );
}
@Test
public void testGetHTMLForm()
{
assertEquals(
expectedAtmosHTMLForm( AtmosService.DEFAULTEMCIPADDRESS , AtmosService.DEFAULTEMCPORT , AtmosService.DEFAULTUID , AtmosService.DEFAULTSHAREDSECRET ) ,
defaultTester.getHTMLForm() );
}
private String expectedAtmosServiceString( String header , String emcIpAddress , int emcPortNumber , String emcUid , String emcSharedSecret )
{
StringBuilder strb = new StringBuilder();
String newline = System.getProperty( "line.separator" );
strb.append( header + AtmosService.PARAMEMCIPADDRESS + "=" + emcIpAddress + newline );
strb.append( header + AtmosService.PARAMEMCPORT + "=" + emcPortNumber + newline );
strb.append( header + AtmosService.PARAMEMCUID + "=" + emcUid + newline );
strb.append( header + AtmosService.PARAMEMCSHAREDSECRET + "=" + emcSharedSecret + newline );
strb.append( header + AtmosService.PARAMEMCSTORAGEFOLDER + "=" + AtmosService.DEFAULTEMCFOLDER + newline );
return strb.toString();
}
private String expectedAtmosHTMLForm( String emcIpAddress , int emcPortNumber , String emcUid , String emcSharedSecret )
{
StringBuilder strb = new StringBuilder();
String newline = "<br /><br />" + System.getProperty( "line.separator" );
String space = " ";
strb.append( "<label>Atmos Hostname or IP Address:</label>" + space + space + "<input type='text' name='" + AtmosService.PARAMEMCIPADDRESS
+ "' size='40' value='" + StringUtils.safeHTML( emcIpAddress ) + "' /> " + newline );
strb.append( "<label>Port Number: </label> " );
strb.append( "<input type='radio' name='" + AtmosService.PARAMEMCPORT + "' value='80' " );
if( emcPortNumber == 80 )
{
strb.append( "checked='checked'" );
}
strb.append( " /> <label>80</label>" );
strb.append( space + space );
strb.append( "<input type='radio' name='" + AtmosService.PARAMEMCPORT + "' value='443' " );
if( emcPortNumber == 443 )
{
strb.append( "checked='checked'" );
}
strb.append( " /> <label> 443 </label> " + newline );
strb.append( "<label>Atmos Subtenant/UID:</label>" + space + space + "<input type='text' name='" + AtmosService.PARAMEMCUID + "' size='60' value='"
+ StringUtils.safeHTML( emcUid ) + "' /> " + newline );
strb.append( "<label>Atmos Shared Secret:</label>" + space + space + "<input type='text' name='" + AtmosService.PARAMEMCSHAREDSECRET
+ "' size='40' value='" + StringUtils.safeHTML( emcSharedSecret ) + "' /> " + newline );
return strb.toString();
}
} // end class