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 mod._xmloff.Draw;
25 
26 import java.io.PrintWriter;
27 
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.SOfficeFactory;
33 
34 import com.sun.star.container.XNamed;
35 import com.sun.star.drawing.XDrawPages;
36 import com.sun.star.drawing.XDrawPagesSupplier;
37 import com.sun.star.lang.XComponent;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XInterface;
41 
42 /**
43  * Test for object which is represented by service
44  * <code>com.sun.star.comp.Draw.XMLContentImporter</code>. <p>
45  * Object implements the following interfaces :
46  * <ul>
47  *  <li><code>com::sun::star::lang::XInitialization</code></li>
48  *  <li><code>com::sun::star::document::XImporter</code></li>
49  *  <li><code>com::sun::star::document::XFilter</code></li>
50  *  <li><code>com::sun::star::document::ImportFilter</code></li>
51  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
52  *  <li><code>com::sun::star::xml::sax::XDocumentHandler</code></li>
53  * </ul>
54  * @see com.sun.star.lang.XInitialization
55  * @see com.sun.star.document.XImporter
56  * @see com.sun.star.document.XFilter
57  * @see com.sun.star.document.ImportFilter
58  * @see com.sun.star.beans.XPropertySet
59  * @see com.sun.star.xml.sax.XDocumentHandler
60  * @see ifc.lang._XInitialization
61  * @see ifc.document._XImporter
62  * @see ifc.document._XFilter
63  * @see ifc.document._XExporter
64  * @see ifc.beans._XPropertySet
65  * @see ifc.xml.sax._XDocumentHandler
66  */
67 public class XMLContentImporter extends TestCase {
68     XComponent xDrawDoc = null;
69 
70     /**
71     * New spreadsheet document created.
72     */
initialize( TestParameters tParam, PrintWriter log )73     protected void initialize( TestParameters tParam, PrintWriter log ) {
74 
75         // get a soffice factory object
76         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
77 
78         try {
79             log.println( "creating a drawdocument" );
80             xDrawDoc = SOF.createDrawDoc(null);
81         } catch ( Exception e ) {
82             // Some exception occured.FAILED
83             e.printStackTrace( log );
84             throw new StatusException( "Couldn't create document", e );
85         }
86     }
87 
88     /**
89     * Spreadsheet document destroyed.
90     */
cleanup( TestParameters tParam, PrintWriter log )91     protected void cleanup( TestParameters tParam, PrintWriter log ) {
92         log.println( "    disposing document " );
93         xDrawDoc.dispose();
94     }
95 
96     /**
97     * Creating a Testenvironment for the interfaces to be tested.
98     * Creates an instance of the service
99     * <code>com.sun.star.comp.Draw.XMLContentImporter</code><p>
100     *
101     * The Draw document is set as a target document for importer.
102     * Imported XML-data contains only content tags including pages
103     * named 'NewSlide1' and 'NewSlide2'.
104     * Pages getting from target document is checked after import
105     *     Object relations created :
106     * <ul>
107     *  <li> <code>'XDocumentHandler.XMLData'</code> for
108     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
109     *  <li> <code>'XDocumentHandler.ImportChecker'</code> for
110     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
111     *  <li> <code>'TargetDocument'</code> for
112     *      {@link ifc.document._XImporter} interface </li>
113     * </ul>
114     */
createTestEnvironment(TestParameters tParam, PrintWriter log )115     public synchronized TestEnvironment createTestEnvironment
116         (TestParameters tParam, PrintWriter log ) throws StatusException {
117 
118         XInterface oObj = null;
119         Object oInt = null ;
120 
121         // creation of testobject here
122         // first we write what we are intend to do to log file
123         log.println( "creating a test environment" );
124 
125         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
126 
127         try {
128             oInt = xMSF.createInstance
129                 ("com.sun.star.comp.Draw.XMLContentImporter") ;
130 
131         } catch (com.sun.star.uno.Exception e) {
132             e.printStackTrace(log) ;
133             throw new StatusException("Can't create component.", e) ;
134         }
135 
136         oObj = (XInterface) oInt ;
137 
138         // create testobject here
139         log.println( "creating a new environment for Paragraph object" );
140         TestEnvironment tEnv = new TestEnvironment( oObj );
141 
142         // adding relation
143         tEnv.addObjRelation("TargetDocument", xDrawDoc) ;
144 
145         // New XML document generated to be imported to Draw document
146         String[][] xml = new String[][] {
147             {"start", "office:document",
148                 "xmlns:office", "CDATA", "http://openoffice.org/2000/office",
149                 "xmlns:draw", "CDATA", "http://openoffice.org/2000/drawing",
150                 "office:class", "CDATA", "drawing"
151                 ,"office:version", "CDATA", "1.0"
152                 },
153             {"start", "office:body"},
154             {"start", "draw:page",
155                 "draw:name", "CDATA", "NewSlide1",
156                 "draw:master-page-name", "CDATA", "Default"},
157             {"end", "draw:page"},
158             {"start", "draw:page", "draw:name", "CDATA", "NewSlide2",
159                 "draw:master-page-name", "CDATA", "Default"},
160             {"end","draw:page"},
161             {"end", "draw:page"},
162             {"end", "office:body"},
163             {"end", "office:document"}};
164 
165         // adding relation for XDocumentHandler
166         tEnv.addObjRelation("XDocumentHandler.XMLData", xml);
167 
168         //save to log content before import
169         final PrintWriter fLog = log;
170 
171         // Checking target document after import
172         tEnv.addObjRelation("XDocumentHandler.ImportChecker",
173             new ifc.xml.sax._XDocumentHandler.ImportChecker() {
174                 public boolean checkImport() {
175                     XDrawPagesSupplier supp = (XDrawPagesSupplier)
176                         UnoRuntime.queryInterface
177                         (XDrawPagesSupplier.class, xDrawDoc);
178                     final XDrawPages xPages = supp.getDrawPages();
179                     XNamed[] pageArray = new XNamed[ xPages.getCount() ];
180                     for (int i=0; i < xPages.getCount(); i++) {
181                         try {
182                             pageArray[i] = (XNamed) UnoRuntime.queryInterface
183                                 (XNamed.class, xPages.getByIndex(i));
184                         } catch (com.sun.star.uno.Exception e) {
185                             e.printStackTrace(fLog) ;
186                             throw new StatusException
187                                 ("Can't get page name by index.", e) ;
188                         }
189                     }
190                     fLog.println("Slide names after import:");
191                     for ( int i = 0; i < xPages.getCount(); i++ ) {
192                         if ((pageArray[i].getName().equals("NewSlide1"))
193                             || (pageArray[i].getName().equals("NewSlide2"))) {
194                             fLog.println("  " + pageArray[i].getName());
195                         }
196                         else {
197                             fLog.println("Error: some imported values are not exist in target document!");
198                             return false;
199                         }
200                     }
201                     return true;
202                 }
203             });
204 
205         return tEnv;
206     }
207 }
208