1 15 package hivemind.test.rules; 16 17 import hivemind.test.FrameworkTestCase; 18 19 import org.apache.hivemind.ApplicationRuntimeException; 20 import org.apache.hivemind.impl.ModuleImpl; 21 import org.apache.hivemind.internal.Module; 22 import org.apache.hivemind.schema.rules.EnumerationTranslator; 23 24 29 public class TestEnumerationTranslator extends FrameworkTestCase 30 { 31 32 private Module newModule() 33 { 34 ModuleImpl result = new ModuleImpl(); 35 result.setClassResolver(getClassResolver()); 36 37 return result; 38 } 39 40 public void testNull() 41 { 42 Module m = (Module) newMock(Module.class); 43 44 EnumerationTranslator t = new EnumerationTranslator( 45 "java.lang.Boolean,true=TRUE,false=FALSE"); 46 47 assertEquals(null, t.translate(m, null, null, null)); 48 ; 49 } 50 51 public void testMatch() 52 { 53 Module m = newModule(); 54 55 EnumerationTranslator t = new EnumerationTranslator( 56 "java.lang.Boolean,true=TRUE,false=FALSE"); 57 58 assertEquals(Boolean.TRUE, t.translate(m, null, "true", null)); 59 assertEquals(Boolean.FALSE, t.translate(m, null, "false", null)); 60 } 61 62 public void testBadClass() 63 { 64 Module m = newModule(); 65 66 EnumerationTranslator t = new EnumerationTranslator( 67 "lava.jang.Boolean,true=TRUE,false=FALSE"); 68 69 try 70 { 71 t.translate(m, null, "true", null); 72 73 unreachable(); 74 } 75 catch (ApplicationRuntimeException ex) 76 { 77 assertExceptionSubstring(ex, "Unable to convert type 'lava.jang.Boolean'"); 78 } 79 } 80 81 public void testUnrecognizedValue() throws Exception 82 { 83 Module m = newModule(); 84 85 EnumerationTranslator t = new EnumerationTranslator( 86 "java.lang.Boolean,true=TRUE,false=FALSE"); 87 88 try 89 { 90 91 t.translate(m, null, "fred", null); 92 unreachable(); 93 } 94 catch (ApplicationRuntimeException ex) 95 { 96 assertExceptionSubstring(ex, "'fred' is not a recognized enumerated value."); 97 } 98 } 99 100 public void testBadField() throws Exception 101 { 102 Module m = newModule(); 103 104 EnumerationTranslator t = new EnumerationTranslator( 105 "java.lang.Boolean,true=HONEST_TO_GOD_TRUE,false=FALSE"); 106 107 try 108 { 109 t.translate(m, null, "true", null); 110 unreachable(); 111 } 112 catch (ApplicationRuntimeException ex) 113 { 114 assertExceptionSubstring( 115 ex, 116 "Unable to obtain value for static field java.lang.Boolean.HONEST_TO_GOD_TRUE"); 117 } 118 } 119 120 } | Popular Tags |