1*f7cf3d52SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*f7cf3d52SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*f7cf3d52SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*f7cf3d52SAndrew Rist * distributed with this work for additional information 6*f7cf3d52SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*f7cf3d52SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*f7cf3d52SAndrew Rist * "License"); you may not use this file except in compliance 9*f7cf3d52SAndrew Rist * with the License. You may obtain a copy of the License at 10*f7cf3d52SAndrew Rist * 11*f7cf3d52SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*f7cf3d52SAndrew Rist * 13*f7cf3d52SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*f7cf3d52SAndrew Rist * software distributed under the License is distributed on an 15*f7cf3d52SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*f7cf3d52SAndrew Rist * KIND, either express or implied. See the License for the 17*f7cf3d52SAndrew Rist * specific language governing permissions and limitations 18*f7cf3d52SAndrew Rist * under the License. 19*f7cf3d52SAndrew Rist * 20*f7cf3d52SAndrew Rist *************************************************************/ 21*f7cf3d52SAndrew Rist 22*f7cf3d52SAndrew Rist 23cdf0e10cSrcweir package complex.dbaccess; 24cdf0e10cSrcweir 25cdf0e10cSrcweir import com.sun.star.container.XNameAccess; 26cdf0e10cSrcweir import com.sun.star.uno.Exception; 27cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 28cdf0e10cSrcweir import com.sun.star.uno.XNamingService; 29cdf0e10cSrcweir import connectivity.tools.CRMDatabase; 30cdf0e10cSrcweir import connectivity.tools.HsqlDatabase; 31cdf0e10cSrcweir 32cdf0e10cSrcweir // ---------- junit imports ----------------- 33cdf0e10cSrcweir import org.junit.Test; 34cdf0e10cSrcweir import static org.junit.Assert.*; 35cdf0e10cSrcweir // ------------------------------------------ 36cdf0e10cSrcweir 37cdf0e10cSrcweir 38cdf0e10cSrcweir public class DataSource extends TestCase 39cdf0e10cSrcweir { 40cdf0e10cSrcweir 41cdf0e10cSrcweir HsqlDatabase m_database; 42cdf0e10cSrcweir connectivity.tools.DataSource m_dataSource; 43cdf0e10cSrcweir 44cdf0e10cSrcweir // -------------------------------------------------------------------------------------------------------- createTestCase()45cdf0e10cSrcweir private void createTestCase() 46cdf0e10cSrcweir { 47cdf0e10cSrcweir try 48cdf0e10cSrcweir { 49cdf0e10cSrcweir if (m_database == null) 50cdf0e10cSrcweir { 51cdf0e10cSrcweir final CRMDatabase database = new CRMDatabase( getMSF(), false ); 52cdf0e10cSrcweir m_database = database.getDatabase(); 53cdf0e10cSrcweir m_dataSource = m_database.getDataSource(); 54cdf0e10cSrcweir } 55cdf0e10cSrcweir } 56cdf0e10cSrcweir catch (Exception e) 57cdf0e10cSrcweir { 58cdf0e10cSrcweir fail("could not create the test case, error message:\n" + e.getMessage()); 59cdf0e10cSrcweir } 60cdf0e10cSrcweir catch (java.lang.Exception e) 61cdf0e10cSrcweir { 62cdf0e10cSrcweir fail("could not create the test case, error message:\n" + e.getMessage()); 63cdf0e10cSrcweir } 64cdf0e10cSrcweir } 65cdf0e10cSrcweir 66cdf0e10cSrcweir // -------------------------------------------------------------------------------------------------------- 67cdf0e10cSrcweir @Test testRegistrationName()68cdf0e10cSrcweir public void testRegistrationName() 69cdf0e10cSrcweir { 70cdf0e10cSrcweir try 71cdf0e10cSrcweir { 72cdf0e10cSrcweir createTestCase(); 73cdf0e10cSrcweir // 1. check the existing "Bibliography" data source whether it has the proper name 74cdf0e10cSrcweir String dataSourceName = "Bibliography"; 75cdf0e10cSrcweir final connectivity.tools.DataSource bibliography = new connectivity.tools.DataSource(getMSF(), dataSourceName); 76cdf0e10cSrcweir assertEquals("pre-registered database has a wrong name!", dataSourceName, bibliography.getName()); 77cdf0e10cSrcweir // 2. register a newly created data source, and verify it has the proper name 78cdf0e10cSrcweir dataSourceName = "someDataSource"; 79cdf0e10cSrcweir final XNamingService dataSourceRegistrations = UnoRuntime.queryInterface( 80cdf0e10cSrcweir XNamingService.class, getMSF().createInstance( "com.sun.star.sdb.DatabaseContext" ) ); 81cdf0e10cSrcweir final XNameAccess existenceCheck = UnoRuntime.queryInterface( XNameAccess.class, dataSourceRegistrations ); 82cdf0e10cSrcweir if ( existenceCheck.hasByName( "someDataSource" ) ) 83cdf0e10cSrcweir dataSourceRegistrations.revokeObject( "someDataSource" ); 84cdf0e10cSrcweir dataSourceRegistrations.registerObject("someDataSource", m_dataSource.getXDataSource()); 85cdf0e10cSrcweir assertEquals("registration name of a newly registered data source is wrong", dataSourceName, m_dataSource.getName()); 86cdf0e10cSrcweir } 87cdf0e10cSrcweir catch (Exception ex) 88cdf0e10cSrcweir { 89cdf0e10cSrcweir // Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex); 90cdf0e10cSrcweir fail(); 91cdf0e10cSrcweir } 92cdf0e10cSrcweir } 93cdf0e10cSrcweir } 94