1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 package complex.dbaccess;
19 
20 import com.sun.star.accessibility.XAccessible;
21 import com.sun.star.accessibility.XAccessibleContext;
22 import com.sun.star.awt.XExtendedToolkit;
23 import com.sun.star.awt.XWindow;
24 import com.sun.star.beans.Optional;
25 import com.sun.star.beans.XPropertySet;
26 import com.sun.star.container.XNameAccess;
27 import com.sun.star.sdb.CommandType;
28 import com.sun.star.sdb.application.XCopyTableWizard;
29 import com.sun.star.sdb.DataAccessDescriptorFactory;
30 import com.sun.star.sdbc.XConnection;
31 import com.sun.star.sdbcx.XTablesSupplier;
32 import com.sun.star.task.XInteractionHandler;
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.uno.XComponentContext;
35 import connectivity.tools.DbaseDatabase;
36 import java.io.IOException;
37 import util.UITools;
38 
39 // ---------- junit imports -----------------
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 
44 
45 /** complex test case for Base's application UI
46  */
47 public class CopyTableWizard extends CRMBasedTestCase
48 {
49 
50     private DatabaseApplication source;
51     private DbaseDatabase destinationDB = null;
52     private DatabaseApplication dest;
53 
54     @After
55     @Override
after()56     public void after() throws Exception
57     {
58         dest.store();
59         if ( destinationDB != null )
60             destinationDB.close();
61         destinationDB = null;
62         super.after();
63     }
64 
65     @Before
66     @Override
before()67     public void before() throws Exception
68     {
69         createTestCase();
70         source = new DatabaseApplication(m_database.getDatabase());
71         destinationDB = new DbaseDatabase( getMSF() );
72         dest = new DatabaseApplication( destinationDB );
73     }
74 
75 
76     private static class CopyThread implements Runnable
77     {
78 
79         private final XCopyTableWizard copyWizard;
80 
CopyThread(final XCopyTableWizard copyWizard)81         private CopyThread(final XCopyTableWizard copyWizard)
82         {
83             this.copyWizard = copyWizard;
84         }
85 
run()86         public void run()
87         {
88             copyWizard.execute();
89         }
90     }
91 
getActiveWindow()92     private XWindow getActiveWindow()
93     {
94         Object toolKit = null;
95         try
96         {
97             toolKit = getMSF().createInstance("com.sun.star.awt.Toolkit");
98         }
99         catch (com.sun.star.uno.Exception e)
100         {
101             return null;
102         }
103 
104         XExtendedToolkit tk = UnoRuntime.queryInterface( XExtendedToolkit.class, toolKit );
105         Object atw = tk.getActiveTopWindow();
106         return UnoRuntime.queryInterface( XWindow.class, atw );
107     }
108 
109     @Test
copyTable()110     public void copyTable() throws Exception, IOException, java.lang.Exception
111     {
112         copyTable(source,source);
113     }
114 
115     @Test
copyTableDbase()116     public void copyTableDbase() throws Exception, IOException, java.lang.Exception
117     {
118         copyTable(source,dest);
119     }
copyTable(final DatabaseApplication sourceDb,final DatabaseApplication destDb)120     private void copyTable(final DatabaseApplication sourceDb,final DatabaseApplication destDb) throws Exception, IOException, java.lang.Exception
121     {
122         final XConnection destConnection = destDb.getDocumentUI().getActiveConnection();
123 
124         final XConnection sourceConnection = sourceDb.getDocumentUI().getActiveConnection();
125         final XTablesSupplier suppTables = UnoRuntime.queryInterface(XTablesSupplier.class, sourceConnection);
126         final XNameAccess tables = suppTables.getTables();
127 
128         final String[] names = tables.getElementNames();
129         for (int i = 0; i < names.length; i++)
130         {
131             copyTable(names[i], sourceConnection, destConnection);
132         }
133     }
134 
copyTable(final String tableName, final XConnection sourceConnection, final XConnection destConnection)135     private void copyTable(final String tableName, final XConnection sourceConnection, final XConnection destConnection) throws Exception, java.lang.Exception
136     {
137 
138         final XInteractionHandler interAction = new CopyTableInterActionHandler();
139         final XComponentContext context = getComponentContext();
140         final XPropertySet sourceDescriptor = DataAccessDescriptorFactory.get(context).createDataAccessDescriptor();
141         sourceDescriptor.setPropertyValue("CommandType", CommandType.TABLE);
142         sourceDescriptor.setPropertyValue("Command", tableName);
143         sourceDescriptor.setPropertyValue("ActiveConnection", sourceConnection);
144 
145         final XPropertySet destDescriptor = DataAccessDescriptorFactory.get(context).createDataAccessDescriptor();
146         destDescriptor.setPropertyValue("ActiveConnection", destConnection);
147 
148         final XCopyTableWizard copyWizard = com.sun.star.sdb.application.CopyTableWizard.createWithInteractionHandler(
149             context, sourceDescriptor, destDescriptor, interAction);
150         copyWizard.setOperation((short) 0); // com.sun.star.sdb.application.CopyDefinitionAndData
151         Optional<String> auto = new Optional<String>();
152 
153         auto.IsPresent = destConnection.getMetaData().supportsCoreSQLGrammar();
154         if (auto.IsPresent)
155         {
156             auto.Value = "ID_test";
157         }
158         copyWizard.setCreatePrimaryKey(auto);
159         Thread thread = new Thread(new CopyThread(copyWizard));
160         thread.start();
161         util.utils.shortWait();
162 
163         try
164         {
165             final XWindow dialog = getActiveWindow();
166             final UITools uiTools = new UITools(dialog);
167             final XAccessible root = uiTools.getRoot();
168             final XAccessibleContext accContext = root.getAccessibleContext();
169             final int count = accContext.getAccessibleChildCount();
170             String buttonName = "Create";
171             final XAccessibleContext childContext = accContext.getAccessibleChild(count - 3).getAccessibleContext();
172             final String name = childContext.getAccessibleName();
173             if (name != null && !"".equals(name))
174             {
175                 buttonName = name;
176             }
177             try
178             {
179                 uiTools.clickButton(buttonName);
180             }
181             catch (java.lang.Exception exception)
182             {
183                 exception.printStackTrace( System.err );
184             }
185         }
186         catch (com.sun.star.lang.IndexOutOfBoundsException indexOutOfBoundsException)
187         {
188         }
189         util.utils.shortWait();
190 
191         thread.join();
192     }
193 
194 }
195