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.dbaccess; 24 25 import com.sun.star.accessibility.XAccessible; 26 import com.sun.star.accessibility.XAccessibleContext; 27 import com.sun.star.awt.XExtendedToolkit; 28 import com.sun.star.awt.XWindow; 29 import com.sun.star.beans.Optional; 30 import com.sun.star.beans.XPropertySet; 31 import com.sun.star.container.XNameAccess; 32 import com.sun.star.sdb.CommandType; 33 import com.sun.star.sdb.application.XCopyTableWizard; 34 import com.sun.star.sdb.DataAccessDescriptorFactory; 35 import com.sun.star.sdbc.XConnection; 36 import com.sun.star.sdbcx.XTablesSupplier; 37 import com.sun.star.task.XInteractionHandler; 38 import com.sun.star.uno.Exception; 39 import com.sun.star.uno.UnoRuntime; 40 import com.sun.star.uno.XComponentContext; 41 import connectivity.tools.DbaseDatabase; 42 import java.io.IOException; 43 import util.UITools; 44 45 // ---------- junit imports ----------------- 46 import org.junit.After; 47 import org.junit.Before; 48 import org.junit.Test; 49 import static org.junit.Assert.*; 50 // ------------------------------------------ 51 52 /** complex test case for Base's application UI 53 */ 54 public class CopyTableWizard extends CRMBasedTestCase 55 { 56 57 private DatabaseApplication source; 58 private DbaseDatabase destinationDB = null; 59 private DatabaseApplication dest; 60 CopyTableWizard()61 public CopyTableWizard() 62 { 63 super(); 64 } 65 66 // -------------------------------------------------------------------------------------------------------- 67 68 @After 69 @Override after()70 public void after() 71 { 72 dest.store(); 73 if ( destinationDB != null ) 74 destinationDB.close(); 75 destinationDB = null; 76 super.after(); 77 } 78 79 @Before 80 @Override before()81 public void before() 82 { 83 try 84 { 85 createTestCase(); 86 source = new DatabaseApplication(m_database.getDatabase()); 87 destinationDB = new DbaseDatabase( getMSF() ); 88 dest = new DatabaseApplication( destinationDB ); 89 } 90 catch (java.lang.Exception ex) 91 { 92 fail(""); 93 } 94 } 95 96 // -------------------------------------------------------------------------------------------------------- 97 class CopyThread implements Runnable 98 { 99 100 final XCopyTableWizard copyWizard; 101 CopyThread(final XCopyTableWizard copyWizard)102 CopyThread(final XCopyTableWizard copyWizard) 103 { 104 this.copyWizard = copyWizard; 105 } 106 run()107 public void run() 108 { 109 copyWizard.execute(); 110 } 111 } 112 getActiveWindow()113 private XWindow getActiveWindow() 114 { 115 Object toolKit = null; 116 try 117 { 118 toolKit = getMSF().createInstance("com.sun.star.awt.Toolkit"); 119 } 120 catch (com.sun.star.uno.Exception e) 121 { 122 return null; 123 } 124 125 XExtendedToolkit tk = UnoRuntime.queryInterface( XExtendedToolkit.class, toolKit ); 126 Object atw = tk.getActiveTopWindow(); 127 return UnoRuntime.queryInterface( XWindow.class, atw ); 128 } 129 130 @Test copyTable()131 public void copyTable() throws Exception, IOException, java.lang.Exception 132 { 133 copyTable(source,source); 134 } 135 136 @Test copyTableDbase()137 public void copyTableDbase() throws Exception, IOException, java.lang.Exception 138 { 139 copyTable(source,dest); 140 } copyTable(final DatabaseApplication sourceDb,final DatabaseApplication destDb)141 private void copyTable(final DatabaseApplication sourceDb,final DatabaseApplication destDb) throws Exception, IOException, java.lang.Exception 142 { 143 final XConnection destConnection = destDb.getDocumentUI().getActiveConnection(); 144 145 final XConnection sourceConnection = sourceDb.getDocumentUI().getActiveConnection(); 146 final XTablesSupplier suppTables = UnoRuntime.queryInterface(XTablesSupplier.class, sourceConnection); 147 final XNameAccess tables = suppTables.getTables(); 148 149 final String[] names = tables.getElementNames(); 150 for (int i = 0; i < names.length; i++) 151 { 152 copyTable(names[i], sourceConnection, destConnection); 153 } 154 } 155 copyTable(final String tableName, final XConnection sourceConnection, final XConnection destConnection)156 private void copyTable(final String tableName, final XConnection sourceConnection, final XConnection destConnection) throws Exception, IOException, java.lang.Exception 157 { 158 159 final XInteractionHandler interAction = new CopyTableInterActionHandler(); 160 final XComponentContext context = getComponentContext(); 161 final XPropertySet sourceDescriptor = DataAccessDescriptorFactory.get(context).createDataAccessDescriptor(); 162 sourceDescriptor.setPropertyValue("CommandType", CommandType.TABLE); 163 sourceDescriptor.setPropertyValue("Command", tableName); 164 sourceDescriptor.setPropertyValue("ActiveConnection", sourceConnection); 165 166 final XPropertySet destDescriptor = DataAccessDescriptorFactory.get(context).createDataAccessDescriptor(); 167 destDescriptor.setPropertyValue("ActiveConnection", destConnection); 168 169 final XCopyTableWizard copyWizard = com.sun.star.sdb.application.CopyTableWizard.createWithInteractionHandler( 170 context, sourceDescriptor, destDescriptor, interAction); 171 copyWizard.setOperation((short) 0); // com.sun.star.sdb.application.CopyDefinitionAndData 172 Optional<String> auto = new Optional<String>(); 173 174 auto.IsPresent = destConnection.getMetaData().supportsCoreSQLGrammar(); 175 if (auto.IsPresent) 176 { 177 auto.Value = "ID_test"; 178 } 179 copyWizard.setCreatePrimaryKey(auto); 180 Thread thread = new Thread(new CopyThread(copyWizard)); 181 thread.start(); 182 sleep(); 183 184 try 185 { 186 final XWindow dialog = getActiveWindow(); 187 final UITools uiTools = new UITools(getMSF(), dialog); 188 final XAccessible root = uiTools.getRoot(); 189 final XAccessibleContext accContext = root.getAccessibleContext(); 190 final int count = accContext.getAccessibleChildCount(); 191 String buttonName = "Create"; 192 final XAccessibleContext childContext = accContext.getAccessibleChild(count - 3).getAccessibleContext(); 193 final String name = childContext.getAccessibleName(); 194 if (name != null && !"".equals(name)) 195 { 196 buttonName = name; 197 } 198 try 199 { 200 uiTools.clickButton(buttonName); 201 } 202 catch (java.lang.Exception exception) 203 { 204 exception.printStackTrace( System.err ); 205 } 206 } 207 catch (com.sun.star.lang.IndexOutOfBoundsException indexOutOfBoundsException) 208 { 209 } 210 sleep(); 211 212 thread.join(); 213 } 214 sleep()215 private void sleep() 216 { 217 try 218 { 219 Thread.sleep(500); 220 } 221 catch (java.lang.InterruptedException e) 222 { 223 } 224 } 225 // -------------------------------------------------------------------------------------------------------- 226 } 227