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