📄 testfilewatcherplugin.java
字号:
private void doPublish(I_Info prop, boolean deliverFirst, boolean deliverSecond, boolean absSubPath) { String lockExt = prop.get("filewatcher.lockExtention", null); prop.put("mom.topicName", this.oid); prop.put("filewatcher.directoryName", this.dirName); int maximumSize = 10000; long delaySinceLastChange = 1000L; long pollInterval = 600L; prop.put("filewatcher.maximumFileSize", "" + maximumSize); prop.put("filewatcher.delaySinceLastFileChange", "" + delaySinceLastChange); prop.put("filewatcher.pollInterval", "" + pollInterval); prop.put("filewatcher.warnOnEmptyFileDelay", "1000"); String sent = prop.get("filewatcher.sent", null); String discarded = prop.get("filewatcher.discarded", null); org.xmlBlaster.engine.ServerScope engineGlobal = new org.xmlBlaster.engine.ServerScope(); prop.put("mom.connectQos", this.getConnectQos(engineGlobal)); Publisher publisher = null; try { publisher = new Publisher(engineGlobal, "test", prop); publisher.init(); this.updateInterceptor.clear(); // too big String tooBig = this.dirName + File.separator + "tooBig.dat"; writeFile(tooBig, maximumSize+1, lockExt, delaySinceLastChange* 2); int ret = this.updateInterceptor.waitOnUpdate(delaySinceLastChange* 2); assertEquals("expected no updates", 0, ret); File tmp = new File(tooBig); if (deliverFirst) { assertFalse("the file '" + tooBig + "' should have been removed", tmp.exists()); checkMoved("tooBig.dat", absSubPath, discarded); } else { assertTrue("the file '" + tooBig + "' should still be here", tmp.exists()); } this.updateInterceptor.clear(); singleDump("ok.dat", maximumSize-1, lockExt, delaySinceLastChange* 2, deliverFirst, absSubPath, sent); singleDump("ok.gif", maximumSize-1, lockExt, delaySinceLastChange* 2, deliverSecond, absSubPath, sent); } catch (XmlBlasterException ex) { ex.printStackTrace(); assertTrue("An exception should not occur here " + ex.getMessage(), false); } finally { if (publisher != null) { try { publisher.shutdown(); } catch (Throwable ex) { ex.printStackTrace(); fail("exception when shutting down the poller " + ex.getMessage()); } } } } public void testSimplePublish() { boolean deliverDat = true; boolean deliverGif = true; boolean absSubPath = true; I_Info prop = new PropertiesInfo(new Properties()); doPublish(prop, deliverDat, deliverGif, absSubPath); } public void testSimplePublishWithFilter() { boolean deliverDat = false; boolean deliverGif = true; boolean absSubPath = true; I_Info prop = new PropertiesInfo(new Properties()); prop.put("filewatcher.fileFilter", "*.gif"); doPublish(prop, deliverDat, deliverGif, absSubPath); } public void testSimplePublishWithFilterRegex() { boolean deliverDat = true; boolean deliverGif = true; boolean absSubPath = true; I_Info prop = new PropertiesInfo(new Properties()); prop.put("filewatcher.filterType", "regex"); // note that the double backslash would be simple if read from the configuration file prop.put("filewatcher.fileFilter", "(.*\\.dat)|(.*\\.gif)"); doPublish(prop, deliverDat, deliverGif, absSubPath); } public void testPublishWithMoveAbsolute() { boolean deliverDat = true; boolean deliverGif = true; boolean absSubPath = true; I_Info prop = new PropertiesInfo(new Properties()); prop.put("filewatcher.sent", this.dirName + File.separator + "Sent"); prop.put("filewatcher.discarded", this.dirName + File.separator + "Discarded"); doPublish(prop, deliverDat, deliverGif, absSubPath); } public void testPublishWithMoveRelative() { boolean deliverDat = true; boolean deliverGif = true; boolean absSubPath = false; I_Info prop = new PropertiesInfo(new Properties()); prop.put("filewatcher.sent", "Sent"); prop.put("filewatcher.discarded", "Discarded"); doPublish(prop, deliverDat, deliverGif, absSubPath); } public void testPublishWithMoveRelativeLockMode() { boolean deliverDat = true; boolean deliverGif = true; boolean absSubPath = false; I_Info prop = new PropertiesInfo(new Properties()); prop.put("filewatcher.sent", "Sent"); prop.put("filewatcher.discarded", "Discarded"); prop.put("filewatcher.lockExtention", "*.lck"); doPublish(prop, deliverDat, deliverGif, absSubPath); } public void testSimplePublishWithFilterLockMode() { boolean deliverDat = false; boolean deliverGif = true; boolean absSubPath = true; I_Info prop = new PropertiesInfo(new Properties()); prop.put("filewatcher.fileFilter", "*.gif"); prop.put("filewatcher.lockExtention", "*.lck"); doPublish(prop, deliverDat, deliverGif, absSubPath); } /* prop.put("sent", "Sent"); prop.put("discarded", "Discarded"); prop.put("publishKey", ""); prop.put("publishQos", ""); prop.put("connectQos", ""); prop.put("loginName", ""); prop.put("password", ""); prop.put("fileFilter", ""); prop.put("lockExtention", "*.lck");*/ private boolean compareContent(byte[] buf1, byte[] buf2) { if (buf1 == null && buf2 == null) return true; if (buf1 == null || buf2 == null) return false; if (buf1.length != buf2.length) return false; for (int i=0; i < buf1.length; i++) { if (buf1[i] != buf2[i]) return false; } return true; } private byte[] writeFile(String filename, int size, String lockExt, long timeToWait) { try { File lock = null; if (lockExt != null) { String tmp = filename + lockExt.substring(1); lock = new File(tmp); boolean ret = lock.createNewFile(); assertTrue("could not create lock file '" + tmp + "'", ret); int upd = this.updateInterceptor.waitOnUpdate(timeToWait); assertEquals("when writing lock file should not update", 0, upd); } else this.updateInterceptor.waitOnUpdate(timeToWait); byte[] buf = new byte[size]; for (int i=0; i < size; i++) { buf[i] = (byte)i; } FileOutputStream fos = new FileOutputStream(filename); fos.write(buf); fos.close(); if (lockExt != null) { int upd = this.updateInterceptor.waitOnUpdate(timeToWait); assertEquals("when still locked by lockfile should not update", 0, upd); File tmp = new File(filename); assertTrue("file '" + filename + "' should still exist since lock file exists", tmp.exists()); } else this.updateInterceptor.waitOnUpdate(timeToWait); if (lock != null) { boolean ret = lock.delete(); assertTrue("could not remove lock file '" + filename + lockExt.substring(1) + "'", ret); } return buf; } catch (IOException ex) { ex.printStackTrace(); fail("could not write to file '" + filename + "'"); return null; // fake return to make compiler happy } } private void checkDirs() { File file = new File(this.dirName); assertTrue("file '" + this.dirName + "' does not exist", file.exists()); file = new File(this.dirNameSent); assertTrue("file '" + this.dirNameSent + "' does not exist", file.exists()); file = new File(this.dirNameDiscarded); assertTrue("file '" + this.dirNameDiscarded + "' does not exist", file.exists()); } private String getConnectQos(Global glob) { try { ConnectQos connQos = new ConnectQos(glob, "filePollerTestUser", "secret"); connQos.setMaxSessions(100); Address address = connQos.getAddress(); address.setPingInterval(0L); address.setCollectTime(0L); connQos.getClientQueueProperty().setType("RAM"); connQos.getClientQueueProperty().setVersion("1.0"); return connQos.toXml(); } catch (XmlBlasterException ex) { fail("an exception when building the connect qos: " + ex.getMessage()); return null; } } private void delete(String filename) { try { (new File(filename)).delete(); } catch (Throwable ex) { } } private void cleanUpDirs() { delete(this.dirNameSent + File.separator + "ok.dat"); delete(this.dirNameSent + File.separator + "ok.gif"); delete(this.dirNameSent); delete(this.dirNameDiscarded + File.separator + "tooBig.dat"); delete(this.dirNameDiscarded); delete(this.dirName + File.separator + "ok.dat"); delete(this.dirName + File.separator + "ok.dat.lck"); delete(this.dirName + File.separator + "ok.gif"); delete(this.dirName + File.separator + "ok.gif.lck"); delete(this.dirName + File.separator + "tooBig.dat"); delete(this.dirName + File.separator + "tooBig.dat.lck"); delete(this.dirName); } /** * Invoke: java org.xmlBlaster.test.client.TestFilePollerPlugin * <p /> * @deprecated Use the TestRunner from the testsuite to run it:<p /> * <pre> java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.client.TestFilePollerPlugin</pre> */ public static void main(String args[]) { Global global = new Global(); if (global.init(args) != 0) { System.out.println(ME + ": Init failed"); System.exit(1); } TestFileWatcherPlugin test = new TestFileWatcherPlugin(global); test.setUp(); test.testSimplePublish(); test.tearDown(); test.setUp(); test.testDirectories(); test.tearDown(); test.setUp(); test.testSimplePublishWithFilter(); test.tearDown(); test.setUp(); test.testSimplePublishWithFilterRegex(); test.tearDown(); test.setUp(); test.testPublishWithMoveAbsolute(); test.tearDown(); test.setUp(); test.testPublishWithMoveRelative(); test.tearDown(); test.setUp(); test.testPublishWithMoveRelativeLockMode(); test.tearDown(); test.setUp(); test.testSimplePublishWithFilterLockMode(); test.tearDown(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -