Moving the enums package over to the backcompat branch. Won't be in 3.0 as people should use Java enums nowadays

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@751350 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-03-08 01:56:56 +00:00
parent 784a817fef
commit 69d0399fcd
25 changed files with 0 additions and 2484 deletions

View File

@ -1,53 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Broken color enumeration.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id$
*/
public final class Broken1Enum extends Enum {
public static final Broken1Enum RED = new Broken1Enum("Red");
public static final Broken1Enum GREEN = new Broken1Enum("Green");
public static final Broken1Enum GREENISH = new Broken1Enum("Green"); // duplicate not allowed
private Broken1Enum(String color) {
super(color);
}
public static Broken1Enum getEnum(String color) {
return (Broken1Enum) getEnum(Broken1Enum.class, color);
}
public static Map getEnumMap() {
return getEnumMap(Broken1Enum.class);
}
public static List getEnumList() {
return getEnumList(Broken1Enum.class);
}
public static Iterator iterator() {
return iterator(Broken1Enum.class);
}
}

View File

@ -1,86 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Broken Operator enumeration, null class.
*
* @author Stephen Colebourne
* @version $Id$
*/
public abstract class Broken1OperationEnum extends Enum {
// This syntax works for JDK 1.3 and upwards:
// public static final OperationEnum PLUS = new OperationEnum("Plus") {
// public int eval(int a, int b) {
// return (a + b);
// }
// };
// public static final OperationEnum MINUS = new OperationEnum("Minus") {
// public int eval(int a, int b) {
// return (a - b);
// }
// };
// This syntax works for JDK 1.2 and upwards:
public static final Broken1OperationEnum PLUS = new PlusOperation();
private static class PlusOperation extends Broken1OperationEnum {
private PlusOperation() {
super("Plus");
}
public int eval(int a, int b) {
return (a + b);
}
}
public static final Broken1OperationEnum MINUS = new MinusOperation();
private static class MinusOperation extends Broken1OperationEnum {
private MinusOperation() {
super("Minus");
}
public int eval(int a, int b) {
return (a - b);
}
}
private Broken1OperationEnum(String name) {
super(name);
}
public final Class getEnumClass() {
return null;
}
public abstract int eval(int a, int b);
public static Broken1OperationEnum getEnum(String name) {
return (Broken1OperationEnum) getEnum(Broken1OperationEnum.class, name);
}
public static Map getEnumMap() {
return getEnumMap(Broken1OperationEnum.class);
}
public static List getEnumList() {
return getEnumList(Broken1OperationEnum.class);
}
public static Iterator iterator() {
return iterator(Broken1OperationEnum.class);
}
}

View File

@ -1,53 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Broken color enumeration.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id$
*/
public final class Broken2Enum extends Enum {
public static final Broken2Enum RED = new Broken2Enum("Red");
public static final Broken2Enum GREEN = new Broken2Enum("Green");
public static final Broken2Enum BLUE = new Broken2Enum(""); // blank not allowed
private Broken2Enum(String color) {
super(color);
}
public static Broken2Enum getEnum(String color) {
return (Broken2Enum) getEnum(Broken2Enum.class, color);
}
public static Map getEnumMap() {
return getEnumMap(Broken2Enum.class);
}
public static List getEnumList() {
return getEnumList(Broken2Enum.class);
}
public static Iterator iterator() {
return iterator(Broken2Enum.class);
}
}

View File

@ -1,86 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Broken Operator enumeration, getEnumClass() not superclass.
*
* @author Stephen Colebourne
* @version $Id$
*/
public abstract class Broken2OperationEnum extends Enum {
// This syntax works for JDK 1.3 and upwards:
// public static final OperationEnum PLUS = new OperationEnum("Plus") {
// public int eval(int a, int b) {
// return (a + b);
// }
// };
// public static final OperationEnum MINUS = new OperationEnum("Minus") {
// public int eval(int a, int b) {
// return (a - b);
// }
// };
// This syntax works for JDK 1.2 and upwards:
public static final Broken2OperationEnum PLUS = new PlusOperation();
private static class PlusOperation extends Broken2OperationEnum {
private PlusOperation() {
super("Plus");
}
public int eval(int a, int b) {
return (a + b);
}
}
public static final Broken2OperationEnum MINUS = new MinusOperation();
private static class MinusOperation extends Broken2OperationEnum {
private MinusOperation() {
super("Minus");
}
public int eval(int a, int b) {
return (a - b);
}
}
private Broken2OperationEnum(String name) {
super(name);
}
public final Class getEnumClass() {
return ColorEnum.class;
}
public abstract int eval(int a, int b);
public static Broken2OperationEnum getEnum(String name) {
return (Broken2OperationEnum) getEnum(Broken2OperationEnum.class, name);
}
public static Map getEnumMap() {
return getEnumMap(Broken2OperationEnum.class);
}
public static List getEnumList() {
return getEnumList(Broken2OperationEnum.class);
}
public static Iterator iterator() {
return iterator(Broken2OperationEnum.class);
}
}

View File

@ -1,53 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Broken color enumeration.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id$
*/
public final class Broken3Enum extends Enum {
public static final Broken3Enum RED = new Broken3Enum("Red");
public static final Broken3Enum GREEN = new Broken3Enum("Green");
public static final Broken3Enum BLUE = new Broken3Enum(null); // null not allowed
private Broken3Enum(String color) {
super(color);
}
public static Broken3Enum getEnum(String color) {
return (Broken3Enum) getEnum(Broken3Enum.class, color);
}
public static Map getEnumMap() {
return getEnumMap(Broken3Enum.class);
}
public static List getEnumList() {
return getEnumList(Broken3Enum.class);
}
public static Iterator iterator() {
return iterator(Broken3Enum.class);
}
}

View File

@ -1,86 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Broken Operator enumeration, getEnumClass() is Enum.class.
*
* @author Stephen Colebourne
* @version $Id$
*/
public abstract class Broken3OperationEnum extends Enum {
// This syntax works for JDK 1.3 and upwards:
// public static final OperationEnum PLUS = new OperationEnum("Plus") {
// public int eval(int a, int b) {
// return (a + b);
// }
// };
// public static final OperationEnum MINUS = new OperationEnum("Minus") {
// public int eval(int a, int b) {
// return (a - b);
// }
// };
// This syntax works for JDK 1.2 and upwards:
public static final Broken3OperationEnum PLUS = new PlusOperation();
private static class PlusOperation extends Broken3OperationEnum {
private PlusOperation() {
super("Plus");
}
public int eval(int a, int b) {
return (a + b);
}
}
public static final Broken3OperationEnum MINUS = new MinusOperation();
private static class MinusOperation extends Broken3OperationEnum {
private MinusOperation() {
super("Minus");
}
public int eval(int a, int b) {
return (a - b);
}
}
private Broken3OperationEnum(String name) {
super(name);
}
public final Class getEnumClass() {
return Enum.class;
}
public abstract int eval(int a, int b);
public static Broken3OperationEnum getEnum(String name) {
return (Broken3OperationEnum) getEnum(Broken3OperationEnum.class, name);
}
public static Map getEnumMap() {
return getEnumMap(Broken3OperationEnum.class);
}
public static List getEnumList() {
return getEnumList(Broken3OperationEnum.class);
}
public static Iterator iterator() {
return iterator(Broken3OperationEnum.class);
}
}

View File

