xref: /trunk/test/testuno/source/fvt/uno/db/DBAccess.java (revision ffd38472365e95f6a578737bc9a5eb0fac624a86)
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 package fvt.uno.db;
23 
24 import com.sun.star.beans.PropertyValue;
25 import com.sun.star.container.XNameAccess;
26 import com.sun.star.frame.FrameSearchFlag;
27 import com.sun.star.frame.XComponentLoader;
28 import com.sun.star.frame.XModel;
29 import com.sun.star.frame.XStorable;
30 import com.sun.star.lang.XComponent;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.sdb.XOfficeDatabaseDocument;
33 import com.sun.star.sdb.application.XDatabaseDocumentUI;
34 import com.sun.star.sdbcx.XTablesSupplier;
35 import com.sun.star.uno.Exception;
36 import com.sun.star.uno.UnoRuntime;
37 
38 import testlib.uno.HsqlColumnDescriptor;
39 import testlib.uno.DBUtil;
40 
41 import java.io.IOException;
42 
43 // ---------- junit imports -----------------
44 import org.junit.After;
45 import org.junit.AfterClass;
46 import org.junit.Before;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 import org.openoffice.test.common.FileUtil;
50 import org.openoffice.test.common.Testspace;
51 import org.openoffice.test.uno.UnoApp;
52 
53 import static org.junit.Assert.*;
54 
55 /**
56  * test case for Base's application UI
57  */
58 public class DBAccess {
59     static final UnoApp app = new UnoApp();
60     private XOfficeDatabaseDocument m_databaseDocument;
61     private XDatabaseDocumentUI m_documentUI;
62 
63 //  public DBAccess() {
64 //      super();
65 //  }
66 
67     @BeforeClass
68     public static void setUpConnection() throws Exception {
69         app.start();
70     }
71 
72     @AfterClass
73     public static void tearDownConnection() throws InterruptedException, Exception {
74         app.close();
75     }
76 
77     @Before
78     public void before() throws java.lang.Exception {
79         String a = null;
80         switchToDocument(a);
81     }
82 
83     @After
84     public void after() throws java.lang.Exception {
85         closeDocument();
86     }
87 
88     private void closeDocument() {
89         DBUtil.close();
90         m_databaseDocument = null;
91         m_documentUI = null;
92 
93     }
94 
95     private void switchToDocument(String _documentURL)
96             throws java.lang.Exception {
97         // close previous database document
98         closeDocument();
99 
100         if (_documentURL == null) {
101             DBUtil.createNewDocument(getMSF());
102         } else {
103             DBUtil.loadNewDocument(getMSF(), _documentURL);
104         }
105         m_databaseDocument = DBUtil.getDatabaseDocument();
106 
107     }
108 
109 
110     @Test
111     public void testSaveAs() throws Exception, IOException, java.lang.Exception {
112 
113         m_databaseDocument = saveAndReloadDoc(m_databaseDocument, "", "odb");
114         XModel docModel = (XModel) UnoRuntime.queryInterface(XModel.class,
115                 m_databaseDocument);
116         m_documentUI = (XDatabaseDocumentUI) UnoRuntime.queryInterface(XDatabaseDocumentUI.class,
117                 docModel.getCurrentController());
118         m_documentUI.connect();
119         assertTrue("could not connect to " + DBUtil.getDocumentURL(),
120                 m_documentUI.isConnected());
121 
122     }
123 
124     @Test
125     public void testCreateTable() throws java.lang.Exception {
126         // create a table in the database
127         DBUtil.createTable("test", new HsqlColumnDescriptor[] {
128                 new HsqlColumnDescriptor("a", "VARCHAR(50)"),
129                 new HsqlColumnDescriptor("b", "VARCHAR(50)"),
130                 new HsqlColumnDescriptor("c", "VARCHAR(50)") });
131         switchToDocument(DBUtil.getDocumentURL());
132         // ---save and reload database document
133         m_databaseDocument = saveAndReloadDoc(m_databaseDocument, "", "odb");
134 
135         XModel docModel = (XModel) UnoRuntime.queryInterface(XModel.class,
136                 m_databaseDocument);
137         m_documentUI = (XDatabaseDocumentUI) UnoRuntime.queryInterface(XDatabaseDocumentUI.class,
138                 docModel.getCurrentController());
139         m_documentUI.connect();
140         XTablesSupplier suppTables = (XTablesSupplier) UnoRuntime.queryInterface(
141                 XTablesSupplier.class, m_documentUI.getActiveConnection());
142         XNameAccess tables = suppTables.getTables();
143         assertTrue("the newly created table has not been written",
144                 tables.hasByName("test"));
145     }
146 
147     protected XMultiServiceFactory getMSF() {
148         final XMultiServiceFactory xMSF1 = (XMultiServiceFactory) UnoRuntime.queryInterface(
149                 XMultiServiceFactory.class, app.getComponentContext()
150                         .getServiceManager());
151         return xMSF1;
152     }
153 
154     private XOfficeDatabaseDocument saveAndReloadDoc(
155             XOfficeDatabaseDocument m_databaseDocument2, String sFilter,
156             String sExtension) throws java.lang.Exception {
157         String filePath = Testspace.getPath("tmp/basetest." + sExtension);
158         PropertyValue[] aStoreProperties = new PropertyValue[2];
159         aStoreProperties[0] = new PropertyValue();
160         aStoreProperties[1] = new PropertyValue();
161         aStoreProperties[0].Name = "Override";
162         aStoreProperties[0].Value = true;
163         aStoreProperties[1].Name = "FilterName";
164         XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
165                 XStorable.class, m_databaseDocument2);
166         xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
167 
168         return (XOfficeDatabaseDocument) UnoRuntime.queryInterface(XOfficeDatabaseDocument.class,
169                 app.loadDocument(filePath));
170     }
171 }
172