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