1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package complex.extensions;
24 
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.uno.UnoRuntime;
27 
28 import com.sun.star.resource.XResourceBundle;
29 import com.sun.star.resource.XResourceBundleLoader;
30 import com.sun.star.beans.XPropertySet;
31 import com.sun.star.uno.XComponentContext;
32 import com.sun.star.lang.Locale;
33 
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.openoffice.test.OfficeConnection;
40 import static org.junit.Assert.*;
41 
42 public class OfficeResourceLoader
43 {
44     XResourceBundleLoader   m_loader;
45     XResourceBundle         m_bundle;
46 
47     /** Creates a new instance of ValueBinding */
OfficeResourceLoader()48     public OfficeResourceLoader()
49     {
50     }
51 
52     /* ------------------------------------------------------------------ */
53 //    public String[] getTestMethodNames()
54 //    {
55 //        return new String[] {
56 //            "checkSimpleStringAccess",
57 //            "checkLocales"
58 //        };
59 //    }
60 
61     /* ------------------------------------------------------------------ */
62 //    public String getTestObjectName()
63 //    {
64 //        return "Extensions - OfficeResourceLoader";
65 //    }
66 
67     /* ------------------------------------------------------------------ */
before()68     @Before public void before() throws com.sun.star.uno.Exception, java.lang.Exception
69     {
70         XPropertySet orb = UnoRuntime.queryInterface(XPropertySet.class, getMSF());
71         XComponentContext context = UnoRuntime.queryInterface(XComponentContext.class, orb.getPropertyValue("DefaultContext"));
72 
73         m_loader = com.sun.star.resource.OfficeResourceLoader.get( context );
74     }
75 
76     /* ------------------------------------------------------------------ */
after()77     @After public void after() throws com.sun.star.uno.Exception, java.lang.Exception
78     {
79     }
80 
81     /* ------------------------------------------------------------------ */
checkSimpleStringAccess()82     @Test public void checkSimpleStringAccess() throws com.sun.star.uno.Exception, java.lang.Exception
83     {
84         // default bundle (UI locale)
85         m_bundle = m_loader.loadBundle_Default( "orl" );
86 
87         Locale resourceLocale = m_bundle.getLocale();
88 
89         String testString = (String)m_bundle.getByName( "string:1000" );
90 
91         if  (   resourceLocale.Language.equals( "en" )
92             &&  resourceLocale.Country.equals( "US" )
93             &&  resourceLocale.Variant.equals( "" )
94             )
95         {
96             assertTrue( "invalid 'en-US' string", testString.equals( "Dummy String" ) );
97         }
98 
99         if  (   resourceLocale.Language.equals( "de" )
100             &&  resourceLocale.Country.equals( "" )
101             &&  resourceLocale.Variant.equals( "" )
102             )
103         {
104             assertTrue( "invalid 'de' string", testString.equals( "Attrappen-Zeichenkette" ) );
105         }
106 
107         if  (   resourceLocale.Language.equals( "" )
108             &&  resourceLocale.Country.equals( "" )
109             &&  resourceLocale.Variant.equals( "" )
110             )
111         {
112             assertTrue( "invalid unlocalized string", testString.equals( "unlocalized string" ) );
113         }
114     }
115 
116     /* ------------------------------------------------------------------ */
checkLocales()117     @Test public void checkLocales() throws com.sun.star.uno.Exception, java.lang.Exception
118     {
119         // en-US bundle (should always be built and thus present and thus found)
120         m_bundle = m_loader.loadBundle( "orl", new Locale( "en", "US", "" ) );
121         Locale resourceLocale = m_bundle.getLocale();
122         assertTrue( "'en-US' could not be loaded",
123             resourceLocale.Language.equals( "en" ) && resourceLocale.Country.equals( "US" ) && resourceLocale.Variant.equals( "" ) );
124 
125         // some (invalid) locale which is usually no built, and should thus fallback to en-US
126         m_bundle = m_loader.loadBundle( "orl", new Locale( "inv", "al", "id" ) );
127         resourceLocale = m_bundle.getLocale();
128         assertTrue( "non-existing locale request does not fallback to en-US",
129             resourceLocale.Language.equals( "en" ) && resourceLocale.Country.equals( "US" ) && resourceLocale.Variant.equals( "" ) );
130     }
131 
132 
getMSF()133     private XMultiServiceFactory getMSF()
134     {
135         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
136         return xMSF1;
137     }
138 
139     // setup and close connections
setUpConnection()140     @BeforeClass public static void setUpConnection() throws Exception {
141         System.out.println("setUpConnection()");
142         connection.setUp();
143     }
144 
tearDownConnection()145     @AfterClass public static void tearDownConnection()
146         throws InterruptedException, com.sun.star.uno.Exception
147     {
148         System.out.println("tearDownConnection()");
149         connection.tearDown();
150     }
151 
152     private static final OfficeConnection connection = new OfficeConnection();
153 }
154