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 package ifc.sheet;
24 
25 import com.sun.star.sheet.XSpreadsheets;
26 
27 import lib.MultiMethodTest;
28 
29 
30 /**
31 * Testing <code>com.sun.star.sheet.XSpreadsheets</code>
32 * interface methods :
33 * <ul>
34 *  <li><code> insertNewByName()</code></li>
35 *  <li><code> moveByName()</code></li>
36 *  <li><code> copyByName()</code></li>
37 * </ul> <p>
38 * Test is multithread compilant. <p>
39 * @see com.sun.star.sheet.XSpreadsheets
40 */
41 public class _XSpreadsheets extends MultiMethodTest {
42     protected static int uniqCount = 0;
43     public XSpreadsheets oObj = null;
44     protected int uniqNumber = 0;
45 
46     /**
47     * Sets the unique number for the current test.
48     */
49     protected synchronized void before() {
50         uniqNumber = uniqCount++;
51     }
52 
53     /**
54     * Test inserts new sheet using the name returned by the method
55     * <code>newName</code>, copies inserted sheet with the new name,
56     * checks existence of the sheet with this name in collection and removes
57     * the both sheets from the collection. <p>
58     * Has <b> OK </b> status if the sheet with the name of the copy exists
59     * in the collection and no exceptions were thrown. <p>
60     */
61     public void _copyByName() {
62         boolean result = true;
63 
64         //first insert one that should be copied
65         String iS = newName("copyFrom");
66         log.println("Inserting sheet '" + iS + "'");
67         oObj.insertNewByName(iS, (short) 0);
68 
69         String[] eNames = oObj.getElementNames();
70         String NewSheet = newName("copyTo");
71         log.println("Try to copy " + eNames[0] + " to " + NewSheet);
72         oObj.copyByName(eNames[0], NewSheet, (short) 0);
73         result = oObj.hasByName(NewSheet);
74 
75         //remove all inserted sheets
76         try {
77             oObj.removeByName(NewSheet);
78             oObj.removeByName(iS);
79         } catch (com.sun.star.lang.WrappedTargetException e) {
80             log.print("Can't remove sheet by name");
81             e.printStackTrace(log);
82             result = false;
83         } catch (com.sun.star.container.NoSuchElementException e) {
84             log.print("Can't remove sheet by name");
85             e.printStackTrace(log);
86             result = false;
87         }
88 
89         tRes.tested("copyByName()", result);
90     } // finished _copyByName
91 
92     /**
93     * Test inserts new sheet using the name returned by the method
94     * <code>newName</code>, moves the inserted sheet to the new position
95     * in collection, gets all element names in collection and checks the name
96     * of the sheet in the new position. <p>
97     * Has <b> OK </b> status if the sheet name in the new position is equal to
98     * the name of the sheet that was moved. <p>
99     */
100     public void _moveByName() {
101         //first insert one that should be moved
102         String iS = newName("move");
103         oObj.insertNewByName(iS, (short) 0);
104 
105         String[] eNames = oObj.getElementNames();
106         String sheetToMove = eNames[0];
107         log.println("Try to move " + sheetToMove);
108         oObj.moveByName(sheetToMove, (short) 2);
109         eNames = oObj.getElementNames();
110         tRes.tested("moveByName()", sheetToMove.equals(eNames[1]));
111     } // finished _moveByName
112 
113     /**
114     * Test inserts new sheet using the name returned by the method
115     * <code>newName</code>, checks the existence of the inserted sheet in
116     * the collection, removes the sheet, tries to insert the sheet with the
117     * bad name returned by method <code>badName()</code>. <p>
118     * Has <b> OK </b> status if the inserted sheet exists in the collection
119     * after first method call and if exception occured during the second call. <p>
120     */
121     public void _insertNewByName() {
122         boolean result = false;
123 
124         String NewSheet = newName("insert");
125         log.println("Try to insert " + NewSheet);
126         oObj.insertNewByName(NewSheet, (short) 0);
127         result = oObj.hasByName(NewSheet);
128 
129         try {
130             oObj.removeByName(NewSheet);
131         } catch (com.sun.star.lang.WrappedTargetException e) {
132             log.print("Can't remove sheet '" + NewSheet + "':");
133             e.printStackTrace(log);
134             result = false;
135         } catch (com.sun.star.container.NoSuchElementException e) {
136             log.print("Can't remove sheet '" + NewSheet + "':");
137             e.printStackTrace(log);
138             result = false;
139         }
140 
141         try {
142             NewSheet = badName();
143             log.println("Try to insert " + NewSheet);
144             oObj.insertNewByName(NewSheet, (short) 0);
145             log.println(
146                     "No Exception thrown while inserting sheet with invalid name");
147             result &= false;
148             oObj.removeByName(NewSheet);
149         } catch (com.sun.star.uno.RuntimeException e) {
150             log.println(
151                     "Expected exception occured during testing 'insertNewByName'");
152             result &= true;
153         } catch (com.sun.star.lang.WrappedTargetException e) {
154             log.print("Can't remove sheet '" + NewSheet + "':");
155             e.printStackTrace(log);
156             result = false;
157         } catch (com.sun.star.container.NoSuchElementException e) {
158             log.print("Can't remove sheet '" + NewSheet + "':");
159             e.printStackTrace(log);
160             result = false;
161         }
162 
163         tRes.tested("insertNewByName()", result);
164     } // finished _insertByName
165 
166     /**
167     * Method returns unique new name using passed prefix and unique number
168     * of the current test.
169     */
170     public String newName(String prefix) {
171         return prefix + uniqNumber;
172     } // finished newName
173 
174     /**
175     * Method return bad name for a sheet using the name of the current thread.
176     */
177     public String badName() {
178         return "$%#/?\\";
179     } // finished badName
180 } //finish class _XSpreadsheets
181