1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.settings4j.objectresolver;
19
20 import java.io.File;
21 import java.sql.Connection;
22 import java.sql.PreparedStatement;
23 import java.sql.ResultSet;
24
25 import javax.sql.DataSource;
26
27 import junit.framework.TestCase;
28
29 import org.apache.commons.io.FileUtils;
30 import org.settings4j.ContentResolver;
31 import org.settings4j.contentresolver.ClasspathContentResolver;
32
33 public class SpringConfigObjectResolverTest extends TestCase {
34
35 private File testDir;
36
37
38 protected void setUp() throws Exception {
39 super.setUp();
40 FileUtils.deleteDirectory(new File("test"));
41 this.testDir = (new File("test/JavaXMLBeans/".toLowerCase())).getAbsoluteFile();
42 FileUtils.forceMkdir(this.testDir);
43 }
44
45
46 protected void tearDown() throws Exception {
47 FileUtils.deleteDirectory(new File("test"));
48 super.tearDown();
49 }
50
51 public void testSpringContext1() throws Exception {
52 final SpringConfigObjectResolver objectResolver = new SpringConfigObjectResolver();
53
54
55 final ContentResolver contentResolver = new ClasspathContentResolver();
56
57 final String key = "org/settings4j/objectresolver/testSpring1";
58
59 final byte[] springFileContent = contentResolver.getContent(key);
60 assertNotNull(springFileContent);
61
62 final DataSource hsqlDS = (DataSource) objectResolver.getObject(key, contentResolver);
63 assertNotNull(hsqlDS);
64
65
66 Connection conn = null;
67 try {
68 conn = hsqlDS.getConnection();
69 PreparedStatement pstmt = conn.prepareStatement("create Table test ( name VARCHAR )");
70 pstmt.execute();
71 pstmt.close();
72
73 pstmt = conn.prepareStatement("insert into test(name) values (?)");
74 pstmt.setString(1, "Hello World");
75 pstmt.execute();
76 pstmt.close();
77
78 pstmt = conn.prepareStatement("select * from test");
79 final ResultSet rs = pstmt.executeQuery();
80 rs.next();
81 final String result = rs.getString(1);
82 rs.close();
83 pstmt.close();
84
85 pstmt = conn.prepareStatement("drop Table test");
86 pstmt.execute();
87 pstmt.close();
88
89 assertEquals("Hello World", result);
90
91 } finally {
92 if (conn != null) {
93 conn.close();
94 }
95 }
96
97 }
98 }