/** Test method i18nManager.searchForAvailableLanguages() */
 @Test
 public void testSearchForAvailableLanguages() {
   if (I18nModule.isTransToolEnabled()) {
     // Try to load i18n files and a jar from the testdata dir
     File testDataDir =
         new File(
             I18nModule.getTransToolApplicationLanguagesSrcDir(),
             "/../../test/java/org/olat/core/util/i18n/junittestdata/");
     assertTrue(testDataDir.exists());
     Set<String> foundLanguages = i18nMgr.searchForAvailableLanguages(testDataDir);
     // Set must contain some LocalStrings file:
     assertTrue(foundLanguages.contains("de"));
     assertTrue(foundLanguages.contains("en"));
     assertTrue(foundLanguages.contains("pt_BR"));
     // Set must contain some LocaleStrings from the jar package
     assertTrue(foundLanguages.contains("fr"));
     assertTrue(foundLanguages.contains("zh_CN"));
     // Final check
     assertEquals(6, foundLanguages.size());
   } else {
     Set<String> foundLanguages = I18nModule.getAvailableLanguageKeys();
     // Set must contain some LocaleStrings from the jar package
     assertTrue(foundLanguages.contains("fr"));
     assertTrue(foundLanguages.contains("zh_CN"));
   }
 }
 /** Test methods i18nManager.getLocaleOrFallback() */
 @Test
 public void testGetLocaleOrFallback() {
   // Normal case
   Locale locale = i18nMgr.getLocaleOrDefault("de");
   assertNotNull(locale);
   assertEquals(i18nMgr.getLocaleOrNull("de"), locale);
   // Unexisting locale case
   locale = i18nMgr.getLocaleOrDefault("xy");
   assertEquals(I18nModule.getDefaultLocale(), locale);
   // Test trying to get overlay via getLocale method which should not return the overlay
   Locale overlay = i18nMgr.getLocaleOrDefault("de__" + I18nModule.getOverlayName());
   assertEquals(I18nModule.getDefaultLocale(), overlay);
   overlay = i18nMgr.getLocaleOrDefault("zh_CN__" + I18nModule.getOverlayName());
   assertEquals(I18nModule.getDefaultLocale(), overlay);
 }
  @Test
  public void testMoveKeyTask() {
    if (I18nModule.isTransToolEnabled()) {
      prepareDevToolTests();
      Locale testLocale = i18nMgr.getLocaleOrDefault("de");
      String ktm = "key.to.move";
      i18nMgr.setCachingEnabled(true);
      Properties sourcePropResolved = i18nMgr.getResolvedProperties(testLocale, testSourceBundle);
      Properties sourceProp =
          i18nMgr.getPropertiesWithoutResolvingRecursively(testLocale, testSourceBundle);
      String matchString = sourceProp.getProperty(ktm);
      assertTrue(sourcePropResolved.getProperty("key.to.stay2").indexOf(matchString) != -1);

      tDMgr.moveKeyToOtherBundle(testSourceBundle, testTargetBundle, ktm);
      sourceProp = i18nMgr.getPropertiesWithoutResolvingRecursively(testLocale, testSourceBundle);
      Properties targetProp =
          i18nMgr.getPropertiesWithoutResolvingRecursively(testLocale, testTargetBundle);
      assertNull(sourceProp.getProperty(ktm));
      assertNotNull(targetProp.getProperty(ktm));
      Properties sourceMetaProp =
          i18nMgr.getPropertiesWithoutResolvingRecursively(null, testSourceBundle);
      Properties targetMetaProp =
          i18nMgr.getPropertiesWithoutResolvingRecursively(null, testTargetBundle);
      assertNull(sourceMetaProp.getProperty(ktm + I18nManager.METADATA_ANNOTATION_POSTFIX));
      assertNull(sourceMetaProp.getProperty(ktm + I18nManager.METADATA_KEY_PRIORITY_POSTFIX));
      assertTrue(targetMetaProp.containsKey(ktm + I18nManager.METADATA_ANNOTATION_POSTFIX));
      assertEquals(
          "100", targetMetaProp.getProperty(ktm + I18nManager.METADATA_KEY_PRIORITY_POSTFIX));
      // check for changed references in value
      // if correctly done, should still be resolvable
      assertTrue(sourcePropResolved.getProperty("key.to.stay2").indexOf(matchString) != -1);
      tDMgr.logToFile("moveKey");
    }
  }
 // remove after execution!
 @Test
 public void testRemoveReferenceLanguageCopiesTask() {
   if (I18nModule.isTransToolEnabled()) {
     tDMgr.removeReferenceLanguageCopiesTask(false);
     tDMgr.logToFile("refLangCopied");
   }
 }
 /** Test methods i18nManager.countI18nItems() and i18nManager.countBundles() */
 @Ignore
 @Test
 public void testCountI18nItemsAndBundles() {
   I18nModule.initBundleNames(); // remove dirty stuff from previous tests
   int bundleCounter = I18nModule.getBundleNamesContainingI18nFiles().size();
   String testNewBundle = "org.olat.core.util.i18n.junittestdata.new";
   Locale testLocale = i18nMgr.getLocaleOrDefault("de");
   File baseDir = I18nModule.getPropertyFilesBaseDir(testLocale, testNewBundle);
   File testFile = i18nMgr.getPropertiesFile(testLocale, testNewBundle, baseDir);
   // clean first existing files from previous broken testcase
   if (testFile.exists()) {
     i18nMgr.deleteProperties(testLocale, testNewBundle);
   }
   Properties props = i18nMgr.getResolvedProperties(testLocale, testNewBundle);
   assertEquals(0, i18nMgr.countI18nItems(testLocale, testNewBundle, true));
   assertEquals(0, i18nMgr.countI18nItems(testLocale, testNewBundle, false));
   props.setProperty("key.1", "1");
   props.setProperty("key.2", "2");
   i18nMgr.saveOrUpdateProperties(props, testLocale, testNewBundle);
   assertEquals(2, i18nMgr.countI18nItems(testLocale, testNewBundle, false));
   assertEquals(0, i18nMgr.countI18nItems(i18nMgr.getLocaleOrDefault("en"), testNewBundle, false));
   assertEquals(bundleCounter + 1, I18nModule.getBundleNamesContainingI18nFiles().size());
   // test all bundles
   int allCount = i18nMgr.countI18nItems(testLocale, null, true);
   assertEquals(allCount, i18nMgr.countI18nItems(testLocale, null, false));
   props.remove("key.1");
   i18nMgr.saveOrUpdateProperties(props, testLocale, testNewBundle);
   assertEquals(allCount - 1, i18nMgr.countI18nItems(testLocale, null, false));
   i18nMgr.deleteProperties(testLocale, testNewBundle);
   assertEquals(allCount - 2, i18nMgr.countI18nItems(testLocale, null, false));
   assertEquals(bundleCounter, I18nModule.getBundleNamesContainingI18nFiles().size());
   // count bundles tests
   assertEquals(0, i18nMgr.countBundles("org.olat.core.util.i18n.nonexisting", true));
   assertEquals(1, i18nMgr.countBundles("org.olat.core.util.i18n.ui", true));
   // finds 4: regular: i18n.devtools, i18n.ui;
   assertEquals(2, i18nMgr.countBundles("org.olat.core.util.i18n", true));
   assertEquals(0, i18nMgr.countBundles("org.olat.core.util.i18n", false));
   assertEquals(1, i18nMgr.countBundles("org.olat.core.util.i18n.ui", false));
   assertTrue(0 < i18nMgr.countBundles(null, false));
   // clean up
   if (testFile.exists()) {
     i18nMgr.deleteProperties(testLocale, testNewBundle);
   }
   if (testFile.getParentFile().exists()) {
     testFile.getParentFile().delete();
   }
 }
 @Test
 public void testRemoveDeletedKeysTest() {
   // set languages that is used as reference: all keys there are the keys should not to be deleted
   String[] referenceLanguages = new String[] {"de", "en"};
   // set the languages that should be cleaned up
   Set<String> targetLanguages = I18nModule.getTranslatableLanguageKeys();
   // Set<String> targetLanguages = new HashSet<String>();
   // targetLanguages.add("en");
   tDMgr.removeDeletedKeys(false, referenceLanguages, targetLanguages);
 }
 protected void removeDevToolTests() {
   Locale testLocale = i18nMgr.getLocaleOrDefault("de");
   // cleanup devtools source/target files
   // 1) source files
   File baseDir = I18nModule.getPropertyFilesBaseDir(testLocale, testSourceBundle);
   File testSFile = i18nMgr.getPropertiesFile(testLocale, testSourceBundle, baseDir);
   File sourcePath = testSFile.getParentFile().getParentFile();
   FileUtils.deleteDirsAndFiles(sourcePath, true, true);
   // 2) target files
   baseDir = I18nModule.getPropertyFilesBaseDir(testLocale, testTargetBundle);
   File testTFile = i18nMgr.getPropertiesFile(testLocale, testTargetBundle, baseDir);
   File targetPath = testTFile.getParentFile().getParentFile();
   FileUtils.deleteDirsAndFiles(targetPath, true, true);
   // 3) move target files
   baseDir = I18nModule.getPropertyFilesBaseDir(testLocale, testMoveTargetBundle);
   File testMFile = i18nMgr.getPropertiesFile(testLocale, testMoveTargetBundle, baseDir);
   File movePath = testMFile.getParentFile().getParentFile().getParentFile();
   FileUtils.deleteDirsAndFiles(movePath, true, true);
 }
 @Test
 public void testMergePackageTask() {
   if (I18nModule.isTransToolEnabled()) {
     prepareDevToolTests();
     tDMgr.mergePackageTask(testSourceBundle, testTargetBundle);
     i18nMgr.clearCaches();
     assertTrue(
         i18nMgr.getPropertiesWithoutResolvingRecursively(null, testSourceBundle).isEmpty());
     // TODO: manipulate source/target first, merge and check if all is in target!
     // try to merge existing key
   }
 }
 /** Test methods i18nManager.getLanguageTranslated() i18nManager.getLanguageInEnglish() */
 @Test
 public void testGetPropertyFile() {
   if (I18nModule.isTransToolEnabled()) {
     File baseDir =
         I18nModule.getPropertyFilesBaseDir(i18nMgr.getLocaleOrDefault("de"), "org.olat.core");
     assertNotNull(baseDir);
     File file =
         i18nMgr.getPropertiesFile(i18nMgr.getLocaleOrDefault("de"), "org.olat.core", baseDir);
     assertTrue(file.exists());
     // test get metadata file
     file = i18nMgr.getPropertiesFile(null, "org.olat.core", baseDir);
     assertNotNull(file);
     try {
       file = i18nMgr.getPropertiesFile(i18nMgr.getLocaleOrDefault("de"), null, baseDir);
       fail("exception expected for NULL bundle");
     } catch (AssertException e) {
       // do nothing, expected
     }
     // test with file that does not exist
     file = i18nMgr.getPropertiesFile(i18nMgr.getLocaleOrDefault("de"), "hello.world", baseDir);
     assertFalse(file.exists());
   }
 }
 /** Test method i18nManager.buildI18nFilename() */
 @Test
 public void testBuildI18nFilename() {
   String overlay = I18nModule.getOverlayName();
   String testFileName = i18nMgr.buildI18nFilename(new Locale("de"));
   assertEquals("LocalStrings_de.properties", testFileName);
   testFileName = i18nMgr.buildI18nFilename(new Locale("de", "", "__" + overlay));
   assertEquals("LocalStrings_de__" + overlay + ".properties", testFileName);
   testFileName = i18nMgr.buildI18nFilename(new Locale("de", "CH"));
   assertEquals("LocalStrings_de_CH.properties", testFileName);
   testFileName = i18nMgr.buildI18nFilename(new Locale("de", "CH", "VENDOR"));
   assertEquals("LocalStrings_de_CH_VENDOR.properties", testFileName);
   testFileName = i18nMgr.buildI18nFilename(new Locale("de", "CH", "VENDOR__" + overlay));
   assertEquals("LocalStrings_de_CH_VENDOR__" + overlay + ".properties", testFileName);
 }
