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.util;
24 
25 import java.io.PrintWriter;
26 
27 import lib.MultiMethodTest;
28 import lib.Status;
29 import lib.StatusException;
30 
31 import com.sun.star.beans.PropertyValue;
32 import com.sun.star.table.TableSortField;
33 import com.sun.star.util.XSortable;
34 
35 
36 /**
37  * Testing <code>com.sun.star.util.XSortable</code>
38  * interface methods :
39  * <ul>
40  *  <li><code> createSortDescriptor()</code></li>
41  *  <li><code> sort()</code></li>
42  * </ul> <p>
43  * This test needs the following object relations :
44  * <ul>
45  *  <li> <code>'SORTCHECKER'</code> : <code>
46 *    _XSortable.XSortChecker</code> interface implementation
47  *  </li>
48  * <ul><p>
49  * Test is <b> NOT </b> multithread compilant. <p>
50  * @see com.sun.star.util.XSortable
51  */
52 public class _XSortable extends MultiMethodTest {
53     // oObj filled by MultiMethodTest
54     public XSortable oObj = null;
55     XSortChecker checker = null;
56     PropertyValue[] oPV = null;
57 
before()58     protected void before() {
59         checker = (XSortChecker) tEnv.getObjRelation("SORTCHECKER");
60 
61         if (checker == null) {
62             throw new StatusException(Status.failed(
63                                               "Couldn't get relation 'SORTCHECKER'"));
64         }
65 
66         checker.setPrintWriter(log);
67     }
68 
69     /**
70      * Test calls the method. <p>
71      * Has <b> OK </b> status if the length of the returned array
72      * is greater than zero. <p>
73      */
_createSortDescriptor()74     public void _createSortDescriptor() {
75         boolean bResult = false;
76 
77         log.println("test for createSortDescriptor() ");
78         oPV = oObj.createSortDescriptor();
79 
80         if (oPV.length > 0) {
81             bResult = true;
82 
83             for (int k = 0; k < oPV.length; k++) {
84                 log.println("DescriptorProperty " + k + ": Name=" +
85                             oPV[k].Name + "; Value=" + oPV[k].Value);
86 
87                 if (oPV[k].Name.equals("SortFields")) {
88                     TableSortField[] tsf = (TableSortField[]) oPV[k].Value;
89 
90                     for (int l = 0; l < tsf.length; l++) {
91                         log.println("\t isAscending:  " +
92                                     tsf[l].IsAscending);
93                         log.println("\t IsCaseSensitive:  " +
94                                     tsf[l].IsCaseSensitive);
95                         log.println("\t CollatorAlgorithm:  " +
96                                     tsf[l].CollatorAlgorithm);
97                     }
98                 }
99             }
100         }
101 
102         log.println("Found " + oPV.length + " PropertyValues");
103         tRes.tested("createSortDescriptor()", bResult);
104     }
105 
106     /**
107      * Test calls the method using descriptor created before as
108      * parameter. <p>
109      * Has <b> OK </b> status if the method successfully returns
110      * and no exceptions were thrown. <p>
111      * The following method tests are to be completed successfully before :
112      * <ul>
113      *  <li> <code> createSortDescriptor() </code> : to have a descriptor
114      *  for sort. </li>
115      * </ul>
116      */
_sort()117     public void _sort() {
118 
119         checker.prepareToSort();
120 
121         log.println(
122                 "############## Sort algorithm: Alphanumeric Order: Ascending");
123         modifyDescriptor(false, true);
124         oObj.sort(oPV);
125 
126         boolean res = checker.checkSort(false, true);
127         log.println(
128                 "############################################################");
129 
130         log.println(
131                 "############# Sort algorithm: Alphanumeric Order: Descending");
132         modifyDescriptor(false, false);
133         oObj.sort(oPV);
134         res = checker.checkSort(false, false);
135         log.println(
136                 "############################################################");
137 
138         log.println(
139                 "################# Sort algorithm: Numeric Order: Ascending");
140         modifyDescriptor(true, true);
141         oObj.sort(oPV);
142         res = checker.checkSort(true, true);
143         log.println(
144                 "############################################################");
145 
146         log.println(
147                 "################## Sort algorithm: Numeric Order: Descending");
148         modifyDescriptor(true, false);
149         oObj.sort(oPV);
150         res = checker.checkSort(true, false);
151         log.println(
152                 "############################################################");
153 
154         tRes.tested("sort()", res);
155     }
156 
modifyDescriptor(boolean isSortNumeric, boolean isSortAscending)157     protected void modifyDescriptor(boolean isSortNumeric,
158                                     boolean isSortAscending) {
159         for (int i = 0; i < oPV.length; i++) {
160             if (oPV[i].Name.equals("SortFields")) {
161                 TableSortField[] TableFields = (TableSortField[]) oPV[i].Value;
162 
163                 if (TableFields.length == 0) {
164                     TableFields = new TableSortField[1];
165                     TableFields[0] = new TableSortField();
166                 }
167 
168                 for (int k = 0; k < TableFields.length; k++) {
169                     TableFields[k].IsAscending = isSortAscending;
170 
171                     if (isSortNumeric) {
172                         TableFields[k].FieldType = com.sun.star.table.TableSortFieldType.NUMERIC;
173                         TableFields[k].CollatorAlgorithm = "numeric";
174                     } else {
175                         TableFields[k].FieldType = com.sun.star.table.TableSortFieldType.ALPHANUMERIC;
176                         TableFields[k].CollatorAlgorithm = "alphanumeric";
177                     }
178                 }
179 
180                 oPV[i].Value = TableFields;
181             }
182 
183             if (oPV[i].Name.equals("isSortInTable")) {
184                 oPV[i].Value = new Boolean(true);
185             }
186 
187             if (oPV[i].Name.equals("IsSortColumns")) {
188                 oPV[i].Value = new Boolean(false);
189             }
190         }
191 
192         log.println("Modified sort descriptor: ");
193 
194         if (oPV.length > 0) {
195             for (int k = 0; k < oPV.length; k++) {
196                 log.println("DescriptorProperty " + k + ": Name=" +
197                             oPV[k].Name + "; Value=" + oPV[k].Value);
198 
199                 if (oPV[k].Name.equals("SortFields")) {
200                     TableSortField[] tsf = (TableSortField[]) oPV[k].Value;
201 
202                     for (int l = 0; l < tsf.length; l++) {
203                         log.println("\t isAscending:  " +
204                                     tsf[l].IsAscending);
205                         log.println("\t IsCaseSensitive:  " +
206                                     tsf[l].IsCaseSensitive);
207                         log.println("\t CollatorAlgorithm:  " +
208                                     tsf[l].CollatorAlgorithm);
209                     }
210                 }
211             }
212         }
213     }
214 
215     /**
216     * The interface for sort checking.
217     */
218     public static interface XSortChecker {
prepareToSort()219         public void prepareToSort();
220 
checkSort(boolean isSortNumbering, boolean isSortAscending)221         public boolean checkSort(boolean isSortNumbering,
222                                  boolean isSortAscending);
223 
setPrintWriter(PrintWriter log)224         public void setPrintWriter(PrintWriter log);
225     }
226 
227     /**
228     * Forces environment recreation.
229     */
after()230     protected void after() {
231         disposeEnvironment();
232     }
233 
234 } // finish class _XSortable
235