PseudoBundle extends ResourceBundle { public static boolean isEnabled() { return (getMode() != BundleMode.NO_TRANSLATION); } PseudoBundle(ResourceBundle baseBundle); static boolean isEnabled(); @Override Enumeration<String> getKeys(); static final String PSEUDO_BUNDLE_PROPERTY; static final String PSEUDO_BUNDLE_NORMAL; static final String PSEUDO_BUNDLE_WIDE; }
@Test public void testNoProperty() throws Exception { System.setProperties(new Properties()); assertNull(System.getProperty("mct.pseudotranslation")); ResourceBundle origBundle = ResourceBundle.getBundle("properties.PseudoBundle"); ResourceBundle testBundle = BundleFactory.getBundle("properties.PseudoBundle"); assertTrue(!PseudoBundle.isEnabled(), "Pseudotranslation disabled"); assertEquals(origBundle.getString("ascii"), testBundle.getString("ascii")); }
@Test public void testNoTranslation() throws Exception { System.setProperty("mct.pseudotranslation", "none"); ResourceBundle origBundle = ResourceBundle.getBundle("properties.PseudoBundle"); ResourceBundle testBundle = BundleFactory.getBundle("properties.PseudoBundle"); assertTrue(!PseudoBundle.isEnabled(), "Pseudotranslation disabled"); assertEquals(origBundle.getString("ascii"), testBundle.getString("ascii")); }
@Test public void testNormal() throws Exception { System.setProperty("mct.pseudotranslation", "normal"); ResourceBundle origBundle = ResourceBundle.getBundle("properties.PseudoBundle"); ResourceBundle testBundle = BundleFactory.getBundle("properties.PseudoBundle"); String alphabet = origBundle.getString("alphabet"); String special = origBundle.getString("special"); String noTranslation = origBundle.getString("no_normal_translation"); String ascii = origBundle.getString("ascii"); String translated = testBundle.getString("ascii"); assertTrue(PseudoBundle.isEnabled(), "Pseudotranslation enabled"); assertEquals(ascii.length()+2, translated.length()); for (int i=0; i<ascii.length(); ++i) { char c = ascii.charAt(i); if (noTranslation.indexOf(c) >= 0) { assertEquals(c, translated.charAt(i+1)); } else if (alphabet.indexOf(c)>=0 || special.indexOf(c)>=0) { assertTrue(c != translated.charAt(i+1), "Normal translation changes character " + c); } else { assertEquals(c, translated.charAt(i+1)); } } }
@Test public void testWide() { System.setProperty("mct.pseudotranslation", "wide"); ResourceBundle origBundle = ResourceBundle.getBundle("properties.PseudoBundle"); ResourceBundle testBundle = BundleFactory.getBundle("properties.PseudoBundle"); String ascii = origBundle.getString("ascii"); String translated = testBundle.getString("ascii"); assertTrue(PseudoBundle.isEnabled(), "Pseudotranslation enabled"); assertEquals(ascii.length()+2, translated.length()); for (int i=0; i<ascii.length(); ++i) { char c = ascii.charAt(i); assertTrue(c != translated.charAt(i+1), "Wide translation changes character " + c); } }
@Test public void testNormalFormat() { System.setProperty("mct.pseudotranslation", "normal"); ResourceBundle origBundle = ResourceBundle.getBundle("properties.PseudoBundle"); ResourceBundle testBundle = BundleFactory.getBundle("properties.PseudoBundle"); String alphabet = origBundle.getString("alphabet"); String special = origBundle.getString("special"); String noTranslation = origBundle.getString("no_normal_translation"); String messageFormat = origBundle.getString("message_format"); String translated = testBundle.getString("message_format"); assertTrue(PseudoBundle.isEnabled(), "Pseudotranslation enabled"); assertEquals(messageFormat.length()+2, translated.length()); boolean inFormat = false; for (int i=0; i<messageFormat.length(); ++i) { char c = messageFormat.charAt(i); if (c == '{') { inFormat = true; } if (!inFormat && noTranslation.indexOf(c) >= 0) { assertEquals(c, translated.charAt(i+1)); } else if (!inFormat && (alphabet.indexOf(c)>=0 || special.indexOf(c)>=0)) { assertTrue(c != translated.charAt(i+1), "Normal translation changes character " + c); } else { assertEquals(c, translated.charAt(i+1)); } if (c == '}') { inFormat = false; } } }
@Test public void testWideFormat() { System.setProperty("mct.pseudotranslation", "wide"); ResourceBundle origBundle = ResourceBundle.getBundle("properties.PseudoBundle"); ResourceBundle testBundle = BundleFactory.getBundle("properties.PseudoBundle"); String messageFormat = origBundle.getString("message_format"); String translated = testBundle.getString("message_format"); assertTrue(PseudoBundle.isEnabled(), "Pseudotranslation enabled"); assertEquals(messageFormat.length()+2, translated.length()); boolean inFormat = false; for (int i=0; i<messageFormat.length(); ++i) { char c = messageFormat.charAt(i); if (c == '{') { inFormat = true; } if (!inFormat && '!'<=c && c<='~') { assertTrue(c != translated.charAt(i+1), "Wide translation changes character " + c); } else { assertEquals(c, translated.charAt(i+1)); } if (c == '}') { inFormat = false; } } }
@Test public void testDirectCreation() { System.setProperty("mct.pseudotranslation", "none"); ResourceBundle origBundle = new PseudoBundle(ResourceBundle.getBundle("properties.PseudoBundle")); ResourceBundle testBundle = BundleFactory.getBundle("properties.PseudoBundle"); assertTrue(!PseudoBundle.isEnabled(), "Pseudotranslation disabled"); assertEquals(origBundle.getString("ascii"), testBundle.getString("ascii")); }