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  * TypeDetection6FileFormat.java
24  *
25  * Created on 26. April 2004, 10:37
26  */
27 package complex.filter.misc;
28 
29 import com.sun.star.container.XNameAccess;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.uno.XInterface;
33 
34 import util.utils;
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  *
48  * @author  cn93815
49  */
50 public class TypeDetection6FileFormat
51 {
52 
53     static XMultiServiceFactory xMSF;
54 
55     /**
56      * A function to tell the framework, which test functions are available.
57      * @return All test methods.
58      */
59 //    public String[] getTestMethodNames() {
60 //        return new String[]{"checkFilterFactory",
61 //                            "checkTypeDetection"};
62 //    }
63     /** Create the environment for following tests.
64      * Use either a component loader from desktop or
65      * from frame
66      * @throws Exception Exception
67      */
before()68     @Before public void before() throws Exception
69     {
70 
71         xMSF = getMSF();
72         assertNotNull("Could not get XMultiServiceFactory", xMSF);
73 
74     }
75 
76     /**
77      * call the function <CODE>checkFileFormatSupport</CODE> to test <CODE>com.sun.star.document.FilterFactory</CODE>
78      * @see com.sun.star.document.FilterFactory
79      */
checkFilterFactory()80     @Test public void checkFilterFactory()
81     {
82         checkFileFormatSupport("com.sun.star.document.FilterFactory");
83     }
84 
85     /**
86      * call the function <CODE>checkFileFormatSupport</CODE> to test <CODE>com.sun.star.document.TypeDetection</CODE>
87      * @see com.sun.star.document.TypeDetection
88      */
checkTypeDetection()89     @Test public void checkTypeDetection()
90     {
91         checkFileFormatSupport("com.sun.star.document.TypeDetection");
92 
93     }
94 
95     /**
96      * test the given service <CODE>serviceName</CODE>.
97      * The serve was created and the filter 'TypeDetection6FileFormat' was searched
98      * @param serviceName the name of the service to test
99      */
checkFileFormatSupport(String serviceName)100     private void checkFileFormatSupport(String serviceName)
101     {
102         System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
103         System.out.println("testing service '" + serviceName + "'");
104 
105         XInterface oObj = null;
106         try
107         {
108             oObj = getTestObject(serviceName);
109             System.out.println("ImplName: " + utils.getImplName(oObj));
110         }
111         catch (java.lang.Exception e)
112         {
113             fail("could not get test object");
114         }
115         XNameAccess xNA = UnoRuntime.queryInterface(XNameAccess.class, oObj);
116         String msg = "Could not find filter 'TypeDetection6FileFormat'!";
117         msg += "\nMaybe 'TypeDetection6FileFormat.xcu' is not registered.";
118         assertTrue(msg, xNA.hasByName("TypeDetection6FileFormat"));
119 
120 
121     }
122 
123     /**
124      * Creates an instance for the given <CODE>serviceName</CODE>
125      * @param serviceName the name of the service which should be created
126      * @throws Exception was thrown if creataion failes
127      * @return <CODE>XInterface</CODE> of service
128      */
getTestObject(String serviceName)129     public XInterface getTestObject(String serviceName) throws Exception
130     {
131 
132         Object oInterface = xMSF.createInstance(serviceName);
133 
134         if (oInterface == null)
135         {
136             fail("Service wasn't created");
137             throw new Exception("could not create service '" + serviceName + "'");
138         }
139         return (XInterface) oInterface;
140     }
141 
getMSF()142         private XMultiServiceFactory getMSF()
143     {
144         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
145         return xMSF1;
146     }
147 
148     // setup and close connections
setUpConnection()149     @BeforeClass public static void setUpConnection() throws Exception {
150         System.out.println("setUpConnection()");
151         connection.setUp();
152     }
153 
tearDownConnection()154     @AfterClass public static void tearDownConnection()
155         throws InterruptedException, com.sun.star.uno.Exception
156     {
157         System.out.println("tearDownConnection()");
158         connection.tearDown();
159     }
160 
161     private static final OfficeConnection connection = new OfficeConnection();
162 
163 }
164