|
1 | 1 | /* |
2 | | - * Copyright 2002-2013 the original author or authors. |
| 2 | + * Copyright 2002-2014 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
42 | 42 | * @author Dave Syer |
43 | 43 | * @author Juergen Hoeller |
44 | 44 | * @author Chris Beams |
| 45 | + * @author Sam Brannen |
45 | 46 | */ |
46 | 47 | public class JdbcNamespaceIntegrationTests { |
47 | 48 |
|
48 | 49 | @Rule |
49 | 50 | public ExpectedException expected = ExpectedException.none(); |
50 | 51 |
|
| 52 | + |
51 | 53 | @Test |
52 | | - public void testCreateEmbeddedDatabase() throws Exception { |
| 54 | + public void createEmbeddedDatabase() throws Exception { |
53 | 55 | Assume.group(TestGroup.LONG_RUNNING); |
54 | | - |
55 | | - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( |
56 | | - "org/springframework/jdbc/config/jdbc-config.xml"); |
57 | | - assertCorrectSetup(context, "dataSource", "h2DataSource", "derbyDataSource"); |
58 | | - context.close(); |
| 56 | + assertCorrectSetup("jdbc-config.xml", "dataSource", "h2DataSource", "derbyDataSource"); |
59 | 57 | } |
60 | 58 |
|
61 | 59 | @Test |
62 | | - public void testCreateEmbeddedDatabaseAgain() throws Exception { |
| 60 | + public void createEmbeddedDatabaseAgain() throws Exception { |
63 | 61 | // If Derby isn't cleaned up properly this will fail... |
64 | 62 | Assume.group(TestGroup.LONG_RUNNING); |
65 | | - |
66 | | - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( |
67 | | - "org/springframework/jdbc/config/jdbc-config.xml"); |
68 | | - assertCorrectSetup(context, "derbyDataSource"); |
69 | | - context.close(); |
| 63 | + assertCorrectSetup("jdbc-config.xml", "derbyDataSource"); |
70 | 64 | } |
71 | 65 |
|
72 | 66 | @Test |
73 | | - public void testCreateWithResourcePattern() throws Exception { |
74 | | - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( |
75 | | - "org/springframework/jdbc/config/jdbc-config-pattern.xml"); |
76 | | - assertCorrectSetup(context, "dataSource"); |
77 | | - context.close(); |
| 67 | + public void createWithResourcePattern() throws Exception { |
| 68 | + assertCorrectSetup("jdbc-config-pattern.xml", "dataSource"); |
78 | 69 | } |
79 | 70 |
|
80 | 71 | @Test |
81 | | - public void testCreateWithEndings() throws Exception { |
82 | | - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( |
83 | | - "org/springframework/jdbc/config/jdbc-initialize-endings-config.xml"); |
84 | | - assertCorrectSetup(context, 2, "dataSource"); |
85 | | - context.close(); |
| 72 | + public void createWithEndings() throws Exception { |
| 73 | + assertCorrectSetupAndCloseContext("jdbc-initialize-endings-config.xml", 2, "dataSource"); |
86 | 74 | } |
87 | 75 |
|
88 | 76 | @Test |
89 | | - public void testCreateWithEndingsNested() throws Exception { |
90 | | - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( |
91 | | - "org/springframework/jdbc/config/jdbc-initialize-endings-nested-config.xml"); |
92 | | - assertCorrectSetup(context, 2, "dataSource"); |
93 | | - context.close(); |
| 77 | + public void createWithEndingsNested() throws Exception { |
| 78 | + assertCorrectSetupAndCloseContext("jdbc-initialize-endings-nested-config.xml", 2, "dataSource"); |
94 | 79 | } |
95 | 80 |
|
96 | 81 | @Test |
97 | | - public void testCreateAndDestroy() throws Exception { |
98 | | - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( |
99 | | - "org/springframework/jdbc/config/jdbc-destroy-config.xml"); |
| 82 | + public void createAndDestroy() throws Exception { |
| 83 | + ClassPathXmlApplicationContext context = context("jdbc-destroy-config.xml"); |
100 | 84 | try { |
101 | 85 | DataSource dataSource = context.getBean(DataSource.class); |
102 | 86 | JdbcTemplate template = new JdbcTemplate(dataSource); |
103 | | - assertEquals(1, template.queryForInt("select count(*) from T_TEST")); |
| 87 | + assertNumRowsInTestTable(template, 1); |
104 | 88 | context.getBean(DataSourceInitializer.class).destroy(); |
105 | 89 | expected.expect(BadSqlGrammarException.class); // Table has been dropped |
106 | | - assertEquals(1, template.queryForInt("select count(*) from T_TEST")); |
| 90 | + assertNumRowsInTestTable(template, 1); |
| 91 | + } |
| 92 | + finally { |
| 93 | + context.close(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void createAndDestroyNestedWithHsql() throws Exception { |
| 99 | + ClassPathXmlApplicationContext context = context("jdbc-destroy-nested-config.xml"); |
| 100 | + try { |
| 101 | + DataSource dataSource = context.getBean(DataSource.class); |
| 102 | + JdbcTemplate template = new JdbcTemplate(dataSource); |
| 103 | + assertNumRowsInTestTable(template, 1); |
| 104 | + context.getBean(EmbeddedDatabaseFactoryBean.class).destroy(); |
| 105 | + expected.expect(BadSqlGrammarException.class); // Table has been dropped |
| 106 | + assertNumRowsInTestTable(template, 1); |
107 | 107 | } |
108 | 108 | finally { |
109 | 109 | context.close(); |
110 | 110 | } |
111 | 111 | } |
112 | 112 |
|
113 | 113 | @Test |
114 | | - public void testCreateAndDestroyNested() throws Exception { |
115 | | - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( |
116 | | - "org/springframework/jdbc/config/jdbc-destroy-nested-config.xml"); |
| 114 | + public void createAndDestroyNestedWithH2() throws Exception { |
| 115 | + ClassPathXmlApplicationContext context = context("jdbc-destroy-nested-config-h2.xml"); |
117 | 116 | try { |
118 | 117 | DataSource dataSource = context.getBean(DataSource.class); |
119 | 118 | JdbcTemplate template = new JdbcTemplate(dataSource); |
120 | | - assertEquals(1, template.queryForInt("select count(*) from T_TEST")); |
| 119 | + assertNumRowsInTestTable(template, 1); |
121 | 120 | context.getBean(EmbeddedDatabaseFactoryBean.class).destroy(); |
122 | 121 | expected.expect(BadSqlGrammarException.class); // Table has been dropped |
123 | | - assertEquals(1, template.queryForInt("select count(*) from T_TEST")); |
| 122 | + assertNumRowsInTestTable(template, 1); |
124 | 123 | } |
125 | 124 | finally { |
126 | 125 | context.close(); |
127 | 126 | } |
128 | 127 | } |
129 | 128 |
|
130 | 129 | @Test |
131 | | - public void testMultipleDataSourcesHaveDifferentDatabaseNames() throws Exception { |
| 130 | + public void multipleDataSourcesHaveDifferentDatabaseNames() throws Exception { |
132 | 131 | DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); |
133 | 132 | new XmlBeanDefinitionReader(factory).loadBeanDefinitions(new ClassPathResource( |
134 | | - "org/springframework/jdbc/config/jdbc-config-multiple-datasources.xml")); |
| 133 | + "jdbc-config-multiple-datasources.xml", getClass())); |
135 | 134 | assertBeanPropertyValueOf("databaseName", "firstDataSource", factory); |
136 | 135 | assertBeanPropertyValueOf("databaseName", "secondDataSource", factory); |
137 | 136 | } |
138 | 137 |
|
| 138 | + private ClassPathXmlApplicationContext context(String file) { |
| 139 | + return new ClassPathXmlApplicationContext(file, getClass()); |
| 140 | + } |
| 141 | + |
139 | 142 | private void assertBeanPropertyValueOf(String propertyName, String expected, DefaultListableBeanFactory factory) { |
140 | 143 | BeanDefinition bean = factory.getBeanDefinition(expected); |
141 | 144 | PropertyValue value = bean.getPropertyValues().getPropertyValue(propertyName); |
142 | 145 | assertThat(value, is(notNullValue())); |
143 | 146 | assertThat(value.getValue().toString(), is(expected)); |
144 | 147 | } |
145 | 148 |
|
146 | | - private void assertCorrectSetup(ConfigurableApplicationContext context, String... dataSources) { |
147 | | - assertCorrectSetup(context, 1, dataSources); |
| 149 | + private void assertNumRowsInTestTable(JdbcTemplate template, int count) { |
| 150 | + assertEquals(count, template.queryForObject("select count(*) from T_TEST", Integer.class).intValue()); |
148 | 151 | } |
149 | 152 |
|
150 | | - private void assertCorrectSetup(ConfigurableApplicationContext context, int count, String... dataSources) { |
| 153 | + private void assertCorrectSetup(String file, String... dataSources) { |
| 154 | + assertCorrectSetupAndCloseContext(file, 1, dataSources); |
| 155 | + } |
| 156 | + |
| 157 | + private void assertCorrectSetupAndCloseContext(String file, int count, String... dataSources) { |
| 158 | + ConfigurableApplicationContext context = context(file); |
151 | 159 | try { |
152 | 160 | for (String dataSourceName : dataSources) { |
153 | 161 | DataSource dataSource = context.getBean(dataSourceName, DataSource.class); |
154 | 162 | JdbcTemplate template = new JdbcTemplate(dataSource); |
155 | | - assertEquals(count, template.queryForInt("select count(*) from T_TEST")); |
| 163 | + assertNumRowsInTestTable(template, count); |
156 | 164 | } |
157 | 165 | } |
158 | 166 | finally { |
159 | 167 | context.close(); |
160 | 168 | } |
161 | | - |
162 | 169 | } |
163 | 170 |
|
164 | 171 | } |
0 commit comments