KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > framework > test > ValuedEnumTestCase


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.framework.test;
19
20 import org.apache.avalon.framework.ValuedEnum;
21
22 import junit.framework.TestCase;
23
24 import java.util.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 /**
28  *
29  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
30  * @version $Id: ValuedEnumTestCase.java,v 1.3 2004/02/24 22:31:22 niclas Exp $
31  */

32 public class ValuedEnumTestCase extends TestCase
33 {
34     private final static class Color extends ValuedEnum
35     {
36         public static final Color RED = new Color( "Red", 0 );
37         public static final Color RED_NEGATIVE = new Color( "Red", -1 );
38         public static final Color GREEN = new Color( "Green", 1 );
39         public static final Color BLUE = new Color( "Blue", 2 );
40
41         public Color( final String JavaDoc color, final int value )
42         {
43             super( color, value );
44         }
45
46         public Color( final String JavaDoc color, final int value, Map JavaDoc stuff )
47         {
48             super( color, value, stuff );
49         }
50     }
51
52     private final static class OtherColor extends ValuedEnum
53     {
54         public static final OtherColor RED = new OtherColor( "Red", 0 );
55         public static final OtherColor RED_NEGATIVE = new OtherColor( "Red", -1 );
56         public static final OtherColor GREEN = new OtherColor( "Green", 1 );
57         public static final OtherColor BLUE = new OtherColor( "Blue", 2 );
58
59         public OtherColor( final String JavaDoc color, final int value )
60         {
61             super( color, value );
62         }
63
64         public OtherColor( final String JavaDoc color, final int value, Map JavaDoc stuff )
65         {
66             super( color, value, stuff );
67         }
68     }
69
70     public ValuedEnumTestCase( final String JavaDoc name )
71     {
72         super( name );
73     }
74
75     public void testConstructor()
76     {
77         assertNotNull( new Color( "blah", 0, null ) );
78
79         Map JavaDoc entries = new HashMap JavaDoc();
80
81         Color c = new Color( "blah", 0, entries );
82
83         assertTrue( entries.containsKey("blah") );
84         assertTrue( entries.containsValue(c) );
85
86         OtherColor c2 = new OtherColor( "blah", 0, entries );
87         assertTrue( entries.containsKey("blah") );
88         assertFalse( entries.containsValue(c) );
89         assertTrue( entries.containsValue(c2) );
90     }
91
92     public void testEquals()
93     {
94         assertTrue( Color.RED.equals( Color.RED ) );
95         assertTrue( Color.GREEN.equals( Color.GREEN ) );
96         assertTrue( Color.BLUE.equals( Color.BLUE ) );
97
98         assertTrue( !OtherColor.RED.equals( Color.RED ) );
99         assertTrue( !OtherColor.GREEN.equals( Color.GREEN ) );
100         assertTrue( !OtherColor.BLUE.equals( Color.BLUE ) );
101
102         assertTrue( !Color.RED.equals( OtherColor.RED ) );
103         assertTrue( !Color.GREEN.equals( OtherColor.GREEN ) );
104         assertTrue( !Color.BLUE.equals( OtherColor.BLUE ) );
105
106         assertTrue( !Color.RED.equals( Color.GREEN ) );
107         assertTrue( !Color.GREEN.equals( Color.BLUE ) );
108         assertTrue( !Color.BLUE.equals( Color.RED ) );
109
110         assertTrue( !Color.BLUE.equals( null ) );
111
112         assertTrue( new Color(null,0).equals( new Color( null,0 ) ) );
113         assertFalse( new Color(null,0).equals( new Color( "hi",0 ) ) );
114         assertFalse( new Color("hi",0).equals( new Color( null,0 ) ) );
115
116         // todo: is this _really_ desired?
117
assertTrue( Color.RED.equals( Color.RED_NEGATIVE ) );
118         assertTrue( Color.RED_NEGATIVE.equals( Color.RED ) );
119         assertTrue( OtherColor.RED.equals( OtherColor.RED_NEGATIVE ) );
120         assertTrue( OtherColor.RED_NEGATIVE.equals( OtherColor.RED ) );
121     }
122
123     public void testHashCode()
124     {
125         assertTrue( Color.RED.hashCode() == Color.RED.hashCode() );
126         assertTrue( Color.GREEN.hashCode() == Color.GREEN.hashCode() );
127         assertTrue( Color.BLUE.hashCode() == Color.BLUE.hashCode() );
128
129         assertTrue( OtherColor.RED.hashCode() != Color.RED.hashCode() );
130         assertTrue( OtherColor.GREEN.hashCode() != Color.GREEN.hashCode() );
131         assertTrue( OtherColor.BLUE.hashCode() != Color.BLUE.hashCode() );
132
133         assertTrue( Color.RED.hashCode() != OtherColor.RED.hashCode() );
134         assertTrue( Color.GREEN.hashCode() != OtherColor.GREEN.hashCode() );
135         assertTrue( Color.BLUE.hashCode() != OtherColor.BLUE.hashCode() );
136
137         assertTrue( Color.RED.hashCode() != Color.GREEN.hashCode() );
138         assertTrue( Color.GREEN.hashCode() != Color.BLUE.hashCode() );
139         assertTrue( Color.BLUE.hashCode() != Color.RED.hashCode() );
140
141         // todo: is this _really_ desired?
142
assertTrue( Color.RED.hashCode() ==Color.RED_NEGATIVE.hashCode() );
143         assertTrue( Color.RED_NEGATIVE.hashCode() ==Color.RED.hashCode() );
144         assertTrue( OtherColor.RED.hashCode() ==OtherColor.RED_NEGATIVE.hashCode() );
145         assertTrue( OtherColor.RED_NEGATIVE.hashCode() ==OtherColor.RED.hashCode() );
146     }
147
148     public void testGet()
149     {
150         assertEquals( "Red", Color.RED.getName() );
151         assertNull( (new Color(null,0)).getName() );
152     }
153
154     public void testToString()
155     {
156         assertTrue( Color.RED.toString().indexOf( "Red") != -1 );
157         assertTrue( Color.RED.toString().indexOf( Color.class.getName() ) != -1 );
158
159         Color c = new Color(null,0);
160         assertTrue( c.toString().indexOf( "null") != -1 );
161
162     }
163 }
164
Popular Tags