1 /**
  2  * 
  3  */
  4 package org.xmlBlaster.test.classtest;
  5 
  6 import java.io.File;
  7 import java.net.URL;
  8 import java.util.logging.Logger;
  9 
 10 import org.xmlBlaster.util.XmlBlasterException;
 11 import org.xmlBlaster.util.FileLocator;
 12 import org.xmlBlaster.util.Global;
 13 
 14 import junit.framework.TestCase;
 15 
 16 /**
 17  * @author Marcel Ruff
 18  *
 19  */
 20 public class FileLocatorTest extends TestCase {
 21 
 22    private Logger log = Logger.getLogger(FileLocatorTest.class.getName());
 23    
 24    public static void main(String[] args) {
 25       // junit.swingui.TestRunner.run(FileLocatorTest.class);
 26       
 27       FileLocatorTest test = new FileLocatorTest("file locator test");
 28       
 29       try {
 30          test.setUp();
 31          test.testFileInExternalJarFile();
 32          test.tearDown();
 33          
 34          test.setUp();
 35          test.testFileAccess();
 36          test.tearDown();
 37          
 38          test.setUp();
 39          test.testHttpAccess();
 40          test.tearDown();
 41       }
 42       catch (Exception ex) {
 43          ex.printStackTrace();
 44          fail();
 45       }
 46       
 47    }
 48 
 49    /**
 50     * Constructor for FileLocatorTest.
 51     * @param arg0
 52     */
 53    public FileLocatorTest(String arg0) {
 54       super(arg0);
 55    }
 56 
 57    /*
 58     * @see TestCase#setUp()
 59     */
 60    protected void setUp() throws Exception {
 61       super.setUp();
 62    }
 63 
 64    /*
 65     * @see TestCase#tearDown()
 66     */
 67    protected void tearDown() throws Exception {
 68       super.tearDown();
 69    }
 70 
 71    /**
 72     * This test must currently be done manually, the file to be found must reside in a jar file
 73     * outside the jar files normally used by xmlBlaster.
 74     * 
 75     * @throws XmlBlasterException
 76     */
 77    public void testFileInExternalJarFile() throws XmlBlasterException {
 78       String filename = "somefile.xsl";
 79       // String txt = FileLocator.readAsciiFile(filename);
 80       // log.info(txt);
 81       
 82    }
 83 
 84    
 85    public void testHttpAccess() throws XmlBlasterException {
 86       Global glob = Global.instance();
 87       FileLocator locator = new FileLocator(glob);
 88       URL url = locator.findFileInXmlBlasterSearchPath(null, "http://www.xmlblaster.org/empty.html");
 89       assertNotNull(url);
 90       String content = locator.read(url);
 91       assertTrue("Downloaded file is empty", content.length() > 5);
 92    }
 93 
 94    public void testFileAccess() throws XmlBlasterException {
 95       FileLocator.writeFile("FileLocatorTest.dummy", "Hello");
 96       Global glob = Global.instance();
 97       FileLocator locator = new FileLocator(glob);
 98       URL url = locator.findFileInXmlBlasterSearchPath(null, "file:FileLocatorTest.dummy");
 99       assertNotNull(url);
100       String content = locator.read(url);
101       assertNotNull(content);
102       assertEquals("file://FileLocatorTest.dummy", "Hello", content.trim());
103       File f = new File("FileLocatorTest.dummy");
104       f.delete();
105    }
106 }


syntax highlighted by Code2HTML, v. 0.9.1