1 50 package org.apache.avalon.meta.info.test; 51 52 import org.apache.avalon.meta.info.Descriptor; 53 54 import java.io.*; 55 import java.util.Properties ; 56 57 import junit.framework.TestCase; 58 59 60 66 public abstract class AbstractDescriptorTestCase extends TestCase 67 { 68 protected static final String VALID_KEY = "key"; 69 protected static final String VALID_VALUE = "value"; 70 protected static final String INVALID_KEY = "bad-key"; 71 protected static final String DEFAULT_VALUE = "default"; 72 73 public AbstractDescriptorTestCase( String name ) 74 { 75 super( name ); 76 } 77 78 protected Properties getProperties() 79 { 80 Properties props = new Properties (); 81 props.put( VALID_KEY, VALID_VALUE ); 82 83 return props; 84 } 85 86 protected abstract Descriptor getDescriptor(); 87 88 protected void checkDescriptor( Descriptor desc ) 89 { 90 assertEquals( VALID_VALUE, desc.getAttribute( VALID_KEY ) ); 91 assertEquals( DEFAULT_VALUE, desc.getAttribute( INVALID_KEY, DEFAULT_VALUE ) ); 92 93 boolean hasValid = false; 94 boolean hasInvalid = false; 95 String [] names = desc.getAttributeNames(); 96 97 assertNotNull( names ); 98 assertTrue( names.length > 0 ); 99 100 for ( int i = 0; i < names.length; i++ ) 101 { 102 if ( VALID_KEY.equals( names[i] ) ) hasValid = true; 103 if ( INVALID_KEY.equals( names[i] ) ) hasInvalid = true; 104 } 105 106 assertTrue( hasValid ); 107 assertTrue( !hasInvalid ); 108 } 109 110 public void testSerialization() throws IOException, ClassNotFoundException 111 { 112 Descriptor desc = getDescriptor(); 113 checkDescriptor( desc ); 114 115 File file = new File( "test.file" ); 116 ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream( file ) ); 117 oos.writeObject( desc ); 118 oos.close(); 119 120 ObjectInputStream ois = new ObjectInputStream( new FileInputStream( file ) ); 121 Descriptor serialized = (Descriptor) ois.readObject(); 122 ois.close(); 123 file.delete(); 124 125 assertTrue( desc != serialized ); checkDescriptor( serialized ); 127 128 assertEquals( desc, serialized ); 129 assertEquals( desc.hashCode(), serialized.hashCode() ); 130 } 131 } | Popular Tags |