1 22 23 package org.objectweb.perseus.jndi; 24 25 import junit.framework.TestCase; 26 import org.objectweb.perseus.jndi.PropertiesLoader; 27 28 import java.util.Properties ; 29 30 33 public class TestPropertiesLoader extends TestCase { 34 private static final String PROPFILE = "propfile"; 35 private static String propertiesFileName = null; 36 private static Class contextImpl = null; 37 38 protected PropertiesLoader pl = null; 39 40 43 public TestPropertiesLoader(String tn) { 44 super(tn); 45 } 46 47 50 protected void setUp() { 51 pl=null; 52 propertiesFileName = System.getProperty(PROPFILE); 53 contextImpl = ContextImpl.class; 54 } 55 56 public void testSimple() { 57 pl = new PropertiesLoader(); 58 try { 59 pl.load(propertiesFileName); 60 } catch (Exception e) { 61 System.out.println( 62 "The simple test of PropertiesLoader throws the following error:"); 63 e.printStackTrace(); 64 assertTrue(false); 65 } 66 assertTrue(true); 67 } 68 69 public void testPropertiesField() { 70 pl = new PropertiesLoader(); 71 try { 72 pl.load(propertiesFileName); 73 } catch (Exception e) { 74 e.printStackTrace(); 75 assertTrue(false); 76 } 77 Properties p = pl.getProperties(); 78 assertNotNull(p); 79 assertEquals("1", p.getProperty("Field1")); 80 assertEquals("200000", p.getProperty("Field2")); 81 assertEquals("azerty", p.getProperty("Field3")); 82 assertEquals("0.45", p.getProperty("Field4")); 83 } 84 85 public void testContextField() { 86 try { 87 pl = new PropertiesLoader(contextImpl); 88 } catch (Exception e) { 89 e.printStackTrace(); 90 assertTrue("The class "+contextImpl+" doesn't be found", false); 91 } 92 try { 93 pl.load(propertiesFileName); 94 } catch (Exception e) { 95 e.printStackTrace(); 96 assertTrue(false); 97 } 98 assertNotNull("Unexpected null return from the getContext method", 99 pl.getContext()); 100 try { 101 assertTrue("1".equals(pl.getContext().lookup("Field1"))); 102 assertTrue("200000".equals(pl.getContext().lookup("Field2"))); 103 assertTrue("azerty".equals(pl.getContext().lookup("Field3"))); 104 assertTrue("0.45".equals(pl.getContext().lookup("Field4"))); 105 } catch (Exception e) { 106 e.printStackTrace(); 107 assertTrue("A field is unreachable in context", false); 108 } 109 } 110 111 public void testInvokeSetters() { 112 try { 113 pl = new PropertiesLoader(contextImpl); 114 } catch (Exception e) { 115 e.printStackTrace(); 116 assertTrue("The class "+contextImpl+" doesn't be found", false); 117 } 118 try { 119 pl.load(propertiesFileName); 120 } catch (Exception e) { 121 e.printStackTrace(); 122 assertTrue(false); 123 } 124 try { 125 pl.invokeSetters(new MyObject()); 126 assertTrue(true); 127 } catch (Exception e) { 128 e.printStackTrace(); 129 assertTrue(e.getMessage(), false); 130 } 131 } 132 133 private class MyObject { 134 public boolean field1set = false; 135 public boolean field2set = false; 136 public boolean field3set = false; 137 public boolean field4set = false; 138 139 public void setField1(Integer i) { 140 assertTrue(new Integer (1).equals(i)); 141 field1set = true; 142 } 143 public void setField2(Long i) { 144 assertTrue(new Long (200000).equals(i)); 145 field2set = true; 146 } 147 public void setField3(String s) { 148 assertTrue("azerty".equals(s)); 149 field3set = true; 150 } 151 public void setField4(Float f) { 152 assertTrue(new Float (0.45).equals(f)); 153 field3set = true; 154 } 155 } 156 } 157 | Popular Tags |