xref: /AOO41X/main/ucb/qa/complex/tdoc/CheckTransientDocumentsContentProvider.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package complex.tdoc;
28 
29 import com.sun.star.lang.XMultiServiceFactory;
30 import com.sun.star.lang.XServiceInfo;
31 import com.sun.star.sdbc.XResultSet;
32 import com.sun.star.text.XTextDocument;
33 import com.sun.star.ucb.Command;
34 import com.sun.star.ucb.OpenCommandArgument2;
35 import com.sun.star.ucb.OpenMode;
36 import com.sun.star.ucb.XCommandProcessor;
37 import com.sun.star.ucb.XContent;
38 import com.sun.star.ucb.XContentAccess;
39 import com.sun.star.ucb.XContentIdentifier;
40 import com.sun.star.ucb.XContentIdentifierFactory;
41 import com.sun.star.ucb.XContentProvider;
42 import com.sun.star.ucb.XDynamicResultSet;
43 import com.sun.star.uno.UnoRuntime;
44 // import complexlib.ComplexTestCase;
45 import util.WriterTools;
46 
47 import org.junit.After;
48 import org.junit.AfterClass;
49 import org.junit.Before;
50 import org.junit.BeforeClass;
51 import org.junit.Test;
52 import org.openoffice.test.OfficeConnection;
53 import static org.junit.Assert.*;
54 
55 /**
56  *
57  */
58 public class CheckTransientDocumentsContentProvider {
59     // TODO: document doesn't exists
60     private final String testDocuments[] = new String[]{/*"sForm.sxw",*/ "chinese.sxw", "Iterator.sxw"};
61     private final int countDocs = testDocuments.length;
62     private XMultiServiceFactory xMSF = null;
63     private XTextDocument[] xTextDoc = null;
64 
65     public String[] getTestMethodNames() {
66         return new String[]{"checkTransientDocumentsContentProvider"};
67     }
68 
69     @Before public void before() {
70         xMSF = getMSF();
71         xTextDoc = new XTextDocument[countDocs];
72         System.out.println("Open some documents.");
73         for (int i=0; i<countDocs; i++) {
74             String fileName = TestDocument.getUrl(testDocuments[i]);
75             xTextDoc[i] = WriterTools.loadTextDoc(xMSF, fileName);
76             assertNotNull("Can't load document " + fileName, xTextDoc[i]);
77         }
78     }
79     @After public void after() {
80         System.out.println("Close all documents.");
81         for (int i=0; i<countDocs; i++) {
82             xTextDoc[i].dispose();
83         }
84     }
85 
86     /**
87      * Check the provider of document content: open some documents
88      * and look if they are accessible.
89      */
90     @Test public void checkTransientDocumentsContentProvider() {
91         try {
92             // create a content provider
93             Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
94             XContentProvider xContentProvider =
95                             UnoRuntime.queryInterface(XContentProvider.class, o);
96 
97             // create the ucb
98             XContentIdentifierFactory xContentIdentifierFactory =
99                             UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
100             // create a content identifier from the ucb for tdoc
101             XContentIdentifier xContentIdentifier =
102                             xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/");
103             // get content
104             XContent xContent = xContentProvider.queryContent(xContentIdentifier);
105 
106             // actual test: execute an "open" command with the content
107             XCommandProcessor xCommandProcessor = UnoRuntime.queryInterface(XCommandProcessor.class, xContent);
108             // build up the command
109             Command command = new Command();
110             OpenCommandArgument2 commandarg2 = new OpenCommandArgument2();
111             commandarg2.Mode = OpenMode.ALL;
112             command.Name = "open";
113             command.Argument = commandarg2;
114 
115             // execute the command
116             Object result = xCommandProcessor.execute(command, 0, null);
117 
118             // check the result
119             System.out.println("Result: "+ result.getClass().toString());
120             XDynamicResultSet xDynamicResultSet = UnoRuntime.queryInterface(XDynamicResultSet.class, result);
121 
122             // check bug of wrong returned service name.
123             XServiceInfo xServiceInfo = UnoRuntime.queryInterface(XServiceInfo.class, xDynamicResultSet);
124             String[] sNames = xServiceInfo.getSupportedServiceNames();
125             String serviceName = sNames[0];
126             if (sNames.length > 1)
127             {
128                 fail("Implementation has been changed. Check this test!");
129             }
130             assertTrue("The service name '" + serviceName + "' is not valid.", !serviceName.equals("com.sun.star.ucb.DynamicContentResultSet"));
131 
132             XResultSet xResultSet = xDynamicResultSet.getStaticResultSet();
133             XContentAccess xContentAccess = UnoRuntime.queryInterface(XContentAccess.class, xResultSet);
134             // iterate over the result: three docs were opened, we should have at least three content identifier strings
135             int countContentIdentifiers = 0;
136             while(xResultSet.next()) {
137                 countContentIdentifiers++;
138                 String identifier = xContentAccess.queryContentIdentifierString();
139                 System.out.println("Identifier of row " + xResultSet.getRow() + ": " + identifier);
140             }
141             // some feeble test: if the amount >2, we're ok.
142             // 2do: check better
143             assertTrue("Did only find " + countContentIdentifiers + " open documents." +
144                         " Should have been at least 3.", countContentIdentifiers>2);
145         }
146         catch (com.sun.star.uno.Exception e) {
147             e.printStackTrace();
148             fail("Could not create test objects.");
149         }
150 
151     }
152 
153      private XMultiServiceFactory getMSF()
154     {
155         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
156         return xMSF1;
157     }
158 
159     // setup and close connections
160     @BeforeClass public static void setUpConnection() throws Exception {
161         System.out.println("setUpConnection()");
162         connection.setUp();
163     }
164 
165     @AfterClass public static void tearDownConnection()
166         throws InterruptedException, com.sun.star.uno.Exception
167     {
168         System.out.println("tearDownConnection()");
169         connection.tearDown();
170     }
171 
172     private static final OfficeConnection connection = new OfficeConnection();
173 
174 }
175