Exemple #11
0
 @Test
 public void testMovePackageByMovingSingleKeysTask() {
   if (I18nModule.isTransToolEnabled()) {
     prepareDevToolTests();
     tDMgr.movePackageByMovingSingleKeysTask(testSourceBundle, testTargetBundle);
     i18nMgr.clearCaches();
     Properties sourceProp =
         i18nMgr.getPropertiesWithoutResolvingRecursively(null, testSourceBundle);
     assertTrue(sourceProp.isEmpty());
     Properties targetProp =
         i18nMgr.getPropertiesWithoutResolvingRecursively(null, testTargetBundle);
     assertFalse(targetProp.isEmpty());
   }
 }
 @Test
 public void testMoveLanguageTask() {
   if (I18nModule.isTransToolEnabled()) {
     prepareDevToolTests();
     String srcPath =
         I18nModule.getTransToolApplicationLanguagesSrcDir().getAbsolutePath()
             + "/../../test/java/org/olat/core/util/i18n/junittestdata/devtools";
     String targetPath = srcPath + "/../movetarget";
     // only copy: target should exist
     Locale mvLocale = i18nMgr.getLocaleOrDefault("de");
     // test before move
     Properties sourceProperties =
         i18nMgr.getPropertiesWithoutResolvingRecursively(mvLocale, testSourceBundle);
     assertFalse(sourceProperties.isEmpty());
     Properties moveProperties =
         i18nMgr.getPropertiesWithoutResolvingRecursively(mvLocale, testMoveTargetBundle);
     assertTrue(moveProperties.isEmpty());
     // copy
     tDMgr.moveLanguageTask(mvLocale, srcPath, targetPath, false);
     // test after copy
     sourceProperties =
         i18nMgr.getPropertiesWithoutResolvingRecursively(mvLocale, testSourceBundle);
     assertFalse(sourceProperties.isEmpty());
     moveProperties =
         i18nMgr.getPropertiesWithoutResolvingRecursively(mvLocale, testMoveTargetBundle);
     assertFalse(moveProperties.isEmpty());
     // move
     tDMgr.moveLanguageTask(mvLocale, srcPath, targetPath, true);
     // test after move
     sourceProperties =
         i18nMgr.getPropertiesWithoutResolvingRecursively(mvLocale, testSourceBundle);
     assertTrue(sourceProperties.isEmpty());
     moveProperties =
         i18nMgr.getPropertiesWithoutResolvingRecursively(mvLocale, testMoveTargetBundle);
     assertFalse(moveProperties.isEmpty());
   }
 }
