1*c07c6c98SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*c07c6c98SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c07c6c98SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c07c6c98SAndrew Rist  * distributed with this work for additional information
6*c07c6c98SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c07c6c98SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c07c6c98SAndrew Rist  * "License"); you may not use this file except in compliance
9*c07c6c98SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c07c6c98SAndrew Rist  *
11*c07c6c98SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c07c6c98SAndrew Rist  *
13*c07c6c98SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c07c6c98SAndrew Rist  * software distributed under the License is distributed on an
15*c07c6c98SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c07c6c98SAndrew Rist  * KIND, either express or implied.  See the License for the
17*c07c6c98SAndrew Rist  * specific language governing permissions and limitations
18*c07c6c98SAndrew Rist  * under the License.
19*c07c6c98SAndrew Rist  *
20*c07c6c98SAndrew Rist  *************************************************************/
21*c07c6c98SAndrew Rist 
22*c07c6c98SAndrew Rist 
23cdf0e10cSrcweir package complex.tdoc;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.beans.Property;
26cdf0e10cSrcweir import com.sun.star.beans.XPropertySetInfo;
27cdf0e10cSrcweir import com.sun.star.frame.XModel;
28cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
29cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
30cdf0e10cSrcweir import com.sun.star.ucb.Command;
31cdf0e10cSrcweir import com.sun.star.ucb.XCommandProcessor;
32cdf0e10cSrcweir import com.sun.star.ucb.XContent;
33cdf0e10cSrcweir import com.sun.star.ucb.XContentIdentifier;
34cdf0e10cSrcweir import com.sun.star.ucb.XContentIdentifierFactory;
35cdf0e10cSrcweir import com.sun.star.ucb.XContentProvider;
36cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
37cdf0e10cSrcweir import util.WriterTools;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir import org.junit.After;
40cdf0e10cSrcweir import org.junit.AfterClass;
41cdf0e10cSrcweir import org.junit.Before;
42cdf0e10cSrcweir import org.junit.BeforeClass;
43cdf0e10cSrcweir import org.junit.Test;
44cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
45cdf0e10cSrcweir import static org.junit.Assert.*;
46cdf0e10cSrcweir /**
47cdf0e10cSrcweir  *
48cdf0e10cSrcweir  */
49cdf0e10cSrcweir public class CheckTransientDocumentsContent {
50cdf0e10cSrcweir     // TODO: document doesn't exists
51cdf0e10cSrcweir     private final String testDocuments[] = new String[]{"sForm.sxw"};//, "chinese.sxw", "Iterator.sxw"};
52cdf0e10cSrcweir     private final int countDocs = testDocuments.length;
53cdf0e10cSrcweir     private XMultiServiceFactory xMSF = null;
54cdf0e10cSrcweir     private XTextDocument[] xTextDoc = null;
55cdf0e10cSrcweir 
getTestMethodNames()56cdf0e10cSrcweir     public String[] getTestMethodNames() {
57cdf0e10cSrcweir         return new String[] {"checkTransientDocumentsContent"};
58cdf0e10cSrcweir     }
59cdf0e10cSrcweir 
before()60cdf0e10cSrcweir     @Before public void before() {
61cdf0e10cSrcweir         xMSF = getMSF();
62cdf0e10cSrcweir         xTextDoc = new XTextDocument[countDocs];
63cdf0e10cSrcweir         System.out.println("Open some documents.");
64cdf0e10cSrcweir         for (int i=0; i<countDocs; i++) {
65cdf0e10cSrcweir             String fileName = TestDocument.getUrl(testDocuments[i]);
66cdf0e10cSrcweir             xTextDoc[i] = WriterTools.loadTextDoc(xMSF, fileName);
67cdf0e10cSrcweir             assertNotNull("Can't load document " + fileName, xTextDoc[i]);
68cdf0e10cSrcweir         }
69cdf0e10cSrcweir     }
after()70cdf0e10cSrcweir     @After public void after() {
71cdf0e10cSrcweir         System.out.println("Close all documents.");
72cdf0e10cSrcweir         for (int i=0; i<countDocs; i++) {
73cdf0e10cSrcweir             xTextDoc[i].dispose();
74cdf0e10cSrcweir         }
75cdf0e10cSrcweir     }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     /**
78cdf0e10cSrcweir      * Check the content of one document
79cdf0e10cSrcweir      */
checkTransientDocumentsContent()80cdf0e10cSrcweir     @Test public void checkTransientDocumentsContent() {
81cdf0e10cSrcweir         try {
82cdf0e10cSrcweir             // create the ucb
83cdf0e10cSrcweir             XContentIdentifierFactory xContentIdentifierFactory =
84cdf0e10cSrcweir                             UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
85cdf0e10cSrcweir             XContentProvider xContentProvider =
86cdf0e10cSrcweir                             UnoRuntime.queryInterface(XContentProvider.class, xContentIdentifierFactory);
87cdf0e10cSrcweir             // create a content identifier from the ucb for tdoc
88cdf0e10cSrcweir             XContentIdentifier xContentIdentifier =
89cdf0e10cSrcweir                                xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/1");
90cdf0e10cSrcweir             // get content
91cdf0e10cSrcweir             XContent xContent = xContentProvider.queryContent(xContentIdentifier);
92cdf0e10cSrcweir 
93cdf0e10cSrcweir             // actual test: commands to get some properties
94cdf0e10cSrcweir             XCommandProcessor xCommandProcessor = UnoRuntime.queryInterface(XCommandProcessor.class, xContent);
95cdf0e10cSrcweir             // build up the command
96cdf0e10cSrcweir             Command command = new Command();
97cdf0e10cSrcweir             command.Name = "getPropertySetInfo";
98cdf0e10cSrcweir             command.Handle = -1;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir             // execute the command
101cdf0e10cSrcweir             Object result = xCommandProcessor.execute(command, 0, null);
102cdf0e10cSrcweir 
103cdf0e10cSrcweir             // check the result
104cdf0e10cSrcweir             System.out.println("Result: "+ result.getClass().toString());
105cdf0e10cSrcweir             XPropertySetInfo xPropertySetInfo = UnoRuntime.queryInterface(XPropertySetInfo.class, result);
106cdf0e10cSrcweir             Property[] props = xPropertySetInfo.getProperties();
107cdf0e10cSrcweir             boolean res = false;
108cdf0e10cSrcweir             for(int i=0; i<props.length; i++) {
109cdf0e10cSrcweir                 String propName = props[i].Name;
110cdf0e10cSrcweir                 res |= propName.equals("DocumentModel");
111cdf0e10cSrcweir                 System.out.println("Found property: " + propName + "   type: " + props[i].Type.getTypeName());
112cdf0e10cSrcweir             }
113cdf0e10cSrcweir             assertNotNull("Did not find property 'DocumentModel' in the Property array.", res);
114cdf0e10cSrcweir 
115cdf0e10cSrcweir             // get on property
116cdf0e10cSrcweir             command.Name = "getPropertyValues";
117cdf0e10cSrcweir             command.Handle = -1;
118cdf0e10cSrcweir             Property[] prop = new Property[1];
119cdf0e10cSrcweir             prop[0] = new Property();
120cdf0e10cSrcweir             prop[0].Name = "DocumentModel";
121cdf0e10cSrcweir             prop[0].Handle = -1;
122cdf0e10cSrcweir             command.Argument = prop;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir             // execute the command
125cdf0e10cSrcweir             result = xCommandProcessor.execute(command, 0, null);
126cdf0e10cSrcweir 
127cdf0e10cSrcweir             // check the result
128cdf0e10cSrcweir             System.out.println("Result: "+ result.getClass().toString());
129cdf0e10cSrcweir 
130cdf0e10cSrcweir             XModel xModel = UnoRuntime.queryInterface(XModel.class, result);
131cdf0e10cSrcweir             assertTrue("Did not get property 'DocumentModel'.", xModel == null);
132cdf0e10cSrcweir         }
133cdf0e10cSrcweir         catch (com.sun.star.uno.Exception e) {
134cdf0e10cSrcweir             e.printStackTrace();
135cdf0e10cSrcweir             fail("Could not create test objects.");
136cdf0e10cSrcweir         }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir 
getMSF()140cdf0e10cSrcweir      private XMultiServiceFactory getMSF()
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
143cdf0e10cSrcweir         return xMSF1;
144cdf0e10cSrcweir     }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     // setup and close connections
setUpConnection()147cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
148cdf0e10cSrcweir         System.out.println("setUpConnection()");
149cdf0e10cSrcweir         connection.setUp();
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir 
tearDownConnection()152cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
153cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
154cdf0e10cSrcweir     {
155cdf0e10cSrcweir         System.out.println("tearDownConnection()");
156cdf0e10cSrcweir         connection.tearDown();
157cdf0e10cSrcweir     }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 
162cdf0e10cSrcweir }
163