|
16 | 16 | package org.utplsql.sqldev.test.coverage; |
17 | 17 |
|
18 | 18 | import java.io.File; |
| 19 | +import java.io.IOException; |
19 | 20 | import java.nio.charset.StandardCharsets; |
20 | 21 | import java.nio.file.Files; |
21 | 22 | import java.nio.file.Path; |
22 | 23 | import java.sql.Connection; |
23 | | -import java.util.Collections; |
| 24 | +import java.util.Arrays; |
24 | 25 | import java.util.Comparator; |
25 | 26 | import java.util.List; |
26 | 27 | import java.util.Optional; |
27 | | -import java.util.function.Predicate; |
28 | | -import java.util.function.ToLongFunction; |
29 | | -import org.eclipse.xtend2.lib.StringConcatenation; |
30 | | -import org.eclipse.xtext.xbase.lib.CollectionLiterals; |
31 | | -import org.eclipse.xtext.xbase.lib.Exceptions; |
| 28 | + |
32 | 29 | import org.junit.AfterClass; |
33 | 30 | import org.junit.Assert; |
34 | 31 | import org.junit.BeforeClass; |
35 | 32 | import org.junit.Test; |
36 | | -import org.springframework.jdbc.BadSqlGrammarException; |
37 | 33 | import org.springframework.jdbc.datasource.SingleConnectionDataSource; |
38 | 34 | import org.utplsql.sqldev.coverage.CodeCoverageReporter; |
| 35 | +import org.utplsql.sqldev.exception.GenericRuntimeException; |
| 36 | +import org.utplsql.sqldev.model.DatabaseTools; |
| 37 | +import org.utplsql.sqldev.model.FileTools; |
| 38 | +import org.utplsql.sqldev.model.SystemTools; |
39 | 39 | import org.utplsql.sqldev.test.AbstractJdbcTest; |
40 | 40 |
|
41 | | -@SuppressWarnings("all") |
42 | 41 | public class CodeCoverageReporterTest extends AbstractJdbcTest { |
43 | | - @BeforeClass |
44 | | - public static void setup() { |
45 | | - StringConcatenation _builder = new StringConcatenation(); |
46 | | - _builder.append("CREATE OR REPLACE FUNCTION f RETURN INTEGER IS"); |
47 | | - _builder.newLine(); |
48 | | - _builder.append("BEGIN"); |
49 | | - _builder.newLine(); |
50 | | - _builder.append(" "); |
51 | | - _builder.append("RETURN 1;"); |
52 | | - _builder.newLine(); |
53 | | - _builder.append("END f;"); |
54 | | - _builder.newLine(); |
55 | | - AbstractJdbcTest.jdbcTemplate.execute(_builder.toString()); |
56 | | - StringConcatenation _builder_1 = new StringConcatenation(); |
57 | | - _builder_1.append("CREATE OR REPLACE PACKAGE test_f IS"); |
58 | | - _builder_1.newLine(); |
59 | | - _builder_1.append(" "); |
60 | | - _builder_1.append("--%suite"); |
61 | | - _builder_1.newLine(); |
62 | | - _builder_1.newLine(); |
63 | | - _builder_1.append(" "); |
64 | | - _builder_1.append("--%test"); |
65 | | - _builder_1.newLine(); |
66 | | - _builder_1.append(" "); |
67 | | - _builder_1.append("PROCEDURE f;"); |
68 | | - _builder_1.newLine(); |
69 | | - _builder_1.append("END test_f;"); |
70 | | - _builder_1.newLine(); |
71 | | - AbstractJdbcTest.jdbcTemplate.execute(_builder_1.toString()); |
72 | | - StringConcatenation _builder_2 = new StringConcatenation(); |
73 | | - _builder_2.append("CREATE OR REPLACE PACKAGE BODY test_f IS"); |
74 | | - _builder_2.newLine(); |
75 | | - _builder_2.append(" "); |
76 | | - _builder_2.append("--%test"); |
77 | | - _builder_2.newLine(); |
78 | | - _builder_2.append(" "); |
79 | | - _builder_2.append("PROCEDURE f IS"); |
80 | | - _builder_2.newLine(); |
81 | | - _builder_2.append(" "); |
82 | | - _builder_2.append("l_expected INTEGER := 1;"); |
83 | | - _builder_2.newLine(); |
84 | | - _builder_2.append(" "); |
85 | | - _builder_2.append("l_actual INTEGER;"); |
86 | | - _builder_2.newLine(); |
87 | | - _builder_2.append(" "); |
88 | | - _builder_2.append("BEGIN"); |
89 | | - _builder_2.newLine(); |
90 | | - _builder_2.append(" "); |
91 | | - _builder_2.append("l_actual := scott.f();"); |
92 | | - _builder_2.newLine(); |
93 | | - _builder_2.append(" "); |
94 | | - _builder_2.append("ut.expect(l_actual).to_equal(l_expected);"); |
95 | | - _builder_2.newLine(); |
96 | | - _builder_2.append(" "); |
97 | | - _builder_2.append("END f;"); |
98 | | - _builder_2.newLine(); |
99 | | - _builder_2.append("END test_f;"); |
100 | | - _builder_2.newLine(); |
101 | | - AbstractJdbcTest.jdbcTemplate.execute(_builder_2.toString()); |
102 | | - } |
103 | | - |
104 | | - private Path getNewestOutputFile() { |
105 | | - try { |
106 | | - final File file = File.createTempFile("test", ".txt"); |
107 | | - final File dir = file.getParentFile(); |
108 | | - file.delete(); |
109 | | - final Predicate<Path> _function = (Path f) -> { |
110 | | - boolean _isDirectory = f.toFile().isDirectory(); |
111 | | - return (!_isDirectory); |
112 | | - }; |
113 | | - final Predicate<Path> _function_1 = (Path f) -> { |
114 | | - return f.getFileName().toString().startsWith("utplsql_"); |
115 | | - }; |
116 | | - final Predicate<Path> _function_2 = (Path f) -> { |
117 | | - return f.getFileName().toString().endsWith(".html"); |
118 | | - }; |
119 | | - final ToLongFunction<Path> _function_3 = (Path f) -> { |
120 | | - return f.toFile().lastModified(); |
121 | | - }; |
122 | | - final Optional<Path> last = Files.list(dir.toPath()).filter(_function).filter(_function_1).filter(_function_2).max(Comparator.<Path>comparingLong(_function_3)); |
123 | | - return last.get(); |
124 | | - } catch (Throwable _e) { |
125 | | - throw Exceptions.sneakyThrow(_e); |
| 42 | + |
| 43 | + @BeforeClass |
| 44 | + public static void setup() { |
| 45 | + StringBuilder sb = new StringBuilder(); |
| 46 | + sb.append("CREATE OR REPLACE FUNCTION f RETURN INTEGER IS\n"); |
| 47 | + sb.append("BEGIN\n"); |
| 48 | + sb.append(" RETURN 1;\n"); |
| 49 | + sb.append("END f;"); |
| 50 | + jdbcTemplate.execute(sb.toString()); |
| 51 | + sb.setLength(0); |
| 52 | + sb.append("CREATE OR REPLACE PACKAGE test_f IS\n"); |
| 53 | + sb.append(" --%suite\n\n"); |
| 54 | + sb.append(" --%test\n"); |
| 55 | + sb.append(" PROCEDURE f;\n"); |
| 56 | + sb.append("END test_f;"); |
| 57 | + jdbcTemplate.execute(sb.toString()); |
| 58 | + sb.setLength(0); |
| 59 | + sb.append("CREATE OR REPLACE PACKAGE BODY test_f IS\n"); |
| 60 | + sb.append(" --%test\n"); |
| 61 | + sb.append(" PROCEDURE f IS\n"); |
| 62 | + sb.append(" l_expected INTEGER := 1;\n"); |
| 63 | + sb.append(" l_actual INTEGER;\n"); |
| 64 | + sb.append(" BEGIN\n"); |
| 65 | + sb.append(" l_actual := scott.f();\n"); |
| 66 | + sb.append(" ut.expect(l_actual).to_equal(l_expected);\n"); |
| 67 | + sb.append(" END f;\n"); |
| 68 | + sb.append("END test_f;"); |
| 69 | + jdbcTemplate.execute(sb.toString()); |
| 70 | + } |
| 71 | + |
| 72 | + private File createTempFile(String prefix, String suffix) { |
| 73 | + try { |
| 74 | + return File.createTempFile("test", ".txt"); |
| 75 | + } catch (IOException e) { |
| 76 | + final String msg = "Cannot create temporary file with prefix '" + prefix + "' and suffix '" + suffix + "'."; |
| 77 | + throw new GenericRuntimeException(msg, e); |
| 78 | + } |
126 | 79 | } |
127 | | - } |
128 | | - |
129 | | - @Test |
130 | | - public void produceReportAndCloseConnection() { |
131 | | - try { |
132 | | - SingleConnectionDataSource ds = new SingleConnectionDataSource(); |
133 | | - ds.setDriverClassName("oracle.jdbc.OracleDriver"); |
134 | | - ds.setUrl(AbstractJdbcTest.dataSource.getUrl()); |
135 | | - ds.setUsername(AbstractJdbcTest.dataSource.getUsername()); |
136 | | - ds.setPassword(AbstractJdbcTest.dataSource.getPassword()); |
137 | | - final Connection conn = ds.getConnection(); |
138 | | - final List<String> pathList = Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList(":test_f")); |
139 | | - final List<String> includeObjectList = Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList("f")); |
140 | | - final CodeCoverageReporter reporter = new CodeCoverageReporter(pathList, includeObjectList, conn); |
141 | | - final Thread run = reporter.runAsync(); |
142 | | - run.join(20000); |
143 | | - Assert.assertEquals(Boolean.valueOf(true), Boolean.valueOf(conn.isClosed())); |
144 | | - final Path outputFile = this.getNewestOutputFile(); |
145 | | - Assert.assertTrue((outputFile != null)); |
146 | | - byte[] _readAllBytes = Files.readAllBytes(outputFile); |
147 | | - final String content = new String(_readAllBytes, StandardCharsets.UTF_8); |
148 | | - Assert.assertTrue(content.contains("<h3>SCOTT.F</h3><h4><span class=\"green\">100 %</span> lines covered</h4>")); |
149 | | - } catch (Throwable _e) { |
150 | | - throw Exceptions.sneakyThrow(_e); |
| 80 | + |
| 81 | + private Path getNewestOutputFile() { |
| 82 | + final File file = createTempFile("test", ".txt"); |
| 83 | + final File dir = file.getParentFile(); |
| 84 | + file.delete(); |
| 85 | + Optional<Path> last; |
| 86 | + try { |
| 87 | + last = Files.list(dir.toPath()) |
| 88 | + .filter(f -> !f.toFile().isDirectory() && f.getFileName().toString().startsWith("utplsql_") |
| 89 | + && f.getFileName().toString().endsWith(".html")) |
| 90 | + .max(Comparator.comparingLong(f -> f.toFile().lastModified())); |
| 91 | + } catch (IOException e) { |
| 92 | + final String msg = "Cannot get newest output file in " + dir.getAbsolutePath() + "."; |
| 93 | + throw new GenericRuntimeException(msg, e); |
| 94 | + } |
| 95 | + return last.get(); |
151 | 96 | } |
152 | | - } |
153 | | - |
154 | | - @AfterClass |
155 | | - public static void teardown() { |
156 | | - try { |
157 | | - AbstractJdbcTest.jdbcTemplate.execute("DROP PACKAGE test_f"); |
158 | | - } catch (final Throwable _t) { |
159 | | - if (_t instanceof BadSqlGrammarException) { |
160 | | - } else { |
161 | | - throw Exceptions.sneakyThrow(_t); |
162 | | - } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void produceReportAndCloseConnection() { |
| 100 | + // create temporary dataSource, closed by reporter |
| 101 | + SingleConnectionDataSource ds = new SingleConnectionDataSource(); |
| 102 | + ds.setDriverClassName("oracle.jdbc.OracleDriver"); |
| 103 | + ds.setUrl(dataSource.getUrl()); |
| 104 | + ds.setUsername(dataSource.getUsername()); |
| 105 | + ds.setPassword(dataSource.getPassword()); |
| 106 | + final Connection conn = DatabaseTools.getConnection(ds); |
| 107 | + final List<String> pathList = Arrays.asList(":test_f"); |
| 108 | + final List<String> includeObjectList = Arrays.asList("f"); |
| 109 | + final CodeCoverageReporter reporter = new CodeCoverageReporter(pathList, includeObjectList, conn); |
| 110 | + final Thread run = reporter.runAsync(); |
| 111 | + SystemTools.waitForThread(run, 20000); |
| 112 | + Assert.assertTrue(DatabaseTools.isConnectionClosed(conn)); |
| 113 | + final Path outputFile = this.getNewestOutputFile(); |
| 114 | + Assert.assertNotNull(outputFile); |
| 115 | + final String content = new String(FileTools.readFile(outputFile), StandardCharsets.UTF_8); |
| 116 | + Assert.assertTrue( |
| 117 | + content.contains("<h3>SCOTT.F</h3><h4><span class=\"green\">100 %</span> lines covered</h4>")); |
163 | 118 | } |
164 | | - try { |
165 | | - AbstractJdbcTest.jdbcTemplate.execute("DROP FUNCTION f"); |
166 | | - } catch (final Throwable _t_1) { |
167 | | - if (_t_1 instanceof BadSqlGrammarException) { |
168 | | - } else { |
169 | | - throw Exceptions.sneakyThrow(_t_1); |
170 | | - } |
| 119 | + |
| 120 | + @AfterClass |
| 121 | + public static void teardown() { |
| 122 | + executeAndIgnore(jdbcTemplate, "DROP PACKAGE test_f"); |
| 123 | + executeAndIgnore(jdbcTemplate, "DROP FUNCTION f"); |
171 | 124 | } |
172 | | - } |
173 | 125 | } |
0 commit comments