dbe5800b9673bb4b7347e66a1f93b374e4d8afd3
[utils] /
1 package org.wamblee.support.persistence;
2
3 import java.sql.Connection;
4
5 import javax.sql.DataSource;
6
7 import org.junit.Test;
8 import org.wamblee.support.persistence.Database;
9 import org.wamblee.support.persistence.DatabaseBuilder;
10 import org.wamblee.support.persistence.DatabaseProvider;
11 import org.wamblee.support.persistence.ExternalDatabase;
12
13 import static junit.framework.TestCase.*;
14
15 public class ExternalDatabaseTest {
16
17         @Test
18         public void testExternalDB() throws Exception {
19                 // Connect to inmemory db using External database class.
20
21                 Database inmemory = DatabaseBuilder
22                                 .getDatabase(DatabaseProvider.CAPABILITY_IN_MEMORY);
23                 try {
24                         inmemory.start();
25
26                         System.setProperty(ExternalDatabase.DB_URL_PROP, inmemory
27                                         .getExternalJdbcUrl());
28                         System.setProperty(ExternalDatabase.DB_USER_PROP, inmemory
29                                         .getUsername());
30                         System.setProperty(ExternalDatabase.DB_PASSWORD_PROP, inmemory
31                                         .getPassword());
32
33                         Database external = DatabaseBuilder
34                                         .getDatabase(DatabaseProvider.CAPABILITY_EXTERNAL);
35                         assertTrue(external instanceof ExternalDatabase);
36                         try {
37                                 DataSource ds = external.start();
38                                 Connection conn = ds.getConnection();
39                                 try {
40                                         System.out.println("Database name: "
41                                                         + conn.getMetaData().getDatabaseProductName());
42                                 } finally {
43                                         conn.close();
44                                 }
45                         } finally {
46                                 external.stop();
47                         }
48                 } finally {
49                         inmemory.stop();
50                 }
51
52         }
53 }