1 61 62 63 package org.apache.commons.discovery.test; 64 65 66 import java.util.Properties ; 67 68 import junit.framework.Test; 69 import junit.framework.TestCase; 70 import junit.framework.TestSuite; 71 import org.apache.commons.discovery.tools.DefaultClassHolder; 72 import org.apache.commons.discovery.tools.DiscoverSingleton; 73 import org.apache.commons.discovery.tools.ManagedProperties; 74 import org.apache.commons.discovery.tools.PropertiesHolder; 75 import org.apache.commons.discovery.tools.SPInterface; 76 77 78 82 public class TestAll extends TestCase { 83 private static final int logLevel = 84 org.apache.commons.discovery.log.SimpleLog.LOG_LEVEL_INFO; 85 86 87 public TestAll(String testName) { 88 super(testName); 89 } 90 91 public static Test suite() { 92 return new TestSuite(TestAll.class); 93 } 94 95 public void testFindDefaultImpl_1() { 96 org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel); 97 98 TestInterface1 ti = null; 99 100 try { 101 ti = (TestInterface1)DiscoverSingleton.find(TestInterface1.class, 102 TestImpl1_1.class.getName()); 103 104 assertTrue(ti.getClass().getName() + "!=" + TestImpl1_1.class.getName(), 105 ti.getClass().getName().equals(TestImpl1_1.class.getName())); 106 } finally { 107 DiscoverSingleton.release(); 108 } 109 } 110 111 public void testFindDefaultImpl_2() { 112 org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel); 113 114 TestInterface1 ti = null; 115 116 try { 117 ti = (TestInterface1)DiscoverSingleton.find(TestInterface1.class, 118 TestImpl1_2.class.getName()); 119 120 assertTrue(ti.getClass().getName() + "!=" + TestImpl1_2.class.getName(), 121 ti.getClass().getName().equals(TestImpl1_2.class.getName())); 122 } finally { 123 DiscoverSingleton.release(); 124 } 125 } 126 127 public void testCache() { 128 org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel); 129 130 TestInterface1 ti = null; 131 132 try { 133 ti = (TestInterface1)DiscoverSingleton.find(TestInterface1.class, 134 TestImpl1_1.class.getName()); 135 136 assertTrue("1. " + ti.getClass().getName() + "!=" + TestImpl1_1.class.getName(), 137 ti.getClass().getName().equals(TestImpl1_1.class.getName())); 138 139 141 ti = (TestInterface1)DiscoverSingleton.find(TestInterface1.class, 142 TestImpl1_2.class.getName()); 143 144 assertTrue("2. " + ti.getClass().getName() + "!=" + TestImpl1_1.class.getName(), 146 ti.getClass().getName().equals(TestImpl1_1.class.getName())); 147 } finally { 148 DiscoverSingleton.release(); 149 } 150 } 151 152 public void testRelease() { 153 org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel); 154 155 TestInterface1 ti = null; 156 157 try { 158 ti = (TestInterface1)DiscoverSingleton.find(TestInterface1.class, 159 TestImpl1_1.class.getName()); 160 161 assertTrue("1. " + ti.getClass().getName() + "!=" + TestImpl1_1.class.getName(), 162 ti.getClass().getName().equals(TestImpl1_1.class.getName())); 163 164 DiscoverSingleton.release(); 165 166 ti = (TestInterface1)DiscoverSingleton.find(TestInterface1.class, 167 TestImpl1_2.class.getName()); 168 169 assertTrue("2. " + ti.getClass().getName() + "!=" + TestImpl1_2.class.getName(), 171 ti.getClass().getName().equals(TestImpl1_2.class.getName())); 172 } finally { 173 DiscoverSingleton.release(); 174 } 175 } 176 177 public void testFindPropertyImpl_1() { 178 org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel); 179 180 TestInterface1 ti = null; 181 182 try { 183 Properties props = new Properties (); 184 185 props.setProperty(TestInterface1.class.getName(), 186 TestImpl1_2.class.getName()); 187 188 ti = (TestInterface1)DiscoverSingleton.find(TestInterface1.class, props); 189 190 assertTrue(ti.getClass().getName() + "!=" + TestImpl1_2.class.getName(), 191 ti.getClass().getName().equals(TestImpl1_2.class.getName())); 192 } finally { 193 DiscoverSingleton.release(); 194 } 195 } 196 197 public void testMyFactoryManagedProperty() { 198 org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel); 199 200 TestInterface1 ti = null; 201 202 try { 203 ManagedProperties.setProperty(TestInterface1.class.getName(), 204 TestImpl1_2.class.getName()); 205 206 ti = (TestInterface1)DiscoverSingleton.find(TestInterface1.class); 207 208 assertTrue(ti.getClass().getName() + "!=" + TestImpl1_2.class.getName(), 209 ti.getClass().getName().equals(TestImpl1_2.class.getName())); 210 } finally { 211 DiscoverSingleton.release(); 212 213 216 ManagedProperties.setProperty(TestInterface1.class.getName(), null); 217 } 218 } 219 220 221 public void testFindPropFileDefault() { 222 org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel); 223 224 TestInterface1 ti = null; 225 226 try { 227 ti = (TestInterface1)DiscoverSingleton.find(null, 228 new SPInterface(TestInterface1.class), 229 new PropertiesHolder("TestInterface.properties"), 230 new DefaultClassHolder(TestImpl1_2.class.getName())); 231 232 assertTrue(ti.getClass().getName() + "!=" + TestImpl1_1.class.getName(), 233 ti.getClass().getName().equals(TestImpl1_1.class.getName())); 234 } finally { 235 DiscoverSingleton.release(); 236 } 237 } 238 239 public void testFindServiceFileDefault() { 240 org.apache.commons.discovery.log.SimpleLog.setLevel(logLevel); 242 243 TestInterface2 ti = null; 244 245 try { 246 ti = (TestInterface2)DiscoverSingleton.find(null, 247 new SPInterface(TestInterface2.class), 248 null, 249 new DefaultClassHolder(TestImpl2_2.class.getName())); 250 251 assertTrue(ti.getClass().getName() + "!=" + TestImpl2_1.class.getName(), 252 ti.getClass().getName().equals(TestImpl2_1.class.getName())); 253 } finally { 254 DiscoverSingleton.release(); 255 } 256 } 257 258 261 public static void main(String args[]) { 262 String [] testCaseName = { TestAll.class.getName() }; 263 junit.textui.TestRunner.main(testCaseName); 264 } 265 } 266 | Popular Tags |