KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > enums > EnumEqualsTest


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

16 package org.apache.commons.lang.enums;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21
22 /**
23  * Test cases for the {@link Enum} class equals method.
24  *
25  * @author Matthias Eichel
26  * @author Stephen Colebourne
27  * @version $Id: EnumEqualsTest.java 161244 2005-04-14 06:16:36Z ggregory $
28  */

29 public final class EnumEqualsTest extends TestCase {
30
31     public EnumEqualsTest(String JavaDoc name) {
32         super(name);
33     }
34
35     public void setUp() {
36     }
37
38     public static Test suite() {
39         TestSuite suite = new TestSuite(EnumEqualsTest.class);
40         suite.setName("Enum equals Tests");
41         return suite;
42     }
43
44     //-----------------------------------------------------------------------
45
static final class CarColorEnum extends Enum JavaDoc {
46         public static final CarColorEnum BLACK = new CarColorEnum("black");
47         public static final CarColorEnum BROWN = new CarColorEnum("brown");
48         public static final CarColorEnum YELLOW = new CarColorEnum("yellow");
49         public static final CarColorEnum BLUE = new CarColorEnum("blue");
50         public static final CarColorEnum RED = new CarColorEnum("red");
51
52         private CarColorEnum(String JavaDoc enumAsString) {
53             super(enumAsString);
54         }
55     }
56
57     static final class TrafficlightColorEnum extends Enum JavaDoc {
58         public static final TrafficlightColorEnum RED = new TrafficlightColorEnum("red");
59         public static final TrafficlightColorEnum YELLOW = new TrafficlightColorEnum("yellow");
60         public static final TrafficlightColorEnum GREEN = new TrafficlightColorEnum("green");
61
62         private TrafficlightColorEnum(String JavaDoc enumAsString) {
63             super(enumAsString);
64         }
65     }
66
67     static class TotallyUnrelatedClass {
68         private final String JavaDoc name;
69
70         public TotallyUnrelatedClass(final String JavaDoc name) {
71             this.name = name;
72         }
73
74         public String JavaDoc getName() {
75             return name;
76         }
77     }
78
79     //-----------------------------------------------------------------------
80
public void testEquals() {
81         assertEquals(false, CarColorEnum.RED.equals(TrafficlightColorEnum.RED));
82         assertEquals(false, CarColorEnum.YELLOW.equals(TrafficlightColorEnum.YELLOW));
83         
84         assertEquals(false, TrafficlightColorEnum.RED.equals(new TotallyUnrelatedClass("red")));
85         assertEquals(false, CarColorEnum.RED.equals(new TotallyUnrelatedClass("red")));
86         
87         assertEquals(false, TrafficlightColorEnum.RED.equals(new TotallyUnrelatedClass("some")));
88         assertEquals(false, CarColorEnum.RED.equals(new TotallyUnrelatedClass("some")));
89     }
90 }
91
Popular Tags