Skip to content

Commit 99b74df

Browse files
authored
Merge pull request #1007 from hazendaz/master
[tests] Ensure resources are closed fixing sonar issues
2 parents 0b36d3e + ab19768 commit 99b74df

File tree

97 files changed

+333
-188
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+333
-188
lines changed

src/test/java/org/apache/ibatis/autoconstructor/AutoConstructorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public static void setUp() throws Exception {
4747
final ScriptRunner runner = new ScriptRunner(conn);
4848
runner.setLogWriter(null);
4949
runner.runScript(dbReader);
50+
conn.close();
5051
dbReader.close();
5152
session.close();
5253
}

src/test/java/org/apache/ibatis/binding/BindingTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import static org.junit.Assert.assertSame;
2323
import static org.junit.Assert.assertTrue;
2424

25+
import java.io.IOException;
2526
import java.lang.reflect.Method;
2627
import java.util.ArrayList;
2728
import java.util.Collection;
@@ -53,6 +54,7 @@
5354
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
5455
import org.apache.ibatis.transaction.TransactionFactory;
5556
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
57+
import org.junit.Assert;
5658
import org.junit.BeforeClass;
5759
import org.junit.Ignore;
5860
import org.junit.Test;
@@ -784,6 +786,9 @@ public void executeWithCursorAndRowBounds() {
784786
assertEquals(2, blog.getId());
785787
assertFalse(blogIterator.hasNext());
786788

789+
blogs.close();
790+
} catch (IOException e) {
791+
Assert.fail(e.getMessage());
787792
} finally {
788793
session.close();
789794
}

src/test/java/org/apache/ibatis/binding/MapperWithOneAndMany.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,17 +25,17 @@
2525
import org.apache.ibatis.domain.blog.Blog;
2626

2727
public interface MapperWithOneAndMany {
28-
29-
@Select({
30-
"SELECT *",
31-
"FROM blog"
32-
})
33-
@Results({
34-
@Result(
35-
property = "author", column = "author_id",
36-
one = @One(select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor"),
37-
many = @Many(select = "selectPostsById"))
38-
})
39-
List<Blog> selectWithBothOneAndMany();
40-
28+
29+
@Select({
30+
"SELECT *",
31+
"FROM blog"
32+
})
33+
@Results({
34+
@Result(
35+
property = "author", column = "author_id",
36+
one = @One(select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor"),
37+
many = @Many(select = "selectPostsById"))
38+
})
39+
List<Blog> selectWithBothOneAndMany();
40+
4141
}

src/test/java/org/apache/ibatis/jdbc/PooledDataSourceTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -88,6 +88,7 @@ public void ShouldReturnRealConnection() throws Exception {
8888
PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
8989
Connection c = ds.getConnection();
9090
JDBCConnection realConnection = (JDBCConnection) PooledDataSource.unwrapConnection(c);
91+
c.close();
9192
}
9293

9394
@Ignore("See the comments")
@@ -104,7 +105,6 @@ public void shouldReconnectWhenServerKilledLeakedConnection() throws Exception {
104105
final String USERNAME = "admin";
105106
final String PASSWORD = "";
106107

107-
Connection con;
108108
PooledDataSource ds = new PooledDataSource();
109109
ds.setDriver("com.mysql.jdbc.Driver");
110110
ds.setUrl(URL);
@@ -120,14 +120,16 @@ public void shouldReconnectWhenServerKilledLeakedConnection() throws Exception {
120120
// MySQL wait_timeout * 1000 or less. (unit:ms)
121121
ds.setPoolPingConnectionsNotUsedFor(1000);
122122

123-
con = ds.getConnection();
123+
Connection con = ds.getConnection();
124124
exexuteQuery(con);
125125
// Simulate connection leak by not closing.
126126
// con.close();
127127

128128
// Wait for disconnected from mysql...
129129
Thread.sleep(TimeUnit.SECONDS.toMillis(3));
130130

131+
con.close();
132+
131133
// Should return usable connection.
132134
con = ds.getConnection();
133135
exexuteQuery(con);

src/test/java/org/apache/ibatis/jdbc/ScriptRunnerTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,6 +49,7 @@ public void shouldRunScriptsBySendingFullScriptAtOnce() throws Exception {
4949
runner.setStopOnError(false);
5050
runner.setErrorLogWriter(null);
5151
runner.setLogWriter(null);
52+
conn.close();
5253
runJPetStoreScripts(runner);
5354
assertProductsTableExistsAndLoaded();
5455
}
@@ -63,6 +64,7 @@ public void shouldRunScriptsUsingConnection() throws Exception {
6364
runner.setErrorLogWriter(null);
6465
runner.setLogWriter(null);
6566
runJPetStoreScripts(runner);
67+
conn.close();
6668
assertProductsTableExistsAndLoaded();
6769
}
6870

@@ -102,6 +104,8 @@ public void shouldReturnWarningIfEndOfLineTerminatorNotFound() throws Exception
102104
} catch (Exception e) {
103105
assertTrue(e.getMessage().contains("end-of-line terminator"));
104106
}
107+
reader.close();
108+
conn.close();
105109
}
106110

107111
@Test
@@ -123,6 +127,8 @@ public void commentAferStatementDelimiterShouldNotCauseRunnerFail() throws Excep
123127
} catch (Exception e) {
124128
fail(e.getMessage());
125129
}
130+
reader.close();
131+
conn.close();
126132
}
127133

