1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package ifc.frame;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import lib.MultiMethodTest;
27cdf0e10cSrcweir import util.utils;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
30cdf0e10cSrcweir import com.sun.star.frame.XStorable;
31cdf0e10cSrcweir import com.sun.star.io.IOException;
32cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir /**
35cdf0e10cSrcweir * Testing <code>com.sun.star.frame.XStorable</code>
36cdf0e10cSrcweir * interface methods:
37cdf0e10cSrcweir * <ul>
38cdf0e10cSrcweir *  <li><code> getLocation() </code></li>
39cdf0e10cSrcweir *  <li><code> hasLocation() </code></li>
40cdf0e10cSrcweir *  <li><code> isReadonly() </code></li>
41cdf0e10cSrcweir *  <li><code> storeAsURL() </code></li>
42cdf0e10cSrcweir *  <li><code> storeToURL() </code></li>
43cdf0e10cSrcweir *  <li><code> store() </code></li>
44cdf0e10cSrcweir * </ul><p>
45cdf0e10cSrcweir * Test is <b> NOT </b> multithread compilant. <p>
46cdf0e10cSrcweir * @see com.sun.star.frame.XStorable
47cdf0e10cSrcweir */
48cdf0e10cSrcweir public class _XStorable extends MultiMethodTest {
49cdf0e10cSrcweir     public XStorable oObj = null; // oObj filled by MultiMethodTest
50cdf0e10cSrcweir     String storeUrl;
51cdf0e10cSrcweir     boolean stored;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir     /**
54cdf0e10cSrcweir     * Test calls the method. <p>
55cdf0e10cSrcweir     * Has <b> OK </b> status in three cases:
56cdf0e10cSrcweir     * <ol>
57cdf0e10cSrcweir     *  <li>An object has location stored in. Then if method does not return
58cdf0e10cSrcweir     *  null, it has <b> OK </b> status</li>
59cdf0e10cSrcweir     *  <li>An object has no location stored in. Then method storeAsURL() is
60cdf0e10cSrcweir     *  called, and if url is not null and equals to a url where component
61cdf0e10cSrcweir     *  was stored, method has <b> OK </b> status</li>
62cdf0e10cSrcweir     *  <li>An object has no location stored in. Then method storeAsURL() is
63cdf0e10cSrcweir     *  called, and if url is null and method returns null too, method
64cdf0e10cSrcweir     *  has <b> OK </b> status </li>
65cdf0e10cSrcweir     * </ol><p>
66cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
67cdf0e10cSrcweir     * <ul>
68cdf0e10cSrcweir     *  <li> <code> storeAsURL() </code> : stores the object's persistent data
69cdf0e10cSrcweir     *  to a URL and lets the object become the representation of this new
70cdf0e10cSrcweir     *  URL</li>
71cdf0e10cSrcweir     * </ul>
72cdf0e10cSrcweir     */
_getLocation()73cdf0e10cSrcweir     public void _getLocation() {
74cdf0e10cSrcweir         if (oObj.hasLocation()) {
75cdf0e10cSrcweir             // if it has location it should know it
76cdf0e10cSrcweir             tRes.tested("getLocation()", oObj.getLocation() != null);
77cdf0e10cSrcweir         } else {
78cdf0e10cSrcweir             // else try to obtain location
79cdf0e10cSrcweir             requiredMethod("storeAsURL()");
80cdf0e10cSrcweir             if (storeUrl != null) {
81*bb6af6bcSPedro Giffuni                 // if stored successfully - check location
82cdf0e10cSrcweir                 log.println(oObj.getLocation() + "--" + storeUrl);
83cdf0e10cSrcweir                 tRes.tested("getLocation()",
84cdf0e10cSrcweir                     storeUrl.equals(oObj.getLocation()));
85cdf0e10cSrcweir             } else {
86cdf0e10cSrcweir                 // if not - it should not have a location
87cdf0e10cSrcweir                 tRes.tested("getLocation()", oObj.getLocation() == null);
88cdf0e10cSrcweir             }
89cdf0e10cSrcweir         }
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     /**
93cdf0e10cSrcweir     * Test calls the method, then result is checked. <p>
94cdf0e10cSrcweir     * Has <b> OK </b> status if stored url is not null and method does not
95cdf0e10cSrcweir     * return null or if stored url is null and the method returns null.<p>
96cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
97cdf0e10cSrcweir     * <ul>
98cdf0e10cSrcweir     *  <li> <code> storeAsURL() </code>: stores the object's persistent data
99cdf0e10cSrcweir     *  to a URL and lets the object become the representation of this new
100cdf0e10cSrcweir     *  URL</li>
101cdf0e10cSrcweir     * </ul>
102cdf0e10cSrcweir     */
_hasLocation()103cdf0e10cSrcweir     public void _hasLocation() {
104cdf0e10cSrcweir         requiredMethod("storeAsURL()");
105cdf0e10cSrcweir         if (storeUrl != null) {
106*bb6af6bcSPedro Giffuni             // if stored successfully - it should have a location
107cdf0e10cSrcweir             tRes.tested("hasLocation()", oObj.hasLocation());
108cdf0e10cSrcweir         } else {
109cdf0e10cSrcweir             // if not - it should not
110cdf0e10cSrcweir             tRes.tested("hasLocation()", !oObj.hasLocation());
111cdf0e10cSrcweir         }
112cdf0e10cSrcweir     }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     /**
115cdf0e10cSrcweir     * Test calls the method. <p>
116cdf0e10cSrcweir     * Has <b> OK </b> status if value, returned by the method is not equal to
117cdf0e10cSrcweir     * 'stored' variable. ( If it's readonly it should not have been stored. )
118cdf0e10cSrcweir     * <p>
119cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
120cdf0e10cSrcweir     * <ul>
121cdf0e10cSrcweir     *  <li> <code> store() </code> : stores data to the URL from which it
122cdf0e10cSrcweir     *  was loaded </li>
123cdf0e10cSrcweir     * </ul>
124cdf0e10cSrcweir     */
_isReadonly()125cdf0e10cSrcweir     public void _isReadonly() {
126cdf0e10cSrcweir         requiredMethod("store()");
127cdf0e10cSrcweir         tRes.tested("isReadonly()", oObj.isReadonly() != stored);
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     /**
131cdf0e10cSrcweir     * Object is stored into temporary directory. <p>
132cdf0e10cSrcweir     * Has <b> OK </b> status if the method successfully returns
133cdf0e10cSrcweir     * and no exceptions were thrown.
134cdf0e10cSrcweir     */
_storeAsURL()135cdf0e10cSrcweir     public void _storeAsURL() {
136cdf0e10cSrcweir         // getting an url to store
137cdf0e10cSrcweir         String url = (String) utils.getOfficeTemp(
138cdf0e10cSrcweir                                 (XMultiServiceFactory)tParam.getMSF());
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         if (url != null) {
141cdf0e10cSrcweir             url += "xstorable.store.as.test";
142cdf0e10cSrcweir             log.println("store as '" + url + "'");
143cdf0e10cSrcweir             try {
144cdf0e10cSrcweir                 oObj.storeAsURL(url, new PropertyValue[0]);
145cdf0e10cSrcweir                 storeUrl = url;
146cdf0e10cSrcweir                 tRes.tested("storeAsURL()", true);
147cdf0e10cSrcweir             } catch (IOException e) {
148cdf0e10cSrcweir                 log.println("Couldn't store as "+url+" : "+e.getMessage());
149cdf0e10cSrcweir                 e.printStackTrace(log);
150cdf0e10cSrcweir                 storeUrl = null;
151cdf0e10cSrcweir                 tRes.tested("storeAsURL()", false);
152cdf0e10cSrcweir             }
153cdf0e10cSrcweir         } else {
154cdf0e10cSrcweir             log.println("an url to store is not found");
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir     }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     /**
159cdf0e10cSrcweir     * Object is stored into temporary directory. <p>
160cdf0e10cSrcweir     * Has <b> OK </b> status if the method successfully returns
161cdf0e10cSrcweir     * and no exceptions were thrown.
162cdf0e10cSrcweir     */
_storeToURL()163cdf0e10cSrcweir     public void _storeToURL() {
164cdf0e10cSrcweir         // getting an url to store
165cdf0e10cSrcweir         String url = (String) utils.getOfficeTemp(
166cdf0e10cSrcweir                                 (XMultiServiceFactory)tParam.getMSF());
167cdf0e10cSrcweir 
168cdf0e10cSrcweir         if (url != null) {
169cdf0e10cSrcweir             url += "xstorable.store.as.test";
170cdf0e10cSrcweir             log.println("store to '" + url + "'");
171cdf0e10cSrcweir             try {
172cdf0e10cSrcweir                 oObj.storeToURL(url, new PropertyValue[0]);
173cdf0e10cSrcweir                 tRes.tested("storeToURL()", true);
174cdf0e10cSrcweir             } catch (IOException e) {
175cdf0e10cSrcweir                 log.println("Couldn't store to "+url+" : "+e.getMessage());
176cdf0e10cSrcweir                 e.printStackTrace(log);
177cdf0e10cSrcweir                 tRes.tested("storeToURL()", false);
178cdf0e10cSrcweir             }
179cdf0e10cSrcweir         } else {
180cdf0e10cSrcweir             log.println("an url to store is not found");
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir     }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir     /**
185cdf0e10cSrcweir     * Test calls the method. Then result is checked.<p>
186cdf0e10cSrcweir     * Has <b> OK </b> status if:
187cdf0e10cSrcweir     * <ol>
188cdf0e10cSrcweir     *  <li>component was stored, object is not readonly and has location</li>
189*bb6af6bcSPedro Giffuni     *  <li>exception occurred because of component is readonly
190cdf0e10cSrcweir     *  and wasn't stored</li>
191cdf0e10cSrcweir     * </ol>
192cdf0e10cSrcweir     */
_store()193cdf0e10cSrcweir     public void _store() {
194cdf0e10cSrcweir         IOException ioE = null;
195cdf0e10cSrcweir 
196cdf0e10cSrcweir         try {
197cdf0e10cSrcweir             oObj.store();
198cdf0e10cSrcweir             stored = true;
199cdf0e10cSrcweir         } catch (IOException e) {
200cdf0e10cSrcweir             stored = false;
201cdf0e10cSrcweir             ioE = e;
202cdf0e10cSrcweir         }
203cdf0e10cSrcweir         if (oObj.hasLocation() && !oObj.isReadonly()) {
204cdf0e10cSrcweir             tRes.tested("store()", stored);
205cdf0e10cSrcweir             if (!stored) {
206cdf0e10cSrcweir                 log.println("Couldn't store : " + ioE.getMessage());
207cdf0e10cSrcweir                 ioE.printStackTrace(log);
208cdf0e10cSrcweir             }
209cdf0e10cSrcweir         } else {
210cdf0e10cSrcweir             tRes.tested("store()", !stored);
211cdf0e10cSrcweir             if (stored) {
212cdf0e10cSrcweir                 if (!oObj.hasLocation()) {
213cdf0e10cSrcweir                     log.println("Shouldn't store successfully"
214cdf0e10cSrcweir                             + " a document without location");
215cdf0e10cSrcweir                 } else {
216cdf0e10cSrcweir                     log.println("Shouldn't store successfully"
217cdf0e10cSrcweir                             + " a read-only document");
218cdf0e10cSrcweir                 }
219cdf0e10cSrcweir             }
220cdf0e10cSrcweir         }
221cdf0e10cSrcweir     }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir }// finished class _XStorable
224cdf0e10cSrcweir 
225