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 mod._sw;
24 
25 import java.io.PrintWriter;
26 
27 import lib.StatusException;
28 import lib.TestCase;
29 import lib.TestEnvironment;
30 import lib.TestParameters;
31 import util.SOfficeFactory;
32 import util.ValueComparer;
33 
34 import com.sun.star.beans.XPropertySet;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.table.XCell;
37 import com.sun.star.text.XTextContent;
38 import com.sun.star.text.XTextDocument;
39 import com.sun.star.text.XTextRange;
40 import com.sun.star.text.XTextTable;
41 import com.sun.star.uno.UnoRuntime;
42 
43 
44 /**
45  * this class tests the implementation of listed interfaces
46  * @see com.sun.star.beans.XPropertySet
47  * @see com.sun.star.container.XNamed
48  * @see com.sun.star.lang.XComponent
49  * @see com.sun.star.text.TextContent
50  * @see com.sun.star.text.XTextContent
51  * @see com.sun.star.text.XTextTable
52  */
53 public class SwXTextTable extends TestCase {
54     SOfficeFactory SOF;
55     XTextDocument xTextDoc;
56 
initialize(TestParameters tParam, PrintWriter log)57     protected void initialize(TestParameters tParam, PrintWriter log) {
58         SOF = SOfficeFactory.getFactory( (XMultiServiceFactory) tParam.getMSF());
59 
60         try {
61             log.println("creating a textdocument");
62             xTextDoc = SOF.createTextDoc(null);
63         } catch (com.sun.star.uno.Exception e) {
64             // Some exception occures.FAILED
65             e.printStackTrace(log);
66             throw new StatusException("Couldn't create document", e);
67         }
68     }
69 
cleanup(TestParameters tParam, PrintWriter log)70     protected void cleanup(TestParameters tParam, PrintWriter log) {
71         log.println("    disposing xTextDoc ");
72         util.DesktopTools.closeDoc(xTextDoc);
73     }
74 
75     /**
76      this method creates the testenvironment
77      @param tParam    TestParameters class which contains additional
78         test parameters
79      @param log        PrintWriter class to log the test state and result
80 
81      @return    TestEnvironment class
82 
83      @see TestParameters
84      @see PrintWriter
85      */
createTestEnvironment(TestParameters tParam, PrintWriter log)86     protected synchronized TestEnvironment createTestEnvironment(TestParameters tParam,
87                                                                  PrintWriter log) {
88         // creation of testobject here
89         XTextTable oObj = null;
90         TestEnvironment tEnv = null;
91         Object instance = null;
92 
93         try {
94             oObj = SOF.createTextTable(xTextDoc);
95             instance = SOF.createTextTable(xTextDoc);
96         } catch (com.sun.star.uno.Exception uE) {
97             uE.printStackTrace(log);
98             throw new StatusException("Couldn't create testobj : " +
99                                       uE.getMessage(), uE);
100         }
101 
102         int nRow = 6;
103         int nCol = 2;
104         oObj.initialize(nRow, nCol);
105 
106         log.println("Creating instance...");
107         tEnv = new TestEnvironment(oObj);
108 
109         if (SOF.getTableCollection(xTextDoc).getCount() == 0) {
110             try {
111                 SOF.insertTextContent(xTextDoc, oObj);
112             } catch (com.sun.star.uno.Exception uE) {
113                 uE.printStackTrace(log);
114                 throw new StatusException("Couldn't create TextTable : " +
115                                           uE.getMessage(), uE);
116             }
117         }
118 
119         XPropertySet props = (XPropertySet) UnoRuntime.queryInterface(
120                                      XPropertySet.class, oObj);
121 
122         try {
123             props.setPropertyValue("ChartRowAsLabel", new Boolean(true));
124             props.setPropertyValue("ChartColumnAsLabel", new Boolean(true));
125         } catch (Exception e) {
126             e.printStackTrace(log);
127         }
128 
129 
130         //Adding relation for util.XTextTable
131         tEnv.addObjRelation("NROW", new Integer(nRow));
132         tEnv.addObjRelation("NCOL", new Integer(nCol));
133 
134         //Adding ObjRelation for XCellRangeData
135         Object[][] newData = new Object[6][];
136         Object[] line = new Object[2];
137         line[0] = new Double(1.5);
138         line[1] = new Double(2.5);
139         newData[0] = line;
140         newData[1] = line;
141         newData[2] = line;
142         newData[3] = line;
143         newData[4] = line;
144         newData[5] = line;
145         tEnv.addObjRelation("NewData", newData);
146 
147         //Adding relation for util.XSortable
148         final PrintWriter finalLog = log;
149         final XTextTable oTable = oObj;
150         tEnv.addObjRelation("SORTCHECKER",
151                             new ifc.util._XSortable.XSortChecker() {
152             PrintWriter out = finalLog;
153 
154             public void setPrintWriter(PrintWriter log) {
155                 out = log;
156             }
157 
158             public void prepareToSort() {
159                 oTable.getCellByName("A1").setValue(4);
160 
161                 XTextRange textRange = (XTextRange) UnoRuntime.queryInterface(
162                                                XTextRange.class,
163                                                oTable.getCellByName("A2"));
164                 textRange.setString("b");
165                 oTable.getCellByName("A3").setValue(3);
166                 textRange = (XTextRange) UnoRuntime.queryInterface(
167                                     XTextRange.class,
168                                     oTable.getCellByName("A4"));
169                 textRange.setString("a");
170                 oTable.getCellByName("A5").setValue(23);
171                 textRange = (XTextRange) UnoRuntime.queryInterface(
172                                     XTextRange.class,
173                                     oTable.getCellByName("A6"));
174                 textRange.setString("ab");
175 
176             }
177 
178             public boolean checkSort(boolean isSortNumbering,
179                                      boolean isSortAscending) {
180                 out.println("Sort checking...");
181 
182                 boolean res = false;
183                 String[] value = new String[6];
184 
185                 for (int i = 0; i < 6; i++) {
186                     XCell cell = oTable.getCellByName("A" + (i + 1));
187                     XTextRange textRange = (XTextRange) UnoRuntime.queryInterface(
188                                                    XTextRange.class, cell);
189                     value[i] = textRange.getString();
190                 }
191 
192                 if (isSortNumbering) {
193                     if (isSortAscending) {
194                         String[] rightVal = { "3", "4", "23" };
195                         String[] vals = { value[3], value[4], value[5] };
196                         res = ValueComparer.equalValue(vals, rightVal);
197 
198                         if (!res) {
199                             out.println("Expected: 3, 4, 23");
200                             out.println("Getting: " + value[3] + ", " +
201                                             value[4] + ", " + value[5]);
202                         }
203                     } else {
204                         String[] rightVal = { "23", "4", "3" };
205                         String[] vals = { value[0], value[1], value[2] };
206                         res = ValueComparer.equalValue(vals, rightVal);
207 
208                         if (!res) {
209                             out.println("Expected: 23, 4, 3");
210                             out.println("Getting: " + value[1] + ", " +
211                                             value[2] + ", " + value[3]);
212                         }
213                     }
214                 } else {
215                     if (isSortAscending) {
216                         String[] rightVal = { "23", "3", "4", "a", "ab", "b" };
217                         res = ValueComparer.equalValue(value, rightVal);
218 
219                         if (!res) {
220                             out.println("Expected: 23, 3, 4, a, ab, b");
221                             out.println("Getting: " + value[0] + ", " +
222                                             value[1] + ", " + value[2] +
223                                             ", " + value[3] + ", " +
224                                             value[4] + ", " + value[5]);
225                         }
226                     } else {
227                         String[] rightVal = { "b", "ab", "a", "4", "3", "23" };
228                         res = ValueComparer.equalValue(value, rightVal);
229 
230                         if (!res) {
231                             out.println("Expected: b, ab, a, 4, 3, 23");
232                             out.println("Getting: " + value[0] + ", " +
233                                             value[1] + ", " + value[2] +
234                                             ", " + value[3] + ", " +
235                                             value[4] + ", " + value[5]);
236                         }
237                     }
238                 }
239 
240                 if (res) {
241                     out.println("Sorted correctly");
242                 } else {
243                     out.println("Sorted uncorrectly");
244                 }
245 
246                 return res;
247             }
248         });
249 
250         tEnv.addObjRelation("CONTENT",
251                             (XTextContent) UnoRuntime.queryInterface(
252                                     XTextContent.class, instance));
253         tEnv.addObjRelation("RANGE", xTextDoc.getText().createTextCursor());
254 
255         return tEnv;
256     }
257 } // finish class SwXTextTable
258