1 17 18 package org.apache.avalon.framework.test; 19 20 import org.apache.avalon.framework.ValuedEnum; 21 22 import junit.framework.TestCase; 23 24 import java.util.Map ; 25 import java.util.HashMap ; 26 27 32 public class ValuedEnumTestCase extends TestCase 33 { 34 private final static class Color extends ValuedEnum 35 { 36 public static final Color RED = new Color( "Red", 0 ); 37 public static final Color RED_NEGATIVE = new Color( "Red", -1 ); 38 public static final Color GREEN = new Color( "Green", 1 ); 39 public static final Color BLUE = new Color( "Blue", 2 ); 40 41 public Color( final String color, final int value ) 42 { 43 super( color, value ); 44 } 45 46 public Color( final String color, final int value, Map stuff ) 47 { 48 super( color, value, stuff ); 49 } 50 } 51 52 private final static class OtherColor extends ValuedEnum 53 { 54 public static final OtherColor RED = new OtherColor( "Red", 0 ); 55 public static final OtherColor RED_NEGATIVE = new OtherColor( "Red", -1 ); 56 public static final OtherColor GREEN = new OtherColor( "Green", 1 ); 57 public static final OtherColor BLUE = new OtherColor( "Blue", 2 ); 58 59 public OtherColor( final String color, final int value ) 60 { 61 super( color, value ); 62 } 63 64 public OtherColor( final String color, final int value, Map stuff ) 65 { 66 super( color, value, stuff ); 67 } 68 } 69 70 public ValuedEnumTestCase( final String name ) 71 { 72 super( name ); 73 } 74 75 public void testConstructor() 76 { 77 assertNotNull( new Color( "blah", 0, null ) ); 78 79 Map entries = new HashMap (); 80 81 Color c = new Color( "blah", 0, entries ); 82 83 assertTrue( entries.containsKey("blah") ); 84 assertTrue( entries.containsValue(c) ); 85 86 OtherColor c2 = new OtherColor( "blah", 0, entries ); 87 assertTrue( entries.containsKey("blah") ); 88 assertFalse( entries.containsValue(c) ); 89 assertTrue( entries.containsValue(c2) ); 90 } 91 92 public void testEquals() 93 { 94 assertTrue( Color.RED.equals( Color.RED ) ); 95 assertTrue( Color.GREEN.equals( Color.GREEN ) ); 96 assertTrue( Color.BLUE.equals( Color.BLUE ) ); 97 98 assertTrue( !OtherColor.RED.equals( Color.RED ) ); 99 assertTrue( !OtherColor.GREEN.equals( Color.GREEN ) ); 100 assertTrue( !OtherColor.BLUE.equals( Color.BLUE ) ); 101 102 assertTrue( !Color.RED.equals( OtherColor.RED ) ); 103 assertTrue( !Color.GREEN.equals( OtherColor.GREEN ) ); 104 assertTrue( !Color.BLUE.equals( OtherColor.BLUE ) ); 105 106 assertTrue( !Color.RED.equals( Color.GREEN ) ); 107 assertTrue( !Color.GREEN.equals( Color.BLUE ) ); 108 assertTrue( !Color.BLUE.equals( Color.RED ) ); 109 110 assertTrue( !Color.BLUE.equals( null ) ); 111 112 assertTrue( new Color(null,0).equals( new Color( null,0 ) ) ); 113 assertFalse( new Color(null,0).equals( new Color( "hi",0 ) ) ); 114 assertFalse( new Color("hi",0).equals( new Color( null,0 ) ) ); 115 116 assertTrue( Color.RED.equals( Color.RED_NEGATIVE ) ); 118 assertTrue( Color.RED_NEGATIVE.equals( Color.RED ) ); 119 assertTrue( OtherColor.RED.equals( OtherColor.RED_NEGATIVE ) ); 120 assertTrue( OtherColor.RED_NEGATIVE.equals( OtherColor.RED ) ); 121 } 122 123 public void testHashCode() 124 { 125 assertTrue( Color.RED.hashCode() == Color.RED.hashCode() ); 126 assertTrue( Color.GREEN.hashCode() == Color.GREEN.hashCode() ); 127 assertTrue( Color.BLUE.hashCode() == Color.BLUE.hashCode() ); 128 129 assertTrue( OtherColor.RED.hashCode() != Color.RED.hashCode() ); 130 assertTrue( OtherColor.GREEN.hashCode() != Color.GREEN.hashCode() ); 131 assertTrue( OtherColor.BLUE.hashCode() != Color.BLUE.hashCode() ); 132 133 assertTrue( Color.RED.hashCode() != OtherColor.RED.hashCode() ); 134 assertTrue( Color.GREEN.hashCode() != OtherColor.GREEN.hashCode() ); 135 assertTrue( Color.BLUE.hashCode() != OtherColor.BLUE.hashCode() ); 136 137 assertTrue( Color.RED.hashCode() != Color.GREEN.hashCode() ); 138 assertTrue( Color.GREEN.hashCode() != Color.BLUE.hashCode() ); 139 assertTrue( Color.BLUE.hashCode() != Color.RED.hashCode() ); 140 141 assertTrue( Color.RED.hashCode() ==Color.RED_NEGATIVE.hashCode() ); 143 assertTrue( Color.RED_NEGATIVE.hashCode() ==Color.RED.hashCode() ); 144 assertTrue( OtherColor.RED.hashCode() ==OtherColor.RED_NEGATIVE.hashCode() ); 145 assertTrue( OtherColor.RED_NEGATIVE.hashCode() ==OtherColor.RED.hashCode() ); 146 } 147 148 public void testGet() 149 { 150 assertEquals( "Red", Color.RED.getName() ); 151 assertNull( (new Color(null,0)).getName() ); 152 } 153 154 public void testToString() 155 { 156 assertTrue( Color.RED.toString().indexOf( "Red") != -1 ); 157 assertTrue( Color.RED.toString().indexOf( Color.class.getName() ) != -1 ); 158 159 Color c = new Color(null,0); 160 assertTrue( c.toString().indexOf( "null") != -1 ); 161 162 } 163 } 164 | Popular Tags |