@ -1,86 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Broken Operator enumeration, getEnumClass() is ValuedEnum.class.
*
* @author Stephen Colebourne
* @version $Id$
*/
public abstract class Broken4OperationEnum extends Enum {
// This syntax works for JDK 1.3 and upwards:
// public static final OperationEnum PLUS = new OperationEnum("Plus") {
// public int eval(int a, int b) {
// return (a + b);
// }
// };
// public static final OperationEnum MINUS = new OperationEnum("Minus") {
// public int eval(int a, int b) {
// return (a - b);
// }
// };
// This syntax works for JDK 1.2 and upwards:
public static final Broken4OperationEnum PLUS = new PlusOperation();
private static class PlusOperation extends Broken4OperationEnum {
private PlusOperation() {
super("Plus");
}
public int eval(int a, int b) {
return (a + b);
}
}
public static final Broken4OperationEnum MINUS = new MinusOperation();
private static class MinusOperation extends Broken4OperationEnum {
private MinusOperation() {
super("Minus");
}
public int eval(int a, int b) {
return (a - b);
}
}
private Broken4OperationEnum(String name) {
super(name);
}
public final Class getEnumClass() {
return ValuedEnum.class;
}
public abstract int eval(int a, int b);
public static Broken4OperationEnum getEnum(String name) {
return (Broken4OperationEnum) getEnum(Broken4OperationEnum.class, name);
}
public static Map getEnumMap() {
return getEnumMap(Broken4OperationEnum.class);
}
public static List getEnumList() {
return getEnumList(Broken4OperationEnum.class);
}
public static Iterator iterator() {
return iterator(Broken4OperationEnum.class);
}
}

View File

@ -1,86 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Broken Operator enumeration, getEnumClass() is not an Enum class.
*
* @author Stephen Colebourne
* @version $Id$
*/
public abstract class Broken5OperationEnum extends Enum {
// This syntax works for JDK 1.3 and upwards:
// public static final OperationEnum PLUS = new OperationEnum("Plus") {
// public int eval(int a, int b) {
// return (a + b);
// }
// };
// public static final OperationEnum MINUS = new OperationEnum("Minus") {
// public int eval(int a, int b) {
// return (a - b);
// }
// };
// This syntax works for JDK 1.2 and upwards:
public static final Broken5OperationEnum PLUS = new PlusOperation();
private static class PlusOperation extends Broken5OperationEnum {
private PlusOperation() {
super("Plus");
}
public int eval(int a, int b) {
return (a + b);
}
}
public static final Broken5OperationEnum MINUS = new MinusOperation();
private static class MinusOperation extends Broken5OperationEnum {
private MinusOperation() {
super("Minus");
}
public int eval(int a, int b) {
return (a - b);
}
}
private Broken5OperationEnum(String name) {
super(name);
}
public final Class getEnumClass() {
return String.class;
}
public abstract int eval(int a, int b);
public static Broken5OperationEnum getEnum(String name) {
return (Broken5OperationEnum) getEnum(Broken5OperationEnum.class, name);
}
public static Map getEnumMap() {
return getEnumMap(Broken5OperationEnum.class);
}
public static List getEnumList() {
return getEnumList(Broken5OperationEnum.class);
}
public static Iterator iterator() {
return iterator(Broken5OperationEnum.class);
}
}

View File

@ -1,54 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Color enumeration.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id$
*/
public final class ColorEnum extends Enum {
public static final ColorEnum RED = new ColorEnum("Red");
public static final ColorEnum GREEN = new ColorEnum("Green");
public static final ColorEnum BLUE = new ColorEnum("Blue");
private ColorEnum(String color) {
super(color);
}
public static ColorEnum getEnum(String color) {
return (ColorEnum) getEnum(ColorEnum.class, color);
}
public static Map getEnumMap() {
return getEnumMap(ColorEnum.class);
}
public static List getEnumList() {
return getEnumList(ColorEnum.class);
}
public static Iterator iterator() {
return iterator(ColorEnum.class);
}
}

View File

@ -1,31 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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;
/**
* Dummy enumeration - no values.
*
* @author Stephen Colebourne
* @version $Id$
*/
public abstract class DummyEnum extends Enum {
private DummyEnum(String name) {
super(name);
}
}

View File

@ -1,150 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.net.URLClassLoader;
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$
*/
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")));
}
public void testEquals_classloader_equal() throws Exception {
ClassLoader cl = ColorEnum.class.getClassLoader();
if (cl instanceof URLClassLoader) {
URLClassLoader urlCL = (URLClassLoader) cl;
URLClassLoader urlCL1 = new URLClassLoader(urlCL.getURLs(), null);
URLClassLoader urlCL2 = new URLClassLoader(urlCL.getURLs(), null);
Class otherEnumClass1 = urlCL1.loadClass("org.apache.commons.lang.enums.ColorEnum");
Class otherEnumClass2 = urlCL2.loadClass("org.apache.commons.lang.enums.ColorEnum");
Object blue1 = otherEnumClass1.getDeclaredField("BLUE").get(null);
Object blue2 = otherEnumClass2.getDeclaredField("BLUE").get(null);
assertEquals(true, blue1.equals(blue2));
}
}
public void testEquals_classloader_different() throws Exception {
ClassLoader cl = ColorEnum.class.getClassLoader();
if (cl instanceof URLClassLoader) {
URLClassLoader urlCL = (URLClassLoader) cl;
URLClassLoader urlCL1 = new URLClassLoader(urlCL.getURLs(), null);
URLClassLoader urlCL2 = new URLClassLoader(urlCL.getURLs(), null);
Class otherEnumClass1 = urlCL1.loadClass("org.apache.commons.lang.enums.ColorEnum");
Class otherEnumClass2 = urlCL2.loadClass("org.apache.commons.lang.enums.ColorEnum");
Object blue1 = otherEnumClass1.getDeclaredField("BLUE").get(null);
Object blue2 = otherEnumClass2.getDeclaredField("RED").get(null);
assertEquals(false, blue1.equals(blue2));
}
}
//-----------------------------------------------------------------------
public void testCompareTo() {
try {
CarColorEnum.RED.compareTo(TrafficlightColorEnum.RED);
fail();
} catch (ClassCastException ex) {}
try {
CarColorEnum.YELLOW.compareTo(TrafficlightColorEnum.YELLOW);
fail();
} catch (ClassCastException ex) {}
try {
TrafficlightColorEnum.RED.compareTo(new TotallyUnrelatedClass("red"));
fail();
} catch (ClassCastException ex) {}
try {
CarColorEnum.RED.compareTo(new TotallyUnrelatedClass("red"));
fail();
} catch (ClassCastException ex) {}
try {
TrafficlightColorEnum.RED.compareTo(new TotallyUnrelatedClass("some"));
fail();
} catch (ClassCastException ex) {}
try {
CarColorEnum.RED.compareTo(new TotallyUnrelatedClass("some"));
fail();
} catch (ClassCastException ex) {}
}
}

View File

@ -1,553 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.lang.SerializationUtils;
/**
* Test cases for the {@link Enum} class.
*
* @author Stephen Colebourne
* @author Gary D. Gregory
* @version $Id$
*/
public final class EnumTest extends TestCase {
private static final String ENUMS_CLASS_NAME = "org.apache.commons.lang.enums.ColorEnum";
public EnumTest(String name) {
super(name);
}
public void setUp() {
}
public static Test suite() {
TestSuite suite = new TestSuite(EnumTest.class);
suite.setName("Enum Tests");
return suite;
}
public void testName() {
assertEquals("Red", ColorEnum.RED.getName());
assertEquals("Green", ColorEnum.GREEN.getName());
assertEquals("Blue", ColorEnum.BLUE.getName());
}
public void testCompareTo() {
assertTrue(ColorEnum.BLUE.compareTo(ColorEnum.BLUE) == 0);
assertTrue(ColorEnum.RED.compareTo(ColorEnum.BLUE) > 0);
assertTrue(ColorEnum.BLUE.compareTo(ColorEnum.RED) < 0);
try {
ColorEnum.RED.compareTo(null);
fail();
} catch (NullPointerException ex) {}
try {
ColorEnum.RED.compareTo(new Object());
fail();
} catch (ClassCastException ex) {}
}
public void testEquals() {
assertSame(ColorEnum.RED, ColorEnum.RED);
assertSame(ColorEnum.getEnum("Red"), ColorEnum.RED);
assertEquals(false, ColorEnum.RED.equals(null));
assertEquals(true, ColorEnum.RED.equals(ColorEnum.RED));
assertEquals(true, ColorEnum.RED.equals(ColorEnum.getEnum("Red")));
}
public void testHashCode() {
assertEquals(ColorEnum.RED.hashCode(), ColorEnum.RED.hashCode());
assertEquals(7 + ColorEnum.class.hashCode() + 3 * "Red".hashCode(), ColorEnum.RED.hashCode());
}
public void testToString() {
String toString = ColorEnum.RED.toString();
assertEquals("ColorEnum[Red]", toString);
assertSame(toString, ColorEnum.RED.toString());
}
public void testIterator() {
Iterator it = ColorEnum.iterator();
assertSame(ColorEnum.RED, it.next());
assertSame(ColorEnum.GREEN, it.next());
assertSame(ColorEnum.BLUE, it.next());
}
public void testList() {
List list = new ArrayList(ColorEnum.getEnumList());
assertNotNull(list);
assertEquals( list.size(),
ColorEnum.getEnumMap().keySet().size());
Iterator it = list.iterator();
assertSame(ColorEnum.RED, it.next());
assertSame(ColorEnum.GREEN, it.next());
assertSame(ColorEnum.BLUE, it.next());
}
public void testMap() {
Map map = new HashMap(ColorEnum.getEnumMap());
assertNotNull(map);
assertTrue(map.containsValue(ColorEnum.RED));
assertTrue(map.containsValue(ColorEnum.GREEN));
assertTrue(map.containsValue(ColorEnum.BLUE));
assertSame(ColorEnum.RED, map.get("Red"));
assertSame(ColorEnum.GREEN, map.get("Green"));
assertSame(ColorEnum.BLUE, map.get("Blue"));
assertEquals( map.keySet().size(),
ColorEnum.getEnumList().size());
}
public void testGet() {
assertSame(ColorEnum.RED, ColorEnum.getEnum("Red"));
assertSame(ColorEnum.GREEN, ColorEnum.getEnum("Green"));
assertSame(ColorEnum.BLUE, ColorEnum.getEnum("Blue"));
assertSame(null, ColorEnum.getEnum("Pink"));
}
public void testSerialization() {
int hashCode = ColorEnum.RED.hashCode();
assertSame(ColorEnum.RED, SerializationUtils.clone(ColorEnum.RED));
assertEquals(hashCode, SerializationUtils.clone(ColorEnum.RED).hashCode());
assertSame(ColorEnum.GREEN, SerializationUtils.clone(ColorEnum.GREEN));
assertSame(ColorEnum.BLUE, SerializationUtils.clone(ColorEnum.BLUE));
}
public void testBroken1() {
try {
Broken1Enum.RED.getName();
fail();
} catch (ExceptionInInitializerError ex) {
assertTrue(ex.getException() instanceof IllegalArgumentException);
}
}
public void testBroken2() {
try {
Broken2Enum.RED.getName();
fail();
} catch (ExceptionInInitializerError ex) {
assertTrue(ex.getException() instanceof IllegalArgumentException);
}
}
public void testBroken3() {
try {
Broken3Enum.RED.getName();
fail();
} catch (ExceptionInInitializerError ex) {
assertTrue(ex.getException() instanceof IllegalArgumentException);
}
}
public void testBroken1Operation() {
try {
Broken1OperationEnum.PLUS.getName();
fail();
} catch (ExceptionInInitializerError ex) {
assertTrue(ex.getException() instanceof IllegalArgumentException);
}
}
public void testBroken2Operation() {
try {
Broken2OperationEnum.PLUS.getName();
fail();
} catch (ExceptionInInitializerError ex) {
assertTrue(ex.getException() instanceof IllegalArgumentException);
}
}
public void testBroken3Operation() {
try {
Broken3OperationEnum.PLUS.getName();
fail();
} catch (ExceptionInInitializerError ex) {
assertTrue(ex.getException() instanceof IllegalArgumentException);
}
}
public void testBroken4Operation() {
try {
Broken4OperationEnum.PLUS.getName();
fail();
} catch (ExceptionInInitializerError ex) {
assertTrue(ex.getException() instanceof IllegalArgumentException);
}
}
public void testBroken5Operation() {
try {
Broken5OperationEnum.PLUS.getName();
fail();
} catch (ExceptionInInitializerError ex) {
assertTrue(ex.getException() instanceof IllegalArgumentException);
}
}
public void testOperationGet() {
assertSame(OperationEnum.PLUS, OperationEnum.getEnum("Plus"));
assertSame(OperationEnum.MINUS, OperationEnum.getEnum("Minus"));
assertSame(null, OperationEnum.getEnum("Pink"));
}
public void testOperationSerialization() {
assertSame(OperationEnum.PLUS, SerializationUtils.clone(OperationEnum.PLUS));
assertSame(OperationEnum.MINUS, SerializationUtils.clone(OperationEnum.MINUS));
}
public void testOperationToString() {
assertEquals("OperationEnum[Plus]", OperationEnum.PLUS.toString());
}
public void testOperationList() {
List list = OperationEnum.getEnumList();
assertNotNull(list);
assertEquals(2, list.size());
assertEquals(list.size(), OperationEnum.getEnumMap().keySet().size());
Iterator it = list.iterator();
assertSame(OperationEnum.PLUS, it.next());
assertSame(OperationEnum.MINUS, it.next());
}
public void testOperationMap() {
Map map = OperationEnum.getEnumMap();
assertNotNull(map);
assertEquals(map.keySet().size(), OperationEnum.getEnumList().size());
assertTrue(map.containsValue(OperationEnum.PLUS));
assertTrue(map.containsValue(OperationEnum.MINUS));
assertSame(OperationEnum.PLUS, map.get("Plus"));
assertSame(OperationEnum.MINUS, map.get("Minus"));
}
public void testOperationCalculation() {
assertEquals(3, OperationEnum.PLUS.eval(1, 2));
assertEquals(-1, OperationEnum.MINUS.eval(1, 2));
}
//-----------------------------------------------------------------------
public void testExtended1Get() {
assertSame(Extended1Enum.ALPHA, Extended1Enum.getEnum("Alpha"));
assertSame(Extended1Enum.BETA, Extended1Enum.getEnum("Beta"));
assertSame(null, Extended1Enum.getEnum("Gamma"));
assertSame(null, Extended1Enum.getEnum("Delta"));
}
public void testExtended2Get() {
assertSame(Extended1Enum.ALPHA, Extended2Enum.ALPHA);
assertSame(Extended1Enum.BETA, Extended2Enum.BETA);
assertSame(Extended2Enum.ALPHA, Extended2Enum.getEnum("Alpha"));
assertSame(Extended2Enum.BETA, Extended2Enum.getEnum("Beta"));
assertSame(Extended2Enum.GAMMA, Extended2Enum.getEnum("Gamma"));
assertSame(null, Extended2Enum.getEnum("Delta"));
}
public void testExtended3Get() {
assertSame(Extended2Enum.ALPHA, Extended3Enum.ALPHA);
assertSame(Extended2Enum.BETA, Extended3Enum.BETA);
assertSame(Extended2Enum.GAMMA, Extended3Enum.GAMMA);
assertSame(Extended3Enum.ALPHA, Extended3Enum.getEnum("Alpha"));
assertSame(Extended3Enum.BETA, Extended3Enum.getEnum("Beta"));
assertSame(Extended3Enum.GAMMA, Extended3Enum.getEnum("Gamma"));
assertSame(Extended3Enum.DELTA, Extended3Enum.getEnum("Delta"));
}
public void testExtendedSerialization() {
assertSame(Extended1Enum.ALPHA, SerializationUtils.clone(Extended1Enum.ALPHA));
assertSame(Extended1Enum.BETA, SerializationUtils.clone(Extended1Enum.BETA));
assertSame(Extended2Enum.GAMMA, SerializationUtils.clone(Extended2Enum.GAMMA));
assertSame(Extended3Enum.DELTA, SerializationUtils.clone(Extended3Enum.DELTA));
}
public void testExtendedToString() {
assertEquals("Extended1Enum[Alpha]", Extended1Enum.ALPHA.toString());
assertEquals("Extended1Enum[Beta]", Extended1Enum.BETA.toString());
assertEquals("Extended1Enum[Alpha]", Extended2Enum.ALPHA.toString());
assertEquals("Extended1Enum[Beta]", Extended2Enum.BETA.toString());
assertEquals("Extended2Enum[Gamma]", Extended2Enum.GAMMA.toString());
assertEquals("Extended1Enum[Alpha]", Extended3Enum.ALPHA.toString());
assertEquals("Extended1Enum[Beta]", Extended3Enum.BETA.toString());
assertEquals("Extended2Enum[Gamma]", Extended3Enum.GAMMA.toString());
assertEquals("Extended3Enum[Delta]", Extended3Enum.DELTA.toString());
}
public void testExtended1List() {
List list = Extended1Enum.getEnumList();
assertNotNull(list);
assertEquals(2, list.size());
assertEquals(list.size(), Extended1Enum.getEnumMap().keySet().size());
Iterator it = list.iterator();
assertSame(Extended1Enum.ALPHA, it.next());
assertSame(Extended1Enum.BETA, it.next());
}
public void testExtended2List() {
List list = Extended2Enum.getEnumList();
assertNotNull(list);
assertEquals(3, list.size());
assertEquals(list.size(), Extended2Enum.getEnumMap().keySet().size());
Iterator it = list.iterator();
assertSame(Extended2Enum.ALPHA, it.next());
assertSame(Extended2Enum.BETA, it.next());
assertSame(Extended2Enum.GAMMA, it.next());
}
public void testExtended3List() {
List list = Extended3Enum.getEnumList();
assertNotNull(list);
assertEquals(4, list.size());
assertEquals(list.size(), Extended3Enum.getEnumMap().keySet().size());
Iterator it = list.iterator();
assertSame(Extended3Enum.ALPHA, it.next());
assertSame(Extended3Enum.BETA, it.next());
assertSame(Extended3Enum.GAMMA, it.next());
assertSame(Extended3Enum.DELTA, it.next());
}
public void testExtended1Map() {
Map map = Extended1Enum.getEnumMap();
assertNotNull(map);
assertEquals(map.keySet().size(), Extended1Enum.getEnumList().size());
assertTrue(map.containsValue(Extended1Enum.ALPHA));
assertTrue(map.containsValue(Extended1Enum.BETA));
assertSame(Extended1Enum.ALPHA, map.get("Alpha"));
assertSame(Extended1Enum.BETA, map.get("Beta"));
}
public void testExtended2Map() {
Map map = Extended2Enum.getEnumMap();
assertNotNull(map);
assertEquals(map.keySet().size(), Extended2Enum.getEnumList().size());
assertTrue(map.containsValue(Extended2Enum.ALPHA));
assertTrue(map.containsValue(Extended2Enum.BETA));
assertTrue(map.containsValue(Extended2Enum.GAMMA));
assertSame(Extended2Enum.ALPHA, map.get("Alpha"));
assertSame(Extended2Enum.BETA, map.get("Beta"));
assertSame(Extended2Enum.GAMMA, map.get("Gamma"));
}
public void testExtended3Map() {
Map map = Extended3Enum.getEnumMap();
assertNotNull(map);
assertEquals(map.keySet().size(), Extended3Enum.getEnumList().size());
assertTrue(map.containsValue(Extended3Enum.ALPHA));
assertTrue(map.containsValue(Extended3Enum.BETA));
assertTrue(map.containsValue(Extended3Enum.GAMMA));
assertTrue(map.containsValue(Extended3Enum.DELTA));
assertSame(Extended3Enum.ALPHA, map.get("Alpha"));
assertSame(Extended3Enum.BETA, map.get("Beta"));
assertSame(Extended3Enum.GAMMA, map.get("Gamma"));
assertSame(Extended3Enum.DELTA, map.get("Delta"));
}
//-----------------------------------------------------------------------
public void testNested() {
List list = new ArrayList(Nest.ColorEnum.getEnumList());
assertEquals(3, list.size()); // all is well
Iterator it = list.iterator();
assertSame(Nest.ColorEnum.RED, it.next());
assertSame(Nest.ColorEnum.GREEN, it.next());
assertSame(Nest.ColorEnum.BLUE, it.next());
// This nesting works because the enum constants are defined in the SAME
// class as the getEnumList(). It just acts as a normal enum.
}
public void testNestedBroken() {
List list = new ArrayList(NestBroken.ColorEnum.getEnumList());
try {
assertEquals(0, list.size()); // no enums!!!
// this is BROKEN because the enum constants are defined in a DIFFERENT
// class from getEnumList(). Once NestBroken class is referenced,
// and thus class loaded with its enum constants, the getEnumList works:
} catch (AssertionFailedError ex) {
// this actually works and isn't broken on Linux SunJDK1.4.1, so...
assertEquals(3, list.size());
}
new NestBroken();
list = new ArrayList(NestBroken.ColorEnum.getEnumList());
assertEquals(3, list.size()); // all is well!!!
Iterator it = list.iterator();
assertSame(NestBroken.RED, it.next());
assertSame(NestBroken.GREEN, it.next());
assertSame(NestBroken.BLUE, it.next());
}
public void testNestedLinked() {
List list = new ArrayList(NestLinked.ColorEnum.getEnumList());
assertEquals(3, list.size()); // all is well
Iterator it = list.iterator();
assertSame(NestLinked.RED, it.next());
assertSame(NestLinked.GREEN, it.next());
assertSame(NestLinked.BLUE, it.next());
// This nesting works because a static block in the enum class forces a
// class load of the outer class which defines the enum constants.
}
public void testNestedReferenced() {
List list = new ArrayList(NestReferenced.ColorEnum.getEnumList());
assertEquals(3, list.size()); // all is well
Iterator it = list.iterator();
assertSame(NestReferenced.RED, it.next());
assertSame(NestReferenced.GREEN, it.next());
assertSame(NestReferenced.BLUE, it.next());
// This nesting works because the enum constants are actually defined in
// the SAME class as the getEnumList(). The references in the outer class
// are just extra references.
}
public void testColorEnumEqualsWithDifferentClassLoaders() throws SecurityException, IllegalArgumentException,
ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
this.testWithDifferentClassLoaders(ColorEnum.BLUE);
this.testWithDifferentClassLoaders(ColorEnum.GREEN);
this.testWithDifferentClassLoaders(ColorEnum.RED);
}
void testWithDifferentClassLoaders(ColorEnum colorEnum) throws ClassNotFoundException, SecurityException,
NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
// Sanity checks:
assertTrue(colorEnum.equals(colorEnum));
assertNotNull(ColorEnum.class.getClassLoader());
// set up:
ClassLoader myClassLoader = EnumTest.class.getClassLoader();
if (!(myClassLoader instanceof URLClassLoader)) {
fail("EnumTest ClassLoader = " + (myClassLoader == null ? null : myClassLoader.getClass().getName()));
}
ClassLoader classLoader = URLClassLoader.newInstance( ((URLClassLoader)myClassLoader).getURLs(), null);
Object enumObjectFromOtherClassLoader = this.getColorEnum(classLoader, colorEnum.getName());
// the real test, part 1.
try {
ColorEnum testCase = (ColorEnum) enumObjectFromOtherClassLoader;
fail("Should have thrown a ClassCastException for " + testCase);
} catch (ClassCastException e) {
// normal.
}
// the real test, part 2.
assertEquals("The two objects should match even though they are from different class loaders", colorEnum,
enumObjectFromOtherClassLoader);
// the real test, part 3 - testing equals(Object)
int falseCount = 0;
for (Iterator iter = ColorEnum.iterator(); iter.hasNext();) {
ColorEnum element = (ColorEnum) iter.next();
if (!colorEnum.equals(element)) {
falseCount++;
assertFalse(enumObjectFromOtherClassLoader.equals(element));
}
}
assertEquals(ColorEnum.getEnumList().size() - 1, falseCount);
// the real test, part 4 - testing compareTo(Object) == 0
falseCount = 0;
for (Iterator iter = ColorEnum.iterator(); iter.hasNext();) {
ColorEnum element = (ColorEnum) iter.next();
if (!colorEnum.equals(element)) {
falseCount++;
assertFalse( ((Comparable)enumObjectFromOtherClassLoader).compareTo(element) == 0);
}
}
assertEquals(ColorEnum.getEnumList().size() - 1, falseCount);
}
Object getColorEnum(ClassLoader classLoader, String color) throws ClassNotFoundException, SecurityException,
NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
// Sanity check:
ColorEnum.RED.equals(ColorEnum.RED);
assertNotNull(ColorEnum.class.getClassLoader());
// set up:
assertNotNull(classLoader);
assertFalse(classLoader.equals(ColorEnum.class.getClassLoader()));
Class otherColorEnumClass = null;
try {
otherColorEnumClass = classLoader.loadClass(ENUMS_CLASS_NAME);
} catch (ClassNotFoundException e) {
// Dump some information to help debug class loader issues under different JREs, Ant, Eclipse.
System.err.println("Could not load " + ENUMS_CLASS_NAME + " from the class loader " + classLoader);
URLClassLoader urlCl = (URLClassLoader) classLoader;
URL[] urls = urlCl.getURLs();
System.err.println("Class loader has " + urls.length + " URLs:");
for (int i = 0; i < urls.length; i++) {
System.err.println("URL[" + i + "] = " + urls[i]);
}
e.printStackTrace();
throw e;
}
assertNotNull(otherColorEnumClass);
assertNotNull(otherColorEnumClass.getClassLoader());
assertTrue(classLoader.equals(otherColorEnumClass.getClassLoader()));
assertFalse(otherColorEnumClass.getClassLoader().equals(ColorEnum.class.getClassLoader()));
Method method = otherColorEnumClass.getMethod("getEnum", new Class[]{String.class});
Object enumObject = method.invoke(otherColorEnumClass, new Object[]{color});
assertNotNull(enumObject);
assertFalse(ColorEnum.class.equals(enumObject.getClass()));
assertFalse(ColorEnum.class == enumObject.getClass());
return enumObject;
}
public void testEqualsToWrongInstance() {
for (Iterator iter = ColorEnum.iterator(); iter.hasNext();) {
ColorEnum element = (ColorEnum) iter.next();
this.testEqualsToWrongInstance(element);
}
}
void testEqualsToWrongInstance(ColorEnum colorEnum) {
assertEquals(false, colorEnum.equals("test"));
assertEquals(false, colorEnum.equals(new Integer(1)));
assertEquals(false, colorEnum.equals(new Boolean(true)));
assertEquals(false, colorEnum.equals(new StringBuffer("test")));
assertEquals(false, colorEnum.equals(new Object()));
assertEquals(false, colorEnum.equals(null));
assertEquals(false, colorEnum.equals(""));
assertEquals(false, colorEnum.equals(ColorEnum.getEnum(null)));
assertEquals(false, colorEnum.equals(ColorEnum.getEnum("")));
assertEquals(false, colorEnum.equals(ColorEnum.getEnum("This ColorEnum does not exist.")));
}
}

