⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testfilewatcherplugin.java

📁 java开源的企业总线.xmlBlaster
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*-----t-------------------------------------------------------------------------Name:      TestPollerPlugin.javaProject:   xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE file------------------------------------------------------------------------------*/package org.xmlBlaster.test.contrib.filewatcher;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;import java.util.logging.Logger;import org.xmlBlaster.client.I_Callback;import org.xmlBlaster.client.key.SubscribeKey;import org.xmlBlaster.client.key.UnSubscribeKey;import org.xmlBlaster.client.key.UpdateKey;import org.xmlBlaster.client.qos.ConnectQos;import org.xmlBlaster.client.qos.DisconnectQos;import org.xmlBlaster.client.qos.SubscribeQos;import org.xmlBlaster.client.qos.UnSubscribeQos;import org.xmlBlaster.client.qos.UpdateQos;import org.xmlBlaster.contrib.I_Info;import org.xmlBlaster.contrib.PropertiesInfo;import org.xmlBlaster.contrib.filewatcher.Publisher;import org.xmlBlaster.test.MsgInterceptor;import org.xmlBlaster.util.Global;import org.xmlBlaster.util.XmlBlasterException;import org.xmlBlaster.util.qos.address.Address;import junit.framework.TestCase;/** * <p> * This is an interesting example, since it creates a XmlBlaster server instance * in the same JVM , but in a separate thread, talking over CORBA with it. * <p> * Invoke examples:<br /> * <pre> *   java junit.textui.TestRunner -noloading org.xmlBlaster.test.client.TestFilePollerPlugin *   java junit.swingui.TestRunner -noloading org.xmlBlaster.test.client.TestFilePollerPlugin * </pre> * @see org.xmlBlaster.client.I_XmlBlasterAccess */public class TestFileWatcherPlugin extends TestCase implements I_Callback {   private static String ME = "TestFilePollerPlugin";   private Global global;   private static Logger log = Logger.getLogger(TestFileWatcherPlugin.class.getName());   private Global connGlobal;   private String oid = "filepollerTest";   private String dirName;   private String dirNameSent;   private String dirNameDiscarded;   private MsgInterceptor updateInterceptor;   public TestFileWatcherPlugin() {      this(null);   }   private void getBaseDir() {      try {         File dummy = File.createTempFile("dummy", null);         String path = dummy.getCanonicalPath();         dummy.delete();         int pos = path.lastIndexOf(File.separator);         if (pos < 0)            fail("the temporary path is not absolute '" + path + "'");         this.dirName = path.substring(0, pos) + "/testsuitePoller";         log.info("WILL USE THE DIRECTORY '" + this.dirName + "' AS THE BASE DIRECTORY");         this.dirNameSent = this.dirName + "/Sent";         this.dirNameDiscarded = this.dirName + "/Discarded";      }      catch(Exception ex) {         ex.printStackTrace();         fail("exception occured when trying to find out temporary path");      }   }         public TestFileWatcherPlugin(Global global) {      super("TestFilePollerPlugin");      this.global = global;      if (this.global == null) {         this.global = new Global();         this.global.init((String[])null);      }      getBaseDir();   }   /**    * Sets up the fixture.    * <p />    * Connect to xmlBlaster and login    */   protected void setUp() {      /*      File file = new File(this.dirName);      if (file.exists())         FileLocator.deleteDir(file);      */      try {         this.connGlobal = this.global.getClone(null);         this.updateInterceptor = new MsgInterceptor(this.connGlobal, log, null);         this.connGlobal.getXmlBlasterAccess().connect(new ConnectQos(this.connGlobal), this.updateInterceptor);         SubscribeQos subQos = new SubscribeQos(this.connGlobal);         subQos.setWantInitialUpdate(false);         this.connGlobal.getXmlBlasterAccess().subscribe(new SubscribeKey(this.connGlobal, this.oid), subQos);      }      catch (XmlBlasterException ex) {         ex.printStackTrace();         fail("aborting since exception ex: " + ex.getMessage());      }   }         /**    * Tears down the fixture.    * <p />    * cleaning up .... erase() the previous message OID and logout    */   protected void tearDown() {      log.info("Entering tearDown(), test is finished");      cleanUpDirs();      try {         this.connGlobal.getXmlBlasterAccess().unSubscribe(new UnSubscribeKey(this.connGlobal, this.oid), new UnSubscribeQos(this.connGlobal));         this.connGlobal.getXmlBlasterAccess().disconnect(new DisconnectQos(this.connGlobal));         this.connGlobal.shutdown();         this.connGlobal = null;      }      catch (XmlBlasterException ex) {         ex.printStackTrace();         fail("aborting since exception ex: " + ex.getMessage());      }   }   public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) throws XmlBlasterException {      String contentStr = new String(content);      String cont = (contentStr.length() > 10) ? (contentStr.substring(0,10)+"...") : contentStr;      log.info("Receiving update of a message oid=" + updateKey.getOid() +                        " priority=" + updateQos.getPriority() +                        " state=" + updateQos.getState() +                        " content=" + cont);      log.info("further log for receiving update of a message cbSessionId=" + cbSessionId +                     updateKey.toXml() + "\n" + new String(content) + updateQos.toXml());      log.severe("update: should never be invoked (msgInterceptors take care of it since they are passed on subscriptions)");      return "OK";   }   /**    * Tests the creation of the necessary directories    *    */   public void testDirectories() {      // absolute path      I_Info prop = new PropertiesInfo(new Properties());      prop.put("mom.topicName", "dummy");      prop.put("filewatcher.directoryName", this.dirName);      prop.put("filewatcher.sent", this.dirNameSent);      prop.put("filewatcher.discarded", this.dirNameDiscarded);      try {         new Publisher(this.global, "test", prop);      }      catch (XmlBlasterException ex) {         ex.printStackTrace();         assertTrue("An exception should not occur here " + ex.getMessage(), false);      }      checkDirs();      // repeat that with already existing directories      prop = new PropertiesInfo(new Properties());      prop.put("mom.topicName", "dummy");      prop.put("filewatcher.directoryName", this.dirName);      prop.put("filewatcher.sent", this.dirNameSent);      prop.put("filewatcher.discarded", this.dirNameDiscarded);      try {         new Publisher(this.global, "test", prop);      }      catch (XmlBlasterException ex) {         ex.printStackTrace();         assertTrue("An exception should not occur here " + ex.getMessage(), false);      }      checkDirs();      cleanUpDirs();      // relative path are added to the 'directoryName'      prop = new PropertiesInfo(new Properties());      prop.put("mom.topicName", "dummy");      prop.put("filewatcher.directoryName", this.dirName);      prop.put("filewatcher.sent", "Sent");      prop.put("filewatcher.discarded", "Discarded");      try {         new Publisher(this.global, "test", prop);      }      catch (XmlBlasterException ex) {         ex.printStackTrace();         assertTrue("An exception should not occur here " + ex.getMessage(), false);      }      checkDirs();      // relative path are added to the 'directoryName' repeat with existing directories      prop = new PropertiesInfo(new Properties());      prop.put("mom.topicName", "dummy");      prop.put("filewatcher.directoryName", this.dirName);      prop.put("filewatcher.sent", "Sent");      prop.put("filewatcher.discarded", "Discarded");      try {         new Publisher(this.global, "test", prop);      }      catch (XmlBlasterException ex) {         ex.printStackTrace();         assertTrue("An exception should not occur here " + ex.getMessage(), false);      }      checkDirs();            cleanUpDirs();            // now some which should fail:      // existing file but not a directory      File file = new File(this.dirName);      try {         file.createNewFile();      }      catch (IOException ex) {         assertTrue("could not create the file '" + this.dirName + "'", false);      }            prop = new PropertiesInfo(new Properties());      prop.put("mom.topicName", "dummy");      prop.put("filewatcher.directoryName", this.dirName);      prop.put("filewatcher.sent", "Sent");      prop.put("filewatcher.discarded", "Discarded");      try {         new Publisher(this.global, "test", prop);         assertTrue("an exception should occur since '" + this.dirName + "' is a file and should be a directory", false);      }      catch (XmlBlasterException ex) {         log.info("Exception is OK here");      }      cleanUpDirs();            try {         file = new File(this.dirName);         boolean ret = file.mkdir();         assertTrue("could not create directory '" + this.dirName + "'", ret);         file = new File(this.dirNameSent);         file.createNewFile();      }      catch (IOException ex) {         assertTrue("could not create the file '" + this.dirNameSent + "'", false);      }            prop = new PropertiesInfo(new Properties());      prop.put("mom.topicName", "dummy");      prop.put("filewatcher.directoryName", this.dirName);      prop.put("filewatcher.sent", "Sent");      prop.put("filewatcher.discarded", "Discarded");      try {         new Publisher(this.global, "test", prop);         assertTrue("an exception should occur since '" + this.dirName + "' is a file and should be a directory", false);      }      catch (XmlBlasterException ex) {         log.info("Exception is OK here");      }      cleanUpDirs();   }   private void singleDump(String filename, int filesize, String lockExt, long delay, boolean deliver, boolean absSubPath, String movedDir) {      String okFile = this.dirName + File.separator + filename;      byte[] okBuf = writeFile(okFile, filesize, lockExt, delay);      int ret = this.updateInterceptor.waitOnUpdate(delay);      boolean exist = false;      int sent = 1;      String txt = "";      if (!deliver) {         exist = true;         sent = 0;         txt = "not ";      }      assertEquals("expected '" + sent + "' update", sent, ret);      File tmp = new File(okFile);      assertEquals("the file '" + okFile + "' should " + txt + "have been removed", exist, tmp.exists());      if (deliver) {         checkMoved(filename, absSubPath, movedDir);         boolean sameContent = compareContent(okBuf, this.updateInterceptor.getMsgs()[0].getContent());         assertTrue("the content of the file is not the same as the arrived content of the update method", sameContent);         String fileName = this.updateInterceptor.getMsgs()[0].getUpdateQos().getClientProperty("_filename", (String)null);         assertNotNull("The fileName is null", fileName);         assertEquals("", filename, fileName);      }      this.updateInterceptor.clear();   }      private void checkMoved(String name, boolean absSubPath, String subDirName) {      if (subDirName == null)         return;      File discDir = null;      if (absSubPath) {         discDir = new File(subDirName);      }      else {         discDir = new File(new File(this.dirName), subDirName);      }      File tmp = new File(discDir, name);      assertTrue("The directory '" + subDirName + "' must exist", discDir.exists());      assertTrue("The file '" + name + "' must exist in '" + subDirName + "' directory", tmp.exists());   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -