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