View File

@ -1,58 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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;
import junit.textui.TestRunner;
/**
* Test suite for the Enum package.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id$
*/
public class EnumTestSuite extends TestCase {
/**
* Construct a new instance.
*/
public EnumTestSuite(String name) {
super(name);
}
/**
* Command-line interface.
*/
public static void main(String[] args) {
TestRunner.run(suite());
}
/**
* Get the suite of tests
*/
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;
}
}

View File

@ -1,168 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test cases for the {@link Enum} class.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id$
*/
public final class EnumUtilsTest extends TestCase {
public EnumUtilsTest(String name) {
super(name);
}
public void setUp() {
}
public static Test suite() {
TestSuite suite = new TestSuite(EnumUtilsTest.class);
suite.setName("EnumUtils Tests");
return suite;
}
//-----------------------------------------------------------------------
public void testConstructor() {
assertNotNull(new EnumUtils());
Constructor[] cons = EnumUtils.class.getDeclaredConstructors();
assertEquals(1, cons.length);
assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
assertEquals(true, Modifier.isPublic(EnumUtils.class.getModifiers()));
assertEquals(false, Modifier.isFinal(EnumUtils.class.getModifiers()));
}
//-----------------------------------------------------------------------
public void testIterator() {
Iterator it = EnumUtils.iterator(ColorEnum.class);
assertSame(ColorEnum.RED, it.next());
assertSame(ColorEnum.GREEN, it.next());
assertSame(ColorEnum.BLUE, it.next());
it = EnumUtils.iterator(DummyEnum.class);
assertEquals(false, it.hasNext());
}
public void testIteratorEx() {
try {
EnumUtils.iterator(null);
fail();
} catch (IllegalArgumentException ex) {}
try {
EnumUtils.iterator(Object.class);
fail();
} catch (IllegalArgumentException ex) {}
}
//-----------------------------------------------------------------------
public void testList() {
List list = EnumUtils.getEnumList(ColorEnum.class);
Iterator it = list.iterator();
assertSame(ColorEnum.RED, it.next());
assertSame(ColorEnum.GREEN, it.next());
assertSame(ColorEnum.BLUE, it.next());
list = EnumUtils.getEnumList(DummyEnum.class);
assertEquals(0, list.size());
}
public void testListEx() {
try {
EnumUtils.getEnumList(null);
fail();
} catch (IllegalArgumentException ex) {}
try {
EnumUtils.getEnumList(Object.class);
fail();
} catch (IllegalArgumentException ex) {}
}
//-----------------------------------------------------------------------
public void testMap() {
Map map = EnumUtils.getEnumMap(ColorEnum.class);
assertTrue(map.containsValue(ColorEnum.RED));
assertTrue(map.containsValue(ColorEnum.GREEN));
assertTrue(map.containsValue(ColorEnum.BLUE));
assertSame(ColorEnum.RED, map.get("Red"));
assertSame(ColorEnum.GREEN, map.get("Green"));
assertSame(ColorEnum.BLUE, map.get("Blue"));
map = EnumUtils.getEnumMap(DummyEnum.class);
assertEquals(0, map.size());
}
public void testMapEx() {
try {
EnumUtils.getEnumMap(null);
fail();
} catch (IllegalArgumentException ex) {}
try {
EnumUtils.getEnumMap(Object.class);
fail();
} catch (IllegalArgumentException ex) {}
}
//-----------------------------------------------------------------------
public void testGet() {
assertSame(ColorEnum.RED, EnumUtils.getEnum(ColorEnum.class, "Red"));
assertSame(ColorEnum.GREEN, EnumUtils.getEnum(ColorEnum.class, "Green"));
assertSame(ColorEnum.BLUE, EnumUtils.getEnum(ColorEnum.class, "Blue"));
assertSame(null, EnumUtils.getEnum(ColorEnum.class, "Pink"));
assertSame(null, EnumUtils.getEnum(DummyEnum.class, "Pink"));
}
public void testGetEx() {
try {
EnumUtils.getEnum(null, "");
fail();
} catch (IllegalArgumentException ex) {}
try {
EnumUtils.getEnum(Object.class, "Red");
fail();
} catch (IllegalArgumentException ex) {}
}
//-----------------------------------------------------------------------
public void testGetValue() {
assertSame(ValuedColorEnum.RED, EnumUtils.getEnum(ValuedColorEnum.class, 1));
assertSame(ValuedColorEnum.GREEN, EnumUtils.getEnum(ValuedColorEnum.class, 2));
assertSame(ValuedColorEnum.BLUE, EnumUtils.getEnum(ValuedColorEnum.class, 3));
assertSame(null, EnumUtils.getEnum(ValuedColorEnum.class, 4));
assertSame(null, EnumUtils.getEnum(DummyEnum.class, 5));
}
public void testGetValueEx() {
try {
EnumUtils.getEnum(null, 0);
fail();
} catch (IllegalArgumentException ex) {}
try {
EnumUtils.getEnum(Object.class, 2);
fail();
} catch (IllegalArgumentException ex) {}
}
}

