Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _xmloff / Impress / XMLStylesExporter.java
blob23f8cefed8f77344188e6b943abbfefc736355f5
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package mod._xmloff.Impress;
21 import java.io.PrintWriter;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
27 import util.SOfficeFactory;
28 import util.XMLTools;
30 import com.sun.star.container.XNameAccess;
31 import com.sun.star.container.XNameContainer;
32 import com.sun.star.document.XExporter;
33 import com.sun.star.lang.XComponent;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.style.XStyle;
36 import com.sun.star.style.XStyleFamiliesSupplier;
37 import com.sun.star.uno.Any;
38 import com.sun.star.uno.Exception;
39 import com.sun.star.uno.Type;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
42 import com.sun.star.xml.sax.XDocumentHandler;
44 /**
45 * Test for object which is represented by service
46 * <code>com.sun.star.comp.Impress.XMLStylesExporter</code>. <p>
47 * Object implements the following interfaces :
48 * <ul>
49 * <li><code>com::sun::star::lang::XInitialization</code></li>
50 * <li><code>com::sun::star::document::ExportFilter</code></li>
51 * <li><code>com::sun::star::document::XFilter</code></li>
52 * <li><code>com::sun::star::document::XExporter</code></li>
53 * <li><code>com::sun::star::beans::XPropertySet</code></li>
54 * </ul>
55 * @see com.sun.star.lang.XInitialization
56 * @see com.sun.star.document.ExportFilter
57 * @see com.sun.star.document.XFilter
58 * @see com.sun.star.document.XExporter
59 * @see com.sun.star.beans.XPropertySet
60 * @see ifc.lang._XInitialization
61 * @see ifc.document._ExportFilter
62 * @see ifc.document._XFilter
63 * @see ifc.document._XExporter
64 * @see ifc.beans._XPropertySet
66 public class XMLStylesExporter extends TestCase {
67 XComponent xImpressDoc = null;
68 protected static int counter = 0;
70 /**
71 * New spreadsheet document created.
73 @Override
74 protected void initialize( TestParameters tParam, PrintWriter log ) {
76 // get a soffice factory object
77 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF());
79 try {
80 log.println( "creating an impress document" );
81 xImpressDoc = SOF.createImpressDoc(null);
82 } catch ( Exception e ) {
83 // Some exception occurs.FAILED
84 e.printStackTrace( log );
85 throw new StatusException( "Couldn't create document", e );
89 /**
90 * Spreadsheet document disposed
92 @Override
93 protected void cleanup( TestParameters tParam, PrintWriter log ) {
94 log.println( " disposing xImpressDoc " );
95 xImpressDoc.dispose();
98 /**
99 * Creating a Testenvironment for the interfaces to be tested.
100 * Creates an instance of the service
101 * <code>com.sun.star.comp.Impress.XMLStylesExporter</code> with
102 * argument which is an implementation of <code>XDocumentHandler</code>
103 * and which can check if required tags and character data is
104 * exported. <p>
105 * The Impress document is set as a source document for exporter
106 * created. New style 'NewStyle' added to style family "Graphics".
107 * This made for checking if this style is successfully exported within
108 * the document styles information.
109 * Object relations created :
110 * <ul>
111 * <li> <code>'MediaDescriptor'</code> for
112 * {@link ifc.document._XFilter} interface </li>
113 * <li> <code>'XFilter.Checker'</code> for
114 * {@link ifc.document._XFilter} interface </li>
115 * <li> <code>'SourceDocument'</code> for
116 * {@link ifc.document._XExporter} interface </li>
117 * </ul>
119 @Override
120 public synchronized TestEnvironment createTestEnvironment
121 (TestParameters tParam, PrintWriter log) throws StatusException {
123 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
124 XMultiServiceFactory xMSF = tParam.getMSF() ;
125 XInterface oObj = null;
126 FilterChecker filter = new FilterChecker(log);
127 Any arg = new Any(new Type(XDocumentHandler.class),filter);
129 String newName = "NewGraphicsStyle" + counter++;
130 // Adding tags for checking existence of head tag and property value
131 filter.addTag(new XMLTools.Tag("office:document-styles"));
132 filter.addTag(new XMLTools.Tag("office:styles"));
133 filter.addTag(new XMLTools.Tag("style:style","style:name", newName));
135 try {
136 oObj = (XInterface) xMSF.createInstanceWithArguments(
137 "com.sun.star.comp.Impress.XMLStylesExporter",
138 new Object[] {arg});
139 XExporter xEx = UnoRuntime.queryInterface(XExporter.class,oObj);
140 xEx.setSourceDocument(xImpressDoc);
142 // Obtaining and changing property values
143 XStyleFamiliesSupplier styleSup = UnoRuntime.queryInterface
144 (XStyleFamiliesSupplier.class, xImpressDoc) ;
145 XNameAccess styleFamilies = styleSup.getStyleFamilies();
146 String[] styleFamiliesNames = styleFamilies.getElementNames();
147 XNameContainer StyleFamilyName = UnoRuntime.queryInterface(XNameContainer.class,
148 styleFamilies.getByName(styleFamiliesNames[0]));
149 Object SC = SOF.createInstance
150 (xImpressDoc, "com.sun.star.style.Style");
151 XStyle StylePage = UnoRuntime.queryInterface(XStyle.class,SC);
152 StyleFamilyName.insertByName(newName, StylePage);
154 } catch (com.sun.star.uno.Exception e) {
155 e.printStackTrace(log) ;
156 throw new StatusException("Can't create component.", e) ;
159 // create testobject here
160 log.println( "creating a new environment" );
161 TestEnvironment tEnv = new TestEnvironment( oObj );
163 tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
164 new String[] {"FilterName"},
165 new Object[] {"simpress: StarOffice XML (Impress)"}));
166 tEnv.addObjRelation("SourceDocument",xImpressDoc);
167 tEnv.addObjRelation("XFilter.Checker", filter);
168 return tEnv;
172 * This class checks the XML for tags and data required and returns
173 * checking result to <code>XFilter</code> interface test. All
174 * the information about errors occurred in XML data is written
175 * to log specified.
176 * @see ifc.document._XFilter
178 private class FilterChecker extends XMLTools.XMLChecker
179 implements ifc.document._XFilter.FilterChecker {
182 * Creates a class which will write information
183 * into log specified.
185 private FilterChecker(PrintWriter log) {
186 super(log, false) ;
189 * <code>_XFilter.FilterChecker</code> interface method
190 * which returns the result of XML checking.
191 * @return <code>true</code> if the XML data exported was
192 * valid (i.e. all necessary tags and character data exists),
193 * <code>false</code> if some errors occurred.
195 public boolean checkFilter() {
196 return check();