summaryrefslogtreecommitdiffstats
path: root/das-java/trunk/rdb/src/test/java/org/apache/tuscany/das/rdb/test/ConnectionTests.java
blob: 42ff57b831ce1cff1138dff332bd226e78c18da2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.tuscany.das.rdb.test;

import java.sql.Connection;
import java.sql.Statement;
import java.util.Properties;

import org.apache.derby.jdbc.EmbeddedDataSource;
import org.apache.tuscany.das.rdb.DAS;
import org.apache.tuscany.das.rdb.test.framework.DasTest;

import commonj.sdo.DataObject;

/**
 * Test Database Connection with and without authentication required 
 */
public class ConnectionTests extends DasTest {
	Properties p = null;
	
    protected void setUp() throws Exception {        
        super.setUp();
        p = System.getProperties();
        p.put("derby.system.home", "target");

        EmbeddedDataSource ds = new EmbeddedDataSource();       
    	ds.setDatabaseName("dastestcon");
    	ds.setDataSourceName("java/dastestcon");
   		ds.setCreateDatabase("create");
   		ds.setUser("dasuser");
   		ds.setPassword("dasuser");
   		Connection connection = ds.getConnection("dasuser", "dasuser");//to actually create the database
   		Statement stmt = connection.createStatement();
   		try{
   			stmt.executeQuery("select * from BOOK");
   		} catch(Exception e) {
	   		stmt.execute("create table BOOK (id int, name varchar(30))");
	   		stmt.execute("insert into BOOK values (1, 'book1')");
   		}
   		connection.commit();
   		connection.close();   		
    }
    
    protected void tearDown() throws Exception {
    	super.tearDown();
    }

    /**
     * As database is created with user, password, connection without same authentication can not look up data
     */
    public void testConnectionUsingDriverManagerNoAuth() throws Exception {
    	try {
	   		DAS das = DAS.FACTORY.createDAS(getConfig("connectionInfoDriverManagerNoAuth.xml"));
	   		das.getCommand("getBook").executeQuery();
	   		fail("Expected exception, as DB is created with id, pwd, needs same to get data from DB!");
		} catch(Exception e) {
			assertTrue(true);//Expected exception
		}
    }
    
    /**
     * As database is created with user, password, connection with same authentication can look up data
     */
    public void testConnectionUsingDriverManagerAuth() throws Exception {
    	try {
	   		DAS das = DAS.FACTORY.createDAS(getConfig("connectionInfoDriverManagerAuth.xml"));
	   		DataObject root = das.getCommand("getBook").executeQuery();
	   		assertEquals(1, root.getList("BOOK").size());
		} catch(Exception e) {
			fail("Expected DAS to obtain authenticated connection using data source!");
		}
    }    
}