📄 testfilewriter.java
字号:
/*-----t-------------------------------------------------------------------------Name: TestFileWriter.javaProject: xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE file------------------------------------------------------------------------------*/package org.xmlBlaster.test.contrib.filewatcher;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties;import java.util.logging.Logger;import java.util.zip.Adler32;import java.util.zip.CheckedInputStream;import org.xmlBlaster.client.qos.ConnectQos;import org.xmlBlaster.contrib.I_Info;import org.xmlBlaster.contrib.PropertiesInfo;import org.xmlBlaster.contrib.filewatcher.Publisher;import org.xmlBlaster.contrib.filewriter.FileWriter;import org.xmlBlaster.util.Global;import org.xmlBlaster.util.XmlBlasterException;import org.xmlBlaster.util.plugin.I_PluginConfig;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.TestFileWriter * java junit.swingui.TestRunner -noloading org.xmlBlaster.test.client.TestFileWriter * </pre> * @see org.xmlBlaster.client.I_XmlBlasterAccess */public class TestFileWriter extends TestCase { private static String ME = "TestFileWriter"; private Global global; private static Logger log = Logger.getLogger(TestFileWriter.class.getName()); private String oid = "filepollerTest"; private String baseDirName; // for both poller and writer private String pollerDirName; private String pollerDirNameSent; private String pollerDirNameDiscarded; private String writerDirName; private String writerTmpDirName; private FileWriter receiver; private class PluginProperties extends Properties implements I_PluginConfig { private final static long serialVersionUID = 1L; public PluginProperties() { super(); } /** * @see org.xmlBlaster.util.plugin.I_PluginConfig#getParameters() */ public Properties getParameters() { return this; } /** * @see org.xmlBlaster.util.plugin.I_PluginConfig#getPrefix() */ public String getPrefix() { return ""; } public String getType() { return ""; } public String getVersion() { return ""; } } public TestFileWriter() { 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.baseDirName = path.substring(0, pos) + File.separator + "testFileWriter"; this.pollerDirName = baseDirName + File.separator + "poller"; this.writerDirName = baseDirName + File.separator + "writer"; this.writerTmpDirName = writerDirName + File.separator + "tmp"; log.info("WILL USE THE DIRECTORY '" + this.pollerDirName + "' AS THE BASE DIRECTORY"); this.pollerDirNameSent = this.pollerDirName + "/Sent"; this.pollerDirNameDiscarded = this.pollerDirName + "/Discarded"; } catch(Exception ex) { ex.printStackTrace(); fail("exception occured when trying to find out temporary path"); } } public TestFileWriter(Global global) { super("TestFileWriter"); 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() { try { PluginProperties prop = new PluginProperties(); prop.put("mom.topicName", "dummy"); // prop.put("mom.subscribeKey", ""); prop.put("mom.topicName", oid); // must be the same as on poller prop.put("mom.subscribeQos", "<qos/>"); // prop.put("connectQos", "<qos/>"); prop.put("mom.loginName", "testFileWriter"); prop.put("mom.password", "secret"); prop.put("filewriter.directoryName", writerDirName); prop.put("filewriter.tmpDirectoryName", writerTmpDirName); prop.put("filewriter.overwrite", "true"); prop.put("filewriter.lockExtention", ".lck"); prop.put("__useNativeCfg", "false"); // we don't want to set native stuff for testing receiver = new FileWriter(this.global, "fileWriter", prop); } catch (Throwable 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 { receiver.shutdown(); } catch (XmlBlasterException ex) { ex.printStackTrace(); fail("aborting since exception ex: " + ex.getMessage()); } } /** * 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.pollerDirName); prop.put("filewatcher.sent", this.pollerDirNameSent); prop.put("filewatcher.discarded", this.pollerDirNameDiscarded); 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.pollerDirName); prop.put("filewatcher.sent", this.pollerDirNameSent); prop.put("filewatcher.discarded", this.pollerDirNameDiscarded); 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.pollerDirName); 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.pollerDirName); prop.put("filewatcher.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.pollerDirName); try { file.createNewFile(); } catch (IOException ex) { assertTrue("could not create the file '" + this.pollerDirName + "'", false); } prop = new PropertiesInfo(new Properties()); prop.put("mom.topicName", "dummy"); prop.put("filewatcher.directoryName", this.pollerDirName); prop.put("filewatcher.sent", "Sent"); prop.put("filewatcher.discarded", "Discarded"); try { new Publisher(this.global, "test", prop); assertTrue("an exception should occur since '" + this.pollerDirName + "' is a file and should be a directory", false); } catch (XmlBlasterException ex) { log.info("Exception is OK here"); } cleanUpDirs(); try { file = new File(this.pollerDirName); boolean ret = file.mkdir(); assertTrue("could not create directory '" + this.pollerDirName + "'", ret); file = new File(this.pollerDirNameSent); file.createNewFile(); } catch (IOException ex) { assertTrue("could not create the file '" + this.pollerDirNameSent + "'", false); } prop = new PropertiesInfo(new Properties()); prop.put("mom.topicName", "dummy"); prop.put("filewatcher.directoryName", this.pollerDirName); prop.put("filewatcher.sent", "Sent"); prop.put("filewatcher.discarded", "Discarded"); try { new Publisher(this.global, "test", prop); assertTrue("an exception should occur since '" + this.pollerDirName + "' 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.pollerDirName + File.separator + filename; byte[] okBuf = writeFile(okFile, filesize, lockExt); // TODO WAIT FOR UPDATE HERE !!!! boolean exist = false; String txt = ""; if (!deliver) { exist = true; txt = "not "; } doWait(delay); File tmp = new File(okFile); assertEquals("the file '" + okFile + "' should " + txt + "have been removed", exist, tmp.exists()); if (deliver) { checkMoved(filename, absSubPath, movedDir); String receivedFile = this.writerDirName + File.separator + filename; boolean sameContent = compareContent(okBuf, receivedFile); assertTrue("the content of the file is not the same as the arrived content of the update method", sameContent);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -