1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package complex.extensions;
28 
29 import com.sun.star.lang.XMultiServiceFactory;
30 import com.sun.star.uno.UnoRuntime;
31 
32 import com.sun.star.resource.XResourceBundle;
33 import com.sun.star.resource.XResourceBundleLoader;
34 import com.sun.star.beans.XPropertySet;
35 import com.sun.star.uno.XComponentContext;
36 import com.sun.star.lang.Locale;
37 
38 import org.junit.After;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.openoffice.test.OfficeConnection;
44 import static org.junit.Assert.*;
45 
46 public class OfficeResourceLoader
47 {
48     XResourceBundleLoader   m_loader;
49     XResourceBundle         m_bundle;
50 
51     /** Creates a new instance of ValueBinding */
52     public OfficeResourceLoader()
53     {
54     }
55 
56     /* ------------------------------------------------------------------ */
57 //    public String[] getTestMethodNames()
58 //    {
59 //        return new String[] {
60 //            "checkSimpleStringAccess",
61 //            "checkLocales"
62 //        };
63 //    }
64 
65     /* ------------------------------------------------------------------ */
66 //    public String getTestObjectName()
67 //    {
68 //        return "Extensions - OfficeResourceLoader";
69 //    }
70 
71     /* ------------------------------------------------------------------ */
72     @Before public void before() throws com.sun.star.uno.Exception, java.lang.Exception
73     {
74         XPropertySet orb = UnoRuntime.queryInterface(XPropertySet.class, getMSF());
75         XComponentContext context = UnoRuntime.queryInterface(XComponentContext.class, orb.getPropertyValue("DefaultContext"));
76 
77         m_loader = com.sun.star.resource.OfficeResourceLoader.get( context );
78     }
79 
80     /* ------------------------------------------------------------------ */
81     @After public void after() throws com.sun.star.uno.Exception, java.lang.Exception
82     {
83     }
84 
85     /* ------------------------------------------------------------------ */
86     @Test public void checkSimpleStringAccess() throws com.sun.star.uno.Exception, java.lang.Exception
87     {
88         // default bundle (UI locale)
89         m_bundle = m_loader.loadBundle_Default( "orl" );
90 
91         Locale resourceLocale = m_bundle.getLocale();
92 
93         String testString = (String)m_bundle.getByName( "string:1000" );
94 
95         if  (   resourceLocale.Language.equals( "en" )
96             &&  resourceLocale.Country.equals( "US" )
97             &&  resourceLocale.Variant.equals( "" )
98             )
99         {
100             assertTrue( "invalid 'en-US' string", testString.equals( "Dummy String" ) );
101         }
102 
103         if  (   resourceLocale.Language.equals( "de" )
104             &&  resourceLocale.Country.equals( "" )
105             &&  resourceLocale.Variant.equals( "" )
106             )
107         {
108             assertTrue( "invalid 'de' string", testString.equals( "Attrappen-Zeichenkette" ) );
109         }
110 
111         if  (   resourceLocale.Language.equals( "" )
112             &&  resourceLocale.Country.equals( "" )
113             &&  resourceLocale.Variant.equals( "" )
114             )
115         {
116             assertTrue( "invalid unlocalized string", testString.equals( "unlocalized string" ) );
117         }
118     }
119 
120     /* ------------------------------------------------------------------ */
121     @Test public void checkLocales() throws com.sun.star.uno.Exception, java.lang.Exception
122     {
123         // en-US bundle (should always be built and thus present and thus found)
124         m_bundle = m_loader.loadBundle( "orl", new Locale( "en", "US", "" ) );
125         Locale resourceLocale = m_bundle.getLocale();
126         assertTrue( "'en-US' could not be loaded",
127             resourceLocale.Language.equals( "en" ) && resourceLocale.Country.equals( "US" ) && resourceLocale.Variant.equals( "" ) );
128 
129         // some (invalid) locale which is usually no built, and should thus fallback to en-US
130         m_bundle = m_loader.loadBundle( "orl", new Locale( "inv", "al", "id" ) );
131         resourceLocale = m_bundle.getLocale();
132         assertTrue( "non-existing locale request does not fallback to en-US",
133             resourceLocale.Language.equals( "en" ) && resourceLocale.Country.equals( "US" ) && resourceLocale.Variant.equals( "" ) );
134     }
135 
136 
137     private XMultiServiceFactory getMSF()
138     {
139         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
140         return xMSF1;
141     }
142 
143     // setup and close connections
144     @BeforeClass public static void setUpConnection() throws Exception {
145         System.out.println("setUpConnection()");
146         connection.setUp();
147     }
148 
149     @AfterClass public static void tearDownConnection()
150         throws InterruptedException, com.sun.star.uno.Exception
151     {
152         System.out.println("tearDownConnection()");
153         connection.tearDown();
154     }
155 
156     private static final OfficeConnection connection = new OfficeConnection();
157 }
158