Made a start on localization ("lion") utilities.
Change-Id: I838767cb49cb785e98d71475116c264f749d4aac
diff --git a/core/api/src/main/java/org/onosproject/ui/UiLionService.java b/core/api/src/main/java/org/onosproject/ui/UiLionService.java
new file mode 100644
index 0000000..ecfb60b
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/UiLionService.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.onosproject.ui;
+
+import org.onlab.util.ItemNotFoundException;
+import org.onosproject.ui.lion.LionBundle;
+
+/**
+ * Service for accessing registered localization bundles.
+ */
+public interface UiLionService {
+
+ /**
+ * Returns the bundle for the given key. If no such bundle is registered
+ * an ItemNotFound exception will be thrown.
+ *
+ * @param key the bundle key
+ * @return the associated bundle
+ * @throws ItemNotFoundException if no bundle exists for
+ * the given key
+ */
+ LionBundle getBundle(String key);
+
+}
diff --git a/core/api/src/main/java/org/onosproject/ui/lion/LionBundle.java b/core/api/src/main/java/org/onosproject/ui/lion/LionBundle.java
new file mode 100644
index 0000000..a4f07c2
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/lion/LionBundle.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.onosproject.ui.lion;
+
+/**
+ * Encapsulates a bundle of localization strings.
+ */
+public final class LionBundle {
+
+ private final String key;
+
+ private LionBundle(String key) {
+ this.key = key;
+ }
+
+ /**
+ * Returns the key identifying this bundle.
+ *
+ * @return the bundle's key
+ */
+ public String key() {
+ return key;
+ }
+
+
+ // TODO: public static method to generate a bundle from a ResourceBundle
+}
diff --git a/core/api/src/main/java/org/onosproject/ui/lion/LionUtils.java b/core/api/src/main/java/org/onosproject/ui/lion/LionUtils.java
new file mode 100644
index 0000000..d720888
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/lion/LionUtils.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.onosproject.ui.lion;
+
+import java.util.ResourceBundle;
+
+/**
+ * Utility methods for dealing with Localization Bundles etc.
+ */
+public final class LionUtils {
+
+ private static final String DOT = ".";
+
+ // no instantiation
+ private LionUtils() {
+ }
+
+ /**
+ * This method takes a fully qualified name and returns a
+ * {@link ResourceBundle} which is loaded from a properties file with
+ * that base name.
+ * <p>
+ * For example, supposing the jar file contains:
+ * <p>
+ * <pre>
+ * org/onosproject/util/example/SomeBundle.properties
+ * </pre>
+ * <p>
+ * Then, to correctly load the resource bundle associated with
+ * <code>SomeBundle</code>, call:
+ * <pre>
+ * String fqname = "org.onosproject.util.example.SomeBundle";
+ * ResourceBundle res = ResourceUtils.getBundledResource(fqname);
+ * </pre>
+ * <p>
+ * Note that no error is thrown if the properties file does not exist.
+ * This condition will not become apparent until you try and access
+ * a property from the bundle, at which time a
+ * {@link java.util.MissingResourceException} will be thrown.
+ *
+ * @param basename the (fully qualified) basename of the bundle properties
+ * file
+ * @return the associated resource bundle
+ */
+ public static ResourceBundle getBundledResource(String basename) {
+ return ResourceBundle.getBundle(basename);
+ }
+
+ /**
+ * This method takes a class and returns a {@link ResourceBundle} which is
+ * loaded from a properties file with the same base name as the class.
+ * Note that both the class and the properties file(s) need to be in
+ * the same jar file.
+ * <p>
+ * For example, supposing the jar file contains:
+ * <p>
+ * <pre>
+ * org/onosproject/util/example/SomeObject.class
+ * org/onosproject/util/example/SomeObject.properties
+ * </pre>
+ * <p>
+ * Then, to correctly load the resource bundle associated with
+ * <code>SomeObject</code>, call:
+ * <pre>
+ * ResourceBundle res = ResourceUtils.getBundledResource(SomeObject.class);
+ * </pre>
+ * <p>
+ * Note that no error is thrown if the properties file does not exist.
+ * This condition will not become apparent until you try and access
+ * a property from the bundle, at which time a
+ * {@link java.util.MissingResourceException} will be thrown.
+ *
+ * @param c the class
+ * @return the associated resource bundle
+ */
+ public static ResourceBundle getBundledResource(Class<?> c) {
+ return ResourceBundle.getBundle(c.getName());
+ }
+
+ /**
+ * This method returns a {@link ResourceBundle} which is loaded from
+ * a properties file with the specified base name from the same package
+ * as the specified class.
+ * Note that both the class and the properties file(s) need to be in
+ * the same jar file.
+ * <p>
+ * For example, supposing the jar file contains:
+ * <p>
+ * <pre>
+ * org/onosproject/util/example/SomeObject.class
+ * org/onosproject/util/example/DisplayStrings.properties
+ * </pre>
+ * <p>
+ * Then, to correctly load the resource bundle call:
+ * <pre>
+ * ResourceBundle res = ResourceUtils.getBundledResource(SomeObject.class,
+ * "DisplayStrings");
+ * </pre>
+ * <p>
+ * Note that no error is thrown if the properties file does not exist.
+ * This condition will not become apparent until you try and access
+ * a property from the bundle, at which time a
+ * {@link java.util.MissingResourceException} will be thrown.
+ *
+ * @param c the class requesting the bundle
+ * @param baseName the base name of the resource bundle
+ * @return the associated resource bundle
+ */
+ public static ResourceBundle getBundledResource(Class<?> c, String baseName) {
+ String className = c.getName();
+ StringBuilder sb = new StringBuilder();
+ int dot = className.lastIndexOf(DOT);
+ sb.append(className.substring(0, dot));
+ sb.append(DOT).append(baseName);
+ return ResourceBundle.getBundle(sb.toString());
+ }
+}
diff --git a/core/api/src/main/java/org/onosproject/ui/lion/package-info.java b/core/api/src/main/java/org/onosproject/ui/lion/package-info.java
new file mode 100644
index 0000000..69c998d
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/lion/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+/**
+ * Facilities for handling localization of the UI.
+ */
+package org.onosproject.ui.lion;
diff --git a/core/api/src/test/java/org/onosproject/ui/lion/LionBundleTest.java b/core/api/src/test/java/org/onosproject/ui/lion/LionBundleTest.java
new file mode 100644
index 0000000..7100088
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/ui/lion/LionBundleTest.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.onosproject.ui.lion;
+
+import org.junit.Test;
+
+/**
+ * Unit tests for {@link LionBundle}.
+ */
+public class LionBundleTest {
+
+ private LionBundle bundle;
+
+ @Test
+ public void basic() {
+
+// bundle = new LionBundle();
+ }
+}
diff --git a/core/api/src/test/java/org/onosproject/ui/lion/LionUtilsTest.java b/core/api/src/test/java/org/onosproject/ui/lion/LionUtilsTest.java
new file mode 100644
index 0000000..52fd3f6
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/ui/lion/LionUtilsTest.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.onosproject.ui.lion;
+
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onosproject.ui.AbstractUiTest;
+
+import java.util.Locale;
+import java.util.ResourceBundle;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * Unit tests for {@link LionUtils}.
+ */
+public class LionUtilsTest extends AbstractUiTest {
+
+ private static Locale systemLocale;
+
+ private ResourceBundle res;
+
+ @BeforeClass
+ public static void classSetup() {
+ systemLocale = Locale.getDefault();
+ }
+
+ @AfterClass
+ public static void classTeardown() {
+ Locale.setDefault(systemLocale);
+ }
+
+ @Before
+ public void testSetup() {
+ // reset to a known default locale before starting each test
+ Locale.setDefault(Locale.US);
+ }
+
+ @Test
+ public void getBundleByClassAndName() {
+ title("getBundleByClassAndName");
+ res = LionUtils.getBundledResource(LionUtilsTest.class, "SomeResource");
+ assertNotNull("missing resource bundle", res);
+ String v1 = res.getString("key1");
+ String v2 = res.getString("key2");
+ print("v1 is %s, v2 is %s", v1, v2);
+ assertEquals("v1 value wrong", "value one", v1);
+ assertEquals("v2 value wrong", "value two", v2);
+
+ res = LionUtils.getBundledResource(LionUtils.class, "SomeOtherResource");
+ assertNotNull("missing OTHER resource bundle", res);
+ v1 = res.getString("key1");
+ v2 = res.getString("key2");
+ print("v1 is %s, v2 is %s", v1, v2);
+ assertEquals("v1 value wrong", "Hay", v1);
+ assertEquals("v2 value wrong", "Bee", v2);
+ }
+
+ @Test
+ public void getBundleByClassname() {
+ title("getBundleByClassname");
+ res = LionUtils.getBundledResource(LionUtils.class);
+ assertNotNull("missing resource bundle", res);
+ String v1 = res.getString("foo");
+ String v2 = res.getString("boo");
+ print("v1 is %s, v2 is %s", v1, v2);
+ assertEquals("v1 value wrong", "bar", v1);
+ assertEquals("v2 value wrong", "ghost", v2);
+ }
+
+ @Test
+ public void getBundleByFqcn() {
+ title("getBundleByFqcn");
+ String fqcn = "org.onosproject.ui.lion.LionUtils";
+ res = LionUtils.getBundledResource(fqcn);
+ assertNotNull("missing resource bundle", res);
+ String v1 = res.getString("foo");
+ String v2 = res.getString("boo");
+ print("v1 is %s, v2 is %s", v1, v2);
+ assertEquals("v1 value wrong", "bar", v1);
+ assertEquals("v2 value wrong", "ghost", v2);
+ }
+
+ @Test
+ public void messageInEnglish() {
+ title("messageInEnglish");
+ // use default locale
+ res = LionUtils.getBundledResource(LionUtils.class, "MyBundle");
+ print("res locale is %s", res.getLocale().getLanguage());
+ assertEquals("not disk", "disk", res.getString("disk"));
+ assertEquals("not keyboard", "keyboard", res.getString("keyboard"));
+ }
+
+ @Test
+ public void messageInGerman() {
+ title("messageInGerman");
+ Locale.setDefault(new Locale("de", "DE"));
+ res = LionUtils.getBundledResource(LionUtils.class, "MyBundle");
+ print("res locale is %s", res.getLocale().getLanguage());
+ assertEquals("not DE-disk", "Platte", res.getString("disk"));
+ assertEquals("not DE-keyboard", "Tastatur", res.getString("keyboard"));
+ }
+}
diff --git a/core/api/src/test/resources/org/onosproject/ui/lion/LionUtils.properties b/core/api/src/test/resources/org/onosproject/ui/lion/LionUtils.properties
new file mode 100644
index 0000000..9306899
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/ui/lion/LionUtils.properties
@@ -0,0 +1,20 @@
+#
+# Copyright 2017-present Open Networking Laboratory
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+
+# Resource strings for unit testing LionUtils
+foo = bar
+boo = ghost
diff --git a/core/api/src/test/resources/org/onosproject/ui/lion/MyBundle.properties b/core/api/src/test/resources/org/onosproject/ui/lion/MyBundle.properties
new file mode 100644
index 0000000..0aaa183
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/ui/lion/MyBundle.properties
@@ -0,0 +1,21 @@
+#
+# Copyright 2017-present Open Networking Laboratory
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+# Test bundle - default (English)
+computer=computer
+disk=disk
+monitor=monitor
+keyboard=keyboard
diff --git a/core/api/src/test/resources/org/onosproject/ui/lion/MyBundle_de.properties b/core/api/src/test/resources/org/onosproject/ui/lion/MyBundle_de.properties
new file mode 100644
index 0000000..ba0a1a3
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/ui/lion/MyBundle_de.properties
@@ -0,0 +1,20 @@
+#
+# Copyright 2017-present Open Networking Laboratory
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+computer=Computer
+disk=Platte
+monitor=Monitor
+keyboard=Tastatur
diff --git a/core/api/src/test/resources/org/onosproject/ui/lion/SomeOtherResource.properties b/core/api/src/test/resources/org/onosproject/ui/lion/SomeOtherResource.properties
new file mode 100644
index 0000000..fa725b1
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/ui/lion/SomeOtherResource.properties
@@ -0,0 +1,21 @@
+#
+# Copyright 2017-present Open Networking Laboratory
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+
+# a resource bundle for ResourceUtils testing
+
+key1 = Hay
+key2 = Bee
diff --git a/core/api/src/test/resources/org/onosproject/ui/lion/SomeResource.properties b/core/api/src/test/resources/org/onosproject/ui/lion/SomeResource.properties
new file mode 100644
index 0000000..5359317
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/ui/lion/SomeResource.properties
@@ -0,0 +1,21 @@
+#
+# Copyright 2017-present Open Networking Laboratory
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+
+# a resource bundle for ResourceUtils testing
+
+key1 = value one
+key2 = value two