BlueI18n implements ApiRoutable { @CheckForNull static BundleParams getBundleParameters(String restOfPath) { if (restOfPath == null || restOfPath.length() == 0) { return null; } String[] pathTokens = restOfPath.split("/"); List<String> bundleParameters = new ArrayList<>(); for (String pathToken : pathTokens) { if (pathToken.length() > 0) { bundleParameters.add(urlDecode(pathToken)); } } if (bundleParameters.get(0).equals("blue")) { bundleParameters.remove(0); } if (bundleParameters.get(0).equals("rest")) { bundleParameters.remove(0); } if (bundleParameters.get(0).equals("i18n")) { bundleParameters.remove(0); } if (bundleParameters.size() != 3 && bundleParameters.size() != 4) { return null; } BundleParams bundleParams = new BundleParams( bundleParameters.get(0), bundleParameters.get(1), bundleParameters.get(2) ); if (bundleParameters.size() == 4) { String locale = bundleParameters.get(3); String[] localeTokens = locale.split("-|_"); bundleParams.language = localeTokens[0]; if (localeTokens.length > 1) { bundleParams.country = localeTokens[1]; if (localeTokens.length > 2) { bundleParams.variant = localeTokens[2]; } } } return bundleParams; }  @Override String getUrlName(); HttpResponse doDynamic(StaplerRequest request);  }
@Test public void test_getBundleParameters_invalid_url() { Assert.assertNull(BlueI18n.getBundleParameters("pluginx/1.0.0")); Assert.assertNull(BlueI18n.getBundleParameters("/pluginx/1.0.0")); }
@Test public void test_getBundleParameters_valid_url() { BlueI18n.BundleParams bundleParameters = BlueI18n.getBundleParameters("/blue/rest/i18n/pluginx/1.0.0/pluginx.bundle"); Assert.assertNotNull(bundleParameters); Assert.assertEquals("pluginx", bundleParameters.pluginName); Assert.assertEquals("1.0.0", bundleParameters.pluginVersion); Assert.assertEquals("pluginx.bundle", bundleParameters.bundleName); Assert.assertNull(bundleParameters.language); Assert.assertNull(bundleParameters.country); Assert.assertNull(bundleParameters.variant); Assert.assertNull(bundleParameters.getLocale()); bundleParameters = BlueI18n.getBundleParameters("/blue/rest/i18n/pluginx/1.0.0/pluginx.bundle/en"); Assert.assertNotNull(bundleParameters); Assert.assertEquals("pluginx", bundleParameters.pluginName); Assert.assertEquals("1.0.0", bundleParameters.pluginVersion); Assert.assertEquals("pluginx.bundle", bundleParameters.bundleName); Assert.assertEquals("en", bundleParameters.language); Assert.assertNull(bundleParameters.country); Assert.assertNull(bundleParameters.variant); Assert.assertNotNull(bundleParameters.getLocale()); Assert.assertEquals("en", bundleParameters.getLocale().toString()); }
@Test public void test_getBundleParameters_locale() { BlueI18n.BundleParams bundleParameters; bundleParameters = BlueI18n.getBundleParameters("/blue/rest/i18n/pluginx/1.0.0/pluginx.bundle/ja_JP_JP"); Assert.assertEquals("ja_JP_JP_#u-ca-japanese", bundleParameters.getLocale().toString()); bundleParameters = BlueI18n.getBundleParameters("pluginx/1.0.0/pluginx.bundle/ja-JP-JP"); Assert.assertEquals("ja_JP_JP_#u-ca-japanese", bundleParameters.getLocale().toString()); bundleParameters = BlueI18n.getBundleParameters("/blue/rest/i18n/pluginx/1.0.0/pluginx.bundle/ja_JP"); Assert.assertEquals("ja_JP", bundleParameters.getLocale().toString()); bundleParameters = BlueI18n.getBundleParameters("/blue/rest/i18n/pluginx/1.0.0/pluginx.bundle/ja-JP"); Assert.assertEquals("ja_JP", bundleParameters.getLocale().toString()); bundleParameters = BlueI18n.getBundleParameters("/blue/rest/i18n/pluginx/1.0.0/pluginx.bundle/ja"); Assert.assertEquals("ja", bundleParameters.getLocale().toString()); bundleParameters = BlueI18n.getBundleParameters("/blue/rest/i18n/pluginx/1.0.0/pluginx.bundle/ja"); Assert.assertEquals("ja", bundleParameters.getLocale().toString()); }
@Test public void test_getBundleParameters_isReleaseVersion() { BlueI18n.BundleParams bundleParameters; bundleParameters = BlueI18n.getBundleParameters("/blue/rest/i18n/pluginx/1.0/pluginx.bundle"); Assert.assertTrue(bundleParameters.isReleaseVersion()); bundleParameters = BlueI18n.getBundleParameters("pluginx/1.0.0/pluginx.bundle"); Assert.assertTrue(bundleParameters.isReleaseVersion()); bundleParameters = BlueI18n.getBundleParameters("pluginx/1.0-SNAPSHOT/pluginx.bundle"); Assert.assertFalse(bundleParameters.isReleaseVersion()); bundleParameters = BlueI18n.getBundleParameters("pluginx/1.0.0-SNAPSHOT/pluginx.bundle"); Assert.assertFalse(bundleParameters.isReleaseVersion()); bundleParameters = BlueI18n.getBundleParameters("pluginx/1/pluginx.bundle"); Assert.assertFalse(bundleParameters.isReleaseVersion()); }
@Test public void test_getBundleParameters_version_with_slashes() { BlueI18n.BundleParams bundleParameters; bundleParameters = BlueI18n.getBundleParameters("pluginx/1.0.0-SNAPSHOT%20(something%2Felse)/pluginx.bundle"); Assert.assertFalse(bundleParameters.isReleaseVersion()); Assert.assertEquals("1.0.0-SNAPSHOT (something/else)", bundleParameters.pluginVersion); }
@Test public void test_BundleParams_equals() { Assert.assertEquals(BlueI18n.getBundleParameters("pluginx/1.0/pluginx.bundle"), BlueI18n.getBundleParameters("pluginx/1.0/pluginx.bundle")); Assert.assertEquals(BlueI18n.getBundleParameters("pluginx/1.0/pluginx.bundle/en"), BlueI18n.getBundleParameters("pluginx/1.0/pluginx.bundle/en")); Assert.assertNotEquals(BlueI18n.getBundleParameters("pluginx/1.0/pluginx.bundle/en"), BlueI18n.getBundleParameters("pluginx/1.0/pluginx.bundle/")); Assert.assertNotEquals(BlueI18n.getBundleParameters("pluginx/1.0/pluginx.bundle/en"), BlueI18n.getBundleParameters("pluginx/1.0/pluginx.bundle/en_EN")); }