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 lib.MultiMethodTest;
26 
27 import com.sun.star.beans.PropertyValue;
28 import com.sun.star.table.XCellRange;
29 import com.sun.star.uno.Type;
30 import com.sun.star.uno.UnoRuntime;
31 import com.sun.star.util.XImportable;
32 
33 
34 /**
35  * checks the Interface XImportable
36  */
37 public class _XImportable extends MultiMethodTest {
38     public XImportable oObj;
39     protected PropertyValue[] descriptor = null;
40     protected String[] names = new String[] {
41         "DatabaseName", "SourceType", "SourceObject", "IsNative"
42     };
43     protected Type[] types = new Type[] {
44         new Type(String.class), new Type(com.sun.star.sheet.DataImportMode.class),
45         new Type(String.class), new Type(Boolean.class)
46     };
47 
48     /**
49      * creates an ImportDescriptor, the gained PropertyValues can be found
50      * in com.sun.star.sheet.DatabaseImportDescriptor.<br>
51      * Returns OK state is all propertynames and types are the specified.
52      */
53 
_createImportDescriptor()54     public void _createImportDescriptor() {
55         boolean res = true;
56         boolean locResult = false;
57 
58         descriptor = oObj.createImportDescriptor(true);
59         log.print("Getting when calling createImportDescriptor(true) --");
60 
61 
62         //printPropertyValue(descriptor);
63         log.println("done");
64 
65         log.print("Checking PropertyNames -- ");
66         locResult = checkPropertyNames(descriptor, names);
67         log.println("Worked: " + locResult);
68         res &= locResult;
69 
70         log.print("Checking PropertyTypes -- ");
71         locResult = checkPropertyTypes(descriptor, types);
72         log.println("Worked: " + locResult);
73         res &= locResult;
74 
75         descriptor = oObj.createImportDescriptor(false);
76         log.print("Getting when calling createImportDescriptor(false) -- ");
77 
78 
79         //printPropertyValue(descriptor);
80         log.println("done");
81 
82         log.print("Checking PropertyNames -- ");
83         locResult = checkPropertyNames(descriptor, names);
84         log.println("Worked: " + locResult);
85         res &= locResult;
86 
87         log.print("Checking PropertyTypes -- ");
88         locResult = checkPropertyTypes(descriptor, types);
89         log.println("Worked - " + locResult);
90         res &= locResult;
91 
92         tRes.tested("createImportDescriptor()", res);
93     }
94 
_doImport()95     public void _doImport() {
96         requiredMethod("createImportDescriptor()");
97         boolean res = true;
98 
99         log.print("Setting the ImportDescriptor (Bibliograpy, Table, biblio) -- ");
100         descriptor[0].Value = "Bibliography";
101         descriptor[1].Value = com.sun.star.sheet.DataImportMode.TABLE;
102         descriptor[2].Value = "biblio";
103         log.println("done");
104 
105         log.print("Importing data (Bibliograpy, Table, biblio) -- ");
106         oObj.doImport(descriptor);
107         log.println("done");
108 
109         log.println("Checking data");
110         res &= checkA1("Identifier");
111 
112         log.print("Setting the ImportDescriptor (Bibliograpy, SQL, select Author from biblio) -- ");
113         descriptor[0].Value = "Bibliography";
114         descriptor[1].Value = com.sun.star.sheet.DataImportMode.SQL;
115         descriptor[2].Value = "select Author from biblio";
116         log.println("done");
117 
118         log.print("Importing data (Bibliograpy, SQL, select Author from biblio) -- ");
119         oObj.doImport(descriptor);
120         log.println("done");
121 
122         log.println("Checking data");
123         res &= checkA1("Author");
124 
125         tRes.tested("doImport()",res);
126     }
127 
printPropertyValue(PropertyValue[] props)128     protected void printPropertyValue(PropertyValue[] props) {
129         for (int i = 0; i < props.length; i++) {
130             log.println("\tName: " + props[i].Name);
131             log.println("\tValue: " + props[i].Value);
132         }
133     }
134 
checkPropertyNames(PropertyValue[] props, String[] names)135     protected boolean checkPropertyNames(PropertyValue[] props, String[] names) {
136         boolean res = true;
137 
138         for (int i = 0; i < props.length; i++) {
139             boolean locResult = props[i].Name.equals(names[i]);
140 
141             if (!locResult) {
142                 log.println("PropertyName differs for index " + i);
143                 log.println("\tGetting: " + props[i].Name);
144                 log.println("\tExpected: " + names[i]);
145             }
146 
147             res &= locResult;
148         }
149 
150         return res;
151     }
152 
checkPropertyTypes(PropertyValue[] props, Type[] types)153     protected boolean checkPropertyTypes(PropertyValue[] props, Type[] types) {
154         boolean res = true;
155 
156         for (int i = 0; i < props.length; i++) {
157             Type ValueType = new Type(props[i].Value.getClass());
158             boolean locResult = ValueType.equals(types[i]);
159 
160             if (!locResult) {
161                 log.println("PropertyType differs for " + props[i].Name);
162                 log.println("\tGetting: " + ValueType.getTypeName());
163                 log.println("\tExpected: " + types[i].getTypeName());
164             }
165 
166             res &= locResult;
167         }
168 
169         return res;
170     }
171 
checkA1(String expected)172     protected boolean checkA1(String expected) {
173         XCellRange range = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, tEnv.getTestObject());
174         boolean res = false;
175         try{
176             String a1 = range.getCellByPosition(0,0).getFormula();
177             res = a1.equals(expected);
178             if (!res) {
179                 log.println("\tResult differs from expectation");
180                 log.println("\tGetting: "+a1);
181                 log.println("\tExpected: "+expected);
182             } else {
183                 log.println("successful");
184             }
185         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
186             log.println("Couldn't get Cell to check");
187         }
188         return res;
189     }
190 
191     /**
192      * Dispose environment.
193      */
after()194     protected void after() {
195         disposeEnvironment();
196     }
197 
198 }