View File

@ -1,55 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Base extended enumeration.
*
* @author Stephen Colebourne
* @version $Id$
*/
public class Extended1Enum extends Enum {
public static final Extended1Enum ALPHA = new Extended1Enum("Alpha");
public static final Extended1Enum BETA = new Extended1Enum("Beta");
protected Extended1Enum(String name) {
super(name);
}
public static Extended1Enum getEnum(String name) {
return (Extended1Enum) getEnum(Extended1Enum.class, name);
}
public static Map getEnumMap() {
return getEnumMap(Extended1Enum.class);
}
public static List getEnumList() {
return getEnumList(Extended1Enum.class);
}
public static Iterator iterator() {
return iterator(Extended1Enum.class);
}
}

View File

@ -1,54 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Extended enumeration.
*
* @author Stephen Colebourne
* @version $Id$
*/
public class Extended2Enum extends Extended1Enum {
public static final Extended1Enum GAMMA = new Extended2Enum("Gamma");
protected Extended2Enum(String color) {
super(color);
}
public static Extended1Enum getEnum(String name) {
return (Extended1Enum) getEnum(Extended2Enum.class, name);
}
public static Map getEnumMap() {
return getEnumMap(Extended2Enum.class);
}
public static List getEnumList() {
return getEnumList(Extended2Enum.class);
}
public static Iterator iterator() {
return iterator(Extended2Enum.class);
}
}

