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