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.beans.PropertyValue;
26 import com.sun.star.container.XNameAccess;
27 import com.sun.star.frame.FrameSearchFlag;
28 import com.sun.star.frame.XComponentLoader;
29 import com.sun.star.frame.XModel;
30 import com.sun.star.frame.XStorable;
31 import com.sun.star.lang.XComponent;
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 import connectivity.tools.HsqlColumnDescriptor;
38 import connectivity.tools.HsqlDatabase;
39 import connectivity.tools.HsqlTableDescriptor;
40 import java.io.IOException;
41 
42 
43 // ---------- junit imports -----------------
44 import org.junit.After;
45 import org.junit.Before;
46 import org.junit.Test;
47 import static org.junit.Assert.*;
48 // ------------------------------------------
49 
50 /** complex test case for Base's application UI
51  */
52 public class ApplicationController extends TestCase
53 {
54 
55     private HsqlDatabase m_database;
56     private XOfficeDatabaseDocument m_databaseDocument;
57     private XDatabaseDocumentUI m_documentUI;
58 
ApplicationController()59     public ApplicationController()
60     {
61         super();
62     }
63 
64     // --------------------------------------------------------------------------------------------------------
getTestObjectName()65     public String getTestObjectName()
66     {
67         return getClass().getName();
68     }
69 
70     // --------------------------------------------------------------------------------------------------------
impl_closeDocument()71     private void impl_closeDocument()
72     {
73         if (m_database != null)
74         {
75             m_database.close();
76             m_database = null;
77             m_databaseDocument = null;
78             m_documentUI = null;
79         }
80     }
81 
82     // --------------------------------------------------------------------------------------------------------
impl_switchToDocument(String _documentURL)83     private void impl_switchToDocument(String _documentURL) throws java.lang.Exception
84     {
85         // close previous database document
86         impl_closeDocument();
87 
88         // create/load the new database document
89         m_database = (_documentURL == null)
90                 ? new HsqlDatabase(getMSF())
91                 : new HsqlDatabase(getMSF(), _documentURL);
92         m_databaseDocument = m_database.getDatabaseDocument();
93 
94         // load it into a frame
95         final Object object = getMSF().createInstance("com.sun.star.frame.Desktop");
96         final XComponentLoader xComponentLoader = UnoRuntime.queryInterface(XComponentLoader.class, object);
97         final XComponent loadedComponent = xComponentLoader.loadComponentFromURL(m_database.getDocumentURL(), "_blank", FrameSearchFlag.ALL, new PropertyValue[0]);
98 
99         assertTrue("too many document instances!",
100                 UnoRuntime.areSame(loadedComponent, m_databaseDocument));
101 
102         // get the controller, which provides access to various UI operations
103         final XModel docModel = UnoRuntime.queryInterface(XModel.class,
104                 loadedComponent);
105         m_documentUI = UnoRuntime.queryInterface(XDatabaseDocumentUI.class,
106                 docModel.getCurrentController());
107     }
108 
109     // --------------------------------------------------------------------------------------------------------
110     @Before
111     @Override
before()112     public void before() throws java.lang.Exception
113     {
114 		super.before();
115         impl_switchToDocument(null);
116     }
117 
118     // --------------------------------------------------------------------------------------------------------
119     @After
120     @Override
after()121     public void after() throws java.lang.Exception
122     {
123         impl_closeDocument();
124 		super.after();
125     }
126     // --------------------------------------------------------------------------------------------------------
127 
128     @Test
checkSaveAs()129     public void checkSaveAs() throws Exception, IOException, java.lang.Exception
130     {
131         // issue 93737 describes the problem that when you save-as a database document, and do changes to it,
132         // then those changes are saved in the old document, actually
133         final String oldDocumentURL = m_database.getDocumentURL();
134 
135         final String newDocumentURL = createTempFileURL();
136 
137         // store the doc in a new location
138         final XStorable storeDoc = UnoRuntime.queryInterface( XStorable.class, m_databaseDocument );
139         storeDoc.storeAsURL( newDocumentURL, new PropertyValue[] { } );
140 
141         // connect
142         m_documentUI.connect();
143         assertTrue("could not connect to " + m_database.getDocumentURL(), m_documentUI.isConnected());
144 
145         // create a table in the database
146         m_database.createTable(new HsqlTableDescriptor("abc", new HsqlColumnDescriptor[]
147                 {
148                     new HsqlColumnDescriptor("a", "VARCHAR(50)"),
149                     new HsqlColumnDescriptor("b", "VARCHAR(50)"),
150                     new HsqlColumnDescriptor("c", "VARCHAR(50)")
151                 }));
152 
153         // load the old document, and verify there is *no* table therein
154         impl_switchToDocument(oldDocumentURL);
155         m_documentUI.connect();
156         assertTrue("could not connect to " + m_database.getDocumentURL(), m_documentUI.isConnected());
157         XTablesSupplier suppTables = UnoRuntime.queryInterface( XTablesSupplier.class, m_documentUI.getActiveConnection() );
158         XNameAccess tables = suppTables.getTables();
159         assertTrue("the table was created in the wrong database", !tables.hasByName("abc"));
160 
161         // load the new document, and verify there *is* a table therein
162         impl_switchToDocument(newDocumentURL);
163         m_documentUI.connect();
164         assertTrue("could not connect to " + m_database.getDocumentURL(), m_documentUI.isConnected());
165 
166         suppTables = UnoRuntime.queryInterface( XTablesSupplier.class, m_documentUI.getActiveConnection() );
167         tables = suppTables.getTables();
168         assertTrue("the newly created table has not been written", tables.hasByName("abc"));
169     }
170 }
171