ResourceBundleCompiler { static Class<ResourceBundle> compileAndLoad(MessageBundle bundle, String className) throws InstantiationException, IllegalAccessException { return compileAndLoad(bundle, DEFAULT_PARENT_CLASS, className, null); }  static byte[] compile(MessageBundle bundle, String className); static void compileFile(MessageBundle bundle, String className, File dir); static void compileFile(MessageBundle bundle, String parentClassName, String className, File dir); static byte[] compile(MessageBundle bundle, String parentClassName, String className);  }
@Test public void testCompileResourceBundle() throws InstantiationException, IllegalAccessException, NoSuchFieldException { MessageBundle bundle = createBundle(); String parentClassName = GettextResourceBundle.class.getName(); Class<ResourceBundle> bundleClass = ResourceBundleCompiler.compileAndLoad(bundle, parentClassName, "net.jhorstmann.i18n.tools.TestResourceBundle_de", getClass().getClassLoader()); { Field messagesField = bundleClass.getDeclaredField("messages"); messagesField.setAccessible(true); Map messages = (Map) messagesField.get(null); Assert.assertNotNull(messages); Assert.assertEquals(3, messages.size()); Object str1 = messages.get("ctx1\u0004id1"); Assert.assertEquals("str1", str1); Object str2 = messages.get("ctx2\u0004id2"); Assert.assertTrue(str2 instanceof String[]); Assert.assertEquals(2, ((String[])str2).length); String singular = ((String[])str2)[0]; String plural = ((String[])str2)[1]; Assert.assertEquals("plural", singular); Assert.assertEquals("plurals", plural); } ResourceBundle rb = bundleClass.newInstance(); { Assert.assertEquals("str1", GettextResourceBundle.pgettext(rb, "ctx1", "id1")); Assert.assertEquals("plurals", GettextResourceBundle.npgettext(rb, "ctx2", "id2", "id2plural", 0L)); Assert.assertEquals("plural" , GettextResourceBundle.npgettext(rb, "ctx2", "id2", "id2plural", 1L)); Assert.assertEquals("plurals", GettextResourceBundle.npgettext(rb, "ctx2", "id2", "id2plural", 2L)); Assert.assertEquals("plurals", GettextResourceBundle.npgettext(rb, "ctx2", "id2", "id2plural", 3L)); } { Enumeration<String> keys = rb.getKeys(); Assert.assertTrue(keys.hasMoreElements()); } }