1 15 package org.apache.hivemind.util; 16 17 import hivemind.test.FrameworkTestCase; 18 19 import java.io.File ; 20 import java.net.URL ; 21 import java.util.Locale ; 22 23 import org.apache.hivemind.Resource; 24 25 31 public class TestFileResource extends FrameworkTestCase 32 { 33 34 private static final String DIR = "/test-data/TestFileResource"; 35 private static final String BASEFILE = DIR + "/localizable.properties"; 36 37 public void testFindLocalization() 38 throws Exception 39 { 40 String path = getFrameworkPath(BASEFILE); 41 42 44 File file = new File (path); 45 46 if (!file.exists()) 47 throw new IllegalArgumentException ("Configuration error: base path (" + path + ") evaluates to " 48 + file.getCanonicalPath() 49 + ", which does not exist. Set the correct working directory or FRAMEWORK_DIR system property."); 50 51 LocalizedFileResourceFinder f = new LocalizedFileResourceFinder(); 52 53 String enPath = getFrameworkPath(DIR + "/localizable_en.properties"); 54 55 assertEquals(enPath, f.findLocalizedPath(path, Locale.ENGLISH)); 56 57 String frPath = getFrameworkPath(DIR + "/localizable_fr.properties"); 58 59 assertEquals(frPath, f.findLocalizedPath(path, Locale.FRANCE)); 60 } 61 62 public void testNoLocalization() 63 { 64 String path = getFrameworkPath(BASEFILE); 65 LocalizedFileResourceFinder f = new LocalizedFileResourceFinder(); 66 67 assertEquals(path, f.findLocalizedPath(path, Locale.GERMAN)); 68 } 69 70 public void testExisting() 71 throws Exception 72 { 73 String path = getFrameworkPath(BASEFILE); 74 75 Resource r = new FileResource(path); 76 77 File file = new File (path); 78 URL expected = file.toURL(); 79 80 assertEquals(expected, r.getResourceURL()); 81 } 82 83 public void testMissing() 84 throws Exception 85 { 86 String path = "file-does-not-exist.xml"; 87 88 Resource r = new FileResource(path); 89 90 assertEquals(null, r.getResourceURL()); 91 } 92 93 public void testCreateWithLocale() 94 throws Exception 95 { 96 String path = getFrameworkPath(DIR + "/localizable_fr.properties"); 97 98 Resource r = new FileResource(path, Locale.CANADA_FRENCH); 99 100 assertEquals(path, r.getPath()); 101 assertEquals(Locale.CANADA_FRENCH, r.getLocale()); 102 } 103 104 public void testGetLocalization() 105 { 106 String path = getFrameworkPath(BASEFILE); 107 108 Resource base = new FileResource(path); 109 Resource localized = base.getLocalization(Locale.FRANCE); 110 111 String expected = getFrameworkPath(DIR + "/localizable_fr.properties"); 112 113 assertEquals(expected, localized.getPath()); 114 } 115 116 public void testLocalizationMissing() 117 { 118 String path = getFrameworkPath(BASEFILE); 119 120 Resource base = new FileResource(path); 121 Resource localized = base.getLocalization(Locale.CHINA); 122 123 assertSame(base, localized); 124 } 125 126 public void testToString() 127 { 128 String path = getFrameworkPath(BASEFILE); 129 130 Resource r = new FileResource(path); 131 132 assertEquals(path, r.toString()); 133 } 134 135 public void testExtensionLess() 136 { 137 String path = getFrameworkPath(DIR); 138 if (path.startsWith("./")) path = path.substring(2); 140 141 Resource r = new FileResource(path); 142 Resource localized = r.getLocalization(Locale.CHINA); 143 144 assertSame(r, localized); 145 } 146 } 147 | Popular Tags |