1 15 package hivemind.test.rules; 16 17 import java.beans.PropertyEditorManager ; 18 19 import org.apache.hivemind.ApplicationRuntimeException; 20 import org.apache.hivemind.Location; 21 import org.apache.hivemind.Registry; 22 import org.apache.hivemind.schema.Translator; 23 import org.apache.hivemind.schema.rules.SmartTranslator; 24 import org.apache.hivemind.test.HiveMindTestCase; 25 26 31 public class TestSmartTranslator extends HiveMindTestCase 32 { 33 34 37 public void testInt() 38 { 39 Translator t = new SmartTranslator(); 40 41 Object result = t.translate(null, int.class, "-37", null); 42 43 assertEquals(new Integer (-37), result); 44 } 45 46 public void testNullInput() 47 { 48 Translator t = new SmartTranslator(); 49 50 assertNull(t.translate(null, int.class, null, null)); 51 } 52 53 public void testBlankInput() 54 { 55 Translator t = new SmartTranslator(); 56 57 assertEquals("", t.translate(null, String .class, "", null)); 58 } 59 60 public void testDefault() 61 { 62 Translator t = new SmartTranslator("default=100"); 63 64 Object result = t.translate(null, int.class, null, null); 65 66 assertEquals(new Integer (100), result); 67 } 68 69 72 public void testDouble() 73 { 74 Translator t = new SmartTranslator(); 75 76 Object result = t.translate(null, Double .class, "3.14", null); 77 78 assertEquals(new Double ("3.14"), result); 79 } 80 81 85 public void testString() 86 { 87 Translator t = new SmartTranslator(); 88 89 Object result = t.translate(null, String .class, "Fluffy Puppies", null); 90 91 assertEquals("Fluffy Puppies", result); 92 } 93 94 98 public void testObjectAsString() 99 { 100 Translator t = new SmartTranslator(); 101 102 Object result = t.translate(null, Object .class, "Fluffy Puppies", null); 103 104 assertEquals("Fluffy Puppies", result); 105 } 106 107 public void testStringWithNoEditor() 108 { 109 String [] paths = PropertyEditorManager.getEditorSearchPath(); 110 111 try 112 { 113 PropertyEditorManager.setEditorSearchPath(new String [] { "bogus.package" }); 114 Translator t = new SmartTranslator(); 115 116 Object result = t.translate(null, String .class, "Fluffy Puppies", null); 117 118 assertEquals("Fluffy Puppies", result); 119 } 120 finally 121 { 122 PropertyEditorManager.setEditorSearchPath(paths); 123 } 124 125 } 126 127 public void testNoEditor() 128 { 129 Translator t = new SmartTranslator(); 130 Location l = newLocation(); 131 132 try 133 { 134 t.translate(null, Registry.class, "fred", l); 135 136 unreachable(); 137 } 138 catch (ApplicationRuntimeException ex) 139 { 140 assertEquals("Unable to translate 'fred' to type org.apache.hivemind.Registry: " 141 + "No property editor for org.apache.hivemind.Registry.", ex.getMessage()); 142 143 assertSame(l, ex.getLocation()); 144 } 145 } 146 } 147 | Popular Tags |