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