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.document;
29 
30 import lib.MultiMethodTest;
31 
32 import com.sun.star.document.XDocumentInfo;
33 import com.sun.star.lang.ArrayIndexOutOfBoundsException;
34 
35 /**
36 * Testing <code>com.sun.star.document.XDocumentInfo</code>
37 * interface methods :
38 * <ul>
39 *  <li><code> getUserFieldCount()</code></li>
40 *  <li><code> getUserFieldName()</code></li>
41 *  <li><code> setUserFieldName()</code></li>
42 *  <li><code> getUserFieldValue()</code></li>
43 *  <li><code> setUserFieldValue()</code></li>
44 * </ul> <p>
45 * Test is <b> NOT </b> multithread compilant. <p>
46 * @see com.sun.star.document.XDocumentInfo
47 */
48 public class _XDocumentInfo extends MultiMethodTest {
49 
50     public XDocumentInfo oObj = null;
51 
52     short fieldCount;
53 
54     /**
55     * Gets user field count. <p>
56     * Has <b> OK </b> status if count is positive.
57     */
58     public void _getUserFieldCount() {
59         fieldCount = oObj.getUserFieldCount();
60         tRes.tested("getUserFieldCount()", fieldCount >= 0);
61     }
62 
63     String[] oldNames;
64     String[] oldValues;
65 
66     /**
67     * Retrieves all user field names and stores them. <p>
68     * Has <b> OK </b> status if no exceptions were thrown and
69     * names returned are not <code>null</code> values. <p>
70     * The following method tests are to be completed successfully before :
71     * <ul>
72     *  <li> <code> getUserFieldCount() </code> : to obtain number of
73     *    fields </li>
74     * </ul>
75     */
76     public void _getUserFieldName() {
77         requiredMethod("getUserFieldCount()");
78 
79         oldNames = new String[fieldCount];
80 
81         for (short i = 0; i < fieldCount; i++) {
82             try {
83                 oldNames[i] = oObj.getUserFieldName(i);
84                 if (oldNames[i] == null) {
85                     tRes.tested("getUserFieldName()", false);
86                     return;
87                 }
88             } catch (ArrayIndexOutOfBoundsException e) {
89                 log.println("Couldn't get an user field name at " + i);
90                 tRes.tested("getUserFieldName()", false);
91                 return;
92             }
93         }
94 
95         tRes.tested("getUserFieldName()", true);
96     }
97 
98     /**
99     * For each field its name changed and the checked if it's properly
100     * changed. Finally old names are restored.<p>
101     * Has <b> OK </b> status if names were properly changed. <p>
102     * The following method tests are to be completed successfully before :
103     * <ul>
104     *  <li> <code> getUserFieldName() </code> : to retrieve old names </li>
105     * </ul>
106     */
107     public void _setUserFieldName() {
108         requiredMethod("getUserFieldName()");
109 
110         for (short i = 0; i < fieldCount; i++) {
111             String newName = oldNames[i] + "_new";
112 
113             try {
114                 oObj.setUserFieldName(i, newName);
115             } catch (ArrayIndexOutOfBoundsException e) {
116                 log.println("Couldn't set an user field name at " + i);
117                 tRes.tested("setUserFieldName()", false);
118                 return;
119             }
120 
121             try {
122                 if (!newName.equals(oObj.getUserFieldName(i))) {
123                     tRes.tested("setUserFieldName()", false);
124                     return;
125                 }
126             } catch (ArrayIndexOutOfBoundsException e) {
127                 log.println("Couldn't set an user field name at " + i);
128                 tRes.tested("setUserFieldName()", false);
129                 return;
130             } finally {
131                 try {
132                     oObj.setUserFieldName(i, oldNames[i]);
133                 } catch (ArrayIndexOutOfBoundsException e) {
134                     log.println("Couldn't restore an user field name at " + i);
135                     tRes.tested("setUserFieldName()", false);
136                     return;
137                 }
138             }
139         }
140 
141         tRes.tested("setUserFieldName()", true);
142     }
143 
144 
145     /**
146     * Retrieves all user field values and stores them. <p>
147     * Has <b> OK </b> status if no exceptions were thrown and
148     * values returned are not <code>null</code> values. <p>
149     * The following method tests are to be completed successfully before :
150     * <ul>
151     *  <li> <code> getUserFieldCount() </code> : to obtain number of
152     *    fields </li>
153     * </ul>
154     */
155     public void _getUserFieldValue() {
156         requiredMethod("getUserFieldCount()");
157 
158         oldValues = new String[fieldCount];
159 
160         for (short i = 0; i < fieldCount; i++) {
161             try {
162                 oldValues[i] = oObj.getUserFieldValue(i);
163                 if (oldValues[i] == null) {
164                     tRes.tested("getUserFieldValue()", false);
165                     return;
166                 }
167             } catch (ArrayIndexOutOfBoundsException e) {
168                 log.println("Couldn't get an user field value at " + i);
169                 tRes.tested("getUserFieldValue()", false);
170                 return;
171             }
172         }
173 
174         tRes.tested("getUserFieldValue()", true);
175     }
176 
177     /**
178     * For each field its value changed and the checked if it's properly
179     * changed. Finally old values are restored.<p>
180     * Has <b> OK </b> status if values were properly changed. <p>
181     * The following method tests are to be completed successfully before :
182     * <ul>
183     *  <li> <code> getUserFieldValue() </code> : to retrieve old values. </li>
184     * </ul>
185     */
186     public void _setUserFieldValue() {
187         requiredMethod("getUserFieldValue()");
188 
189         for (short i = 0; i < fieldCount; i++) {
190             String newValue = oldValues[i] + "_new";
191 
192             try {
193                 oObj.setUserFieldValue(i, newValue);
194             } catch (ArrayIndexOutOfBoundsException e) {
195                 log.println("Couldn't set an user field value at " + i);
196                 tRes.tested("setUserFieldValue()", false);
197                 return;
198             }
199 
200             try {
201                 if (!newValue.equals(oObj.getUserFieldValue(i))) {
202                     tRes.tested("setUserFieldValue()", false);
203                     return;
204                 }
205             } catch (ArrayIndexOutOfBoundsException e) {
206                 log.println("Couldn't set an user field value at " + i);
207                 tRes.tested("setUserFieldValue()", false);
208                 return;
209             } finally {
210                 try {
211                     oObj.setUserFieldValue(i, oldNames[i]);
212                 } catch (ArrayIndexOutOfBoundsException e) {
213                     log.println("Couldn't restore an user field value at " + i);
214                     tRes.tested("setUserFieldValue()", false);
215                     return;
216                 }
217             }
218         }
219 
220         tRes.tested("setUserFieldValue()", true);
221     }
222 
223 }  // finish class _XDocumentInfo
224 
225