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.tdoc;
24 
25 import com.sun.star.beans.Property;
26 import com.sun.star.beans.XPropertySetInfo;
27 import com.sun.star.frame.XModel;
28 import com.sun.star.lang.XMultiServiceFactory;
29 import com.sun.star.text.XTextDocument;
30 import com.sun.star.ucb.Command;
31 import com.sun.star.ucb.XCommandProcessor;
32 import com.sun.star.ucb.XContent;
33 import com.sun.star.ucb.XContentIdentifier;
34 import com.sun.star.ucb.XContentIdentifierFactory;
35 import com.sun.star.ucb.XContentProvider;
36 import com.sun.star.uno.UnoRuntime;
37 import util.WriterTools;
38 
39 import org.junit.After;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.openoffice.test.OfficeConnection;
45 import static org.junit.Assert.*;
46 /**
47  *
48  */
49 public class CheckTransientDocumentsContent {
50     // TODO: document doesn't exists
51     private final String testDocuments[] = new String[]{"sForm.sxw"};//, "chinese.sxw", "Iterator.sxw"};
52     private final int countDocs = testDocuments.length;
53     private XMultiServiceFactory xMSF = null;
54     private XTextDocument[] xTextDoc = null;
55 
56     public String[] getTestMethodNames() {
57         return new String[] {"checkTransientDocumentsContent"};
58     }
59 
60     @Before public void before() {
61         xMSF = getMSF();
62         xTextDoc = new XTextDocument[countDocs];
63         System.out.println("Open some documents.");
64         for (int i=0; i<countDocs; i++) {
65             String fileName = TestDocument.getUrl(testDocuments[i]);
66             xTextDoc[i] = WriterTools.loadTextDoc(xMSF, fileName);
67             assertNotNull("Can't load document " + fileName, xTextDoc[i]);
68         }
69     }
70     @After public void after() {
71         System.out.println("Close all documents.");
72         for (int i=0; i<countDocs; i++) {
73             xTextDoc[i].dispose();
74         }
75     }
76 
77     /**
78      * Check the content of one document
79      */
80     @Test public void checkTransientDocumentsContent() {
81         try {
82             // create the ucb
83             XContentIdentifierFactory xContentIdentifierFactory =
84                             UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
85             XContentProvider xContentProvider =
86                             UnoRuntime.queryInterface(XContentProvider.class, xContentIdentifierFactory);
87             // create a content identifier from the ucb for tdoc
88             XContentIdentifier xContentIdentifier =
89                                xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/1");
90             // get content
91             XContent xContent = xContentProvider.queryContent(xContentIdentifier);
92 
93             // actual test: commands to get some properties
94             XCommandProcessor xCommandProcessor = UnoRuntime.queryInterface(XCommandProcessor.class, xContent);
95             // build up the command
96             Command command = new Command();
97             command.Name = "getPropertySetInfo";
98             command.Handle = -1;
99 
100             // execute the command
101             Object result = xCommandProcessor.execute(command, 0, null);
102 
103             // check the result
104             System.out.println("Result: "+ result.getClass().toString());
105             XPropertySetInfo xPropertySetInfo = UnoRuntime.queryInterface(XPropertySetInfo.class, result);
106             Property[] props = xPropertySetInfo.getProperties();
107             boolean res = false;
108             for(int i=0; i<props.length; i++) {
109                 String propName = props[i].Name;
110                 res |= propName.equals("DocumentModel");
111                 System.out.println("Found property: " + propName + "   type: " + props[i].Type.getTypeName());
112             }
113             assertNotNull("Did not find property 'DocumentModel' in the Property array.", res);
114 
115             // get on property
116             command.Name = "getPropertyValues";
117             command.Handle = -1;
118             Property[] prop = new Property[1];
119             prop[0] = new Property();
120             prop[0].Name = "DocumentModel";
121             prop[0].Handle = -1;
122             command.Argument = prop;
123 
124             // execute the command
125             result = xCommandProcessor.execute(command, 0, null);
126 
127             // check the result
128             System.out.println("Result: "+ result.getClass().toString());
129 
130             XModel xModel = UnoRuntime.queryInterface(XModel.class, result);
131             assertTrue("Did not get property 'DocumentModel'.", xModel == null);
132         }
133         catch (com.sun.star.uno.Exception e) {
134             e.printStackTrace();
135             fail("Could not create test objects.");
136         }
137 
138     }
139 
140      private XMultiServiceFactory getMSF()
141     {
142         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
143         return xMSF1;
144     }
145 
146     // setup and close connections
147     @BeforeClass public static void setUpConnection() throws Exception {
148         System.out.println("setUpConnection()");
149         connection.setUp();
150     }
151 
152     @AfterClass public static void tearDownConnection()
153         throws InterruptedException, com.sun.star.uno.Exception
154     {
155         System.out.println("tearDownConnection()");
156         connection.tearDown();
157     }
158 
159     private static final OfficeConnection connection = new OfficeConnection();
160 
161 
162 }
163