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 ifc.frame;
25 
26 import lib.MultiMethodTest;
27 import util.utils;
28 
29 import com.sun.star.beans.Property;
30 import com.sun.star.frame.XDocumentTemplates;
31 import com.sun.star.frame.XStorable;
32 import com.sun.star.sdbc.XResultSet;
33 import com.sun.star.sdbc.XRow;
34 import com.sun.star.ucb.Command;
35 import com.sun.star.ucb.OpenCommandArgument2;
36 import com.sun.star.ucb.XCommandProcessor;
37 import com.sun.star.ucb.XContent;
38 import com.sun.star.ucb.XContentAccess;
39 import com.sun.star.ucb.XDynamicResultSet;
40 import com.sun.star.uno.AnyConverter;
41 import com.sun.star.uno.Type;
42 import com.sun.star.uno.UnoRuntime;
43 /**
44 * Testing <code>com.sun.star.frame.XDesktop</code>
45 * interface methods:
46 * <ul>
47 *  <li><code> getContent() </code></li>
48 *  <li><code> storeTemplate() </code></li>
49 *  <li><code> addTemplate() </code></li>
50 *  <li><code> removeTemplate() </code></li>
51 *  <li><code> renameTemplate() </code></li>
52 *  <li><code> addGroup() </code></li>
53 *  <li><code> removeGroup() </code></li>
54 *  <li><code> renameGroup() </code></li>
55 *  <li><code> update() </code></li>
56 * </ul><p>
57 */
58 public class _XDocumentTemplates extends MultiMethodTest {
59     public XDocumentTemplates oObj = null; // oObj filled by MultiMethodTest
60     protected XContent content = null;
61     /**
62     * Test calls the method and prints contents list to log.<p>
63     * Has <b> OK </b> status if the XContent isn't empty.
64     */
65     public void _getContent() {
66         content = oObj.getContent();
67         log.println("Content list:" + getContentList(content));
68         tRes.tested("getContent()", content != null);
69     }
70 
71     /**
72     * Test calls the method and checks that new group was added. <p>
73     * Has <b> OK </b> status if method returns true and new group was added.
74     */
75     public void _addGroup() {
76         requiredMethod("getContent()");
77         if (getSubContent(content, "XDocumentTemplatesTemp") != null ) {
78             oObj.removeGroup("XDocumentTemplatesTemp");
79         }
80         if (getSubContent(content, "XDocumentTemplates") != null ) {
81             oObj.removeGroup("XDocumentTemplates");
82         }
83         boolean res  = oObj.addGroup("XDocumentTemplatesTemp");
84         log.println("Method returned: " + res);
85         res &= getSubContent(content, "XDocumentTemplatesTemp") != null;
86         tRes.tested("addGroup()", res);
87     }
88 
89     XContent groupContent = null;
90     /**
91     * Test calls the method and checks that content has no group with old name
92     * and that content has group with new name. <p>
93     * Has <b> OK </b> status if method returns true, content has no group with
94     * old name and content has group with new name.<p>
95     */
96     public void _renameGroup() {
97         requiredMethod("addGroup()");
98         boolean res = oObj.renameGroup("XDocumentTemplatesTemp",
99             "XDocumentTemplates");
100         log.println("Method returned: " + res);
101         groupContent = getSubContent(content, "XDocumentTemplates");
102         res &= getSubContent(content, "XDocumentTemplatesTemp") == null;
103         res &= groupContent != null;
104         tRes.tested("renameGroup()", res);
105     }
106 
107     /**
108     * Test calls the method and checks that group content has new template. <p>
109     * Has <b> OK </b> status if method returns true and group content has new
110     * template.<p>
111     */
112     public void _addTemplate() {
113         requiredMethod("renameGroup()");
114         String testDoc = utils.getFullTestURL("report.stw");
115         log.println("Adding template from " + testDoc);
116         boolean res = oObj.addTemplate("XDocumentTemplates",
117             "ANewTemplateTemp",testDoc);
118         log.println("Method returned: " + res);
119         res &= getSubContent(groupContent, "ANewTemplateTemp") != null;
120         tRes.tested("addTemplate()", res);
121     }
122 
123     /**
124     * Test calls the method and checks that group content has no template with
125     * old name and that group content has template with new name. <p>
126     * Has <b> OK </b> status if method returns true, group content has no
127     * template with old name and group content has template with new name.<p>
128     */
129     public void _renameTemplate() {
130         requiredMethod("addTemplate()");
131         boolean res = oObj.renameTemplate("XDocumentTemplates",
132                                           "ANewTemplateTemp",
133                                           "ANewTemplate");
134         log.println("Method returned: " + res);
135         res &= getSubContent(groupContent, "ANewTemplateTemp") == null;
136         res &= getSubContent(groupContent, "ANewTemplate") != null;
137 
138         tRes.tested("renameTemplate()", res);
139     }
140 
141     /**
142     * Test calls the method and checks that group content has new template. <p>
143     * Has <b> OK </b> status if method returns true and new template was created.<p>
144     */
145     public void _storeTemplate() {
146         requiredMethod("renameGroup()");
147         XStorable store = (XStorable) tEnv.getObjRelation("Store");
148         boolean res = oObj.storeTemplate("XDocumentTemplates",
149                                          "NewStoreTemplate",
150                                          store);
151         log.println("Method returned: " + res);
152         res &= getSubContent(groupContent, "NewStoreTemplate") != null;
153         tRes.tested("storeTemplate()", res);
154     }
155 
156     /**
157     * Test calls the method and checks that group content has no deleted template. <p>
158     * Has <b> OK </b> status if method returns true and group content has no
159     * deleted template.<p>
160     */
161     public void _removeTemplate() {
162         requiredMethod("renameTemplate()");
163         boolean res = oObj.removeTemplate("XDocumentTemplates", "ANewTemplate");
164         log.println("Method returned: " + res);
165         res &= getSubContent(groupContent, "ANewTemplate") == null;
166         tRes.tested("removeTemplate()", res);
167     }
168 
169     /**
170     * Test calls the method and checks that content has no deleted group. <p>
171     * Has <b> OK </b> status if method returns true and content has no deleted
172     * group.<p>
173     */
174     public void _removeGroup() {
175         requiredMethod("renameGroup()");
176         executeMethod("renameTemplate()");
177         boolean res = oObj.removeGroup("XDocumentTemplates");
178         log.println("Method returned: " + res);
179         res &= getSubContent(content, "XDocumentTemplates") == null;
180         tRes.tested("removeGroup()", res);
181     }
182 
183     /**
184     * Test calls the method. <p>
185     * Has <b> OK </b> status if no exception occurs.<p>
186     */
187     public void _update() {
188         oObj.update();
189         tRes.tested("update()",true);
190     }
191 
192     /**
193      * Returns the string representation of content passed as parameter.
194      */
195     protected String getContentList(XContent content) {
196         XResultSet statRes = getStatResultSet(content);
197         String ret = "";
198         try {
199             statRes.first();
200             XRow row = (XRow)UnoRuntime.queryInterface(XRow.class, statRes);
201             while(! statRes.isAfterLast()) {
202                 ret += "\n    " + row.getString(1);
203                 statRes.next();
204             }
205         } catch (com.sun.star.sdbc.SQLException e) {
206             log.println("Exception occured:" + e);
207         }
208 
209         return ret;
210     }
211 
212     protected XResultSet getStatResultSet(XContent content) {
213         XResultSet statResSet = null;
214         try {
215             statResSet = getDynaResultSet(content).getStaticResultSet();
216         } catch(com.sun.star.ucb.ListenerAlreadySetException e) {
217             log.println("Exception occured:" + e);
218         }
219         return statResSet;
220     }
221 
222     protected XDynamicResultSet getDynaResultSet(XContent content) {
223         Command command = new Command();
224         OpenCommandArgument2 comArg = new OpenCommandArgument2();
225         Property[] comProps = new Property[1];
226         comArg.Mode = com.sun.star.ucb.OpenMode.ALL;
227         comProps[0] = new Property();
228         comProps[0].Name = "Title";
229         comArg.Properties = comProps;
230 
231         command.Name = "open";
232         command.Handle = -1;
233         command.Argument = comArg;
234 
235         XCommandProcessor comProc = (XCommandProcessor)
236             UnoRuntime.queryInterface(XCommandProcessor.class, content);
237 
238         XDynamicResultSet DynResSet = null;
239         try {
240             DynResSet = (XDynamicResultSet) AnyConverter.toObject(
241                 new Type(XDynamicResultSet.class),comProc.execute(command, 0, null));
242         } catch(com.sun.star.ucb.CommandAbortedException e) {
243             log.println("Couldn't execute command:" + e);
244         } catch(com.sun.star.uno.Exception e) {
245             log.println("Couldn't execute command:" + e);
246         }
247 
248         return DynResSet;
249     }
250 
251     protected XContent getSubContent(XContent content, String subName) {
252         XResultSet statRes = getStatResultSet(content);
253         XRow row = (XRow)UnoRuntime.queryInterface(XRow.class, statRes);
254         XContentAccess contAcc = (XContentAccess)
255             UnoRuntime.queryInterface(XContentAccess.class, statRes);
256         XContent subContent = null;
257         try {
258             statRes.first();
259             while(!statRes.isAfterLast()) {
260                 if ( subName.equals(row.getString(1)) ) {
261                     subContent = contAcc.queryContent();
262                 }
263                 statRes.next();
264             }
265         } catch(com.sun.star.sdbc.SQLException e) {
266             log.println("Exception occured:" + e);
267         }
268 
269         return subContent;
270     }
271 }
272 
273