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