xref: /AOO41X/main/framework/qa/complex/ModuleManager/CheckXModuleManager.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 
28 package complex.ModuleManager;
29 
30 import com.sun.star.beans.*;
31 import com.sun.star.frame.*;
32 import com.sun.star.lang.*;
33 import com.sun.star.uno.AnyConverter;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.util.*;
36 import com.sun.star.container.*;
37 
38 
39 
40 // ---------- junit imports -----------------
41 import org.junit.After;
42 import org.junit.AfterClass;
43 import org.junit.Before;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.openoffice.test.OfficeConnection;
47 import static org.junit.Assert.*;
48 // ------------------------------------------
49 
50 //-----------------------------------------------
51 /** @short  todo document me
52  */
53 public class CheckXModuleManager
54 {
55     //-------------------------------------------
56     // some const
57 
58     //-------------------------------------------
59     // member
60 
61     /** points to the global uno service manager. */
62     private XMultiServiceFactory m_xSmgr = null;
63 
64     /** the module manager for testing. */
65     private XModuleManager m_xMM = null;
66 
67     /** a special frame used to load documents there. */
68     private XComponentLoader m_xLoader = null;
69 
70     //-------------------------------------------
71     // test environment
72 
73     //-------------------------------------------
74     /** @short  A function to tell the framework,
75                 which test functions are available.
76 
77         @return All test methods.
78         @todo   Think about selection of tests from outside ...
79      */
80 //    public String[] getTestMethodNames()
81 //    {
82 //        return new String[]
83 //        {
84 //            "checkModuleIdentification"        ,
85 //            "checkModuleConfigurationReadable" ,
86 //            "checkModuleConfigurationWriteable",
87 //            "checkModuleConfigurationQueries"
88 //        };
89 //    }
90 
91     //-------------------------------------------
92     /** @short  Create the environment for following tests.
93 
94         @descr  Use either a component loader from desktop or
95                 from frame
96      */
97     @Before public void before()
98         throws java.lang.Exception
99     {
100         // get uno service manager from global test environment
101         m_xSmgr = getMSF();
102 
103         // create module manager
104         m_xMM = UnoRuntime.queryInterface(XModuleManager.class, m_xSmgr.createInstance("com.sun.star.frame.ModuleManager"));
105 
106         // create desktop instance to create a special frame to load documents there.
107         XFrame xDesktop = UnoRuntime.queryInterface(XFrame.class, m_xSmgr.createInstance("com.sun.star.frame.Desktop"));
108 
109         m_xLoader = UnoRuntime.queryInterface(XComponentLoader.class, xDesktop.findFrame("_blank", 0));
110     }
111 
112     //-------------------------------------------
113     /** @short  close the environment.
114      */
115     @After public void after()
116         throws java.lang.Exception
117     {
118         XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, m_xLoader);
119         xClose.close(false);
120 
121         m_xLoader = null;
122         m_xMM     = null;
123         m_xSmgr   = null;
124     }
125 
126     //-------------------------------------------
127     /** @todo document me
128      */
129     @Test public void checkModuleIdentification()
130         throws java.lang.Exception
131     {
132         impl_identifyModulesBasedOnDocs("com.sun.star.text.TextDocument"                );
133         impl_identifyModulesBasedOnDocs("com.sun.star.text.WebDocument"                 );
134         impl_identifyModulesBasedOnDocs("com.sun.star.text.GlobalDocument"              );
135         impl_identifyModulesBasedOnDocs("com.sun.star.formula.FormulaProperties"        );
136         impl_identifyModulesBasedOnDocs("com.sun.star.sheet.SpreadsheetDocument"        );
137         impl_identifyModulesBasedOnDocs("com.sun.star.drawing.DrawingDocument"          );
138         impl_identifyModulesBasedOnDocs("com.sun.star.presentation.PresentationDocument");
139         impl_identifyModulesBasedOnDocs("com.sun.star.sdb.OfficeDatabaseDocument"       );
140         // TODO: fails
141         // impl_identifyModulesBasedOnDocs("com.sun.star.chart.ChartDocument"              );
142     }
143 
144     //-------------------------------------------
145     /** @todo document me
146      */
147     @Test public void checkModuleConfigurationReadable()
148         throws java.lang.Exception
149     {
150     }
151 
152     //-------------------------------------------
153     /** @todo document me
154      */
155     @Test public void checkModuleConfigurationWriteable()
156         throws java.lang.Exception
157     {
158         // modules supporting real documents
159         impl_checkReadOnlyPropsOfModule("com.sun.star.text.TextDocument"                );
160         impl_checkReadOnlyPropsOfModule("com.sun.star.text.WebDocument"                 );
161         impl_checkReadOnlyPropsOfModule("com.sun.star.text.GlobalDocument"              );
162         impl_checkReadOnlyPropsOfModule("com.sun.star.formula.FormulaProperties"        );
163         impl_checkReadOnlyPropsOfModule("com.sun.star.sheet.SpreadsheetDocument"        );
164         impl_checkReadOnlyPropsOfModule("com.sun.star.drawing.DrawingDocument"          );
165         impl_checkReadOnlyPropsOfModule("com.sun.star.presentation.PresentationDocument");
166         impl_checkReadOnlyPropsOfModule("com.sun.star.sdb.OfficeDatabaseDocument"       );
167         impl_checkReadOnlyPropsOfModule("com.sun.star.chart.ChartDocument"              );
168 
169         // other modules
170         impl_checkReadOnlyPropsOfModule("com.sun.star.sdb.FormDesign"       );
171         impl_checkReadOnlyPropsOfModule("com.sun.star.sdb.TextReportDesign" );
172         impl_checkReadOnlyPropsOfModule("com.sun.star.sdb.RelationDesign"   );
173         impl_checkReadOnlyPropsOfModule("com.sun.star.sdb.QueryDesign"      );
174         impl_checkReadOnlyPropsOfModule("com.sun.star.sdb.TableDesign"      );
175         impl_checkReadOnlyPropsOfModule("com.sun.star.sdb.DataSourceBrowser");
176         impl_checkReadOnlyPropsOfModule("com.sun.star.frame.Bibliography"   );
177         impl_checkReadOnlyPropsOfModule("com.sun.star.script.BasicIDE"      );
178         impl_checkReadOnlyPropsOfModule("com.sun.star.frame.StartModule"    );
179     }
180 
181     //-------------------------------------------
182     /** @todo document me
183      */
184     @Test public void checkModuleConfigurationQueries()
185         throws java.lang.Exception
186     {
187         impl_searchModulesByDocumentService("com.sun.star.text.TextDocument"                );
188         impl_searchModulesByDocumentService("com.sun.star.text.WebDocument"                 );
189         impl_searchModulesByDocumentService("com.sun.star.text.GlobalDocument"              );
190         impl_searchModulesByDocumentService("com.sun.star.formula.FormulaProperties"        );
191         impl_searchModulesByDocumentService("com.sun.star.sheet.SpreadsheetDocument"        );
192         impl_searchModulesByDocumentService("com.sun.star.drawing.DrawingDocument"          );
193         impl_searchModulesByDocumentService("com.sun.star.presentation.PresentationDocument");
194         impl_searchModulesByDocumentService("com.sun.star.sdb.OfficeDatabaseDocument"       );
195         impl_searchModulesByDocumentService("com.sun.star.chart.ChartDocument"              );
196     }
197 
198     //-------------------------------------------
199     /** @todo document me
200      */
201     private void impl_searchModulesByDocumentService(String sDocumentService)
202         throws java.lang.Exception
203     {
204         System.out.println("search modules matching document service '"+sDocumentService+"' ...");
205 
206         NamedValue[] lProps          = new NamedValue[1];
207                      lProps[0]       = new NamedValue();
208                      lProps[0].Name  = "ooSetupFactoryDocumentService";
209                      lProps[0].Value = sDocumentService;
210 
211         XContainerQuery xMM     = UnoRuntime.queryInterface(XContainerQuery.class, m_xMM);
212         XEnumeration    xResult = xMM.createSubSetEnumerationByProperties(lProps);
213         while(xResult.hasMoreElements())
214         {
215             PropertyValue[] lModuleProps      = (PropertyValue[])AnyConverter.toArray(xResult.nextElement());
216             int             c                 = lModuleProps.length;
217             int             i                 = 0;
218             String          sFoundModule      = "";
219             String          sFoundDocService  = "";
220             for (i=0; i<c; ++i)
221             {
222                 if (lModuleProps[i].Name.equals("ooSetupFactoryModuleIdentifier"))
223                 {
224                     sFoundModule = AnyConverter.toString(lModuleProps[i].Value);
225                 }
226                 if (lModuleProps[i].Name.equals("ooSetupFactoryDocumentService"))
227                 {
228                     sFoundDocService = AnyConverter.toString(lModuleProps[i].Value);
229                 }
230             }
231 
232             if (sFoundModule.length() < 1)
233             {
234                 fail("Miss module identifier in result set. Returned data are incomplete.");
235             }
236 
237             if ( ! sFoundDocService.equals(sDocumentService))
238             {
239                 fail("Query returned wrong module '" + sFoundModule + "' with DocumentService='" + sFoundDocService + "'.");
240             }
241 
242             System.out.println("Found module '"+sFoundModule+"'.");
243         }
244     }
245 
246     //-------------------------------------------
247     /** @todo document me
248      */
249     private void impl_identifyModulesBasedOnDocs(String sModule)
250         throws java.lang.Exception
251     {
252         System.out.println("check identification of module '"+sModule+"' ...");
253 
254         XNameAccess     xMM          = UnoRuntime.queryInterface(XNameAccess.class, m_xMM);
255         PropertyValue[] lModuleProps = (PropertyValue[])AnyConverter.toArray(xMM.getByName(sModule));
256         int             c            = lModuleProps.length;
257         int             i            = 0;
258         String          sFactoryURL  = "";
259 
260         for (i=0; i<c; ++i)
261         {
262             if (lModuleProps[i].Name.equals("ooSetupFactoryEmptyDocumentURL"))
263             {
264                 sFactoryURL = AnyConverter.toString(lModuleProps[i].Value);
265                 break;
266             }
267         }
268 
269         PropertyValue[] lArgs             = new PropertyValue[1];
270                         lArgs[0]          = new PropertyValue();
271                         lArgs[0].Name     = "Hidden";
272                         lArgs[0].Value    = Boolean.TRUE;
273 
274         XComponent      xModel            = m_xLoader.loadComponentFromURL(sFactoryURL, "_self", 0, lArgs);
275         XFrame          xFrame            = UnoRuntime.queryInterface(XFrame.class, m_xLoader);
276         XController     xController       = xFrame.getController();
277 
278         String          sModuleFrame      = m_xMM.identify(xFrame     );
279         String          sModuleController = m_xMM.identify(xController);
280         String          sModuleModel      = m_xMM.identify(xModel     );
281 
282         if ( ! sModuleFrame.equals(sModule))
283         {
284             fail("Identification of module '" + sModule + "' failed if frame was used as entry point.");
285         }
286         if ( ! sModuleController.equals(sModule))
287         {
288             fail("Identification of module '" + sModule + "' failed if controller was used as entry point.");
289         }
290         if ( ! sModuleModel.equals(sModule))
291         {
292             fail("Identification of module '" + sModule + "' failed if model was used as entry point.");
293         }
294     }
295 
296     //-------------------------------------------
297     /** @todo document me
298      */
299     private void impl_checkReadOnlyPropsOfModule(String sModule)
300         throws java.lang.Exception
301     {
302         XNameReplace xWrite = UnoRuntime.queryInterface(XNameReplace.class, m_xMM);
303 
304         impl_checkReadOnlyPropOfModule(xWrite, sModule, "ooSetupFactoryDocumentService"     , "test");
305         impl_checkReadOnlyPropOfModule(xWrite, sModule, "ooSetupFactoryActualFilter"        , "test");
306         impl_checkReadOnlyPropOfModule(xWrite, sModule, "ooSetupFactoryActualTemplateFilter", "test");
307         impl_checkReadOnlyPropOfModule(xWrite, sModule, "ooSetupFactoryEmptyDocumentURL"    , "test");
308     }
309 
310     //-------------------------------------------
311     /** @todo document me
312      */
313     private void impl_checkReadOnlyPropOfModule(XNameReplace xMM        ,
314                                                 String       sModule    ,
315                                                 String       sPropName  ,
316                                                 Object       aPropValue )
317         throws java.lang.Exception
318     {
319         PropertyValue[] lChanges          = new PropertyValue[1];
320                         lChanges[0]       = new PropertyValue();
321                         lChanges[0].Name  = sPropName;
322                         lChanges[0].Value = aPropValue;
323 
324         // Note: Exception is expected !
325         System.out.println("check readonly state of module '"+sModule+"' for property '"+sPropName+"' ...");
326         try
327         {
328             xMM.replaceByName(sModule, lChanges);
329             fail("Was able to write READONLY property '"+sPropName+"' of module '"+sModule+"' configuration.");
330         }
331         catch(Throwable ex)
332             {}
333     }
334 
335 
336 
337     private XMultiServiceFactory getMSF()
338     {
339         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
340         return xMSF1;
341     }
342 
343     // setup and close connections
344     @BeforeClass public static void setUpConnection() throws Exception {
345         System.out.println("setUpConnection()");
346         connection.setUp();
347     }
348 
349     @AfterClass public static void tearDownConnection()
350         throws InterruptedException, com.sun.star.uno.Exception
351     {
352         System.out.println("tearDownConnection()");
353         connection.tearDown();
354     }
355 
356     private static final OfficeConnection connection = new OfficeConnection();
357 
358 }
359