|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package com.sun.tdk.jcov.report; |
| 26 | + |
| 27 | +import com.sun.tdk.jcov.Instr; |
| 28 | +import com.sun.tdk.jcov.RepGen; |
| 29 | +import com.sun.tdk.jcov.instrument.DataRoot; |
| 30 | +import com.sun.tdk.jcov.instrument.InstrumentationOptions; |
| 31 | +import com.sun.tdk.jcov.instrument.Util; |
| 32 | +import org.testng.annotations.AfterClass; |
| 33 | +import org.testng.annotations.BeforeClass; |
| 34 | +import org.testng.annotations.Test; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | +import java.nio.file.Files; |
| 38 | +import java.nio.file.Path; |
| 39 | +import java.nio.file.Paths; |
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.List; |
| 42 | + |
| 43 | +import static org.testng.Assert.assertTrue; |
| 44 | + |
| 45 | +public class BasicReportTest { |
| 46 | + Path test_dir; |
| 47 | + Path template; |
| 48 | + Path result; |
| 49 | + int method_slot = -1; |
| 50 | + |
| 51 | + @BeforeClass |
| 52 | + public void setup() throws Exception { |
| 53 | + System.setProperty("jcov.selftest", "true"); |
| 54 | + |
| 55 | + Path data_dir = Paths.get(System.getProperty("user.dir")); |
| 56 | + |
| 57 | + //clear |
| 58 | + test_dir = data_dir.resolve("instr_test"); |
| 59 | + System.out.println("test dir = " + test_dir); |
| 60 | + Util.rmRF(test_dir); |
| 61 | + |
| 62 | + //prepare bytecode |
| 63 | + List<Path> classFiles = new Util(test_dir).copyBytecode(BasicUserCode.class.getName()); |
| 64 | + |
| 65 | + //instrument |
| 66 | + template = test_dir.resolve("template.xml"); |
| 67 | + List<String> params = new ArrayList<>(); |
| 68 | + params.add("-t"); |
| 69 | + params.add(template.toString()); |
| 70 | + params.add(classFiles.get(0).toString()); |
| 71 | + new Instr().run(params.toArray(new String[0])); |
| 72 | + |
| 73 | + //run |
| 74 | + new Util(test_dir).runClass(BasicUserCode.class, new String[] {}); |
| 75 | + |
| 76 | + //save coverage |
| 77 | + result = test_dir.resolve("result.xml"); |
| 78 | + DataRoot.getInstance(0).write(result.toString(), InstrumentationOptions.MERGE.OVERWRITE); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void textReport() throws IOException { |
| 83 | + Path report = test_dir.resolve("report.txt"); |
| 84 | + List<String> params = new ArrayList<>(); |
| 85 | + params.add("-format"); |
| 86 | + params.add("text"); |
| 87 | + params.add("-o"); |
| 88 | + params.add(report.toString()); |
| 89 | + params.add(result.toString()); |
| 90 | + new RepGen().run(params.toArray(new String[0])); |
| 91 | + assertTrue(Files.isRegularFile(report)); |
| 92 | + assertTrue(Files.readAllLines(report).contains( |
| 93 | + "MTH+: main([Ljava/lang/String;)V hits: 1 blocks: 75% (3/4); branches: 50% (1/2); lines: 75% (3/4);" |
| 94 | + )); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void htmlReport() throws IOException { |
| 99 | + Path report = test_dir.resolve("report.html"); |
| 100 | + List<String> params = new ArrayList<>(); |
| 101 | + params.add("-o"); |
| 102 | + params.add(report.toString()); |
| 103 | + params.add(result.toString()); |
| 104 | + new RepGen().run(params.toArray(new String[0])); |
| 105 | + assertTrue(Files.isDirectory(report)); |
| 106 | + Path classHtml = report.resolve(BasicUserCode.class.getName().replace('.', '/') + ".html"); |
| 107 | + assertTrue(Files.readAllLines(classHtml).stream().anyMatch(l -> l.contains("<b>60</b>%(3/5)"))); |
| 108 | + } |
| 109 | + |
| 110 | + @AfterClass |
| 111 | + public void tearDown() throws IOException { |
| 112 | + Util.rmRF(test_dir); |
| 113 | + } |
| 114 | +} |
0 commit comments