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