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 package complex.sfx2.standalonedocinfo;
24 
25 import complex.sfx2.standalonedocinfo.TestHelper;
26 import complex.sfx2.standalonedocinfo.StandaloneDocumentInfoTest;
27 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.document.XStandaloneDocumentInfo;
29 import com.sun.star.io.XTempFile;
30 import com.sun.star.frame.XLoadable;
31 import com.sun.star.frame.XStorable;
32 import com.sun.star.beans.PropertyValue;
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.uno.AnyConverter;
36 
37 
38 public class Test01 implements StandaloneDocumentInfoTest {
39     XMultiServiceFactory m_xMSF = null;
40     TestHelper m_aTestHelper = null;
41 
Test01( XMultiServiceFactory xMSF )42     public Test01 ( XMultiServiceFactory xMSF ) {
43         m_xMSF = xMSF;
44         m_aTestHelper = new TestHelper( "Test01: " );
45     }
46 
test()47     public boolean test() {
48         try {
49             final String sDocTitle [] = new String [] {
50                 "ODF_Doc", "OOo6_Doc", "old_binary_Doc" };
51             final String sFilterName [] = new String [] {
52                 "writer8", "StarOffice XML (Writer)", "StarWriter 5.0" };
53 
54             for (int i = 0; i < 3; ++i ) {
55                 m_aTestHelper.Message ( "==============================" );
56                 m_aTestHelper.Message ( sFilterName[i] );
57                 m_aTestHelper.Message ( "==============================" );
58                 //create a new temporary file
59                 Object oTempFile = m_xMSF.createInstance ( "com.sun.star.io.TempFile" );
60                 XTempFile xTempFile = UnoRuntime.queryInterface(XTempFile.class, oTempFile);
61 
62                 //create a text document and initiallize it
63                 Object oTextDocument = m_xMSF.createInstance ( "com.sun.star.text.TextDocument" );
64                 XLoadable xLoadable = UnoRuntime.queryInterface(XLoadable.class, oTextDocument);
65                 xLoadable.initNew();
66                 m_aTestHelper.Message ( "New document initialized." );
67 
68                 //store the instance to the temporary file URL
69                 XStorable xStorable = UnoRuntime.queryInterface(XStorable.class, oTextDocument);
70                 String sURL = AnyConverter.toString ( xTempFile.getUri () );
71                 PropertyValue aProps[] = new PropertyValue[2];
72                 aProps[0] = new PropertyValue();
73                 aProps[0].Name = "DocumentTitle";
74                 aProps[0].Value = sDocTitle[i];
75                 aProps[1] = new PropertyValue();
76                 aProps[1].Name = "FilterName";
77                 aProps[1].Value = sFilterName[i];
78                 m_aTestHelper.Message ( "Set title: " +
79                         sDocTitle[i] );
80                 xStorable.storeToURL ( sURL, aProps );
81                 m_aTestHelper.Message ( "Document stored." );
82 
83                 //create StandaloneDocumentInfo object and load it from the file
84                 Object oStandaloneDocInfo = m_xMSF.createInstance (
85                         "com.sun.star.document.StandaloneDocumentInfo" );
86                 XStandaloneDocumentInfo xStandaloneDocInfo =
87                         UnoRuntime.queryInterface(XStandaloneDocumentInfo.class, oStandaloneDocInfo);
88                 xStandaloneDocInfo.loadFromURL ( sURL );
89                 m_aTestHelper.Message ( "StandaloneDocumentInfo loaded." );
90 
91                 //get the title from the object and check it
92                 XPropertySet xPropSet =
93                         UnoRuntime.queryInterface(XPropertySet.class, oStandaloneDocInfo);
94                 String sTitle = xPropSet.getPropertyValue ( "Title" ).toString ();
95                 m_aTestHelper.Message ( "Get title: " + sTitle );
96                 if ( sTitle.compareTo ( sDocTitle[i] ) != 0 ) {
97                     m_aTestHelper.Error ( "Title not match. Expected \""
98                             + sDocTitle[i] +
99                             "\"" );
100                     return false;
101                 } else {
102                     m_aTestHelper.Message ( "Title matched." );
103                 }
104 
105                 //set a new title to the object
106                 sTitle += "_new";
107                 xPropSet.setPropertyValue ( "Title", sTitle );
108                 m_aTestHelper.Message ( "Set new title: " + sTitle );
109 
110                 //store the object to the same file
111                 xStandaloneDocInfo.storeIntoURL ( sURL );
112                 m_aTestHelper.Message ( "Document info stored." );
113 
114                 //create a new StandaloneDocumentInfo object and load it from the file
115                 Object oStandaloneDocInfo_ = m_xMSF.createInstance (
116                         "com.sun.star.document.StandaloneDocumentInfo" );
117                 XStandaloneDocumentInfo xStandaloneDocInfo_ =
118                         UnoRuntime.queryInterface(XStandaloneDocumentInfo.class, oStandaloneDocInfo_);
119                 xStandaloneDocInfo_.loadFromURL ( sURL );
120                 m_aTestHelper.Message ( "New StandaloneDocumentInfo loaded." );
121 
122                 //get the title and check it
123                 XPropertySet xPropSet_ = UnoRuntime.queryInterface(XPropertySet.class, oStandaloneDocInfo_);
124                 String sTitle_ = xPropSet_.getPropertyValue ( "Title" ).toString ();
125                 m_aTestHelper.Message ( "Get new title: " + sTitle_ );
126                 if ( sTitle_.compareTo ( sTitle ) != 0 ) {
127                     m_aTestHelper.Error ( "New title not matched. Expected: \"" + sTitle
128                             + "\"." );
129                     return false;
130                 } else {
131                     m_aTestHelper.Message ( "New title matched." );
132                 }
133             }
134         } catch ( Exception e ) {
135             m_aTestHelper.Error( "Exception: " + e );
136             return false;
137         }
138         return true;
139     }
140 }
141