FileDocCategorySizeDatePackage
GenericsTest.javaAPI DocGlassfish v2 API8961Fri May 04 22:23:50 BST 2007com.sun.enterprise.management

GenericsTest

public final class GenericsTest extends TestCase
Demo of generic do/don't issues.

Fields Summary
Constructors Summary
public GenericsTest()

	
Methods Summary
private java.util.SetgetSetOfSerializable()
Method is "good"; no warnings.

	    final Set<Serializable> result    = new HashSet<Serializable>();
	    result.add( new String("hello") );
	    result.add( new Integer(0) );
	    result.add( new Boolean(false) );
	    return result;
	
private java.util.SetgetSetOfSerializableUnknown()
Method is "good"; no warnings.

	    final Set<Serializable> result    = new HashSet<Serializable>();
	    result.add( new String("hello") );
	    result.add( new Integer(0) );
	    result.add( new Boolean(false) );
	    return result;
	
public voidtestAssign()

	    final Set<Serializable> serializableSet  = new HashSet<Serializable>();
	    final Set<String>       stringSet        = new HashSet<String>();
	    
	    // these of course must work; the types match
	    serializableSet.add( new Integer(0) );
	    serializableSet.add( new String() );
	    serializableSet.add( new Boolean(false) );
	    
	    stringSet.add( "hello" );
	    
	    serializableSet.addAll( stringSet );
	    
	    // This is counterintuitive"; we just added all members of
	    // 'stringSet' to 'serializableSet', but we cannot assign the Set itself:
	    //      Set<Serializable> illegal = stringSet;
	    
	   // However, we can make this assignment if we use a wildcard Set,
	   // no further add() calls to the set can be made.
	   Set<? extends Serializable> unknownSub1 = null;
	   unknownSub1  = stringSet;
	   unknownSub1  = serializableSet;
	
public voidtestCheckedSet()

	    final Set<Object> s = new HashSet<Object>();
	    TypeCast.checkSet( s, String.class );
	    TypeCast.checkSet( s, Integer.class );
	    
	    s.add( "hello" );
	    TypeCast.checkSet( s, String.class );
	    TypeCast.checkSet( s, Object.class );
	    TypeCast.checkSet( s, Serializable.class );
	    
	    try { TypeCast.checkSet( s, Integer.class ); assert false;}
	        catch( Exception e ) {/*good, expected*/}
	    
	    final Set<String> ss = TypeCast.checkedStringSet( s );
	    try
	    {
	        // it's NOT a Set<Integer>, but let's verify that
	        // the exception is thrown.
	        final Set<Integer> x = TypeCast.asSet(ss);
	        x.add( new Integer(0) );
	        assert false;
	    }
	    catch( Exception e ) {/*good, expected*/}
	    
	    final Set<String> sss = TypeCast.checkedStringSet( ss );
	    // assert( sss == ss );  bummer, it's not smart enough to not wrap it twice
	    
	    
	    final Set<Object> mixed = new HashSet<Object>();
	    mixed.add( "hello" );
	    mixed.add( new Integer(0) );
	    final Set<String> bogus = TypeCast.asSet(mixed);
	    final Set<String> checkedBogus = Collections.checkedSet( bogus, String.class );
	    // that worked.  Our variant should reject it:
	    try
	    {
	        TypeCast.checkedStringSet( bogus );
	    }
	    catch( Exception e ) {/*good, expected*/}
	
public voidtest_getSetOfSerializable()

        // all OK.  's1' can contain any Serializable
        final Set<Serializable> s1  = getSetOfSerializable();
        TypeCast.checkSet( s1, Serializable.class );
        
        // all OK, no warning.
        final Set<? extends Serializable> s2  = getSetOfSerializable();
        TypeCast.checkSet( s2, Serializable.class );