Exemple #13
0
 @After
 public void tearDown() throws Exception {
   Locale testLocale = i18nMgr.getLocaleOrDefault("de");
   File baseDir = I18nModule.getPropertyFilesBaseDir(testLocale, testNewBundle);
   // only delete files when basedir available
   if (baseDir == null) return;
   File testFile = i18nMgr.getPropertiesFile(testLocale, testNewBundle, baseDir);
   // delete not only new/_i18n/LocalStrings_de.properties, delete also _i18n and new itself
   if (testFile.getParentFile().getParentFile().exists()) {
     FileUtils.deleteDirsAndFiles(testFile.getParentFile().getParentFile(), true, true);
   }
   // don't do inline translation markup
   i18nMgr.setMarkLocalizedStringsEnabled(null, false);
   // cleanup dev tools test case files
   removeDevToolTests();
 }
 /** Test methods i18nManager.createLocal() */
 @Test
 public void testCreateLocale() {
   // standard locale
   Locale loc = i18nMgr.createLocale("de");
   assertNotNull(loc);
   assertEquals("de", loc.getLanguage());
   assertEquals("", loc.getCountry());
   assertEquals("", loc.getVariant());
   // with country
   loc = i18nMgr.createLocale("de_CH");
   assertNotNull(loc);
   assertEquals("de", loc.getLanguage());
   assertEquals("CH", loc.getCountry());
   assertEquals("", loc.getVariant());
   // with variant
   loc = i18nMgr.createLocale("de_CH_ZH");
   assertNotNull(loc);
   assertEquals("de", loc.getLanguage());
   assertEquals("CH", loc.getCountry());
   assertEquals("ZH", loc.getVariant());
   // with variant but no country
   loc = i18nMgr.createLocale("de__VENDOR");
   assertNotNull(loc);
   assertEquals("de", loc.getLanguage());
   assertEquals("", loc.getCountry());
   assertEquals("VENDOR", loc.getVariant());
   //
   // With overlay
   // with language
   String overlay = I18nModule.getOverlayName();
   loc = i18nMgr.createLocale("de");
   Locale over = i18nMgr.createOverlay(loc);
   assertEquals("de____" + overlay, over.toString());
   assertEquals(i18nMgr.createOverlayKeyForLanguage(loc.toString()), i18nMgr.getLocaleKey(over));
   // with country
   loc = i18nMgr.createLocale("de_CH");
   over = i18nMgr.createOverlay(loc);
   assertEquals("de_CH___" + overlay, over.toString());
   assertEquals(i18nMgr.createOverlayKeyForLanguage(loc.toString()), i18nMgr.getLocaleKey(over));
   // with variant
   loc = i18nMgr.createLocale("de_CH_ZH");
   over = i18nMgr.createOverlay(loc);
   assertEquals("de_CH_ZH__" + overlay, over.toString());
   assertEquals(i18nMgr.createOverlayKeyForLanguage(loc.toString()), i18nMgr.getLocaleKey(over));
 }
 @Test
 public void testRenameLanguageTask() {
   if (I18nModule.isTransToolEnabled()) {
     prepareDevToolTests();
     // create source
     Locale xxLocale = new Locale("xx");
     String key = "key.to.move";
     I18nItem i18nItem = i18nMgr.getI18nItem(testSourceBundle, key, xxLocale);
     i18nMgr.saveOrUpdateI18nItem(i18nItem, "I have to go");
     i18nMgr.setAnnotation(i18nItem, "an annotation");
     i18nMgr.setKeyPriority(testSourceBundle, key, 100);
     // copy to target
     Locale yyLocale = new Locale("yy");
     tDMgr.renameLanguageTask(xxLocale, yyLocale);
     i18nMgr.clearCaches();
     // test
     Properties deletedProperties =
         i18nMgr.getPropertiesWithoutResolvingRecursively(xxLocale, testSourceBundle);
     assertTrue(deletedProperties.isEmpty());
     Properties targetProp =
         i18nMgr.getPropertiesWithoutResolvingRecursively(yyLocale, testSourceBundle);
     assertFalse(targetProp.isEmpty());
   }
 }
 /**
  * Test methods i18nManager.getProperties(), i18nManager.saveOrUpdateProperties() and
  * i18nManager.deleteProperties()
  */
 @Ignore
 @Test
 public void testGetSaveOrUpdateAndDeleteProperties() {
   // test with existing files
   Properties props =
       i18nMgr.getResolvedProperties(i18nMgr.getLocaleOrDefault("de"), "org.olat.core");
   assertNotNull(props);
   assertTrue(props.size() > 0);
   // test with non existing files
   String testNewBundle = "org.olat.core.util.i18n.junittestdata.new";
   Locale testLocale = i18nMgr.getLocaleOrDefault("de");
   File baseDir = I18nModule.getPropertyFilesBaseDir(testLocale, testNewBundle);
   File testFile = i18nMgr.getPropertiesFile(testLocale, testNewBundle, baseDir);
   // clean first existing files from previous broken testcase
   if (testFile.exists()) {
     i18nMgr.deleteProperties(testLocale, testNewBundle);
   }
   props = i18nMgr.getResolvedProperties(testLocale, testNewBundle);
   assertNotNull(props);
   assertEquals(0, props.size());
   // test deleting of non existing
   assertFalse(testFile.exists());
   i18nMgr.deleteProperties(testLocale, testNewBundle);
   assertFalse(testFile.exists());
   // test saving of non existing
   props.setProperty("test.key", "Hello wörld !");
   i18nMgr.saveOrUpdateProperties(props, testLocale, testNewBundle);
   assertTrue(testFile.exists());
   assertEquals(
       "Hello wörld !",
       i18nMgr.getLocalizedString(testNewBundle, "test.key", null, testLocale, false, false));
   // test saving of existing
   props = new Properties();
   props.setProperty("hello.world", "&%çest françias, ê alors");
   i18nMgr.saveOrUpdateProperties(props, testLocale, testNewBundle);
   assertTrue(testFile.exists());
   assertEquals(
       "&%çest françias, ê alors",
       i18nMgr.getLocalizedString(testNewBundle, "hello.world", null, testLocale, false, false));
   assertFalse(
       "Hello wörld !"
           .equals(
               i18nMgr.getLocalizedString(
                   testNewBundle, "test.key", null, testLocale, false, false)));
   // test various special cases
   props.setProperty("chinesetest", "请选择你的大学");
   props.setProperty("specialtest", "that's like \"really\" bad");
   props.setProperty("multiline", "bla\tbla\nsecond line");
   props.setProperty("html", "<h1>Hello World</h1>");
   props.setProperty("empty.property", "");
   i18nMgr.saveOrUpdateProperties(props, testLocale, testNewBundle);
   i18nMgr.clearCaches();
   assertEquals(
       "请选择你的大学",
       i18nMgr.getLocalizedString(testNewBundle, "chinesetest", null, testLocale, false, false));
   assertEquals(
       "that's like \"really\" bad",
       i18nMgr.getLocalizedString(testNewBundle, "specialtest", null, testLocale, false, false));
   assertEquals(
       "bla\tbla\nsecond line",
       i18nMgr.getLocalizedString(testNewBundle, "multiline", null, testLocale, false, false));
   assertEquals(
       "<h1>Hello World</h1>",
       i18nMgr.getLocalizedString(testNewBundle, "html", null, testLocale, false, false));
   assertEquals(
       "",
       i18nMgr.getLocalizedString(
           testNewBundle, "empty.property", null, testLocale, false, false));
   assertEquals(
       null,
       i18nMgr.getLocalizedString(
           testNewBundle, "not.existing.property", null, testLocale, false, false));
   // test deleting of existing
   i18nMgr.deleteProperties(testLocale, testNewBundle);
   assertFalse(testFile.exists());
   assertFalse(
       "Hello wörld !"
           .equals(
               i18nMgr.getLocalizedString(
                   testNewBundle, "test.key", null, testLocale, false, false)));
   assertFalse(
       "&%çest françias, ê alors"
           .equals(
               i18nMgr.getLocalizedString(
                   testNewBundle, "hello.world", null, testLocale, false, false)));
   // clean up
   if (testFile.exists()) {
     i18nMgr.deleteProperties(testLocale, testNewBundle);
   }
   if (testFile.getParentFile().exists()) testFile.getParentFile().delete();
 }