View File

@ -1,54 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Extended enumeration.
*
* @author Stephen Colebourne
* @version $Id$
*/
public class Extended3Enum extends Extended2Enum {
public static final Extended1Enum DELTA = new Extended3Enum("Delta");
protected Extended3Enum(String name) {
super(name);
}
public static Extended1Enum getEnum(String name) {
return (Extended1Enum) Enum.getEnum(Extended3Enum.class, name);
}
public static Map getEnumMap() {
return Enum.getEnumMap(Extended3Enum.class);
}
public static List getEnumList() {
return Enum.getEnumList(Extended3Enum.class);
}
public static Iterator iterator() {
return Enum.iterator(Extended3Enum.class);
}
}

View File

@ -1,63 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Color enumeration demonstrating a normal simple nesting case.
* All is well here as the nested enum class is really no different
* to any other class.
*
* @author Stephen Colebourne
* @version $Id$
*/
public final class Nest {
public Nest() {
super();
}
public static final class ColorEnum extends Enum {
public static final ColorEnum RED = new ColorEnum("Red");
public static final ColorEnum GREEN = new ColorEnum("Green");
public static final ColorEnum BLUE = new ColorEnum("Blue");
private ColorEnum(String color) {
super(color);
}
public static ColorEnum getEnum(String color) {
return (ColorEnum) getEnum(ColorEnum.class, color);
}
public static Map getEnumMap() {
return getEnumMap(ColorEnum.class);
}
public static List getEnumList() {
return getEnumList(ColorEnum.class);
}
public static Iterator iterator() {
return iterator(ColorEnum.class);
}
}
}

View File

@ -1,67 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Color enumeration which demonstrates how to break the enum system.
* <p>
* The class loader sees the two classes here as independent - the enum
* class is nested, not an inner class. Calling getEnumList() on ColorEnum
* will return an empty list, unless and until the NestBroken class is
* referenced.
*
* @author Stephen Colebourne
* @version $Id$
*/
public final class NestBroken {
public static final ColorEnum RED = new ColorEnum("Red");
public static final ColorEnum GREEN = new ColorEnum("Green");
public static final ColorEnum BLUE = new ColorEnum("Blue");
public NestBroken() {
super();
}
public static final class ColorEnum extends Enum {
private ColorEnum(String color) {
super(color);
}
public static ColorEnum getEnum(String color) {
return (ColorEnum) getEnum(ColorEnum.class, color);
}
public static Map getEnumMap() {
return getEnumMap(ColorEnum.class);
}
public static List getEnumList() {
return getEnumList(ColorEnum.class);
}
public static Iterator iterator() {
return iterator(ColorEnum.class);
}
}
}

