View Javadoc
1   /* ***************************************************************************
2    * Copyright (c) 2008 Brabenetz Harald, Austria.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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          // Classpath is readonly => the XML-Spring-Config and Properties
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          // test DataSource
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(); // NOPMD must have Result - An exception would break the test.
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  }