1eba4d44aSLiu Zhe /************************************************************** 2eba4d44aSLiu Zhe * 3eba4d44aSLiu Zhe * Licensed to the Apache Software Foundation (ASF) under one 4eba4d44aSLiu Zhe * or more contributor license agreements. See the NOTICE file 5eba4d44aSLiu Zhe * distributed with this work for additional information 6eba4d44aSLiu Zhe * regarding copyright ownership. The ASF licenses this file 7eba4d44aSLiu Zhe * to you under the Apache License, Version 2.0 (the 8eba4d44aSLiu Zhe * "License"); you may not use this file except in compliance 9eba4d44aSLiu Zhe * with the License. You may obtain a copy of the License at 10eba4d44aSLiu Zhe * 11eba4d44aSLiu Zhe * http://www.apache.org/licenses/LICENSE-2.0 12eba4d44aSLiu Zhe * 13eba4d44aSLiu Zhe * Unless required by applicable law or agreed to in writing, 14eba4d44aSLiu Zhe * software distributed under the License is distributed on an 15eba4d44aSLiu Zhe * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16eba4d44aSLiu Zhe * KIND, either express or implied. See the License for the 17eba4d44aSLiu Zhe * specific language governing permissions and limitations 18eba4d44aSLiu Zhe * under the License. 19eba4d44aSLiu Zhe * 20eba4d44aSLiu Zhe *************************************************************/ 21eba4d44aSLiu Zhe 22eba4d44aSLiu Zhe package fvt.uno.db; 23eba4d44aSLiu Zhe 24eba4d44aSLiu Zhe import com.sun.star.beans.PropertyValue; 25eba4d44aSLiu Zhe import com.sun.star.container.XNameAccess; 26eba4d44aSLiu Zhe import com.sun.star.frame.FrameSearchFlag; 27eba4d44aSLiu Zhe import com.sun.star.frame.XComponentLoader; 28eba4d44aSLiu Zhe import com.sun.star.frame.XModel; 29eba4d44aSLiu Zhe import com.sun.star.frame.XStorable; 30eba4d44aSLiu Zhe import com.sun.star.lang.XComponent; 31eba4d44aSLiu Zhe import com.sun.star.lang.XMultiServiceFactory; 32eba4d44aSLiu Zhe import com.sun.star.sdb.XOfficeDatabaseDocument; 33eba4d44aSLiu Zhe import com.sun.star.sdb.application.XDatabaseDocumentUI; 34eba4d44aSLiu Zhe import com.sun.star.sdbcx.XTablesSupplier; 35eba4d44aSLiu Zhe import com.sun.star.uno.Exception; 36eba4d44aSLiu Zhe import com.sun.star.uno.UnoRuntime; 37eba4d44aSLiu Zhe 38eba4d44aSLiu Zhe import testlib.uno.HsqlColumnDescriptor; 39eba4d44aSLiu Zhe import testlib.uno.DBUtil; 40eba4d44aSLiu Zhe 41eba4d44aSLiu Zhe import java.io.IOException; 42eba4d44aSLiu Zhe 43eba4d44aSLiu Zhe // ---------- junit imports ----------------- 44eba4d44aSLiu Zhe import org.junit.After; 45*d01bf5ecSDamjan Jovanovic import org.junit.AfterClass; 46eba4d44aSLiu Zhe import org.junit.Before; 47*d01bf5ecSDamjan Jovanovic import org.junit.BeforeClass; 48eba4d44aSLiu Zhe import org.junit.Test; 49eba4d44aSLiu Zhe import org.openoffice.test.common.FileUtil; 50eba4d44aSLiu Zhe import org.openoffice.test.common.Testspace; 51eba4d44aSLiu Zhe import org.openoffice.test.uno.UnoApp; 52eba4d44aSLiu Zhe 53eba4d44aSLiu Zhe import static org.junit.Assert.*; 54eba4d44aSLiu Zhe 55eba4d44aSLiu Zhe /** 56eba4d44aSLiu Zhe * test case for Base's application UI 57eba4d44aSLiu Zhe */ 58eba4d44aSLiu Zhe public class DBAccess { 59*d01bf5ecSDamjan Jovanovic static final UnoApp app = new UnoApp(); 60eba4d44aSLiu Zhe private XOfficeDatabaseDocument m_databaseDocument; 61eba4d44aSLiu Zhe private XDatabaseDocumentUI m_documentUI; 62eba4d44aSLiu Zhe 63eba4d44aSLiu Zhe // public DBAccess() { 64eba4d44aSLiu Zhe // super(); 65eba4d44aSLiu Zhe // } 66eba4d44aSLiu Zhe 67*d01bf5ecSDamjan Jovanovic @BeforeClass setUpConnection()68*d01bf5ecSDamjan Jovanovic public static void setUpConnection() throws Exception { 69*d01bf5ecSDamjan Jovanovic app.start(); 70*d01bf5ecSDamjan Jovanovic } 71*d01bf5ecSDamjan Jovanovic 72*d01bf5ecSDamjan Jovanovic @AfterClass tearDownConnection()73*d01bf5ecSDamjan Jovanovic public static void tearDownConnection() throws InterruptedException, Exception { 74*d01bf5ecSDamjan Jovanovic app.close(); 75*d01bf5ecSDamjan Jovanovic } 76eba4d44aSLiu Zhe 77eba4d44aSLiu Zhe @Before before()78eba4d44aSLiu Zhe public void before() throws java.lang.Exception { 79eba4d44aSLiu Zhe String a = null; 80eba4d44aSLiu Zhe switchToDocument(a); 81eba4d44aSLiu Zhe } 82eba4d44aSLiu Zhe 83eba4d44aSLiu Zhe @After after()84eba4d44aSLiu Zhe public void after() throws java.lang.Exception { 85eba4d44aSLiu Zhe closeDocument(); 86eba4d44aSLiu Zhe } 87eba4d44aSLiu Zhe closeDocument()88eba4d44aSLiu Zhe private void closeDocument() { 89eba4d44aSLiu Zhe DBUtil.close(); 90eba4d44aSLiu Zhe m_databaseDocument = null; 91eba4d44aSLiu Zhe m_documentUI = null; 92eba4d44aSLiu Zhe 93eba4d44aSLiu Zhe } 94eba4d44aSLiu Zhe switchToDocument(String _documentURL)95eba4d44aSLiu Zhe private void switchToDocument(String _documentURL) 96eba4d44aSLiu Zhe throws java.lang.Exception { 97eba4d44aSLiu Zhe // close previous database document 98eba4d44aSLiu Zhe closeDocument(); 99eba4d44aSLiu Zhe 100eba4d44aSLiu Zhe if (_documentURL == null) { 101eba4d44aSLiu Zhe DBUtil.createNewDocument(getMSF()); 102eba4d44aSLiu Zhe } else { 103eba4d44aSLiu Zhe DBUtil.loadNewDocument(getMSF(), _documentURL); 104eba4d44aSLiu Zhe } 105eba4d44aSLiu Zhe m_databaseDocument = DBUtil.getDatabaseDocument(); 106eba4d44aSLiu Zhe 107eba4d44aSLiu Zhe } 108eba4d44aSLiu Zhe 109eba4d44aSLiu Zhe 110eba4d44aSLiu Zhe @Test testSaveAs()111eba4d44aSLiu Zhe public void testSaveAs() throws Exception, IOException, java.lang.Exception { 112eba4d44aSLiu Zhe 113eba4d44aSLiu Zhe m_databaseDocument = saveAndReloadDoc(m_databaseDocument, "", "odb"); 114eba4d44aSLiu Zhe XModel docModel = (XModel) UnoRuntime.queryInterface(XModel.class, 115eba4d44aSLiu Zhe m_databaseDocument); 116eba4d44aSLiu Zhe m_documentUI = (XDatabaseDocumentUI) UnoRuntime.queryInterface(XDatabaseDocumentUI.class, 117eba4d44aSLiu Zhe docModel.getCurrentController()); 118eba4d44aSLiu Zhe m_documentUI.connect(); 119eba4d44aSLiu Zhe assertTrue("could not connect to " + DBUtil.getDocumentURL(), 120eba4d44aSLiu Zhe m_documentUI.isConnected()); 121eba4d44aSLiu Zhe 122eba4d44aSLiu Zhe } 123eba4d44aSLiu Zhe 124eba4d44aSLiu Zhe @Test testCreateTable()125eba4d44aSLiu Zhe public void testCreateTable() throws java.lang.Exception { 126eba4d44aSLiu Zhe // create a table in the database 127eba4d44aSLiu Zhe DBUtil.createTable("test", new HsqlColumnDescriptor[] { 128eba4d44aSLiu Zhe new HsqlColumnDescriptor("a", "VARCHAR(50)"), 129eba4d44aSLiu Zhe new HsqlColumnDescriptor("b", "VARCHAR(50)"), 130eba4d44aSLiu Zhe new HsqlColumnDescriptor("c", "VARCHAR(50)") }); 131eba4d44aSLiu Zhe switchToDocument(DBUtil.getDocumentURL()); 132eba4d44aSLiu Zhe // ---save and reload database document 133eba4d44aSLiu Zhe m_databaseDocument = saveAndReloadDoc(m_databaseDocument, "", "odb"); 134eba4d44aSLiu Zhe 135eba4d44aSLiu Zhe XModel docModel = (XModel) UnoRuntime.queryInterface(XModel.class, 136eba4d44aSLiu Zhe m_databaseDocument); 137eba4d44aSLiu Zhe m_documentUI = (XDatabaseDocumentUI) UnoRuntime.queryInterface(XDatabaseDocumentUI.class, 138eba4d44aSLiu Zhe docModel.getCurrentController()); 139eba4d44aSLiu Zhe m_documentUI.connect(); 140eba4d44aSLiu Zhe XTablesSupplier suppTables = (XTablesSupplier) UnoRuntime.queryInterface( 141eba4d44aSLiu Zhe XTablesSupplier.class, m_documentUI.getActiveConnection()); 142eba4d44aSLiu Zhe XNameAccess tables = suppTables.getTables(); 143eba4d44aSLiu Zhe assertTrue("the newly created table has not been written", 144eba4d44aSLiu Zhe tables.hasByName("test")); 145eba4d44aSLiu Zhe } 146eba4d44aSLiu Zhe getMSF()147eba4d44aSLiu Zhe protected XMultiServiceFactory getMSF() { 148eba4d44aSLiu Zhe final XMultiServiceFactory xMSF1 = (XMultiServiceFactory) UnoRuntime.queryInterface( 149eba4d44aSLiu Zhe XMultiServiceFactory.class, app.getComponentContext() 150eba4d44aSLiu Zhe .getServiceManager()); 151eba4d44aSLiu Zhe return xMSF1; 152eba4d44aSLiu Zhe } 153eba4d44aSLiu Zhe saveAndReloadDoc( XOfficeDatabaseDocument m_databaseDocument2, String sFilter, String sExtension)154eba4d44aSLiu Zhe private XOfficeDatabaseDocument saveAndReloadDoc( 155eba4d44aSLiu Zhe XOfficeDatabaseDocument m_databaseDocument2, String sFilter, 156eba4d44aSLiu Zhe String sExtension) throws java.lang.Exception { 157eba4d44aSLiu Zhe String filePath = Testspace.getPath("tmp/basetest." + sExtension); 158eba4d44aSLiu Zhe PropertyValue[] aStoreProperties = new PropertyValue[2]; 159eba4d44aSLiu Zhe aStoreProperties[0] = new PropertyValue(); 160eba4d44aSLiu Zhe aStoreProperties[1] = new PropertyValue(); 161eba4d44aSLiu Zhe aStoreProperties[0].Name = "Override"; 162eba4d44aSLiu Zhe aStoreProperties[0].Value = true; 163eba4d44aSLiu Zhe aStoreProperties[1].Name = "FilterName"; 164eba4d44aSLiu Zhe XStorable xStorable = (XStorable) UnoRuntime.queryInterface( 165eba4d44aSLiu Zhe XStorable.class, m_databaseDocument2); 166eba4d44aSLiu Zhe xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties); 167eba4d44aSLiu Zhe 168eba4d44aSLiu Zhe return (XOfficeDatabaseDocument) UnoRuntime.queryInterface(XOfficeDatabaseDocument.class, 169eba4d44aSLiu Zhe app.loadDocument(filePath)); 170eba4d44aSLiu Zhe } 171eba4d44aSLiu Zhe } 172