001: package org.drools.agent;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.IOException;
006: import java.net.URL;
007: import java.net.URLEncoder;
008: import java.util.ArrayList;
009: import java.util.List;
010: import java.util.Properties;
011:
012: import junit.framework.TestCase;
013:
014: import org.drools.RuleBase;
015: import org.drools.RuleBaseFactory;
016: import org.drools.common.DroolsObjectInputStream;
017: import org.drools.rule.Package;
018:
019: public class URLScannerTest extends TestCase {
020:
021: public void testFileURLCache() throws Exception {
022: File dir = RuleBaseAssemblerTest.getTempDirectory();
023:
024: String url = "http://localhost:8080/foo/bar.bar/packages/IMINYRURL/LATEST";
025: String fileName = URLEncoder.encode(url, "UTF-8");
026:
027: File f = new File(dir, fileName);
028:
029: Package p = new Package("x");
030:
031: RuleBaseAssemblerTest.writePackage(p, f);
032:
033: DroolsObjectInputStream in = new DroolsObjectInputStream(
034: new FileInputStream(f));
035: Package p_ = (Package) in.readObject();
036: assertEquals("x", p_.getName());
037:
038: }
039:
040: // public void testGetURL() throws Exception {
041: // String url = "http://localhost:8080/foo/bar.bar/packages/IMINYRURL/LATEST";
042: // URL u = new URL(url);
043: // assertEquals(url, URLScanner.getURL( u ));
044: // //URLConnection con = u.openConnection();
045: // //con.connect();
046: // }
047:
048: public void testGetFiles() throws Exception {
049:
050: URL u1 = new URL(
051: "http://localhost:8080/foo/bar.bar/packages/IMINYRURL/LATEST");
052: URL u2 = new URL(
053: "http://localhost:8080/foo/bar.bar/packages/IMINYRURL/PROD");
054: URLScanner scan = new URLScanner();
055:
056: File dir = RuleBaseAssemblerTest.getTempDirectory();
057: File[] result = scan.getFiles(new URL[] { u1, u2 }, dir);
058:
059: assertEquals(2, result.length);
060: assertEquals(dir.getPath(), result[0].getParent());
061:
062: File f1 = result[0];
063: File f2 = result[1];
064: assertEquals(
065: "http%3A%2F%2Flocalhost%3A8080%2Ffoo%2Fbar.bar%2Fpackages%2FIMINYRURL%2FLATEST",
066: f1.getName());
067: assertEquals(
068: "http%3A%2F%2Flocalhost%3A8080%2Ffoo%2Fbar.bar%2Fpackages%2FIMINYRURL%2FPROD",
069: f2.getName());
070:
071: }
072:
073: public void testConfig() throws Exception {
074: URLScanner scan = new URLScanner();
075:
076: File dir = RuleBaseAssemblerTest.getTempDirectory();
077:
078: Properties config = new Properties();
079: config.setProperty(RuleAgent.LOCAL_URL_CACHE, dir.getPath());
080: config.setProperty(RuleAgent.URLS,
081: "http://goo.ber http://wee.waa");
082:
083: scan.configure(config);
084:
085: assertNotNull(scan.lastUpdated);
086: assertEquals(2, scan.urls.length);
087: assertEquals("http://goo.ber", scan.urls[0].toExternalForm());
088: assertEquals("http://wee.waa", scan.urls[1].toExternalForm());
089: assertNotNull(scan.localCacheFileScanner);
090:
091: assertEquals(2, scan.localCacheFileScanner.files.length);
092:
093: assertEquals("http%3A%2F%2Fgoo.ber",
094: scan.localCacheFileScanner.files[0].getName());
095: assertEquals("http%3A%2F%2Fwee.waa",
096: scan.localCacheFileScanner.files[1].getName());
097:
098: }
099:
100: public void testLastUpdatedError() {
101: LastUpdatedPing ping = new LastUpdatedPing();
102: assertTrue(ping.isError());
103: ping.responseMessage = "ABC";
104: ping.lastUpdated = 42;
105: assertTrue(ping.isError());
106: ping.responseMessage = "200 OK";
107: assertFalse(ping.isError());
108: }
109:
110: public void testUpdateNoLocalCache() throws Exception {
111: URLScanner scan = new URLScanner();
112: File dir = RuleBaseAssemblerTest.getTempDirectory();
113:
114: int numfiles = dir.list().length;
115:
116: Properties config = new Properties();
117: //config.setProperty( RuleAgent.LOCAL_URL_CACHE, dir.getPath() );
118: config.setProperty(RuleAgent.URLS,
119: "http://goo2.ber http://wee2.waa");
120:
121: scan.configure(config);
122:
123: scan.httpClient = new IHttpClient() {
124:
125: public LastUpdatedPing checkLastUpdated(URL url)
126: throws IOException {
127: LastUpdatedPing ping = new LastUpdatedPing();
128: ping.lastUpdated = 123;
129: ping.responseMessage = "200 OK";
130: return ping;
131: }
132:
133: public Package fetchPackage(URL url) throws IOException {
134: if (url.toExternalForm().equals("http://goo2.ber")) {
135: return new Package("goo2.ber");
136: } else {
137: return new Package("wee2.waa");
138: }
139: }
140:
141: };
142:
143: assertNull(scan.localCacheFileScanner);
144: assertNull(scan.localCacheDir);
145:
146: RuleBase rb = RuleBaseFactory.newRuleBase();
147: AgentEventListener list = getNilListener();
148: PackageProvider.applyChanges(rb, false, scan
149: .loadPackageChanges(), list);
150:
151: assertEquals(2, rb.getPackages().length);
152:
153: assertExists(new String[] { "goo2.ber", "wee2.waa" }, rb
154: .getPackages());
155:
156: assertEquals(numfiles, dir.list().length);
157: }
158:
159: private AgentEventListener getNilListener() {
160: return new MockListener();
161: }
162:
163: private void assertExists(String[] names, Package[] packages) {
164: for (int i = 0; i < packages.length; i++) {
165: String name = packages[i].getName();
166: int matches = 0;
167: for (int j = 0; j < names.length; j++) {
168: if (name.equals(names[j])) {
169: matches++;
170: }
171: }
172: assertEquals("Should only have one package named " + name,
173: 1, matches);
174: }
175:
176: }
177:
178: public void testUpdateWithLocalCache() {
179: URLScanner scan = new URLScanner();
180: scan.listener = new MockListener();
181: File dir = RuleBaseAssemblerTest.getTempDirectory();
182:
183: Properties config = new Properties();
184: config.setProperty(RuleAgent.LOCAL_URL_CACHE, dir.getPath());
185: config.setProperty(RuleAgent.URLS,
186: "http://goo.ber http://wee.waa");
187:
188: scan.configure(config);
189:
190: scan.httpClient = new IHttpClient() {
191:
192: public LastUpdatedPing checkLastUpdated(URL url)
193: throws IOException {
194: LastUpdatedPing ping = new LastUpdatedPing();
195: ping.lastUpdated = 123;
196: ping.responseMessage = "200 OK";
197: return ping;
198: }
199:
200: public Package fetchPackage(URL url) throws IOException {
201: if (url.toExternalForm().equals("http://goo.ber")) {
202: return new Package("goo.ber");
203: } else {
204: return new Package("wee.waa");
205: }
206: }
207:
208: };
209:
210: assertNotNull(scan.localCacheFileScanner);
211: assertNotNull(scan.localCacheDir);
212:
213: RuleBase rb = RuleBaseFactory.newRuleBase();
214: PackageProvider.applyChanges(rb, false, scan
215: .loadPackageChanges(), getNilListener());
216:
217: assertEquals(2, rb.getPackages().length);
218: assertTrue("goo.ber".equals(rb.getPackages()[0].getName())
219: || "goo.ber".equals(rb.getPackages()[1].getName()));
220: assertTrue("wee.waa".equals(rb.getPackages()[0].getName())
221: || "wee.waa".equals(rb.getPackages()[1].getName()));
222:
223: //assertEquals( 2, dir.list().length );
224:
225: //now check with failures:
226: scan.httpClient = new IHttpClient() {
227:
228: public LastUpdatedPing checkLastUpdated(URL url)
229: throws IOException {
230: LastUpdatedPing ping = new LastUpdatedPing();
231:
232: if (url.toExternalForm().equals("http://wee.waa")) {
233: ping.lastUpdated = -1;
234: ping.responseMessage = "XXX";
235:
236: } else {
237: ping.lastUpdated = 123;
238: ping.responseMessage = "200 OK";
239: }
240: return ping;
241: }
242:
243: public Package fetchPackage(URL url) throws IOException {
244: throw new IOException("poo");
245: }
246:
247: };
248:
249: rb = RuleBaseFactory.newRuleBase();
250: assertEquals(0, rb.getPackages().length);
251: PackageProvider.applyChanges(rb, true, scan
252: .loadPackageChanges(), getNilListener());
253:
254: assertEquals(2, rb.getPackages().length);
255:
256: final boolean[] fetchCalled = new boolean[1];
257:
258: fetchCalled[0] = false;
259:
260: //now check with IOExceptions
261: scan.httpClient = new IHttpClient() {
262:
263: public LastUpdatedPing checkLastUpdated(URL url)
264: throws IOException {
265: LastUpdatedPing ping = new LastUpdatedPing();
266: ping.lastUpdated = 1234;
267: ping.responseMessage = "200 OK";
268: return ping;
269: }
270:
271: public Package fetchPackage(URL url) throws IOException {
272: fetchCalled[0] = true;
273: throw new IOException("poo");
274: }
275:
276: };
277:
278: Package[] changes = scan.loadPackageChanges();
279: assertEquals(0, changes.length);
280: assertEquals(true, fetchCalled[0]);
281:
282: }
283:
284: public void testColdStartWithError() throws Exception {
285: //this will show starting up and reading packages from the dir when the remote one doesn't respond
286: URLScanner scan = new URLScanner();
287: scan.listener = new MockListener();
288: File dir = RuleBaseAssemblerTest.getTempDirectory();
289:
290: Package p1 = new Package("goo.ber");
291: Package p2 = new Package("wee.waa");
292:
293: File f1 = URLScanner.getLocalCacheFileForURL(dir, new URL(
294: "http://goo.ber"));
295: File f2 = URLScanner.getLocalCacheFileForURL(dir, new URL(
296: "http://wee.waa"));
297:
298: RuleBaseAssemblerTest.writePackage(p1, f1);
299: RuleBaseAssemblerTest.writePackage(p2, f2);
300:
301: Properties config = new Properties();
302: config.setProperty(RuleAgent.LOCAL_URL_CACHE, dir.getPath());
303: config.setProperty(RuleAgent.URLS,
304: "http://goo.ber http://wee.waa");
305:
306: scan.configure(config);
307:
308: scan.httpClient = new IHttpClient() {
309:
310: public LastUpdatedPing checkLastUpdated(URL url)
311: throws IOException {
312: throw new IOException();
313: }
314:
315: public Package fetchPackage(URL url) throws IOException {
316: throw new IOException();
317: }
318:
319: };
320:
321: assertNotNull(scan.localCacheFileScanner);
322: assertNotNull(scan.localCacheDir);
323:
324: RuleBase rb = RuleBaseFactory.newRuleBase();
325: PackageProvider.applyChanges(rb, true, scan
326: .loadPackageChanges(), getNilListener());
327: assertEquals(2, rb.getPackages().length);
328:
329: }
330:
331: }
|