db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r597610 - in /db/derby/code/trunk/java: testing/org/apache/derbyTesting/functionTests/tests/tools/ tools/org/apache/derby/iapi/tools/i18n/ tools/org/apache/derby/impl/tools/sysinfo/
Date Fri, 23 Nov 2007 09:19:03 GMT
Author: kahatlen
Date: Fri Nov 23 01:19:03 2007
New Revision: 597610

URL: http://svn.apache.org/viewvc?rev=597610&view=rev
Log:
DERBY-3142: sysinfo ignores derby.ui.locale

Fetch sysinfo's resource bundle from LocalizedResource, which
recognizes derby.ui.locale, instead of directly from
ResourceBundle.getBundle(), which only considers the JVM's default
locale.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/SysinfoLocaleTest.java
  (with props)
Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/_Suite.java
    db/derby/code/trunk/java/tools/org/apache/derby/iapi/tools/i18n/LocalizedResource.java
    db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/sysinfo/Main.java

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/SysinfoLocaleTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/SysinfoLocaleTest.java?rev=597610&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/SysinfoLocaleTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/SysinfoLocaleTest.java
Fri Nov 23 01:19:03 2007
@@ -0,0 +1,213 @@
+/*
+
+   Derby - Class org.apache.derbyTesting.functionTests.tests.tools.SysinfoLocaleTest
+
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You 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.apache.derbyTesting.functionTests.tests.tools;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Locale;
+import java.util.Properties;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.apache.derbyTesting.junit.BaseTestCase;
+import org.apache.derbyTesting.junit.Derby;
+import org.apache.derbyTesting.junit.SecurityManagerSetup;
+import org.apache.derbyTesting.junit.SystemPropertyTestSetup;
+
+/**
+ * This test verifies that <code>sysinfo</code> correctly localizes its
+ * messages according to the default locale and <code>derby.ui.locale</code>.
+ */
+public class SysinfoLocaleTest extends BaseTestCase {
+
+    /** The encoding sysinfo should use for its output. */
+    private static final String ENCODING = "UTF-8";
+
+    /** Default locale to run this test under. */
+    private final Locale defaultLocale;
+
+    /**
+     * Tells whether or not this test expects sysinfo's output to be localized
+     * to German.
+     */
+    private final boolean localizedToGerman;
+
+    /** The default locale before this test started. */
+    private Locale savedLocale;
+
+    /**
+     * Create a test.
+     *
+     * @param defaultLocale the default locale for this test
+     * @param german true if output is expected to be localized to German
+     */
+    private SysinfoLocaleTest(Locale defaultLocale, boolean german) {
+        super("testSysinfoLocale");
+        this.defaultLocale = defaultLocale;
+        this.localizedToGerman = german;
+    }
+
+    /**
+     * Set up the test environment.
+     */
+    protected void setUp() {
+        savedLocale = Locale.getDefault();
+        Locale.setDefault(defaultLocale);
+    }
+
+    /**
+     * Tear down the test environment.
+     */
+    protected void tearDown() throws Exception {
+        Locale.setDefault(savedLocale);
+        savedLocale = null;
+        super.tearDown();
+    }
+
+    /**
+     * Create a suite of tests.
+     *
+     * @return a test suite with different combinations of
+     * <code>derby.ui.locale</code> and default locale
+     */
+    public static Test suite() {
+        if (!Derby.hasTools()) {
+            return new TestSuite("empty: no tools support");
+        }
+
+        TestSuite suite = new TestSuite("SysinfoLocaleTest");
+
+        // Create test combinations. Messages should be localized to the
+        // locale specified by derby.ui.locale, if it's set. Otherwise, the
+        // JVM's default locale should be used.
+        suite.addTest(createTest(Locale.ITALY, null, false));
+        suite.addTest(createTest(Locale.ITALY, "it_IT", false));
+        suite.addTest(createTest(Locale.ITALY, "de_DE", true));
+        suite.addTest(createTest(Locale.GERMANY, null, true));
+        suite.addTest(createTest(Locale.GERMANY, "it_IT", false));
+        suite.addTest(createTest(Locale.GERMANY, "de_DE", true));
+
+        // This test creates a class loader. We don't want to grant that
+        // permission to derbyTesting.jar since that means none of the tests
+        // will notice if one of the product jars misses a privileged block
+        // around the creation of a class loader.
+        return SecurityManagerSetup.noSecurityManager(suite);
+    }
+
+    /**
+     * Create a single test case.
+     *
+     * @param loc default locale for the test case
+     * @param ui <code>derby.ui.locale</code> for the test case
+     * @param german whether output is expected to be German
+     */
+    private static Test createTest(Locale loc, String ui, boolean german) {
+        Properties prop = new Properties();
+        if (ui != null) {
+            prop.setProperty("derby.ui.locale", ui);
+        }
+        // always set the encoding so that we can reliably read the output
+        prop.setProperty("derby.ui.codeset", ENCODING);
+
+        Test test = new SysinfoLocaleTest(loc, german);
+        return new SystemPropertyTestSetup(test, prop);
+    }
+
+    /**
+     * Run a sysinfo class that is loaded in a separate class loader. A
+     * separate class loader is required in order to force sysinfo to re-read
+     * <code>derby.ui.locale</code> (happens when the class is loaded).
+     */
+    private static void runSysinfo() throws Exception {
+        final String className = "org.apache.derby.tools.sysinfo";
+        URL[] urls = {
+            Class.forName(className).getProtectionDomain().
+                    getCodeSource().getLocation()
+        };
+        URLClassLoader loader = new URLClassLoader(urls, null);
+
+        Class copy = Class.forName(className, true, loader);
+        Method main = copy.getMethod("main", new Class[] { String[].class });
+        main.invoke(null, new Object[] { new String[0] });
+    }
+
+    /**
+     * Run sysinfo and return its output as a string.
+     *
+     * @return output from sysinfo
+     */
+    private static String getSysinfoOutput() throws Exception {
+        final PrintStream savedSystemOut = System.out;
+        final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+
+        try {
+            System.setOut(new PrintStream(bytes, true, ENCODING));
+            runSysinfo();
+        } finally {
+            System.setOut(savedSystemOut);
+        }
+
+        return bytes.toString(ENCODING);
+    }
+
+    /**
+     * Some German strings that are expected to be in sysinfo's output when
+     * localized to German.
+     */
+    private static final String[] GERMAN_STRINGS = {
+        "Name des Betriebssystems",
+        "Java-Benutzerausgangsverzeichnis",
+        "Derby-Informationen",
+        "Informationen zur L\u00E4ndereinstellung",
+    };
+
+    /**
+     * Some Italian strings that are expected to be in sysinfo's output when
+     * localized to Italian.
+     */
+    private static final String[] ITALIAN_STRINGS = {
+        "Nome SO",
+        "Home utente Java",
+        "Informazioni su Derby",
+        "Informazioni sulla locale",
+    };
+
+    /**
+     * Test method which checks that the output from sysinfo is correctly
+     * localized.
+     */
+    public void testSysinfoLocale() throws Exception {
+        String output = getSysinfoOutput();
+
+        for (int i = 0; i < GERMAN_STRINGS.length; i++) {
+            assertEquals(localizedToGerman,
+                         output.indexOf(GERMAN_STRINGS[i]) != -1);
+        }
+
+        for (int i = 0; i < ITALIAN_STRINGS.length; i++) {
+            assertEquals(localizedToGerman,
+                         output.indexOf(ITALIAN_STRINGS[i]) == -1);
+        }
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/SysinfoLocaleTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/_Suite.java?rev=597610&r1=597609&r2=597610&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/_Suite.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/_Suite.java
Fri Nov 23 01:19:03 2007
@@ -52,6 +52,7 @@
         suite.addTest(ImportExportProcedureTest.suite());
         suite.addTest(ToolScripts.suite());
         suite.addTest(SysinfoCPCheckTest.suite());
+        suite.addTest(SysinfoLocaleTest.suite());
 
         // SysinfoAPITest currently fails when run against jars, so is
         // disabled. Only the first jar file on the classpath properly

Modified: db/derby/code/trunk/java/tools/org/apache/derby/iapi/tools/i18n/LocalizedResource.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/iapi/tools/i18n/LocalizedResource.java?rev=597610&r1=597609&r2=597610&view=diff
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/iapi/tools/i18n/LocalizedResource.java
(original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/iapi/tools/i18n/LocalizedResource.java
Fri Nov 23 01:19:03 2007
@@ -166,7 +166,7 @@
 			return;
 		}
 		if ( locale == null || locale.toString().equals("none") ){
-			res = ResourceBundle.getBundle(MESSAGE_FILE);
+			res = ResourceBundle.getBundle(messageFileName);
 		}
 		else
 		try {

Modified: db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/sysinfo/Main.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/sysinfo/Main.java?rev=597610&r1=597609&r2=597610&view=diff
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/sysinfo/Main.java (original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/sysinfo/Main.java Fri Nov 23
01:19:03 2007
@@ -86,6 +86,27 @@
 
 public final class Main {
 
+    /**
+     * Name of file which contains messages for sysinfo.
+     */
+    private final static String MESSAGE_FILE =
+        "org.apache.derby.loc.sysinfoMessages";
+
+    /**
+     * Resource for localizing the sysinfo messages.
+     *
+     * The default LocalizedResource reads messages from the toolsmessages
+     * bundle. Create this instance to read messages from sysinfoMessages. Use
+     * the locale and codeset specified by derby.ui.locale and derby.ui.codeset
+     * if they are set.
+     *
+     * Note that this variable must be initialized when the class is loaded in
+     * order to work correctly for the API methods that don't call
+     * <code>main()</code>.
+     */
+    private final static LocalizedResource LOCALIZED_RESOURCE =
+        new LocalizedResource(null, null, MESSAGE_FILE);
+
   /**
     Application entry point for SysInfo.   This will print out
     the Derby product information as well as a snapshot of
@@ -1086,16 +1107,9 @@
 	/*
 	** Message handling
 	*/
-	private static ResourceBundle getBundle() {
-		try {
-			return ResourceBundle.getBundle("org.apache.derby.loc.sysinfoMessages");
-		} catch (MissingResourceException mre) {
-		}
-		return null;
-	}
 
 	public static String getTextMessage(String msgId) {
-		return getCompleteMessage(msgId, (Object[]) null);
+		return getCompleteMessage(msgId, new Object[0]);
 	}
 	public static String getTextMessage(String msgId, Object a1) {
 
@@ -1114,9 +1128,7 @@
 	/**
 	 */
 	public static String getCompleteMessage(String msgId, Object[] arguments) {
-
-		// we have a base file (sysinfoMessages.properties) so don't give us a last chance.
-		return org.apache.derby.iapi.services.i18n.MessageService.formatMessage(getBundle(), msgId,
arguments, false);
+        return LOCALIZED_RESOURCE.getTextMessage(msgId, arguments);
 	}
 
     /**



Mime
View raw message