View File

@ -1,74 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Color enumeration which demonstrates how to define the constants in a
* different class to the Enum. The extra <code>static{}</code> block is
* needed to ensure that the enum constants are created before the
* static methods on the ColorEnum are used.
* <p>
* The class loader sees the two classes here as independent - the enum
* class is nested, not an inner class. The static block thus forces the
* class load of the outer class, which is needed to initialise the enums.
*
* @author Stephen Colebourne
* @version $Id$
*/
public final class NestLinked {
public static final ColorEnum RED = new ColorEnum("Red");
public static final ColorEnum GREEN = new ColorEnum("Green");
public static final ColorEnum BLUE = new ColorEnum("Blue");
public NestLinked() {
super();
}
public static final class ColorEnum extends Enum {
static {
// Explicitly reference the class where the enums are defined
Object obj = NestLinked.RED;
}
private ColorEnum(String color) {
super(color);
}
public static ColorEnum getEnum(String color) {
return (ColorEnum) getEnum(ColorEnum.class, color);
}
public static Map getEnumMap() {
return getEnumMap(ColorEnum.class);
}
public static List getEnumList() {
return getEnumList(ColorEnum.class);
}
public static Iterator iterator() {
return iterator(ColorEnum.class);
}
}
}

View File

@ -1,70 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Color enumeration which demonstrates how to provide a view of the constants
* in a different class to the Enum. This technique is the safest, however it
* is obviously inconvenient as it involves defining two sets of constants.
* See NestedLinked for an alternative.
*
* @author Stephen Colebourne
* @version $Id$
*/
public final class NestReferenced {
public static final ColorEnum RED = ColorEnum.RED;
public static final ColorEnum GREEN = ColorEnum.GREEN;
public static final ColorEnum BLUE = ColorEnum.BLUE;
public NestReferenced() {
super();
}
public static final class ColorEnum extends Enum {
// must be defined here, not just in outer class
private static final ColorEnum RED = new ColorEnum("Red");
private static final ColorEnum GREEN = new ColorEnum("Green");
private static final ColorEnum BLUE = new ColorEnum("Blue");
private ColorEnum(String color) {
super(color);
}
public static ColorEnum getEnum(String color) {
return (ColorEnum) getEnum(ColorEnum.class, color);
}
public static Map getEnumMap() {
return getEnumMap(ColorEnum.class);
}
public static List getEnumList() {
return getEnumList(ColorEnum.class);
}
public static Iterator iterator() {
return iterator(ColorEnum.class);
}
}
}

View File

@ -1,86 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Operator enumeration.
*
* @author Stephen Colebourne
* @version $Id$
*/
public abstract class OperationEnum extends Enum {
// This syntax works for JDK 1.3 and upwards:
// public static final OperationEnum PLUS = new OperationEnum("Plus") {
// public int eval(int a, int b) {
// return (a + b);
// }
// };
// public static final OperationEnum MINUS = new OperationEnum("Minus") {
// public int eval(int a, int b) {
// return (a - b);
// }
// };
// This syntax works for JDK 1.2 and upwards:
public static final OperationEnum PLUS = new PlusOperation();
private static class PlusOperation extends OperationEnum {
private PlusOperation() {
super("Plus");
}
public int eval(int a, int b) {
return (a + b);
}
}
public static final OperationEnum MINUS = new MinusOperation();
private static class MinusOperation extends OperationEnum {
private MinusOperation() {
super("Minus");
}
public int eval(int a, int b) {
return (a - b);
}
}
private OperationEnum(String name) {
super(name);
}
public final Class getEnumClass() {
return OperationEnum.class;
}
public abstract int eval(int a, int b);
public static OperationEnum getEnum(String name) {
return (OperationEnum) getEnum(OperationEnum.class, name);
}
public static Map getEnumMap() {
return getEnumMap(OperationEnum.class);
}
public static List getEnumList() {
return getEnumList(OperationEnum.class);
}
public static Iterator iterator() {
return iterator(OperationEnum.class);
}
}

View File

@ -1,58 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Color enumeration.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id$
*/
public final class ValuedColorEnum extends ValuedEnum {
public static final ValuedColorEnum RED = new ValuedColorEnum("Red", 1);
public static final ValuedColorEnum GREEN = new ValuedColorEnum("Green", 2);
public static final ValuedColorEnum BLUE = new ValuedColorEnum("Blue", 3);
private ValuedColorEnum(String color, int value) {
super(color, value);
}
public static ValuedColorEnum getEnum(String color) {
return (ValuedColorEnum) getEnum(ValuedColorEnum.class, color);
}
public static ValuedColorEnum getEnum(int value) {
return (ValuedColorEnum) getEnum(ValuedColorEnum.class, value);
}
public static Map getEnumMap() {
return getEnumMap(ValuedColorEnum.class);
}
public static List getEnumList() {
return getEnumList(ValuedColorEnum.class);
}
public static Iterator iterator() {
return iterator(ValuedColorEnum.class);
}
}

View File