128134
@Test
@@ -144,6 +150,8 @@ public void shouldReturnWarningIfNotTheCurrentDelimiterUsed() throws Exception {
144150
} catch (Exception e) {
145151
assertTrue(e.getMessage().contains("end-of-line terminator"));
146152
}
153+
reader.close();
154+
conn.close();
147155
}
148156

149157
@Test
@@ -165,6 +173,8 @@ public void changingDelimiterShouldNotCauseRunnerFail() throws Exception {
165173
} catch (Exception e) {
166174
fail(e.getMessage());
167175
}
176+
reader.close();
177+
conn.close();
168178
}
169179

170180
@Test
@@ -182,6 +192,7 @@ public void testLogging() throws Exception {
182192

183193
Reader reader = new StringReader("select userid from account where userid = 'j2ee';");
184194
runner.runScript(reader);
195+
conn.close();
185196

186197
assertEquals(
187198
"select userid from account where userid = 'j2ee'" + System.getProperty("line.separator")
@@ -204,6 +215,7 @@ public void testLoggingFullScipt() throws Exception {
204215

205216
Reader reader = new StringReader("select userid from account where userid = 'j2ee';");
206217
runner.runScript(reader);
218+
conn.close();
207219

208220
assertEquals(
209221
"select userid from account where userid = 'j2ee';" + System.getProperty("line.separator")
@@ -223,6 +235,7 @@ private void assertProductsTableExistsAndLoaded() throws IOException, SQLExcepti
223235
SqlRunner executor = new SqlRunner(conn);
224236
List<Map<String, Object>> products = executor.selectAll("SELECT * FROM PRODUCT");
225237
assertEquals(16, products.size());
238+
conn.close();
226239
} finally {
227240
ds.forceCloseAll();
228241
}

src/test/java/org/apache/ibatis/parsing/XPathParserTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@ public void shouldTestXPathParserMethods() throws Exception {
4141
XNode node = parser.evalNode("/employee/height");
4242
assertEquals("employee/height", node.getPath());
4343
assertEquals("employee[${id_var}]_height", node.getValueBasedIdentifier());
44+
inputStream.close();
4445
}
4546

4647
}

src/test/java/org/apache/ibatis/submitted/ancestor_ref/AncestorRefTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,6 +45,7 @@ public static void setUp() throws Exception {
4545
ScriptRunner runner = new ScriptRunner(conn);
4646
runner.setLogWriter(null);
4747
runner.runScript(reader);
48+
conn.close();
4849
reader.close();
4950
session.close();
5051
}

src/test/java/org/apache/ibatis/submitted/array_result_type/ArrayResultTypeTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@ public static void setUp() throws Exception {
4646
ScriptRunner runner = new ScriptRunner(conn);
4747
runner.setLogWriter(null);
4848
runner.runScript(reader);
49+
conn.close();
4950
reader.close();
5051
session.close();
5152
}

src/test/java/org/apache/ibatis/submitted/association_nested/FolderMapperTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,9 @@ public void testFindWithChildren() throws Exception {
4646
stmt.execute("insert into folder values(4, 'Folder 2_1', 3)");
4747
stmt.execute("insert into folder values(5, 'Folder 2_2', 3)");
4848

49+
stmt.close();
50+
conn.close();
51+
4952
/**
5053
* Root/
5154
* Folder 1/
@@ -65,6 +68,7 @@ public void testFindWithChildren() throws Exception {
6568

6669
Assert.assertEquals(3, folders.size());
6770

71+
inputStream.close();
6872
session.close();
6973
}
7074

src/test/java/org/apache/ibatis/submitted/associationtest/AssociationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@ public static void setUp() throws Exception {
4646
ScriptRunner runner = new ScriptRunner(conn);
4747
runner.setLogWriter(null);
4848
runner.runScript(reader);
49+
conn.close();
4950
reader.close();
5051
session.close();
5152
}

0 commit comments

Comments
 (0)