From b0d8436ce85d7d681e5f90215e6a6aac9657a013 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sat, 2 Nov 2002 13:17:06 +0000 Subject: [PATCH] Test to ensure that duplicate enums cannot be created Test to ensure that null and blank strings cannot be created Bug found by Howard Lewis Ship git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137109 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/lang/enum/Broken1Enum.java | 89 +++++++++++++++++++ .../apache/commons/lang/enum/Broken2Enum.java | 89 +++++++++++++++++++ .../apache/commons/lang/enum/Broken3Enum.java | 89 +++++++++++++++++++ .../apache/commons/lang/enum/EnumTest.java | 29 +++++- 4 files changed, 295 insertions(+), 1 deletion(-) create mode 100644 src/test/org/apache/commons/lang/enum/Broken1Enum.java create mode 100644 src/test/org/apache/commons/lang/enum/Broken2Enum.java create mode 100644 src/test/org/apache/commons/lang/enum/Broken3Enum.java diff --git a/src/test/org/apache/commons/lang/enum/Broken1Enum.java b/src/test/org/apache/commons/lang/enum/Broken1Enum.java new file mode 100644 index 000000000..783a17808 --- /dev/null +++ b/src/test/org/apache/commons/lang/enum/Broken1Enum.java @@ -0,0 +1,89 @@ +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +package org.apache.commons.lang.enum; + +import java.util.Iterator; +import java.util.List; +import java.util.Map; +/** + * Broken color enumeration. + * + * @author Stephen Colebourne + * @version $Id: Broken1Enum.java,v 1.1 2002/11/02 13:17:06 scolebourne Exp $ + */ +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); + } +} diff --git a/src/test/org/apache/commons/lang/enum/Broken2Enum.java b/src/test/org/apache/commons/lang/enum/Broken2Enum.java new file mode 100644 index 000000000..5f7eee683 --- /dev/null +++ b/src/test/org/apache/commons/lang/enum/Broken2Enum.java @@ -0,0 +1,89 @@ +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +package org.apache.commons.lang.enum; + +import java.util.Iterator; +import java.util.List; +import java.util.Map; +/** + * Broken color enumeration. + * + * @author Stephen Colebourne + * @version $Id: Broken2Enum.java,v 1.1 2002/11/02 13:17:06 scolebourne Exp $ + */ +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); + } +} diff --git a/src/test/org/apache/commons/lang/enum/Broken3Enum.java b/src/test/org/apache/commons/lang/enum/Broken3Enum.java new file mode 100644 index 000000000..c09afce14 --- /dev/null +++ b/src/test/org/apache/commons/lang/enum/Broken3Enum.java @@ -0,0 +1,89 @@ +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +package org.apache.commons.lang.enum; + +import java.util.Iterator; +import java.util.List; +import java.util.Map; +/** + * Broken color enumeration. + * + * @author Stephen Colebourne + * @version $Id: Broken3Enum.java,v 1.1 2002/11/02 13:17:06 scolebourne Exp $ + */ +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); + } +} diff --git a/src/test/org/apache/commons/lang/enum/EnumTest.java b/src/test/org/apache/commons/lang/enum/EnumTest.java index bf1352245..442dc3047 100644 --- a/src/test/org/apache/commons/lang/enum/EnumTest.java +++ b/src/test/org/apache/commons/lang/enum/EnumTest.java @@ -66,7 +66,7 @@ * Test cases for the {@link Enum} class. * * @author Stephen Colebourne - * @version $Id: EnumTest.java,v 1.2 2002/10/13 18:41:24 sullis Exp $ + * @version $Id: EnumTest.java,v 1.3 2002/11/02 13:17:06 scolebourne Exp $ */ public final class EnumTest extends TestCase { @@ -155,4 +155,31 @@ public void testSerialization() { assertSame(ColorEnum.BLUE, SerializationUtils.clone(ColorEnum.BLUE)); } + public void testBroken1() { + try { + Broken1Enum.RED.getName(); + fail(); + } catch (ExceptionInInitializerError ex) { + assertTrue(ex.getCause() instanceof IllegalArgumentException); + } + } + + public void testBroken2() { + try { + Broken2Enum.RED.getName(); + fail(); + } catch (ExceptionInInitializerError ex) { + assertTrue(ex.getCause() instanceof IllegalArgumentException); + } + } + + public void testBroken3() { + try { + Broken3Enum.RED.getName(); + fail(); + } catch (ExceptionInInitializerError ex) { + assertTrue(ex.getCause() instanceof IllegalArgumentException); + } + } + }