@ -1,243 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.net.URLClassLoader;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.lang.SerializationUtils;
/**
* Test cases for the {@link Enum} class.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id$
*/
public final class ValuedEnumTest extends TestCase {
public ValuedEnumTest(String name) {
super(name);
}
public void setUp() {
}
public static Test suite() {
TestSuite suite = new TestSuite(ValuedEnumTest.class);
suite.setName("ValuedEnum Tests");
return suite;
}
public void testName() {
assertEquals("Red", ValuedColorEnum.RED.getName());
assertEquals("Green", ValuedColorEnum.GREEN.getName());
assertEquals("Blue", ValuedColorEnum.BLUE.getName());
}
public void testValue() {
assertEquals(1, ValuedColorEnum.RED.getValue());
assertEquals(2, ValuedColorEnum.GREEN.getValue());
assertEquals(3, ValuedColorEnum.BLUE.getValue());
}
public void testCompareTo() {
assertTrue(ValuedColorEnum.BLUE.compareTo(ValuedColorEnum.BLUE) == 0);
assertTrue(ValuedColorEnum.RED.compareTo(ValuedColorEnum.BLUE) < 0);
assertTrue(ValuedColorEnum.BLUE.compareTo(ValuedColorEnum.RED) > 0);
}
public void testCompareTo_classloader_equal() throws Exception {
ClassLoader cl = ValuedColorEnum.class.getClassLoader();
if (cl instanceof URLClassLoader) {
URLClassLoader urlCL = (URLClassLoader) cl;
URLClassLoader urlCL1 = new URLClassLoader(urlCL.getURLs(), null);
URLClassLoader urlCL2 = new URLClassLoader(urlCL.getURLs(), null);
Class otherEnumClass1 = urlCL1.loadClass("org.apache.commons.lang.enums.ValuedColorEnum");
Class otherEnumClass2 = urlCL2.loadClass("org.apache.commons.lang.enums.ValuedColorEnum");
Object blue1 = otherEnumClass1.getDeclaredField("BLUE").get(null);
Object blue2 = otherEnumClass2.getDeclaredField("BLUE").get(null);
assertTrue(((Comparable) blue1).compareTo(blue2) == 0);
}
}
public void testCompareTo_classloader_different() throws Exception {
ClassLoader cl = ValuedColorEnum.class.getClassLoader();
if (cl instanceof URLClassLoader) {
URLClassLoader urlCL = (URLClassLoader) cl;
URLClassLoader urlCL1 = new URLClassLoader(urlCL.getURLs(), null);
URLClassLoader urlCL2 = new URLClassLoader(urlCL.getURLs(), null);
Class otherEnumClass1 = urlCL1.loadClass("org.apache.commons.lang.enums.ValuedColorEnum");
Class otherEnumClass2 = urlCL2.loadClass("org.apache.commons.lang.enums.ValuedColorEnum");
Object blue1 = otherEnumClass1.getDeclaredField("BLUE").get(null);
Object blue2 = otherEnumClass2.getDeclaredField("RED").get(null);
assertTrue(((Comparable) blue1).compareTo(blue2) != 0);
}
}
public void testCompareTo_nonEnumType() {
try {
ValuedColorEnum.BLUE.compareTo(new TotallyUnrelatedClass(ValuedColorEnum.BLUE.getValue()));
fail();
} catch (ClassCastException ex) {
// expected
}
}
public void testCompareTo_otherEnumType() {
try {
ValuedColorEnum.BLUE.compareTo(ValuedLanguageEnum.ENGLISH);
fail();
} catch (ClassCastException ex) {
// expected
}
}
public void testCompareTo_otherType() {
try {
ValuedColorEnum.BLUE.compareTo("Blue");
fail();
} catch (ClassCastException ex) {
// expected
}
}
public void testCompareTo_null() {
try {
ValuedColorEnum.BLUE.compareTo(null);
fail();
} catch (NullPointerException ex) {
// expected
}
}
public void testEquals() {
assertSame(ValuedColorEnum.RED, ValuedColorEnum.RED);
assertSame(ValuedColorEnum.getEnum("Red"), ValuedColorEnum.RED);
}
public void testEquals_classloader_equal() throws Exception {
ClassLoader cl = ValuedColorEnum.class.getClassLoader();
if (cl instanceof URLClassLoader) {
URLClassLoader urlCL = (URLClassLoader) cl;
URLClassLoader urlCL1 = new URLClassLoader(urlCL.getURLs(), null);
URLClassLoader urlCL2 = new URLClassLoader(urlCL.getURLs(), null);
Class otherEnumClass1 = urlCL1.loadClass("org.apache.commons.lang.enums.ValuedColorEnum");
Class otherEnumClass2 = urlCL2.loadClass("org.apache.commons.lang.enums.ValuedColorEnum");
Object blue1 = otherEnumClass1.getDeclaredField("BLUE").get(null);
Object blue2 = otherEnumClass2.getDeclaredField("BLUE").get(null);
assertEquals(true, blue1.equals(blue2));
}
}
public void testEquals_classloader_different() throws Exception {
ClassLoader cl = ValuedColorEnum.class.getClassLoader();
if (cl instanceof URLClassLoader) {
URLClassLoader urlCL = (URLClassLoader) cl;
URLClassLoader urlCL1 = new URLClassLoader(urlCL.getURLs(), null);
URLClassLoader urlCL2 = new URLClassLoader(urlCL.getURLs(), null);
Class otherEnumClass1 = urlCL1.loadClass("org.apache.commons.lang.enums.ValuedColorEnum");
Class otherEnumClass2 = urlCL2.loadClass("org.apache.commons.lang.enums.ValuedColorEnum");
Object blue1 = otherEnumClass1.getDeclaredField("BLUE").get(null);
Object blue2 = otherEnumClass2.getDeclaredField("RED").get(null);
assertEquals(false, blue1.equals(blue2));
}
}
public void testToString() {
String toString = ValuedColorEnum.RED.toString();
assertEquals("ValuedColorEnum[Red=1]", toString);
assertSame(toString, ValuedColorEnum.RED.toString());
}
public void testIterator() {
Iterator it = ValuedColorEnum.iterator();
assertSame(ValuedColorEnum.RED, it.next());
assertSame(ValuedColorEnum.GREEN, it.next());
assertSame(ValuedColorEnum.BLUE, it.next());
}
public void testList() {
List list = ValuedColorEnum.getEnumList();
assertNotNull(list);
assertEquals( list.size(),
ValuedColorEnum.getEnumMap().keySet().size());
Iterator it = list.iterator();
assertSame(ValuedColorEnum.RED, it.next());
assertSame(ValuedColorEnum.GREEN, it.next());
assertSame(ValuedColorEnum.BLUE, it.next());
}
public void testMap() {
Map map = ValuedColorEnum.getEnumMap();
assertNotNull(map);
assertEquals( map.keySet().size(),
ValuedColorEnum.getEnumList().size());
assertTrue(map.containsValue(ValuedColorEnum.RED));
assertTrue(map.containsValue(ValuedColorEnum.GREEN));
assertTrue(map.containsValue(ValuedColorEnum.BLUE));
assertSame(ValuedColorEnum.RED, map.get("Red"));
assertSame(ValuedColorEnum.GREEN, map.get("Green"));
assertSame(ValuedColorEnum.BLUE, map.get("Blue"));
}
public void testGet() {
assertSame(ValuedColorEnum.RED, ValuedColorEnum.getEnum("Red"));
assertSame(ValuedColorEnum.GREEN, ValuedColorEnum.getEnum("Green"));
assertSame(ValuedColorEnum.BLUE, ValuedColorEnum.getEnum("Blue"));
assertSame(null, ValuedColorEnum.getEnum("Pink"));
}
public void testGetValue() {
assertSame(ValuedColorEnum.RED, ValuedColorEnum.getEnum(1));
assertSame(ValuedColorEnum.GREEN, ValuedColorEnum.getEnum(2));
assertSame(ValuedColorEnum.BLUE, ValuedColorEnum.getEnum(3));
assertSame(null, ValuedColorEnum.getEnum(4));
}
public void testSerialization() {
assertSame(ValuedColorEnum.RED, SerializationUtils.clone(ValuedColorEnum.RED));
assertSame(ValuedColorEnum.GREEN, SerializationUtils.clone(ValuedColorEnum.GREEN));
assertSame(ValuedColorEnum.BLUE, SerializationUtils.clone(ValuedColorEnum.BLUE));
}
//-----------------------------------------------------------------------s
static class TotallyUnrelatedClass {
private final int value;
public TotallyUnrelatedClass(final int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
}

View File

@ -1,57 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Language enumeration.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id$
*/
public final class ValuedLanguageEnum extends ValuedEnum {
public static final ValuedLanguageEnum ENGLISH = new ValuedLanguageEnum("English", 1);
public static final ValuedLanguageEnum FRENCH = new ValuedLanguageEnum("French", 2);
public static final ValuedLanguageEnum GERMAN = new ValuedLanguageEnum("German", 3);
private ValuedLanguageEnum(String color, int value) {
super(color, value);
}
public static ValuedLanguageEnum getEnum(String color) {
return (ValuedLanguageEnum) getEnum(ValuedLanguageEnum.class, color);
}
public static ValuedLanguageEnum getEnum(int value) {
return (ValuedLanguageEnum) getEnum(ValuedLanguageEnum.class, value);
}
public static Map getEnumMap() {
return getEnumMap(ValuedLanguageEnum.class);
}
public static List getEnumList() {
return getEnumList(ValuedLanguageEnum.class);
}
public static Iterator iterator() {
return iterator(ValuedLanguageEnum.class);
}
}