1 package org.sapia.regis.util; 2 3 import java.io.FileNotFoundException ; 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.util.Properties ; 7 8 import junit.framework.TestCase; 9 10 public class ResourceFinderTest extends TestCase { 11 12 public ResourceFinderTest(String arg0) { 13 super(arg0); 14 } 15 16 public void testFindCatchAllResource() throws IOException { 17 InputStream is = ResourceFinder.findResource("(dummy.properties), etc/bootstrap2.properties"); 18 Properties props = new Properties (); 19 props.load(is); 20 is.close(); 21 assertEquals("file:etc/bootstrap2.xml", props.getProperty("org.sapia.regis.local.bootstrap")); 22 } 23 24 public void testFindResource() throws IOException { 25 InputStream is = ResourceFinder.findResource("(etc/bootstrap1.properties), etc/bootstrap2.properties"); 26 Properties props = new Properties (); 27 props.load(is); 28 is.close(); 29 assertEquals("file:etc/bootstrap1.xml", props.getProperty("org.sapia.regis.local.bootstrap")); 30 } 31 32 public void testFindMisconfiguredResource() throws IOException { 33 InputStream is = ResourceFinder.findResource("(etc/bootstrap1.properties, etc/bootstrap2.properties"); 34 Properties props = new Properties (); 35 props.load(is); 36 is.close(); 37 assertEquals("file:etc/bootstrap1.xml", props.getProperty("org.sapia.regis.local.bootstrap")); 38 } 39 40 public void testGetResource() throws IOException { 41 InputStream is = ResourceFinder.getResource("(etc/foo.properties), etc/bar.properties"); 42 assertTrue(is == null); 43 } 44 45 public void testFindProperties() throws IOException { 46 Properties props = new Properties (); 47 ResourceFinder.findProperties("(etc/foo.properties), etc/bootstrap2.properties", props); 48 assertEquals("file:etc/bootstrap2.xml", props.getProperty("org.sapia.regis.local.bootstrap")); 49 50 props.clear(); 51 ResourceFinder.findProperties("(etc/foo.properties, etc/bootstrap2.properties), etc/bootstrap1.properties", props); 52 assertEquals("file:etc/bootstrap1.xml", props.getProperty("org.sapia.regis.local.bootstrap")); 53 54 props.clear(); 55 try{ 56 ResourceFinder.findProperties("(etc/foo.properties, etc/bar.properties), etc/snafu.properties", props); 57 fail("Should have thrown FileNotFoundException"); 58 }catch(FileNotFoundException e){} 59 } 60 61 public void testLoadProperties() throws IOException { 62 Properties props = new Properties (); 63 ResourceFinder.loadProperties("(etc/foo.properties, etc/bar.properties), etc/snafu.properties", props); 64 } 65 66 public void testLoadReplaceProperties() throws IOException { 67 Properties props = new Properties (); 68 System.setProperty("rootDir", "."); 69 ResourceFinder.loadProperties("(etc/first.properties, etc/bar.properties), etc/second.properties", props); 70 71 assertEquals("./etc", props.getProperty("baseDir")); 72 assertEquals("./etc/bootstrap1.properties", props.getProperty("path")); 73 assertEquals("./etc/bootstrap1.properties.bak", props.getProperty("backup")); 74 } 75 76 } 77 | Popular Tags |