merge the formfield patch from ooo-build
[ooovba.git] / sfx2 / qa / complex / standalonedocumentinfo / Test01.java
blob67c45b50ce08a09ac701ec353857c14c366c1edf
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Test01.java,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package complex.standalonedocumentinfo;
32 import com.sun.star.beans.Property;
33 import com.sun.star.beans.XProperty;
34 import com.sun.star.beans.XPropertySetInfo;
35 import com.sun.star.io.IOException;
36 import com.sun.star.io.XInputStream;
37 import com.sun.star.io.XOutputStream;
38 import complexlib.ComplexTestCase;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.document.XStandaloneDocumentInfo;
42 import com.sun.star.io.XTempFile;
43 import com.sun.star.frame.XLoadable;
44 import com.sun.star.frame.XStorable;
45 import com.sun.star.beans.PropertyValue;
46 import com.sun.star.beans.XPropertySet;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.uno.AnyConverter;
49 import com.sun.star.task.ErrorCodeIOException;
50 import java.util.Properties;
52 import java.util.Random;
53 import share.LogWriter;
55 public class Test01 implements StandaloneDocumentInfoTest {
56 XMultiServiceFactory m_xMSF = null;
57 TestHelper m_aTestHelper = null;
59 public Test01 ( XMultiServiceFactory xMSF, LogWriter aLogWriter ) {
60 m_xMSF = xMSF;
61 m_aTestHelper = new TestHelper( aLogWriter, "Test01: " );
64 public boolean test() {
65 try {
66 final String sDocTitle [] = new String [] {
67 "ODF_Doc", "OOo6_Doc", "old_binary_Doc" };
68 final String sFilterName [] = new String [] {
69 "writer8", "StarOffice XML (Writer)", "StarWriter 5.0" };
71 for (int i = 0; i < 3; ++i ) {
72 m_aTestHelper.Message ( "==============================" );
73 m_aTestHelper.Message ( sFilterName[i] );
74 m_aTestHelper.Message ( "==============================" );
75 //create a new temporary file
76 Object oTempFile = m_xMSF.createInstance ( "com.sun.star.io.TempFile" );
77 XTempFile xTempFile = (XTempFile) UnoRuntime.queryInterface (
78 XTempFile.class, oTempFile );
80 //create a text document and initiallize it
81 Object oTextDocument = m_xMSF.createInstance ( "com.sun.star.text.TextDocument" );
82 XLoadable xLoadable = (XLoadable) UnoRuntime.queryInterface (
83 XLoadable.class, oTextDocument );
84 xLoadable.initNew();
85 m_aTestHelper.Message ( "New document initialized." );
87 //store the instance to the temporary file URL
88 XStorable xStorable = (XStorable) UnoRuntime.queryInterface (
89 XStorable.class, oTextDocument );
90 String sURL = AnyConverter.toString ( xTempFile.getUri () );
91 PropertyValue aProps[] = new PropertyValue[2];
92 aProps[0] = new PropertyValue();
93 aProps[0].Name = "DocumentTitle";
94 aProps[0].Value = sDocTitle[i];
95 aProps[1] = new PropertyValue();
96 aProps[1].Name = "FilterName";
97 aProps[1].Value = sFilterName[i];
98 m_aTestHelper.Message ( "Set title: " +
99 sDocTitle[i] );
100 xStorable.storeToURL ( sURL, aProps );
101 m_aTestHelper.Message ( "Document stored." );
103 //create StandaloneDocumentInfo object and load it from the file
104 Object oStandaloneDocInfo = m_xMSF.createInstance (
105 "com.sun.star.document.StandaloneDocumentInfo" );
106 XStandaloneDocumentInfo xStandaloneDocInfo =
107 (XStandaloneDocumentInfo) UnoRuntime.queryInterface (
108 XStandaloneDocumentInfo.class, oStandaloneDocInfo );
109 xStandaloneDocInfo.loadFromURL ( sURL );
110 m_aTestHelper.Message ( "StandaloneDocumentInfo loaded." );
112 //get the title from the object and check it
113 XPropertySet xPropSet =
114 (XPropertySet)UnoRuntime.queryInterface (
115 XPropertySet.class, oStandaloneDocInfo );
116 String sTitle = xPropSet.getPropertyValue ( "Title" ).toString ();
117 m_aTestHelper.Message ( "Get title: " + sTitle );
118 if ( sTitle.compareTo ( sDocTitle[i] ) != 0 ) {
119 m_aTestHelper.Error ( "Title not match. Expected \""
120 + sDocTitle[i] +
121 "\"" );
122 return false;
123 } else {
124 m_aTestHelper.Message ( "Title matched." );
127 //set a new title to the object
128 sTitle += "_new";
129 xPropSet.setPropertyValue ( "Title", sTitle );
130 m_aTestHelper.Message ( "Set new title: " + sTitle );
132 //store the object to the same file
133 xStandaloneDocInfo.storeIntoURL ( sURL );
134 m_aTestHelper.Message ( "Document info stored." );
136 //create a new StandaloneDocumentInfo object and load it from the file
137 Object oStandaloneDocInfo_ = m_xMSF.createInstance (
138 "com.sun.star.document.StandaloneDocumentInfo" );
139 XStandaloneDocumentInfo xStandaloneDocInfo_ =
140 (XStandaloneDocumentInfo)UnoRuntime.queryInterface (
141 XStandaloneDocumentInfo.class, oStandaloneDocInfo_ );
142 xStandaloneDocInfo_.loadFromURL ( sURL );
143 m_aTestHelper.Message ( "New StandaloneDocumentInfo loaded." );
145 //get the title and check it
146 XPropertySet xPropSet_ = (XPropertySet)UnoRuntime.queryInterface (
147 XPropertySet.class, oStandaloneDocInfo_ );
148 String sTitle_ = xPropSet_.getPropertyValue ( "Title" ).toString ();
149 m_aTestHelper.Message ( "Get new title: " + sTitle_ );
150 if ( sTitle_.compareTo ( sTitle ) != 0 ) {
151 m_aTestHelper.Error ( "New title not matched. Expected: \"" + sTitle
152 + "\"." );
153 return false;
154 } else {
155 m_aTestHelper.Message ( "New title matched." );
158 } catch ( Exception e ) {
159 m_aTestHelper.Error( "Exception: " + e );
160 return false;
162 return true;