From aa1e8866bb64e4b5f68fd0fcb0010c66745d849e Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Tue, 1 Jun 2004 20:54:57 +0000 Subject: [PATCH] Fix Enum equals to work correctly by reflection bug 28180, from Matthias Eichel git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137837 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/enums/Enum.java | 10 ++- .../commons/lang/enums/EnumEqualsTest.java | 90 +++++++++++++++++++ .../commons/lang/enums/EnumTestSuite.java | 3 +- 3 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 src/test/org/apache/commons/lang/enums/EnumEqualsTest.java diff --git a/src/java/org/apache/commons/lang/enums/Enum.java b/src/java/org/apache/commons/lang/enums/Enum.java index 220540b0b..8f5992666 100644 --- a/src/java/org/apache/commons/lang/enums/Enum.java +++ b/src/java/org/apache/commons/lang/enums/Enum.java @@ -228,8 +228,9 @@ * @author Stephen Colebourne * @author Chris Webb * @author Mike Bowler - * @since 1.0 - * @version $Id: Enum.java,v 1.1 2004/02/23 04:34:20 ggregory Exp $ + * @author Matthias Eichel + * @since 2.1 (class existed in enum package from v1.0) + * @version $Id: Enum.java,v 1.2 2004/06/01 20:54:57 scolebourne Exp $ */ public abstract class Enum implements Comparable, Serializable { @@ -537,7 +538,10 @@ public final boolean equals(Object other) { // classes are in the same class loader. return iName.equals(((Enum) other).iName); } else { - // This and other are in different class loaders, we must use reflection. + // This and other are in different class loaders, we must check indirectly + if (other.getClass().getName().equals(this.getClass().getName()) == false) { + return false; + } try { Method mth = other.getClass().getMethod("getName", null); String name = (String) mth.invoke(other, null); diff --git a/src/test/org/apache/commons/lang/enums/EnumEqualsTest.java b/src/test/org/apache/commons/lang/enums/EnumEqualsTest.java new file mode 100644 index 000000000..4f85af4a9 --- /dev/null +++ b/src/test/org/apache/commons/lang/enums/EnumEqualsTest.java @@ -0,0 +1,90 @@ +/* + * Copyright 2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang.enums; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Test cases for the {@link Enum} class equals method. + * + * @author Matthias Eichel + * @author Stephen Colebourne + * @version $Id: EnumEqualsTest.java,v 1.1 2004/06/01 20:54:57 scolebourne Exp $ + */ +public final class EnumEqualsTest extends TestCase { + + public EnumEqualsTest(String name) { + super(name); + } + + public void setUp() { + } + + public static Test suite() { + TestSuite suite = new TestSuite(EnumEqualsTest.class); + suite.setName("Enum equals Tests"); + return suite; + } + + //----------------------------------------------------------------------- + static final class CarColorEnum extends Enum { + public static final CarColorEnum BLACK = new CarColorEnum("black"); + public static final CarColorEnum BROWN = new CarColorEnum("brown"); + public static final CarColorEnum YELLOW = new CarColorEnum("yellow"); + public static final CarColorEnum BLUE = new CarColorEnum("blue"); + public static final CarColorEnum RED = new CarColorEnum("red"); + + private CarColorEnum(String enumAsString) { + super(enumAsString); + } + } + + static final class TrafficlightColorEnum extends Enum { + public static final TrafficlightColorEnum RED = new TrafficlightColorEnum("red"); + public static final TrafficlightColorEnum YELLOW = new TrafficlightColorEnum("yellow"); + public static final TrafficlightColorEnum GREEN = new TrafficlightColorEnum("green"); + + private TrafficlightColorEnum(String enumAsString) { + super(enumAsString); + } + } + + static class TotallyUnrelatedClass { + private final String name; + + public TotallyUnrelatedClass(final String name) { + this.name = name; + } + + public String getName() { + return name; + } + } + + //----------------------------------------------------------------------- + public void testEquals() { + assertEquals(false, CarColorEnum.RED.equals(TrafficlightColorEnum.RED)); + assertEquals(false, CarColorEnum.YELLOW.equals(TrafficlightColorEnum.YELLOW)); + + assertEquals(false, TrafficlightColorEnum.RED.equals(new TotallyUnrelatedClass("red"))); + assertEquals(false, CarColorEnum.RED.equals(new TotallyUnrelatedClass("red"))); + + assertEquals(false, TrafficlightColorEnum.RED.equals(new TotallyUnrelatedClass("some"))); + assertEquals(false, CarColorEnum.RED.equals(new TotallyUnrelatedClass("some"))); + } +} diff --git a/src/test/org/apache/commons/lang/enums/EnumTestSuite.java b/src/test/org/apache/commons/lang/enums/EnumTestSuite.java index 794f54f83..4304c25dc 100644 --- a/src/test/org/apache/commons/lang/enums/EnumTestSuite.java +++ b/src/test/org/apache/commons/lang/enums/EnumTestSuite.java @@ -24,7 +24,7 @@ * Test suite for the Enum package. * * @author Stephen Colebourne - * @version $Id: EnumTestSuite.java,v 1.1 2004/02/23 04:34:20 ggregory Exp $ + * @version $Id: EnumTestSuite.java,v 1.2 2004/06/01 20:54:57 scolebourne Exp $ */ public class EnumTestSuite extends TestCase { @@ -49,6 +49,7 @@ public static Test suite() { TestSuite suite = new TestSuite(); suite.setName("Commons-Lang-Enum Tests"); suite.addTest(EnumTest.suite()); + suite.addTest(EnumEqualsTest.suite()); suite.addTest(EnumUtilsTest.suite()); suite.addTest(ValuedEnumTest.suite()); return suite;