Removed deprecated core.enums package
This commit is contained in:
parent
28aa34f7ff
commit
5472e975f6
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2013 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -16,21 +16,27 @@
|
||||||
|
|
||||||
package org.springframework.tests.sample.beans;
|
package org.springframework.tests.sample.beans;
|
||||||
|
|
||||||
import org.springframework.core.enums.ShortCodedLabeledEnum;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Rob Harrop
|
* @author Rob Harrop
|
||||||
|
* @author Juergen Hoeller
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public class Colour extends ShortCodedLabeledEnum {
|
public class Colour {
|
||||||
|
|
||||||
public static final Colour RED = new Colour(0, "RED");
|
public static final Colour RED = new Colour("RED");
|
||||||
public static final Colour BLUE = new Colour(1, "BLUE");
|
public static final Colour BLUE = new Colour("BLUE");
|
||||||
public static final Colour GREEN = new Colour(2, "GREEN");
|
public static final Colour GREEN = new Colour("GREEN");
|
||||||
public static final Colour PURPLE = new Colour(3, "PURPLE");
|
public static final Colour PURPLE = new Colour("PURPLE");
|
||||||
|
|
||||||
private Colour(int code, String label) {
|
private final String name;
|
||||||
super(code, label);
|
|
||||||
|
public Colour(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,192 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2002-2013 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.core.enums;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.ObjectInputStream;
|
|
||||||
import java.io.ObjectOutputStream;
|
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Keith Donald
|
|
||||||
* @author Juergen Hoeller
|
|
||||||
* @author Sam Brannen
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public class LabeledEnumTests extends TestCase {
|
|
||||||
|
|
||||||
private byte[] serializeObject(final Object obj) throws IOException {
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
||||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
|
||||||
oos.writeObject(obj);
|
|
||||||
oos.close();
|
|
||||||
return baos.toByteArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Object deserializeObject(final byte[] serializedBytes) throws IOException, ClassNotFoundException {
|
|
||||||
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedBytes));
|
|
||||||
Object obj = ois.readObject();
|
|
||||||
ois.close();
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Object serializeAndDeserializeObject(Object obj) throws IOException, ClassNotFoundException {
|
|
||||||
return deserializeObject(serializeObject(obj));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testCodeFound() {
|
|
||||||
Dog golden = (Dog) StaticLabeledEnumResolver.instance().getLabeledEnumByCode(Dog.class, new Short((short) 11));
|
|
||||||
Dog borderCollie = (Dog) StaticLabeledEnumResolver.instance().getLabeledEnumByCode(Dog.class,
|
|
||||||
new Short((short) 13));
|
|
||||||
assertSame(golden, Dog.GOLDEN_RETRIEVER);
|
|
||||||
assertSame(borderCollie, Dog.BORDER_COLLIE);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testCodeFoundForAbstractEnums() {
|
|
||||||
ValuedEnum one = (ValuedEnum) StaticLabeledEnumResolver.instance().getLabeledEnumByCode(ValuedEnum.class,
|
|
||||||
new Short((short) 1));
|
|
||||||
ValuedEnum two = (ValuedEnum) StaticLabeledEnumResolver.instance().getLabeledEnumByCode(ValuedEnum.class,
|
|
||||||
new Short((short) 2));
|
|
||||||
assertSame(one, ValuedEnum.ONE);
|
|
||||||
assertSame(two, ValuedEnum.TWO);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testDeserializationOfInnerClassEnums() throws Exception {
|
|
||||||
assertSame(serializeAndDeserializeObject(Other.THING1), Other.THING1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testDeserializationOfStandAloneEnums() throws Exception {
|
|
||||||
assertSame(serializeAndDeserializeObject(StandAloneStaticLabeledEnum.ENUM1),
|
|
||||||
StandAloneStaticLabeledEnum.ENUM1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testLabelFound() {
|
|
||||||
Dog golden = (Dog) StaticLabeledEnumResolver.instance().getLabeledEnumByLabel(Dog.class, "Golden Retriever");
|
|
||||||
Dog borderCollie = (Dog) StaticLabeledEnumResolver.instance().getLabeledEnumByLabel(Dog.class, "Border Collie");
|
|
||||||
assertSame(golden, Dog.GOLDEN_RETRIEVER);
|
|
||||||
assertSame(borderCollie, Dog.BORDER_COLLIE);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testLabelFoundForStandAloneEnum() {
|
|
||||||
StandAloneStaticLabeledEnum enum1 = (StandAloneStaticLabeledEnum)
|
|
||||||
StaticLabeledEnumResolver.instance().getLabeledEnumByLabel(StandAloneStaticLabeledEnum.class, "Enum1");
|
|
||||||
StandAloneStaticLabeledEnum enum2 = (StandAloneStaticLabeledEnum)
|
|
||||||
StaticLabeledEnumResolver.instance().getLabeledEnumByLabel(StandAloneStaticLabeledEnum.class, "Enum2");
|
|
||||||
assertSame(enum1, StandAloneStaticLabeledEnum.ENUM1);
|
|
||||||
assertSame(enum2, StandAloneStaticLabeledEnum.ENUM2);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testLabelFoundForAbstractEnums() {
|
|
||||||
ValuedEnum one = (ValuedEnum)
|
|
||||||
StaticLabeledEnumResolver.instance().getLabeledEnumByLabel(ValuedEnum.class, "one");
|
|
||||||
ValuedEnum two = (ValuedEnum)
|
|
||||||
StaticLabeledEnumResolver.instance().getLabeledEnumByLabel(ValuedEnum.class, "two");
|
|
||||||
assertSame(one, ValuedEnum.ONE);
|
|
||||||
assertSame(two, ValuedEnum.TWO);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testDoesNotMatchWrongClass() {
|
|
||||||
try {
|
|
||||||
StaticLabeledEnumResolver.instance().getLabeledEnumByCode(Dog.class,
|
|
||||||
new Short((short) 1));
|
|
||||||
fail("Should have failed");
|
|
||||||
}
|
|
||||||
catch (IllegalArgumentException e) {
|
|
||||||
// expected
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testEquals() {
|
|
||||||
assertEquals("Code equality means equals", Dog.GOLDEN_RETRIEVER, new Dog(11, "Golden Retriever"));
|
|
||||||
assertFalse("Code inequality means notEquals", Dog.GOLDEN_RETRIEVER.equals(new Dog(12, "Golden Retriever")));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings({ "serial", "unused" })
|
|
||||||
private static class Other extends StaticLabeledEnum {
|
|
||||||
|
|
||||||
public static final Other THING1 = new Other(1, "Thing1");
|
|
||||||
|
|
||||||
public static final Other THING2 = new Other(2, "Thing2");
|
|
||||||
|
|
||||||
|
|
||||||
private Other(int code, String name) {
|
|
||||||
super(code, name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
|
||||||
private static class Dog extends StaticLabeledEnum {
|
|
||||||
|
|
||||||
public static final Dog GOLDEN_RETRIEVER = new Dog(11, null) {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getLabel() {
|
|
||||||
return "Golden Retriever";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Overriding getType() is no longer necessary as of Spring 2.5;
|
|
||||||
// however, this is left here to provide valid testing for
|
|
||||||
// backwards compatibility.
|
|
||||||
@Override
|
|
||||||
public Class getType() {
|
|
||||||
return Dog.class;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static final Dog BORDER_COLLIE = new Dog(13, "Border Collie");
|
|
||||||
public static final Dog WHIPPET = new Dog(14, "Whippet");
|
|
||||||
|
|
||||||
// Ignore this
|
|
||||||
public static final Other THING1 = Other.THING1;
|
|
||||||
|
|
||||||
|
|
||||||
private Dog(int code, String name) {
|
|
||||||
super(code, name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
|
||||||
private static abstract class ValuedEnum extends StaticLabeledEnum {
|
|
||||||
|
|
||||||
public static final ValuedEnum ONE = new ValuedEnum(1, "one") {
|
|
||||||
@Override
|
|
||||||
public int getValue() {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static final ValuedEnum TWO = new ValuedEnum(2, "two") {
|
|
||||||
@Override
|
|
||||||
public int getValue() {
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private ValuedEnum(int code, String name) {
|
|
||||||
super(code, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract int getValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,36 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2002-2007 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.core.enums;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stand-alone static enum for use in {@link LabeledEnumTests}.
|
|
||||||
*
|
|
||||||
* @author Sam Brannen
|
|
||||||
* @since 2.5
|
|
||||||
*/
|
|
||||||
public class StandAloneStaticLabeledEnum extends StaticLabeledEnum {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
public static final StandAloneStaticLabeledEnum ENUM1 = new StandAloneStaticLabeledEnum(1, "Enum1");
|
|
||||||
public static final StandAloneStaticLabeledEnum ENUM2 = new StandAloneStaticLabeledEnum(2, "Enum2");
|
|
||||||
|
|
||||||
|
|
||||||
private StandAloneStaticLabeledEnum(int code, String name) {
|
|
||||||
super(code, name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2013 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -21,7 +21,6 @@ import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.core.enums.LabeledEnum;
|
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
import org.springframework.web.servlet.support.BindStatus;
|
import org.springframework.web.servlet.support.BindStatus;
|
||||||
|
|
@ -40,10 +39,6 @@ import org.springframework.web.servlet.support.BindStatus;
|
||||||
* {@link #exhaustiveCompare exhaustive comparison} with the aim being to <strong>prove</strong> equality rather
|
* {@link #exhaustiveCompare exhaustive comparison} with the aim being to <strong>prove</strong> equality rather
|
||||||
* than disprove it.
|
* than disprove it.
|
||||||
*
|
*
|
||||||
* <p>Special support is given for instances of {@link LabeledEnum} with a {@code String}-based
|
|
||||||
* comparison of the candidate value against the code of the {@link LabeledEnum}. This can be useful when a
|
|
||||||
* {@link LabeledEnum} is used to define a list of '{@code <option>}' elements in HTML.
|
|
||||||
*
|
|
||||||
* <p>Next, an attempt is made to compare the {@code String} representations of both the candidate and bound
|
* <p>Next, an attempt is made to compare the {@code String} representations of both the candidate and bound
|
||||||
* values. This may result in {@code true} in a number of cases due to the fact both values will be represented
|
* values. This may result in {@code true} in a number of cases due to the fact both values will be represented
|
||||||
* as {@code Strings} when shown to the user.
|
* as {@code Strings} when shown to the user.
|
||||||
|
|
@ -153,18 +148,7 @@ abstract class SelectedValueComparator {
|
||||||
PropertyEditor editor, Map<PropertyEditor, Object> convertedValueCache) {
|
PropertyEditor editor, Map<PropertyEditor, Object> convertedValueCache) {
|
||||||
|
|
||||||
String candidateDisplayString = ValueFormatter.getDisplayString(candidate, editor, false);
|
String candidateDisplayString = ValueFormatter.getDisplayString(candidate, editor, false);
|
||||||
if (boundValue instanceof LabeledEnum) {
|
if (boundValue.getClass().isEnum()) {
|
||||||
LabeledEnum labeledEnum = (LabeledEnum) boundValue;
|
|
||||||
String enumCodeAsString = ObjectUtils.getDisplayString(labeledEnum.getCode());
|
|
||||||
if (enumCodeAsString.equals(candidateDisplayString)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
String enumLabelAsString = ObjectUtils.getDisplayString(labeledEnum.getLabel());
|
|
||||||
if (enumLabelAsString.equals(candidateDisplayString)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (boundValue.getClass().isEnum()) {
|
|
||||||
Enum boundEnum = (Enum) boundValue;
|
Enum boundEnum = (Enum) boundValue;
|
||||||
String enumCodeAsString = ObjectUtils.getDisplayString(boundEnum.name());
|
String enumCodeAsString = ObjectUtils.getDisplayString(boundEnum.name());
|
||||||
if (enumCodeAsString.equals(candidateDisplayString)) {
|
if (enumCodeAsString.equals(candidateDisplayString)) {
|
||||||
|
|
|
||||||
|
|
@ -185,59 +185,12 @@ public class OptionTagTests extends AbstractHtmlElementTagTests {
|
||||||
try {
|
try {
|
||||||
tag.doStartTag();
|
tag.doStartTag();
|
||||||
fail("Must not be able to use <option> tag without exposed context.");
|
fail("Must not be able to use <option> tag without exposed context.");
|
||||||
} catch (IllegalStateException ex) {
|
}
|
||||||
|
catch (IllegalStateException ex) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testWithEnum() throws Exception {
|
|
||||||
String selectName = "testBean.favouriteColour";
|
|
||||||
getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), selectName, false));
|
|
||||||
|
|
||||||
String value = Colour.GREEN.getCode().toString();
|
|
||||||
String label = Colour.GREEN.getLabel();
|
|
||||||
|
|
||||||
this.tag.setValue(value);
|
|
||||||
this.tag.setLabel(label);
|
|
||||||
|
|
||||||
int result = this.tag.doStartTag();
|
|
||||||
assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
|
|
||||||
result = this.tag.doEndTag();
|
|
||||||
assertEquals(Tag.EVAL_PAGE, result);
|
|
||||||
|
|
||||||
String output = getOutput();
|
|
||||||
|
|
||||||
assertOptionTagOpened(output);
|
|
||||||
assertOptionTagClosed(output);
|
|
||||||
assertContainsAttribute(output, "value", value);
|
|
||||||
assertContainsAttribute(output, "selected", "selected");
|
|
||||||
assertBlockTagContains(output, label);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testWithEnumNotSelected() throws Exception {
|
|
||||||
String selectName = "testBean.favouriteColour";
|
|
||||||
getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), selectName, false));
|
|
||||||
|
|
||||||
String value = Colour.BLUE.getCode().toString();
|
|
||||||
String label = Colour.BLUE.getLabel();
|
|
||||||
|
|
||||||
this.tag.setValue(value);
|
|
||||||
this.tag.setLabel(label);
|
|
||||||
|
|
||||||
int result = this.tag.doStartTag();
|
|
||||||
assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
|
|
||||||
result = this.tag.doEndTag();
|
|
||||||
assertEquals(Tag.EVAL_PAGE, result);
|
|
||||||
|
|
||||||
String output = getOutput();
|
|
||||||
|
|
||||||
assertOptionTagOpened(output);
|
|
||||||
assertOptionTagClosed(output);
|
|
||||||
assertContainsAttribute(output, "value", value);
|
|
||||||
assertAttributeNotPresent(output, "selected");
|
|
||||||
assertBlockTagContains(output, label);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testWithPropertyEditor() throws Exception {
|
public void testWithPropertyEditor() throws Exception {
|
||||||
String selectName = "testBean.stringArray";
|
String selectName = "testBean.stringArray";
|
||||||
BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false) {
|
BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue