|
1 | 1 | /* |
2 | | - * Copyright 2012-2023 the original author or authors. |
| 2 | + * Copyright 2012-2024 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. |
@@ -61,88 +61,99 @@ void after() { |
61 | 61 |
|
62 | 62 | @Test |
63 | 63 | void autoConfiguredInMemoryDataSourceIsShutdown() throws Exception { |
64 | | - ConfigurableApplicationContext context = getContext( |
65 | | - () -> createContext(DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class)); |
66 | | - Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class)); |
67 | | - context.close(); |
68 | | - then(statement).should().execute("SHUTDOWN"); |
| 64 | + try (ConfigurableApplicationContext context = getContext( |
| 65 | + () -> createContext(DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) { |
| 66 | + Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class)); |
| 67 | + context.close(); |
| 68 | + then(statement).should().execute("SHUTDOWN"); |
| 69 | + } |
69 | 70 | } |
70 | 71 |
|
71 | 72 | @Test |
72 | 73 | void autoConfiguredExternalDataSourceIsNotShutdown() throws Exception { |
73 | | - ConfigurableApplicationContext context = getContext(() -> createContext("org.postgresql.Driver", |
74 | | - DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class)); |
75 | | - Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class)); |
76 | | - context.close(); |
77 | | - then(statement).should(never()).execute("SHUTDOWN"); |
| 74 | + try (ConfigurableApplicationContext context = getContext(() -> createContext("org.postgresql.Driver", |
| 75 | + DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) { |
| 76 | + Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class)); |
| 77 | + context.close(); |
| 78 | + then(statement).should(never()).execute("SHUTDOWN"); |
| 79 | + } |
78 | 80 | } |
79 | 81 |
|
80 | 82 | @Test |
81 | 83 | void h2ServerIsNotShutdown() throws Exception { |
82 | | - ConfigurableApplicationContext context = getContext(() -> createContext("org.h2.Driver", |
83 | | - "jdbc:h2:hsql://localhost", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class)); |
84 | | - Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class)); |
85 | | - context.close(); |
86 | | - then(statement).should(never()).execute("SHUTDOWN"); |
| 84 | + try (ConfigurableApplicationContext context = getContext(() -> createContext("org.h2.Driver", |
| 85 | + "jdbc:h2:hsql://localhost", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) { |
| 86 | + Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class)); |
| 87 | + context.close(); |
| 88 | + then(statement).should(never()).execute("SHUTDOWN"); |
| 89 | + } |
87 | 90 | } |
88 | 91 |
|
89 | 92 | @Test |
90 | 93 | void inMemoryH2IsShutdown() throws Exception { |
91 | | - ConfigurableApplicationContext context = getContext(() -> createContext("org.h2.Driver", "jdbc:h2:mem:test", |
92 | | - DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class)); |
93 | | - Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class)); |
94 | | - context.close(); |
95 | | - then(statement).should().execute("SHUTDOWN"); |
| 94 | + try (ConfigurableApplicationContext context = getContext(() -> createContext("org.h2.Driver", |
| 95 | + "jdbc:h2:mem:test", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) { |
| 96 | + Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class)); |
| 97 | + context.close(); |
| 98 | + then(statement).should().execute("SHUTDOWN"); |
| 99 | + } |
96 | 100 | } |
97 | 101 |
|
98 | 102 | @Test |
99 | 103 | void hsqlServerIsNotShutdown() throws Exception { |
100 | | - ConfigurableApplicationContext context = getContext(() -> createContext("org.hsqldb.jdbcDriver", |
101 | | - "jdbc:hsqldb:hsql://localhost", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class)); |
102 | | - Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class)); |
103 | | - context.close(); |
104 | | - then(statement).should(never()).execute("SHUTDOWN"); |
| 104 | + try (ConfigurableApplicationContext context = getContext(() -> createContext("org.hsqldb.jdbcDriver", |
| 105 | + "jdbc:hsqldb:hsql://localhost", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) { |
| 106 | + Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class)); |
| 107 | + context.close(); |
| 108 | + then(statement).should(never()).execute("SHUTDOWN"); |
| 109 | + } |
105 | 110 | } |
106 | 111 |
|
107 | 112 | @Test |
108 | 113 | void inMemoryHsqlIsShutdown() throws Exception { |
109 | | - ConfigurableApplicationContext context = getContext(() -> createContext("org.hsqldb.jdbcDriver", |
110 | | - "jdbc:hsqldb:mem:test", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class)); |
111 | | - Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class)); |
112 | | - context.close(); |
113 | | - then(statement).should().execute("SHUTDOWN"); |
| 114 | + try (ConfigurableApplicationContext context = getContext(() -> createContext("org.hsqldb.jdbcDriver", |
| 115 | + "jdbc:hsqldb:mem:test", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) { |
| 116 | + Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class)); |
| 117 | + context.close(); |
| 118 | + then(statement).should().execute("SHUTDOWN"); |
| 119 | + } |
114 | 120 | } |
115 | 121 |
|
116 | 122 | @Test |
117 | 123 | void derbyClientIsNotShutdown() throws Exception { |
118 | | - ConfigurableApplicationContext context = getContext(() -> createContext("org.apache.derby.jdbc.ClientDriver", |
119 | | - "jdbc:derby://localhost", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class)); |
120 | | - Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class)); |
121 | | - context.close(); |
122 | | - then(statement).should(never()).execute("SHUTDOWN"); |
| 124 | + try (ConfigurableApplicationContext context = getContext( |
| 125 | + () -> createContext("org.apache.derby.jdbc.ClientDriver", "jdbc:derby://localhost", |
| 126 | + DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) { |
| 127 | + Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class)); |
| 128 | + context.close(); |
| 129 | + then(statement).should(never()).execute("SHUTDOWN"); |
| 130 | + } |
123 | 131 | } |
124 | 132 |
|
125 | 133 | @Test |
126 | 134 | void inMemoryDerbyIsShutdown() throws Exception { |
127 | | - ConfigurableApplicationContext context = getContext( |
| 135 | + try (ConfigurableApplicationContext context = getContext( |
128 | 136 | () -> createContext("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:memory:test;create=true", |
129 | | - DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class)); |
130 | | - HikariDataSource dataSource = context.getBean(HikariDataSource.class); |
131 | | - JdbcTemplate jdbc = new JdbcTemplate(dataSource); |
132 | | - jdbc.execute("SELECT 1 FROM SYSIBM.SYSDUMMY1"); |
133 | | - HikariPoolMXBean pool = dataSource.getHikariPoolMXBean(); |
134 | | - // Prevent a race between Hikari's initialization and Derby shutdown |
135 | | - Awaitility.await() |
136 | | - .atMost(Duration.ofSeconds(30)) |
137 | | - .until(pool::getIdleConnections, (idle) -> idle == dataSource.getMinimumIdle()); |
138 | | - context.close(); |
139 | | - // Connect should fail as DB no longer exists |
140 | | - assertThatExceptionOfType(SQLException.class) |
141 | | - .isThrownBy(() -> new EmbeddedDriver().connect("jdbc:derby:memory:test", new Properties())) |
142 | | - .satisfies((ex) -> assertThat(ex.getSQLState()).isEqualTo("XJ004")); |
143 | | - // Shut Derby down fully so that it closes its log file |
144 | | - assertThatExceptionOfType(SQLException.class) |
145 | | - .isThrownBy(() -> new EmbeddedDriver().connect("jdbc:derby:;shutdown=true", new Properties())); |
| 137 | + DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class))) { |
| 138 | + HikariDataSource dataSource = context.getBean(HikariDataSource.class); |
| 139 | + JdbcTemplate jdbc = new JdbcTemplate(dataSource); |
| 140 | + jdbc.execute("SELECT 1 FROM SYSIBM.SYSDUMMY1"); |
| 141 | + HikariPoolMXBean pool = dataSource.getHikariPoolMXBean(); |
| 142 | + // Prevent a race between Hikari's initialization and Derby shutdown |
| 143 | + Awaitility.await() |
| 144 | + .atMost(Duration.ofSeconds(30)) |
| 145 | + .until(pool::getIdleConnections, (idle) -> idle == dataSource.getMinimumIdle()); |
| 146 | + context.close(); |
| 147 | + // Connect should fail as DB no longer exists |
| 148 | + assertThatExceptionOfType(SQLException.class).isThrownBy(() -> connect("jdbc:derby:memory:test")) |
| 149 | + .satisfies((ex) -> assertThat(ex.getSQLState()).isEqualTo("XJ004")); |
| 150 | + // Shut Derby down fully so that it closes its log file |
| 151 | + assertThatExceptionOfType(SQLException.class).isThrownBy(() -> connect("jdbc:derby:;shutdown=true")); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private void connect(String jdbcUrl) throws SQLException { |
| 156 | + new EmbeddedDriver().connect(jdbcUrl, new Properties()).close(); |
146 | 157 | } |
147 | 158 |
|
148 | 159 | } |
0 commit comments