1 16 17 package org.springframework.core; 18 19 import java.util.Set ; 20 21 import junit.framework.TestCase; 22 23 28 public class ConstantsTests extends TestCase { 29 30 public void testConstants() { 31 Constants c = new Constants(A.class); 32 assertEquals(A.class.getName(), c.getClassName()); 33 assertEquals(5, c.getSize()); 34 35 assertEquals(c.asNumber("DOG").intValue(), A.DOG); 36 assertEquals(c.asNumber("dog").intValue(), A.DOG); 37 assertEquals(c.asNumber("cat").intValue(), A.CAT); 38 39 try { 40 c.asNumber("bogus"); 41 fail("Can't get bogus field"); 42 } 43 catch (ConstantException ex) { 44 } 45 46 assertTrue(c.asString("S1").equals(A.S1)); 47 try { 48 c.asNumber("S1"); 49 fail("Wrong type"); 50 } 51 catch (ConstantException ex) { 52 } 53 54 Set values = c.getValues(""); 55 assertEquals(c.getSize(), values.size()); 56 assertTrue(values.contains(new Integer (0))); 57 assertTrue(values.contains(new Integer (66))); 58 assertTrue(values.contains("")); 59 60 values = c.getValues("D"); 61 assertEquals(1, values.size()); 62 assertTrue(values.contains(new Integer (0))); 63 64 values = c.getValuesForProperty("myProperty"); 65 assertEquals(2, values.size()); 66 assertTrue(values.contains(new Integer (1))); 67 assertTrue(values.contains(new Integer (2))); 68 69 assertEquals(c.toCode(new Integer (0), ""), "DOG"); 70 assertEquals(c.toCode(new Integer (0), "D"), "DOG"); 71 assertEquals(c.toCode(new Integer (0), "DO"), "DOG"); 72 assertEquals(c.toCode(new Integer (0), "DoG"), "DOG"); 73 assertEquals(c.toCode(new Integer (66), ""), "CAT"); 74 assertEquals(c.toCode(new Integer (66), "C"), "CAT"); 75 assertEquals(c.toCode(new Integer (66), "ca"), "CAT"); 76 assertEquals(c.toCode(new Integer (66), "cAt"), "CAT"); 77 assertEquals(c.toCode("", ""), "S1"); 78 assertEquals(c.toCode("", "s"), "S1"); 79 assertEquals(c.toCode("", "s1"), "S1"); 80 try { 81 c.toCode("bogus", "bogus"); 82 fail("Should have thrown ConstantException"); 83 } 84 catch (ConstantException ex) { 85 } 87 88 assertEquals(c.toCodeForProperty(new Integer (1), "myProperty"), "MY_PROPERTY_NO"); 89 assertEquals(c.toCodeForProperty(new Integer (2), "myProperty"), "MY_PROPERTY_YES"); 90 try { 91 c.toCodeForProperty("bogus", "bogus"); 92 fail("Should have thrown ConstantException"); 93 } 94 catch (ConstantException ex) { 95 } 97 } 98 99 100 public static class A { 101 102 public static final int DOG = 0; 103 public static final int CAT = 66; 104 public static final String S1 = ""; 105 106 public static final int MY_PROPERTY_NO = 1; 107 public static final int MY_PROPERTY_YES = 2; 108 109 110 protected static final int P = -1; 111 protected boolean f; 112 static final Object o = new Object (); 113 } 114 115 } 116 | Popular Tags |