xref: /AOO41X/test/testuno/source/fvt/uno/db/DBAccess.java (revision eba4d44a33e5be0b2528d5a9a6f0dcbf65adaa0d)
1*eba4d44aSLiu Zhe /**************************************************************
2*eba4d44aSLiu Zhe  *
3*eba4d44aSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4*eba4d44aSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5*eba4d44aSLiu Zhe  * distributed with this work for additional information
6*eba4d44aSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7*eba4d44aSLiu Zhe  * to you under the Apache License, Version 2.0 (the
8*eba4d44aSLiu Zhe  * "License"); you may not use this file except in compliance
9*eba4d44aSLiu Zhe  * with the License.  You may obtain a copy of the License at
10*eba4d44aSLiu Zhe  *
11*eba4d44aSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12*eba4d44aSLiu Zhe  *
13*eba4d44aSLiu Zhe  * Unless required by applicable law or agreed to in writing,
14*eba4d44aSLiu Zhe  * software distributed under the License is distributed on an
15*eba4d44aSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*eba4d44aSLiu Zhe  * KIND, either express or implied.  See the License for the
17*eba4d44aSLiu Zhe  * specific language governing permissions and limitations
18*eba4d44aSLiu Zhe  * under the License.
19*eba4d44aSLiu Zhe  *
20*eba4d44aSLiu Zhe  *************************************************************/
21*eba4d44aSLiu Zhe 
22*eba4d44aSLiu Zhe package fvt.uno.db;
23*eba4d44aSLiu Zhe 
24*eba4d44aSLiu Zhe import com.sun.star.beans.PropertyValue;
25*eba4d44aSLiu Zhe import com.sun.star.container.XNameAccess;
26*eba4d44aSLiu Zhe import com.sun.star.frame.FrameSearchFlag;
27*eba4d44aSLiu Zhe import com.sun.star.frame.XComponentLoader;
28*eba4d44aSLiu Zhe import com.sun.star.frame.XModel;
29*eba4d44aSLiu Zhe import com.sun.star.frame.XStorable;
30*eba4d44aSLiu Zhe import com.sun.star.lang.XComponent;
31*eba4d44aSLiu Zhe import com.sun.star.lang.XMultiServiceFactory;
32*eba4d44aSLiu Zhe import com.sun.star.sdb.XOfficeDatabaseDocument;
33*eba4d44aSLiu Zhe import com.sun.star.sdb.application.XDatabaseDocumentUI;
34*eba4d44aSLiu Zhe import com.sun.star.sdbcx.XTablesSupplier;
35*eba4d44aSLiu Zhe import com.sun.star.uno.Exception;
36*eba4d44aSLiu Zhe import com.sun.star.uno.UnoRuntime;
37*eba4d44aSLiu Zhe 
38*eba4d44aSLiu Zhe import testlib.uno.HsqlColumnDescriptor;
39*eba4d44aSLiu Zhe import testlib.uno.DBUtil;
40*eba4d44aSLiu Zhe 
41*eba4d44aSLiu Zhe import java.io.IOException;
42*eba4d44aSLiu Zhe 
43*eba4d44aSLiu Zhe // ---------- junit imports -----------------
44*eba4d44aSLiu Zhe import org.junit.After;
45*eba4d44aSLiu Zhe import org.junit.Before;
46*eba4d44aSLiu Zhe import org.junit.Test;
47*eba4d44aSLiu Zhe import org.openoffice.test.common.FileUtil;
48*eba4d44aSLiu Zhe import org.openoffice.test.common.Testspace;
49*eba4d44aSLiu Zhe import org.openoffice.test.uno.UnoApp;
50*eba4d44aSLiu Zhe 
51*eba4d44aSLiu Zhe import static org.junit.Assert.*;
52*eba4d44aSLiu Zhe 
53*eba4d44aSLiu Zhe /**
54*eba4d44aSLiu Zhe  * test case for Base's application UI
55*eba4d44aSLiu Zhe  */
56*eba4d44aSLiu Zhe public class DBAccess {
57*eba4d44aSLiu Zhe 	UnoApp app = new UnoApp();
58*eba4d44aSLiu Zhe 	private XOfficeDatabaseDocument m_databaseDocument;
59*eba4d44aSLiu Zhe 	private XDatabaseDocumentUI m_documentUI;
60*eba4d44aSLiu Zhe 
61*eba4d44aSLiu Zhe //	public DBAccess() {
62*eba4d44aSLiu Zhe //		super();
63*eba4d44aSLiu Zhe //	}
64*eba4d44aSLiu Zhe 
65*eba4d44aSLiu Zhe 
66*eba4d44aSLiu Zhe 	@Before
before()67*eba4d44aSLiu Zhe 	public void before() throws java.lang.Exception {
68*eba4d44aSLiu Zhe 		app.start();
69*eba4d44aSLiu Zhe 		String a = null;
70*eba4d44aSLiu Zhe 		switchToDocument(a);
71*eba4d44aSLiu Zhe 	}
72*eba4d44aSLiu Zhe 
73*eba4d44aSLiu Zhe 	@After
after()74*eba4d44aSLiu Zhe 	public void after() throws java.lang.Exception {
75*eba4d44aSLiu Zhe 		closeDocument();
76*eba4d44aSLiu Zhe 		app.close();
77*eba4d44aSLiu Zhe 	}
78*eba4d44aSLiu Zhe 
closeDocument()79*eba4d44aSLiu Zhe 	private void closeDocument() {
80*eba4d44aSLiu Zhe 		DBUtil.close();
81*eba4d44aSLiu Zhe 		m_databaseDocument = null;
82*eba4d44aSLiu Zhe 		m_documentUI = null;
83*eba4d44aSLiu Zhe 
84*eba4d44aSLiu Zhe 	}
85*eba4d44aSLiu Zhe 
switchToDocument(String _documentURL)86*eba4d44aSLiu Zhe 	private void switchToDocument(String _documentURL)
87*eba4d44aSLiu Zhe 			throws java.lang.Exception {
88*eba4d44aSLiu Zhe 		// close previous database document
89*eba4d44aSLiu Zhe 		closeDocument();
90*eba4d44aSLiu Zhe 
91*eba4d44aSLiu Zhe 		if (_documentURL == null) {
92*eba4d44aSLiu Zhe 			DBUtil.createNewDocument(getMSF());
93*eba4d44aSLiu Zhe 		} else {
94*eba4d44aSLiu Zhe 			DBUtil.loadNewDocument(getMSF(), _documentURL);
95*eba4d44aSLiu Zhe 		}
96*eba4d44aSLiu Zhe 		m_databaseDocument = DBUtil.getDatabaseDocument();
97*eba4d44aSLiu Zhe 
98*eba4d44aSLiu Zhe 	}
99*eba4d44aSLiu Zhe 
100*eba4d44aSLiu Zhe 
101*eba4d44aSLiu Zhe 	@Test
testSaveAs()102*eba4d44aSLiu Zhe 	public void testSaveAs() throws Exception, IOException, java.lang.Exception {
103*eba4d44aSLiu Zhe 
104*eba4d44aSLiu Zhe 		m_databaseDocument = saveAndReloadDoc(m_databaseDocument, "", "odb");
105*eba4d44aSLiu Zhe 		XModel docModel = (XModel) UnoRuntime.queryInterface(XModel.class,
106*eba4d44aSLiu Zhe 				m_databaseDocument);
107*eba4d44aSLiu Zhe 		m_documentUI = (XDatabaseDocumentUI) UnoRuntime.queryInterface(XDatabaseDocumentUI.class,
108*eba4d44aSLiu Zhe 				docModel.getCurrentController());
109*eba4d44aSLiu Zhe 		m_documentUI.connect();
110*eba4d44aSLiu Zhe 		assertTrue("could not connect to " + DBUtil.getDocumentURL(),
111*eba4d44aSLiu Zhe 				m_documentUI.isConnected());
112*eba4d44aSLiu Zhe 
113*eba4d44aSLiu Zhe 	}
114*eba4d44aSLiu Zhe 
115*eba4d44aSLiu Zhe 	@Test
testCreateTable()116*eba4d44aSLiu Zhe 	public void testCreateTable() throws java.lang.Exception {
117*eba4d44aSLiu Zhe 		// create a table in the database
118*eba4d44aSLiu Zhe 		DBUtil.createTable("test", new HsqlColumnDescriptor[] {
119*eba4d44aSLiu Zhe 				new HsqlColumnDescriptor("a", "VARCHAR(50)"),
120*eba4d44aSLiu Zhe 				new HsqlColumnDescriptor("b", "VARCHAR(50)"),
121*eba4d44aSLiu Zhe 				new HsqlColumnDescriptor("c", "VARCHAR(50)") });
122*eba4d44aSLiu Zhe 		switchToDocument(DBUtil.getDocumentURL());
123*eba4d44aSLiu Zhe 		// ---save and reload database document
124*eba4d44aSLiu Zhe 		m_databaseDocument = saveAndReloadDoc(m_databaseDocument, "", "odb");
125*eba4d44aSLiu Zhe 
126*eba4d44aSLiu Zhe 		XModel docModel = (XModel) UnoRuntime.queryInterface(XModel.class,
127*eba4d44aSLiu Zhe 				m_databaseDocument);
128*eba4d44aSLiu Zhe 		m_documentUI = (XDatabaseDocumentUI) UnoRuntime.queryInterface(XDatabaseDocumentUI.class,
129*eba4d44aSLiu Zhe 				docModel.getCurrentController());
130*eba4d44aSLiu Zhe 		m_documentUI.connect();
131*eba4d44aSLiu Zhe 		XTablesSupplier suppTables = (XTablesSupplier) UnoRuntime.queryInterface(
132*eba4d44aSLiu Zhe 				XTablesSupplier.class, m_documentUI.getActiveConnection());
133*eba4d44aSLiu Zhe 		XNameAccess tables = suppTables.getTables();
134*eba4d44aSLiu Zhe 		assertTrue("the newly created table has not been written",
135*eba4d44aSLiu Zhe 				tables.hasByName("test"));
136*eba4d44aSLiu Zhe 	}
137*eba4d44aSLiu Zhe 
getMSF()138*eba4d44aSLiu Zhe 	protected XMultiServiceFactory getMSF() {
139*eba4d44aSLiu Zhe 		final XMultiServiceFactory xMSF1 = (XMultiServiceFactory) UnoRuntime.queryInterface(
140*eba4d44aSLiu Zhe 				XMultiServiceFactory.class, app.getComponentContext()
141*eba4d44aSLiu Zhe 						.getServiceManager());
142*eba4d44aSLiu Zhe 		return xMSF1;
143*eba4d44aSLiu Zhe 	}
144*eba4d44aSLiu Zhe 
saveAndReloadDoc( XOfficeDatabaseDocument m_databaseDocument2, String sFilter, String sExtension)145*eba4d44aSLiu Zhe 	private XOfficeDatabaseDocument saveAndReloadDoc(
146*eba4d44aSLiu Zhe 			XOfficeDatabaseDocument m_databaseDocument2, String sFilter,
147*eba4d44aSLiu Zhe 			String sExtension) throws java.lang.Exception {
148*eba4d44aSLiu Zhe 		String filePath = Testspace.getPath("tmp/basetest." + sExtension);
149*eba4d44aSLiu Zhe 		PropertyValue[] aStoreProperties = new PropertyValue[2];
150*eba4d44aSLiu Zhe 		aStoreProperties[0] = new PropertyValue();
151*eba4d44aSLiu Zhe 		aStoreProperties[1] = new PropertyValue();
152*eba4d44aSLiu Zhe 		aStoreProperties[0].Name = "Override";
153*eba4d44aSLiu Zhe 		aStoreProperties[0].Value = true;
154*eba4d44aSLiu Zhe 		aStoreProperties[1].Name = "FilterName";
155*eba4d44aSLiu Zhe 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
156*eba4d44aSLiu Zhe 				XStorable.class, m_databaseDocument2);
157*eba4d44aSLiu Zhe 		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
158*eba4d44aSLiu Zhe 
159*eba4d44aSLiu Zhe 		return (XOfficeDatabaseDocument) UnoRuntime.queryInterface(XOfficeDatabaseDocument.class,
160*eba4d44aSLiu Zhe 				app.loadDocument(filePath));
161*eba4d44aSLiu Zhe 	}
162*eba4d44aSLiu Zhe }
163