KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > meta > info > test > TypeTestCase


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6
7  Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24
25  4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
26     must not be used to endorse or promote products derived from this software
27     without prior written permission. For written permission, please contact
28     apache@apache.org.
29
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation. For more information on the
47  Apache Software Foundation, please see <http://www.apache.org/>.
48
49 */

50 package org.apache.avalon.meta.info.test;
51
52 import junit.framework.TestCase;
53 import org.apache.avalon.meta.info.*;
54 import org.apache.avalon.framework.configuration.Configuration;
55 import org.apache.avalon.framework.configuration.DefaultConfiguration;
56
57 import java.util.Properties JavaDoc;
58 import java.io.*;
59
60 /**
61  * TypeTestCase does XYZ
62  *
63  * @author <a HREF="bloritsch.at.apache.org">Berin Loritsch</a>
64  * @version CVS $ Revision: 1.1 $
65  */

66 public class TypeTestCase extends TestCase
67 {
68     private InfoDescriptor m_descriptor;
69     private CategoryDescriptor[] m_loggers;
70     private ContextDescriptor m_context;
71     private ServiceDescriptor[] m_services;
72     private DependencyDescriptor[] m_dependencies;
73     private StageDescriptor[] m_stages;
74     private ExtensionDescriptor[] m_extensions;
75     private Configuration m_defaults;
76     private ReferenceDescriptor m_reference;
77     private String JavaDoc m_key;
78
79     public TypeTestCase( String JavaDoc name )
80     {
81         super( name );
82     }
83
84     public void setUp()
85     {
86         m_reference = new ReferenceDescriptor(TypeTestCase.class.getName());
87         m_key = TypeTestCase.class.getName();
88         m_descriptor = new InfoDescriptor(TypeTestCase.class.getName());
89         m_loggers = new CategoryDescriptor[] {
90             new CategoryDescriptor("name", new Properties JavaDoc())
91         };
92         m_context = new ContextDescriptor(
93           TypeTestCase.class.getName(), new EntryDescriptor[0]);
94         m_services = new ServiceDescriptor[] {
95             new ServiceDescriptor(m_reference)
96         };
97         m_dependencies = new DependencyDescriptor[] {
98             new DependencyDescriptor("role", m_reference)
99         };
100         m_stages = new StageDescriptor[] {
101             new StageDescriptor( m_key )
102         };
103         m_extensions = new ExtensionDescriptor[] {
104             new ExtensionDescriptor( m_key )
105         };
106         m_defaults = new DefaultConfiguration("default");
107     }
108
109     private void checkType(Type type)
110     {
111         assertNotNull(type);
112         checkArray(m_loggers, type.getCategories());
113         assertEquals( m_defaults, type.getConfiguration() );
114         assertEquals( m_context, type.getContext());
115         checkArray(m_dependencies, type.getDependencies());
116         assertEquals(m_dependencies[0], type.getDependency(m_dependencies[0].getKey()));
117         assertEquals(m_extensions[0], type.getExtension( m_stages[0].getKey() ) );
118         checkArray(m_extensions, type.getExtensions());
119         assertEquals( m_descriptor, type.getInfo() );
120         assertEquals( m_services[0], type.getService(m_reference));
121         assertEquals( m_services[0], type.getService( m_services[0].getReference().getClassname()));
122         checkArray(m_services, type.getServices());
123         checkArray(m_stages, type.getStages());
124         assertTrue(type.isaCategory(m_loggers[0].getName()));
125         assertTrue( !type.isaCategory( "fake name" ) );
126     }
127
128     private void checkArray( Object JavaDoc[] orig, Object JavaDoc[] other )
129     {
130         assertEquals(orig.length, other.length);
131         for (int i = 0; i < orig.length; i++)
132         {
133             assertEquals( orig[i], other[i] );
134         }
135     }
136
137     public void testType()
138     {
139         Type type =
140           new Type(
141             m_descriptor, m_loggers, m_context, m_services, m_dependencies,
142             m_stages, m_extensions, m_defaults);
143         checkType(type);
144     }
145
146     public void testSerialization() throws IOException, ClassNotFoundException JavaDoc
147     {
148         Type type =
149           new Type(
150             m_descriptor, m_loggers, m_context, m_services, m_dependencies,
151             m_stages, m_extensions, m_defaults );
152
153         checkType( type );
154
155         File file = new File( "test.out" );
156         ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream( file ) );
157         oos.writeObject( type );
158         oos.close();
159
160         ObjectInputStream ois = new ObjectInputStream( new FileInputStream( file ) );
161         Type serialized = (Type) ois.readObject();
162         ois.close();
163         file.delete();
164
165         checkType( serialized );
166
167         assertEquals( "equality", type, serialized );
168         assertEquals( "hashcode", type.hashCode(), serialized.hashCode() );
169
170     }
171  }
Popular Tags