1 23 package com.sun.enterprise.management; 24 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.Set ; 28 import java.util.HashSet ; 29 30 31 import java.io.Serializable ; 32 33 34 import com.sun.appserv.management.util.misc.TypeCast; 35 36 39 public final class GenericsTest extends junit.framework.TestCase 40 { 41 public 42 GenericsTest() 43 { 44 } 45 46 private static class A implements Serializable 47 { 48 public static final long serialVersionUID = 999; 49 50 public A() {} 51 } 52 53 private static final class AA extends A 54 { 55 public static final long serialVersionUID = 9999; 56 public AA() {} 57 } 58 59 127 128 131 private Set <? extends Serializable > 132 getSetOfSerializableUnknown() 133 { 134 final Set <Serializable > result = new HashSet <Serializable >(); 135 result.add( new String ("hello") ); 136 result.add( new Integer (0) ); 137 result.add( new Boolean (false) ); 138 return result; 139 } 140 141 156 157 158 161 private Set <Serializable > 162 getSetOfSerializable() 163 { 164 final Set <Serializable > result = new HashSet <Serializable >(); 165 result.add( new String ("hello") ); 166 result.add( new Integer (0) ); 167 result.add( new Boolean (false) ); 168 return result; 169 } 170 171 173 public void 174 test_getSetOfSerializable() 175 { 176 final Set <Serializable > s1 = getSetOfSerializable(); 178 TypeCast.checkSet( s1, Serializable .class ); 179 180 final Set <? extends Serializable > s2 = getSetOfSerializable(); 182 TypeCast.checkSet( s2, Serializable .class ); 183 } 184 185 199 200 201 public void 202 testAssign() 203 { 204 final Set <Serializable > serializableSet = new HashSet <Serializable >(); 205 final Set <String > stringSet = new HashSet <String >(); 206 207 serializableSet.add( new Integer (0) ); 209 serializableSet.add( new String () ); 210 serializableSet.add( new Boolean (false) ); 211 212 stringSet.add( "hello" ); 213 214 serializableSet.addAll( stringSet ); 215 216 220 Set <? extends Serializable > unknownSub1 = null; 223 unknownSub1 = stringSet; 224 unknownSub1 = serializableSet; 225 } 226 227 228 public void 229 testCheckedSet() 230 { 231 final Set <Object > s = new HashSet <Object >(); 232 TypeCast.checkSet( s, String .class ); 233 TypeCast.checkSet( s, Integer .class ); 234 235 s.add( "hello" ); 236 TypeCast.checkSet( s, String .class ); 237 TypeCast.checkSet( s, Object .class ); 238 TypeCast.checkSet( s, Serializable .class ); 239 240 try { TypeCast.checkSet( s, Integer .class ); assert false;} 241 catch( Exception e ) {} 242 243 final Set <String > ss = TypeCast.checkedStringSet( s ); 244 try 245 { 246 final Set <Integer > x = TypeCast.asSet(ss); 249 x.add( new Integer (0) ); 250 assert false; 251 } 252 catch( Exception e ) {} 253 254 final Set <String > sss = TypeCast.checkedStringSet( ss ); 255 257 258 final Set <Object > mixed = new HashSet <Object >(); 259 mixed.add( "hello" ); 260 mixed.add( new Integer (0) ); 261 final Set <String > bogus = TypeCast.asSet(mixed); 262 final Set <String > checkedBogus = Collections.checkedSet( bogus, String .class ); 263 try 265 { 266 TypeCast.checkedStringSet( bogus ); 267 } 268 catch( Exception e ) {} 269 } 270 } 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | Popular Tags |