1+ /*
2+ * Copyright 2018 Philipp Salvisberg <philipp.salvisberg@trivadis.com>
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+ package org.utplsql.sqldev.tests
17+
18+ import java.io.File
19+ import java.nio.file.Files
20+ import java.nio.file.Path
21+ import java.util.Comparator
22+ import org.junit.AfterClass
23+ import org.junit.Assert
24+ import org.junit.BeforeClass
25+ import org.junit.Test
26+ import org.springframework.jdbc.BadSqlGrammarException
27+ import org.springframework.jdbc.datasource.SingleConnectionDataSource
28+ import org.utplsql.sqldev.CodeCoverageReporter
29+ import java.nio.charset.StandardCharsets
30+
31+ class CodeCoverageReporterTest extends AbstractJdbcTest {
32+
33+ @BeforeClass
34+ def static void setup () {
35+ jdbcTemplate. execute(' ' '
36+ CREATE OR REPLACE FUNCTION f RETURN INTEGER IS
37+ BEGIN
38+ RETURN 1;
39+ END f;
40+ ' ' ' )
41+ jdbcTemplate. execute(' ' '
42+ CREATE OR REPLACE PACKAGE test_f IS
43+ --%suite
44+
45+ --%test
46+ PROCEDURE f;
47+ END test_f;
48+ ' ' ' )
49+ jdbcTemplate. execute(' ' '
50+ CREATE OR REPLACE PACKAGE BODY test_f IS
51+ --%test
52+ PROCEDURE f IS
53+ l_expected INTEGER := 1;
54+ l_actual INTEGER;
55+ BEGIN
56+ l_actual := scott.f();
57+ ut.expect(l_actual).to_equal(l_expected);
58+ END f;
59+ END test_f;
60+ ' ' ' )
61+ }
62+
63+ private def Path getNewestOutputFile () {
64+ val file = File . createTempFile(" test" , " .txt" )
65+ val dir = file. parentFile
66+ file. delete
67+ val last = Files . list(dir. toPath)
68+ . filter([f | ! f. toFile. directory])
69+ . filter([f | f. fileName. toString. startsWith(" utplsql_" )])
70+ . filter([f | f. fileName. toString. endsWith(" .html" )])
71+ . max(Comparator . comparingLong([f|f. toFile(). lastModified()]))
72+ if (last. isPresent) {
73+ return last. get
74+ } else {
75+ return null
76+ }
77+ }
78+
79+ @Test
80+ def void produceReportAndCloseConnection () {
81+ // create temporary dataSource, closed by reporter
82+ var ds = new SingleConnectionDataSource ()
83+ ds. driverClassName = " oracle.jdbc.OracleDriver"
84+ ds. url = dataSource. url
85+ ds. username = dataSource. username
86+ ds. password = dataSource. password
87+ val conn = ds. connection
88+ val pathList= #[' :test_f' ]
89+ val includeObjectList = #[' f' ]
90+ val reporter = new CodeCoverageReporter (pathList, includeObjectList, conn)
91+ val run = reporter. runAsync
92+ run. join(2000 )
93+ Assert . assertEquals(true , conn. isClosed)
94+ val outputFile = getNewestOutputFile
95+ Assert . assertTrue(outputFile !== null )
96+ val content = new String (Files . readAllBytes(outputFile), StandardCharsets . UTF_8 )
97+ Assert . assertTrue(content. contains(' <h3>SCOTT.F</h3><h4><span class="green">100 %</span> lines covered</h4>' ))
98+ }
99+
100+ @AfterClass
101+ def static void teardown () {
102+ try {
103+ jdbcTemplate. execute(" DROP PACKAGE test_f" )
104+ } catch (BadSqlGrammarException e) {
105+ // ignore
106+ }
107+ try {
108+ jdbcTemplate. execute(" DROP FUNCTION f" )
109+ } catch (BadSqlGrammarException e) {
110+ // ignore
111+ }
112+ }
113+
114+
115+
116+ }
0 commit comments