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