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
This commit is contained in:
Stephen Colebourne 2002-11-02 13:17:06 +00:00
parent f44c297fa2
commit b0d8436ce8
4 changed files with 295 additions and 1 deletions

View File

@ -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
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.enum;
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: 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);
}
}

View File

@ -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
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.enum;
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: 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);
}
}

View File

@ -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
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.enum;
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: 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);
}
}

View File

@ -66,7 +66,7 @@ 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: 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 final class EnumTest extends TestCase {
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);
}
}
}