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 
24 package ifc.script;
25 
26 import lib.MultiMethodTest;
27 
28 import com.sun.star.container.XSet;
29 import com.sun.star.lang.XMultiServiceFactory;
30 import com.sun.star.script.XTypeConverter;
31 import com.sun.star.uno.Any;
32 import com.sun.star.uno.Type;
33 import com.sun.star.uno.TypeClass;
34 
35 /**
36 * Testing <code>com.sun.star.script.XTypeConverter</code>
37 * interface methods :
38 * <ul>
39 *  <li><code> convertTo()</code></li>
40 *  <li><code> convertToSimpleType()</code></li>
41 * </ul> <p>
42 * @see com.sun.star.script.XTypeConverter
43 */
44 public class _XTypeConverter extends MultiMethodTest {
45 
46     /**
47      * oObj filled by MultiMethodTest
48      */
49     public XTypeConverter oObj = null;
50 
51     /**
52     * Test creates instance of <code>TypeDescriptionManager</code>,
53     * and converts it to <code>XSet</code>. <p>
54     * Has <b> OK </b> status if returned value is instance of <code>XSet</code>
55     * and no exceptions were thrown. <p>
56     * @see com.sun.star.comp.stoc.TypeDescriptionManager
57     * @see com.sun.star.container.XSet
58     */
_convertTo()59     public void _convertTo() {
60         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
61         Object value = null ;
62 
63         try {
64             value = xMSF.createInstance
65                 ("com.sun.star.comp.stoc.TypeDescriptionManager") ;
66         } catch (com.sun.star.uno.Exception e) {
67             log.println("Can't create value to convert") ;
68             e.printStackTrace(log) ;
69             tRes.tested("convertTo()", false) ;
70         }
71 
72         try {
73             Type destType = new Type(XSet.class) ;
74 
75             Object o = oObj.convertTo(value, destType);
76 
77             boolean result;
78             if (o instanceof Any) {
79                 result = ((Any)o).getType().equals(destType);
80             }
81             else {
82                 result = (o instanceof XSet);
83             }
84 
85             tRes.tested("convertTo()", result) ;
86         } catch (com.sun.star.script.CannotConvertException e) {
87             log.println("Exception while converting value.") ;
88             e.printStackTrace(log) ;
89             tRes.tested("convertTo()", false) ;
90         } catch (com.sun.star.lang.IllegalArgumentException e) {
91             log.println("Exception while converting value.") ;
92             e.printStackTrace(log) ;
93             tRes.tested("convertTo()", false) ;
94         }
95     }
96 
97     /**
98     * Test creates instance of <code>Integer</code>,
99     * and converts it to <code>String</code>. <p>
100     * Has <b> OK </b> status if returned value is instance of <code>String</code>,
101     * if returned value is string representation of integer value that
102     * was passed to method and no exceptions were thrown. <p>
103     */
_convertToSimpleType()104     public void _convertToSimpleType() {
105         Object value = new Integer(123) ;
106         Object destValue = null ;
107 
108         try {
109             destValue = oObj.convertToSimpleType(value, TypeClass.STRING) ;
110         } catch (com.sun.star.script.CannotConvertException e) {
111             log.println("Exception while converting value.") ;
112             e.printStackTrace(log) ;
113             tRes.tested("convertToSimpleType()", false) ;
114             return ;
115         } catch (com.sun.star.lang.IllegalArgumentException e) {
116             log.println("Exception while converting value.") ;
117             e.printStackTrace(log) ;
118             tRes.tested("convertToSimpleType()", false) ;
119             return ;
120         }
121 
122         tRes.tested("convertToSimpleType()", destValue != null &&
123             destValue instanceof String &&
124             ((String) destValue).equals("123")) ;
125     }
126 }
127 
128