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 mod._fwl;
25 
26 import com.sun.star.beans.NamedValue;
27 import com.sun.star.container.XNameAccess;
28 import java.io.PrintWriter;
29 
30 import lib.Status;
31 import lib.StatusException;
32 import lib.TestCase;
33 import lib.TestEnvironment;
34 import lib.TestParameters;
35 import util.utils;
36 
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
40 import com.sun.star.beans.PropertyValue;
41 import com.sun.star.frame.XStorable;
42 import com.sun.star.text.XTextContent;
43 import com.sun.star.text.XTextDocument;
44 import util.SOfficeFactory;
45 
46 /**
47  * Test for object which is represented by service
48  * <code>com.sun.star.document.TypeDetection</code>. <p>
49  *
50  * Object implements the following interfaces :
51  * <ul>
52  *  <li> <code>com::sun::star::container::XContainerQuery</code></li>
53  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
54  *  <li> <code>com::sun::star::container::XNameAccess</code></li>
55  *  <li> <code>com::sun::star::container::XNameContainer</code></li>
56  *  <li> <code>com::sun::star::container::XNameReplace</code></li>
57  *  <li> <code>com::sun::star::document::XTypeDetection</code></li>
58  *  <li> <code>com::sun::star::util::XFlushable</code></li>
59  * </ul> <p>
60  *
61  * @see com.sun.star.container.XContainerQuery
62  * @see com.sun.star.container.XElementAccess
63  * @see com.sun.star.container.XNameAccess
64  * @see com.sun.star.container.XNameContainer
65  * @see com.sun.star.container.XNameReplace
66  * @see com.sun.star.document.XTypeDetection
67  * @see com.sun.star.util.XFlushable
68  * @see ifc.container._XContainerQuery
69  * @see ifc.container._XElementAccess
70  * @see ifc.container._XNameAccess
71  * @see ifc.container._XNameContainer
72  * @see ifc.container._XNameReplace
73  * @see ifc.document._XTypeDetection
74  * @see ifc.util._XFlushable
75  */
76 public class TypeDetection extends TestCase {
77 
78     /**
79     * Disposes text document.
80     */
cleanup( TestParameters tParam, PrintWriter log )81     protected void cleanup( TestParameters tParam, PrintWriter log ) {
82         log.println( "    disposing xTextDoc " );
83         util.DesktopTools.closeDoc(xTextDoc);
84     }
85 
86     XTextDocument xTextDoc = null;
87 
88     /**
89     * Creating a Testenvironment for the interfaces to be tested.
90     * Creates an instance of the service
91     * <code>com.sun.star.document.TypeDetection</code>. <p>
92     */
createTestEnvironment(TestParameters Param, PrintWriter log)93     protected TestEnvironment createTestEnvironment
94             (TestParameters Param, PrintWriter log) {
95         XInterface oObj = null;
96         Object oInterface = null ;
97 
98         try {
99             oInterface = ((XMultiServiceFactory)Param.getMSF()).createInstance
100                 ("com.sun.star.document.TypeDetection") ;
101         } catch (com.sun.star.uno.Exception e) {
102             log.println("Couldn't get service");
103             e.printStackTrace(log);
104             throw new StatusException("Couldn't get TypeDetection", e );
105         }
106 
107         if (oInterface == null) {
108             log.println("Service wasn't created") ;
109             throw new StatusException(Status.failed("Service wasn't created")) ;
110         }
111 
112         oObj = (XInterface) oInterface ;
113         log.println("ImplName: "+utils.getImplName(oObj));
114 
115         log.println( "creating a new environment for object" );
116         TestEnvironment tEnv = new TestEnvironment( oObj );
117 
118         XNameAccess xNA = (XNameAccess) UnoRuntime.queryInterface
119             (XNameAccess.class, oObj);
120         String[] elementNames = xNA.getElementNames();
121         String elementName = elementNames[0];
122         Object[] instance = null;
123         try{
124             instance = (Object[]) xNA.getByName(elementName);
125         } catch (com.sun.star.container.NoSuchElementException e){
126             throw new StatusException(
127             Status.failed("Couldn't get elements from object"));
128         } catch (com.sun.star.lang.WrappedTargetException e){
129             throw new StatusException(
130             Status.failed("Couldn't get elements from object"));
131         }
132 
133         log.println("adding INSTANCE 1 as obj relation to environment");
134 
135         setPropertyValueValue((PropertyValue[])instance, "Preferred", "INSTANCE1");
136         tEnv.addObjRelation("INSTANCE" +1, instance);
137 
138         // com.sun.star.container.XContainerQuery
139         NamedValue[] querySequenze = new NamedValue[1];
140 		NamedValue query = new NamedValue();
141 		query.Name = "Name";
142         query.Value = "writer_Text";
143         querySequenze[0] = query;
144 
145 
146         log.println("create text document with bookmarks");
147         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF() );
148         String fileURL = null;
149         try {
150             xTextDoc = SOF.createTextDoc( null );
151             XInterface xBookMark = SOF.createBookmark( xTextDoc );
152             SOF.insertTextContent( xTextDoc, (XTextContent) xBookMark );
153 
154             fileURL = utils.getOfficeTemp((XMultiServiceFactory)Param.getMSF() );
155             fileURL = fileURL + "bookmarks.oot";
156 
157             XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDoc);
158             System.out.println(fileURL);
159             store.storeToURL(fileURL, new PropertyValue[0]);
160 
161         } catch( com.sun.star.uno.Exception e ) {
162             e.printStackTrace( log );
163             throw new StatusException( "Couldn't create Bookmark", e );
164         }
165 
166         tEnv.addObjRelation("XContainerQuery.createSubSetEnumerationByProperties",
167             querySequenze);
168         tEnv.addObjRelation("XTypeDetection.bookmarkDoc", fileURL+"#bookmark");
169 
170         return tEnv;
171     } // finish method getTestEnvironment
172 
173 
setPropertyValueValue(PropertyValue[] props, String pName, Object pValue)174     protected void setPropertyValueValue(PropertyValue[] props, String pName, Object pValue) {
175         int i = 0;
176         while (i < props.length && !props[i].Name.equals(pName)) {
177             i++;
178         }
179         props[i].Value = pValue;
180     }
181 
182 }
183 
184