1 50 package org.apache.avalon.meta.info.test; 51 52 import junit.framework.TestCase; 53 import org.apache.avalon.framework.Version; 54 import org.apache.avalon.meta.info.ReferenceDescriptor; 55 56 import java.io.*; 57 58 64 public class ReferenceDescriptorTestCase extends TestCase 65 { 66 private static final String m_classname = ReferenceDescriptorTestCase.class.getName(); 67 private static final Version m_version = Version.getVersion("1.2.3"); 68 69 public ReferenceDescriptorTestCase( String name ) 70 { 71 super( name ); 72 } 73 74 public void testConstructor() 75 { 76 try 77 { 78 new ReferenceDescriptor(null, m_version); 79 fail("Did not throw the expected NullPointerException"); 80 } 81 catch (NullPointerException npe) 82 { 83 } 85 86 try 87 { 88 new ReferenceDescriptor( null ); 89 fail( "Did not throw the expected NullPointerException" ); 90 } 91 catch ( NullPointerException npe ) 92 { 93 } 95 96 ReferenceDescriptor ref = new ReferenceDescriptor( m_classname, m_version ); 97 checkDescriptor( ref, m_classname, m_version ); 98 99 ref = new ReferenceDescriptor( m_classname + ":3.2.1" ); 100 checkDescriptor( ref, m_classname, Version.getVersion("3.2.1")); 101 } 102 103 private void checkDescriptor(ReferenceDescriptor ref, String classname, Version version) 104 { 105 assertNotNull(ref); 106 assertNotNull(ref.getClassname()); 107 assertEquals(classname, ref.getClassname()); 108 assertNotNull(ref.getVersion()); 109 assertEquals(version, ref.getVersion()); 110 } 111 112 public void testCompliance() 113 { 114 ReferenceDescriptor ref = new ReferenceDescriptor( m_classname, m_version ); 115 ReferenceDescriptor any = new ReferenceDescriptor( m_classname, new Version( -1, 0, 0 ) ); 116 117 assertTrue( "anything matches explicit", any.matches( ref ) ); 118 assertFalse( "explicit does not match anything", ref.matches( any ) ); 119 } 120 121 public void testSerialization() throws IOException, ClassNotFoundException 122 { 123 ReferenceDescriptor entry = new ReferenceDescriptor( m_classname, m_version ); 124 checkDescriptor( entry, m_classname, m_version ); 125 126 File file = new File( "test.out" ); 127 ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream( file ) ); 128 oos.writeObject( entry ); 129 oos.close(); 130 131 ObjectInputStream ois = new ObjectInputStream( new FileInputStream( file ) ); 132 ReferenceDescriptor serialized = (ReferenceDescriptor) ois.readObject(); 133 ois.close(); 134 file.delete(); 135 136 checkDescriptor( entry, m_classname, m_version ); 137 138 assertEquals( entry, serialized ); 139 assertEquals( entry.hashCode(), serialized.hashCode() ); 140 } 141 } | Popular Tags |