xref: /AOO41X/main/qadevOOo/tests/java/ifc/frame/_XStorable.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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.PropertyValue;
34 import com.sun.star.frame.XStorable;
35 import com.sun.star.io.IOException;
36 import com.sun.star.lang.XMultiServiceFactory;
37 
38 /**
39 * Testing <code>com.sun.star.frame.XStorable</code>
40 * interface methods:
41 * <ul>
42 *  <li><code> getLocation() </code></li>
43 *  <li><code> hasLocation() </code></li>
44 *  <li><code> isReadonly() </code></li>
45 *  <li><code> storeAsURL() </code></li>
46 *  <li><code> storeToURL() </code></li>
47 *  <li><code> store() </code></li>
48 * </ul><p>
49 * Test is <b> NOT </b> multithread compilant. <p>
50 * @see com.sun.star.frame.XStorable
51 */
52 public class _XStorable extends MultiMethodTest {
53     public XStorable oObj = null; // oObj filled by MultiMethodTest
54     String storeUrl;
55     boolean stored;
56 
57     /**
58     * Test calls the method. <p>
59     * Has <b> OK </b> status in three cases:
60     * <ol>
61     *  <li>An object has location stored in. Then if method does not return
62     *  null, it has <b> OK </b> status</li>
63     *  <li>An object has no location stored in. Then method storeAsURL() is
64     *  called, and if url is not null and equals to a url where component
65     *  was stored, method has <b> OK </b> status</li>
66     *  <li>An object has no location stored in. Then method storeAsURL() is
67     *  called, and if url is null and method returns null too, method
68     *  has <b> OK </b> status </li>
69     * </ol><p>
70     * The following method tests are to be completed successfully before :
71     * <ul>
72     *  <li> <code> storeAsURL() </code> : stores the object's persistent data
73     *  to a URL and lets the object become the representation of this new
74     *  URL</li>
75     * </ul>
76     */
77     public void _getLocation() {
78         if (oObj.hasLocation()) {
79             // if it has location it should know it
80             tRes.tested("getLocation()", oObj.getLocation() != null);
81         } else {
82             // else try to obtain location
83             requiredMethod("storeAsURL()");
84             if (storeUrl != null) {
85                 // if stored succesfully - check location
86                 log.println(oObj.getLocation() + "--" + storeUrl);
87                 tRes.tested("getLocation()",
88                     storeUrl.equals(oObj.getLocation()));
89             } else {
90                 // if not - it should not have a location
91                 tRes.tested("getLocation()", oObj.getLocation() == null);
92             }
93         }
94     }
95 
96     /**
97     * Test calls the method, then result is checked. <p>
98     * Has <b> OK </b> status if stored url is not null and method does not
99     * return null or if stored url is null and the method returns null.<p>
100     * The following method tests are to be completed successfully before :
101     * <ul>
102     *  <li> <code> storeAsURL() </code>: stores the object's persistent data
103     *  to a URL and lets the object become the representation of this new
104     *  URL</li>
105     * </ul>
106     */
107     public void _hasLocation() {
108         requiredMethod("storeAsURL()");
109         if (storeUrl != null) {
110             // if stored succesfully - it should have a location
111             tRes.tested("hasLocation()", oObj.hasLocation());
112         } else {
113             // if not - it should not
114             tRes.tested("hasLocation()", !oObj.hasLocation());
115         }
116     }
117 
118     /**
119     * Test calls the method. <p>
120     * Has <b> OK </b> status if value, returned by the method is not equal to
121     * 'stored' variable. ( If it's readonly it should not have been stored. )
122     * <p>
123     * The following method tests are to be completed successfully before :
124     * <ul>
125     *  <li> <code> store() </code> : stores data to the URL from which it
126     *  was loaded </li>
127     * </ul>
128     */
129     public void _isReadonly() {
130         requiredMethod("store()");
131         tRes.tested("isReadonly()", oObj.isReadonly() != stored);
132     }
133 
134     /**
135     * Object is stored into temporary directory. <p>
136     * Has <b> OK </b> status if the method successfully returns
137     * and no exceptions were thrown.
138     */
139     public void _storeAsURL() {
140         // getting an url to store
141         String url = (String) utils.getOfficeTemp(
142                                 (XMultiServiceFactory)tParam.getMSF());
143 
144         if (url != null) {
145             url += "xstorable.store.as.test";
146             log.println("store as '" + url + "'");
147             try {
148                 oObj.storeAsURL(url, new PropertyValue[0]);
149                 storeUrl = url;
150                 tRes.tested("storeAsURL()", true);
151             } catch (IOException e) {
152                 log.println("Couldn't store as "+url+" : "+e.getMessage());
153                 e.printStackTrace(log);
154                 storeUrl = null;
155                 tRes.tested("storeAsURL()", false);
156             }
157         } else {
158             log.println("an url to store is not found");
159         }
160     }
161 
162     /**
163     * Object is stored into temporary directory. <p>
164     * Has <b> OK </b> status if the method successfully returns
165     * and no exceptions were thrown.
166     */
167     public void _storeToURL() {
168         // getting an url to store
169         String url = (String) utils.getOfficeTemp(
170                                 (XMultiServiceFactory)tParam.getMSF());
171 
172         if (url != null) {
173             url += "xstorable.store.as.test";
174             log.println("store to '" + url + "'");
175             try {
176                 oObj.storeToURL(url, new PropertyValue[0]);
177                 tRes.tested("storeToURL()", true);
178             } catch (IOException e) {
179                 log.println("Couldn't store to "+url+" : "+e.getMessage());
180                 e.printStackTrace(log);
181                 tRes.tested("storeToURL()", false);
182             }
183         } else {
184             log.println("an url to store is not found");
185         }
186     }
187 
188     /**
189     * Test calls the method. Then result is checked.<p>
190     * Has <b> OK </b> status if:
191     * <ol>
192     *  <li>component was stored, object is not readonly and has location</li>
193     *  <li>exception occured because of component is readonly
194     *  and wasn't stored</li>
195     * </ol>
196     */
197     public void _store() {
198         IOException ioE = null;
199 
200         try {
201             oObj.store();
202             stored = true;
203         } catch (IOException e) {
204             stored = false;
205             ioE = e;
206         }
207         if (oObj.hasLocation() && !oObj.isReadonly()) {
208             tRes.tested("store()", stored);
209             if (!stored) {
210                 log.println("Couldn't store : " + ioE.getMessage());
211                 ioE.printStackTrace(log);
212             }
213         } else {
214             tRes.tested("store()", !stored);
215             if (stored) {
216                 if (!oObj.hasLocation()) {
217                     log.println("Shouldn't store successfully"
218                             + " a document without location");
219                 } else {
220                     log.println("Shouldn't store successfully"
221                             + " a read-only document");
222                 }
223             }
224         }
225     }
226 
227 }// finished class _XStorable
228 
229