1 package test.crispy.properties;
2
3 import java.net.URL;
4 import java.util.Properties;
5
6 import junit.framework.TestCase;
7 import net.sf.crispy.PropertiesLoader;
8 import net.sf.crispy.impl.ServiceManager;
9 import net.sf.crispy.impl.xmlrpc.MiniXmlRpcServer;
10 import net.sf.crispy.properties.ClassPathPropertiesLoader;
11 import net.sf.crispy.properties.ClassPropertiesLoader;
12 import net.sf.crispy.properties.FilePropertiesLoader;
13 import net.sf.crispy.properties.UrlPropertiesLoader;
14 import test.crispy.Run;
15 import test.crispy.example.service.Calculator;
16 import test.crispy.example.service.CalculatorImpl;
17 import test.crispy.example.service.Echo;
18 import test.crispy.example.service.EchoImpl;
19
20 public class PropertiesLoaderTest extends TestCase {
21
22
23 private static MiniXmlRpcServer miniXmlRpcServer = null;
24
25 protected void setUp() throws Exception {
26 super.setUp();
27 if (miniXmlRpcServer == null) {
28 miniXmlRpcServer = new MiniXmlRpcServer(9093);
29
30 miniXmlRpcServer.addService(Echo.class.getName(), EchoImpl.class.getName());
31 miniXmlRpcServer.addService(Calculator.class.getName(), CalculatorImpl.class.getName());
32
33 miniXmlRpcServer.start();
34 }
35 }
36
37 public void testClassPathPropertiesLoader() throws Exception {
38 PropertiesLoader lvLoader = new ClassPathPropertiesLoader("test-xml-rpc.properties");
39 Properties lvProperties = lvLoader.load();
40 assertNotNull(lvProperties);
41 assertTrue(lvProperties.size() == 4);
42
43 ServiceManager lvManager = new ServiceManager(lvLoader);
44 Echo lvEcho = (Echo) lvManager.createService(Echo.class);
45 String lvHello = lvEcho.echo("Hello");
46 assertEquals(lvHello, "Hello");
47 }
48
49 public void testClassPathPropertiesLoaderFail() throws Exception {
50 PropertiesLoader lvLoader = new ClassPathPropertiesLoader(null);
51 try {
52 lvLoader.load();
53 fail();
54 } catch (Exception e) {
55 assertTrue(true);
56 }
57 lvLoader = new ClassPathPropertiesLoader("no-valid-file.xml");
58 try {
59 lvLoader.load();
60 fail();
61 } catch (Exception e) {
62 assertTrue(true);
63 }
64
65 }
66
67 public void testClassPropertiesLoader() throws Exception {
68 PropertiesLoader lvLoader = new ClassPropertiesLoader(this.getClass(), "test-xml-rpc2.properties");
69 Properties lvProperties = lvLoader.load();
70 assertNotNull(lvProperties);
71 assertTrue(lvProperties.size() == 4);
72
73
74
75 ServiceManager lvManager = new ServiceManager(lvLoader);
76 Echo lvEcho = (Echo) lvManager.createService(Echo.class);
77 String lvHello = lvEcho.echo("Hello");
78
79 assertEquals(lvHello, "Hello");
80 }
81
82 public void testClassPropertiesLoaderFail() throws Exception {
83 PropertiesLoader lvLoader = new ClassPropertiesLoader(null, null);
84 try {
85 lvLoader.load();
86 fail();
87 } catch (Exception e) {
88 assertTrue(true);
89 }
90 lvLoader = new ClassPropertiesLoader(this.getClass(), null);
91 try {
92 lvLoader.load();
93 fail();
94 } catch (Exception e) {
95 assertTrue(true);
96 }
97 lvLoader = new ClassPropertiesLoader (null, "no-valid-file.xml");
98 try {
99 lvLoader.load();
100 fail();
101 } catch (Exception e) {
102 assertTrue(true);
103 }
104 lvLoader = new ClassPropertiesLoader (this.getClass(), "no-valid-file.xml");
105 try {
106 lvLoader.load();
107 fail();
108 } catch (Exception e) {
109 assertTrue(true);
110 }
111 }
112
113 public void testFilePropertiesLoader() throws Exception {
114 URL lvUrl = Run.class.getResource("/test-xml-rpc.properties");
115 PropertiesLoader lvLoader = new FilePropertiesLoader(lvUrl.getPath());
116 Properties lvProperties = lvLoader.load();
117 assertNotNull(lvProperties);
118 assertTrue(lvProperties.size() == 4);
119
120
121
122 ServiceManager lvManager = new ServiceManager(lvLoader);
123 Echo lvEcho = (Echo) lvManager.createService(Echo.class);
124 String lvHello = lvEcho.echo("Hello");
125
126 assertEquals(lvHello, "Hello");
127 }
128
129 public void testFilePropertiesLoaderFail() throws Exception {
130 PropertiesLoader lvLoader = new FilePropertiesLoader(null);
131 try {
132 lvLoader.load();
133 fail();
134 } catch (Exception e) {
135 assertTrue(true);
136 }
137 }
138
139 public void testUrlPropertiesLoader() throws Exception {
140 URL lvUrl = Run.class.getResource("/test-xml-rpc.properties");
141 PropertiesLoader lvLoader = new UrlPropertiesLoader(lvUrl);
142 Properties lvProperties = lvLoader.load();
143 assertNotNull(lvProperties);
144 assertTrue(lvProperties.size() == 4);
145
146 lvLoader = new UrlPropertiesLoader("file:" + lvUrl.getFile());
147 lvProperties = lvLoader.load();
148 assertNotNull(lvProperties);
149 assertTrue(lvProperties.size() == 4);
150
151
152
153 ServiceManager lvManager = new ServiceManager(lvLoader);
154 Echo lvEcho = (Echo) lvManager.createService(Echo.class);
155 String lvHello = lvEcho.echo("Hello");
156
157 assertEquals(lvHello, "Hello");
158 }
159
160 public void testUrlPropertiesLoaderFail() throws Exception {
161 PropertiesLoader lvLoader = new UrlPropertiesLoader((URL) null);
162 try {
163 lvLoader.load();
164 fail();
165 } catch (Exception e) {
166 assertTrue(true);
167 }
168 }
169
170 public void testUrlPropertiesLoaderFailInvalidUrl() throws Exception {
171 try {
172 PropertiesLoader lvLoader = new UrlPropertiesLoader("xyz://bla");
173 lvLoader.load();
174 fail();
175 } catch (Exception e) {
176 assertTrue(true);
177 }
178 }
179
180 }