commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/enums Enum.java
Date Tue, 01 Jun 2004 20:54:57 GMT
scolebourne    2004/06/01 13:54:57

  Modified:    lang/src/test/org/apache/commons/lang/enums
                        EnumTestSuite.java
               lang/src/java/org/apache/commons/lang/enums Enum.java
  Added:       lang/src/test/org/apache/commons/lang/enums
                        EnumEqualsTest.java
  Log:
  Fix Enum equals to work correctly by reflection
  bug 28180, from Matthias Eichel
  
  Revision  Changes    Path
  1.2       +2 -1      jakarta-commons/lang/src/test/org/apache/commons/lang/enums/EnumTestSuite.java
  
  Index: EnumTestSuite.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/enums/EnumTestSuite.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- EnumTestSuite.java	23 Feb 2004 04:34:20 -0000	1.1
  +++ EnumTestSuite.java	1 Jun 2004 20:54:57 -0000	1.2
  @@ -49,6 +49,7 @@
           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;
  
  
  
  1.1                  jakarta-commons/lang/src/test/org/apache/commons/lang/enums/EnumEqualsTest.java
  
  Index: EnumEqualsTest.java
  ===================================================================
  /*
   * 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")));
      }
  }
  
  
  
  1.2       +7 -3      jakarta-commons/lang/src/java/org/apache/commons/lang/enums/Enum.java
  
  Index: Enum.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/enums/Enum.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Enum.java	23 Feb 2004 04:34:20 -0000	1.1
  +++ Enum.java	1 Jun 2004 20:54:57 -0000	1.2
  @@ -228,7 +228,8 @@
    * @author Stephen Colebourne
    * @author Chris Webb
    * @author Mike Bowler
  - * @since 1.0
  + * @author Matthias Eichel
  + * @since 2.1 (class existed in enum package from v1.0)
    * @version $Id$
    */
   public abstract class Enum implements Comparable, Serializable {
  @@ -537,7 +538,10 @@
               // 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);
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message