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.container.XNameAccess;
26 import com.sun.star.uno.Exception;
27 import com.sun.star.uno.UnoRuntime;
28 import com.sun.star.uno.XNamingService;
29 import connectivity.tools.CRMDatabase;
30 import connectivity.tools.HsqlDatabase;
31 
32 // ---------- junit imports -----------------
33 import org.junit.Test;
34 import static org.junit.Assert.*;
35 // ------------------------------------------
36 
37 
38 public class DataSource extends TestCase
39 {
40 
41     HsqlDatabase m_database;
42     connectivity.tools.DataSource m_dataSource;
43 
44     // --------------------------------------------------------------------------------------------------------
createTestCase()45     private void createTestCase()
46     {
47         try
48         {
49             if (m_database == null)
50             {
51                 final CRMDatabase database = new CRMDatabase( getMSF(), false );
52                 m_database = database.getDatabase();
53                 m_dataSource = m_database.getDataSource();
54             }
55         }
56         catch (Exception e)
57         {
58             fail("could not create the test case, error message:\n" + e.getMessage());
59         }
60         catch (java.lang.Exception e)
61         {
62             fail("could not create the test case, error message:\n" + e.getMessage());
63         }
64     }
65 
66     // --------------------------------------------------------------------------------------------------------
67     @Test
testRegistrationName()68     public void testRegistrationName()
69     {
70         try
71         {
72             createTestCase();
73             // 1. check the existing "Bibliography" data source whether it has the proper name
74             String dataSourceName = "Bibliography";
75             final connectivity.tools.DataSource bibliography = new connectivity.tools.DataSource(getMSF(), dataSourceName);
76             assertEquals("pre-registered database has a wrong name!", dataSourceName, bibliography.getName());
77             // 2. register a newly created data source, and verify it has the proper name
78             dataSourceName = "someDataSource";
79             final XNamingService dataSourceRegistrations = UnoRuntime.queryInterface(
80                 XNamingService.class, getMSF().createInstance( "com.sun.star.sdb.DatabaseContext" ) );
81             final XNameAccess existenceCheck = UnoRuntime.queryInterface( XNameAccess.class, dataSourceRegistrations );
82             if ( existenceCheck.hasByName( "someDataSource" ) )
83                 dataSourceRegistrations.revokeObject( "someDataSource" );
84             dataSourceRegistrations.registerObject("someDataSource", m_dataSource.getXDataSource());
85             assertEquals("registration name of a newly registered data source is wrong", dataSourceName, m_dataSource.getName());
86         }
87         catch (Exception ex)
88         {
89             // Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
90             fail();
91         }
92     }
93 }
94