xref: /AOO41X/main/qadevOOo/tests/java/mod/_xmloff/Draw/XMLImporter.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._xmloff.Draw;
29 
30 import java.io.PrintWriter;
31 
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.SOfficeFactory;
37 
38 import com.sun.star.container.XNamed;
39 import com.sun.star.drawing.XDrawPages;
40 import com.sun.star.drawing.XDrawPagesSupplier;
41 import com.sun.star.lang.XComponent;
42 import com.sun.star.lang.XMultiServiceFactory;
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.uno.XInterface;
45 
46 /**
47  * Test for object which is represented by service
48  * <code>com.sun.star.comp.Draw.XMLImporter</code>. <p>
49  * Object implements the following interfaces :
50  * <ul>
51  *  <li><code>com::sun::star::lang::XInitialization</code></li>
52  *  <li><code>com::sun::star::document::XImporter</code></li>
53  *  <li><code>com::sun::star::document::XFilter</code></li>
54  *  <li><code>com::sun::star::document::ImportFilter</code></li>
55  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
56  *  <li><code>com::sun::star::xml::sax::XDocumentHandler</code></li>
57  * </ul>
58  * @see com.sun.star.lang.XInitialization
59  * @see com.sun.star.document.XImporter
60  * @see com.sun.star.document.XFilter
61  * @see com.sun.star.document.ImportFilter
62  * @see com.sun.star.beans.XPropertySet
63  * @see com.sun.star.xml.sax.XDocumentHandler
64  * @see ifc.lang._XInitialization
65  * @see ifc.document._XImporter
66  * @see ifc.document._XFilter
67  * @see ifc.document._XExporter
68  * @see ifc.beans._XPropertySet
69  * @see ifc.xml.sax._XDocumentHandler
70  */
71 public class XMLImporter extends TestCase {
72     XComponent xDrawDoc = null;
73 
74     /**
75     * New spreadsheet document created.
76     */
77     protected void initialize( TestParameters tParam, PrintWriter log ) {
78 
79         // get a soffice factory object
80         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
81 
82         try {
83             log.println( "creating a drawdocument" );
84             xDrawDoc = SOF.createDrawDoc(null);
85         } catch ( Exception e ) {
86             // Some exception occures.FAILED
87             e.printStackTrace( log );
88             throw new StatusException( "Couldn't create document", e );
89         }
90     }
91 
92     /**
93     * Spreadsheet document destroyed.
94     */
95     protected void cleanup( TestParameters tParam, PrintWriter log ) {
96         log.println( "    disposing document " );
97         xDrawDoc.dispose();
98     }
99 
100     /**
101     * Creating a Testenvironment for the interfaces to be tested.
102     * Creates an instance of the service
103     * <code>com.sun.star.comp.Draw.XMLImporter</code><p>
104     *
105     * The Draw document is set as a target document for importer.
106     * Imported XML-data contains only content tags including pages
107     * named 'NewSlide1' and 'NewSlide2'.
108     * Pages getting from target document are checked after import.
109     *     Object relations created :
110     * <ul>
111     *  <li> <code>'XDocumentHandler.XMLData'</code> for
112     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
113     *  <li> <code>'XDocumentHandler.ImportChecker'</code> for
114     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
115     *  <li> <code>'TargetDocument'</code> for
116     *      {@link ifc.document._XImporter} interface </li>
117     * </ul>
118     */
119     public synchronized TestEnvironment createTestEnvironment
120         (TestParameters tParam, PrintWriter log ) throws StatusException {
121 
122         XInterface oObj = null;
123         Object oInt = null ;
124 
125         // creation of testobject here
126         // first we write what we are intend to do to log file
127         log.println( "creating a test environment" );
128 
129         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
130 
131         try {
132             oInt = xMSF.createInstance("com.sun.star.comp.Draw.XMLImporter") ;
133 
134         } catch (com.sun.star.uno.Exception e) {
135             e.printStackTrace(log) ;
136             throw new StatusException("Can't create component.", e) ;
137         }
138 
139         oObj = (XInterface) oInt ;
140 
141         // create testobject here
142         log.println( "creating a new environment for Paragraph object" );
143         TestEnvironment tEnv = new TestEnvironment( oObj );
144 
145         // adding relation
146         tEnv.addObjRelation("TargetDocument", xDrawDoc) ;
147 
148         // adding relation for XDocumentHandler
149         String[][] xml = new String[][] {
150             {"start", "office:document",
151                 "xmlns:office", "CDATA", "http://openoffice.org/2000/office",
152                 "xmlns:draw", "CDATA", "http://openoffice.org/2000/drawing",
153                 "office:class", "CDATA", "drawing"
154                 ,"office:version", "CDATA", "1.0"
155                 },
156             {"start", "office:body"},
157             {"start", "draw:page",
158                 "draw:name", "CDATA", "NewSlide1",
159                 "draw:master-page-name", "CDATA", "Default"},
160             {"end", "draw:page"},
161             {"start", "draw:page",
162                 "draw:name", "CDATA", "NewSlide2",
163                 "draw:master-page-name", "CDATA", "Default"},
164             {"end","draw:page"},
165             {"end", "draw:page"},
166             {"end", "office:body"},
167             {"end", "office:document"}} ;
168 
169         tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
170 
171         //save to log content before import
172         final PrintWriter fLog = log;
173 
174         // Checking target document after import
175         tEnv.addObjRelation("XDocumentHandler.ImportChecker",
176             new ifc.xml.sax._XDocumentHandler.ImportChecker() {
177                 public boolean checkImport() {
178                     XDrawPagesSupplier supp = (XDrawPagesSupplier)
179                         UnoRuntime.queryInterface
180                         (XDrawPagesSupplier.class, xDrawDoc);
181                     final XDrawPages xPages = supp.getDrawPages();
182                     XNamed[] pageArray = new XNamed[ xPages.getCount() ];
183                     for (int i=0; i < xPages.getCount(); i++) {
184                         try {
185                             pageArray[i] = (XNamed) UnoRuntime.queryInterface
186                                 (XNamed.class, xPages.getByIndex(i));
187                         } catch (com.sun.star.uno.Exception e) {
188                             e.printStackTrace(fLog) ;
189                             throw new StatusException
190                                 ("Can't get page name by index.", e) ;
191                         }
192                     }
193                     fLog.println("Slide names after import:");
194                     for ( int i = 0; i < xPages.getCount(); i++ ) {
195                         if ((pageArray[i].getName().equals("NewSlide1"))
196                             || (pageArray[i].getName().equals("NewSlide2"))) {
197                             fLog.println("  " + pageArray[i].getName());
198                         }
199                         else {
200                             fLog.println("Error: some imported values are not exist in target document!");
201                             return false;
202                         }
203                     }
204                     return true;
205                 }
206             });
207 
208         return tEnv;
209     } // end of getTestEnvironment
210 
211 }
212