Cobertura检查不会使构建失败

时间:2018-09-05 04:34:05

标签: java maven code-coverage cobertura maven-cobertura-plugin

我创建了一个基本的Maven包。 src / main / java目录包含:

public class Blah {
    public int blah(){
        return 1;
    }

    public int bluh(){
        return 2;
    }
}

src / test / java目录包含:

import static org.junit.Assert.assertEquals;


import org.junit.Test;


public class BlahTest {
    @Test
    public void blahTest() {
        Blah b = new Blah();
        assertEquals(1, b.blah());
    }
}

pom如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cob_test</groupId>
    <artifactId>cob_test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <check>
                        <haltOnFailure>true</haltOnFailure>
                        <branchRate>75</branchRate>
                        <lineRate>85</lineRate>
                        <totalBranchRate>75</totalBranchRate>
                        <totalLineRate>85</totalLineRate>
                        <packageLineRate>75</packageLineRate>
                        <packageBranchRate>85</packageBranchRate>
                    </check>
                </configuration>
            </plugin>
        </plugins>
    </reporting>

</project>

我运行mvn install时,未验证检查部分中的各种参数。由于有2个功能,因此我希望覆盖率达到50%,并且预期的覆盖率下限会更高。因此,构建应该失败。另外,有没有一种方法可以在命令行构建后立即显示软件包级别的覆盖范围号,而不是将其包含在html文件中。

详细的拆分很有帮助,但是我也想在违反最小覆盖范围限制时使构建失败。

1 个答案:

答案 0 :(得分:1)

您必须执行

public class RsyncCommandLine {

    /** Logger */
    private static final Logger logger = LogManager.getLogger(RsyncCommandLine.class);


    public String startRsync(String source, String destination) throws IOException {
        List<String> commands = createCommandLine(source, destination);
        List<String> lines = new ArrayList<>();
        Integer exitCode = null;
        try {
            ProcessBuilder processBuilder = new ProcessBuilder(commands).redirectErrorStream(true);

            final Process process = processBuilder.start();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                lines.add(line);
            }
            // Allow process to run up to 60 seconds
            process.waitFor(60, TimeUnit.SECONDS);
            // Get exit code from process
            exitCode = process.exitValue();
            //Convert exit code to meaningful statement
            String exitMessage = exitCodeToErrorMessage(exitCode);               
            lines.add(exitMessage);
        } catch (Exception ex) {
            logger.error(ex);
        }

        return convertLinesToString(lines);
    }


    private String exitCodeToErrorMessage(Integer exitCode) {
        String errorMessage = null;
        switch (exitCode) {
        case 0:
            errorMessage = "Success.";
            break;
        case 1:
            errorMessage = "Syntax or usage error.";
            break;
        case 2:
            errorMessage = "Protocol incompatibility.";
            break;
        case 3:
            errorMessage = "Errors selecting input/output files, dirs.";
            break;
        case 4:
            errorMessage = "Requested action not supported: an attempt was made to manipulate 64-bit files on a platform that cannot support them; or an option was specified that is supported by the client and not by the server.";
            break;
        case 5:
            errorMessage = "Error starting client-server protocol.";
            break;
        case 6:
            errorMessage = "Daemon unable to append to log-file.";
            break;
        case 10:
            errorMessage = "Error in socket I/O.";
            break;
        case 11:
            errorMessage = "Error in file I/O.";
            break;
        case 12:
            errorMessage = "Error in rsync protocol data stream.";
            break;
        case 13:
            errorMessage = "Errors with program diagnostics.";
            break;
        case 14:
            errorMessage = "Error in IPC code.";
            break;
        case 20:
            errorMessage = "Received SIGUSR1 or SIGINT.";
            break;
        case 21:
            errorMessage = "Some error returned by waitpid().";
            break;
        case 22:
            errorMessage = "Error allocating core memory buffers.";
            break;
        case 23:
            errorMessage = "Partial transfer due to error.";
            break;
        case 24:
            errorMessage = "Partial transfer due to vanished source files.";
            break;
        case 25:
            errorMessage = "The --max-delete limit stopped deletions.";
            break;
        case 30:
            errorMessage = "Timeout in data send/receive.";
            break;
        case 35:
            errorMessage = "Timeout waiting for daemon connection.";
            break;
        default:
            errorMessage = "Unrecognised error code.";
        }
        return errorMessage;
    }

    protected String convertLinesToString(List<String> lines) {
        String result = null;

        if (lines != null && !lines.isEmpty()) {
            StringBuilder builder = new StringBuilder();
            for (String line : lines) {
                builder.append(line).append(" ");
            }
            result = builder.toString().trim();
        }
        return result;
    }

    protected List<String> createCommandLine(String source, String destination) {
        // rsync -rtvuch <source> <destination>
        List<String> commands = new ArrayList<>();

        commands.add("rsync");
        commands.add("-rtvuch");

        String escapedSource = source.trim();
        String escapedDestination = destination.trim();

        commands.add(escapedSource);
        commands.add(escapedDestination);
        logger.debug("escapedSource " + escapedSource);
        logger.debug("escapedDestination " + escapedDestination);

        return commands;
    }

}

要运行cobertura,因为它默认是在真实的生命周期阶段运行。

如果要更改,可以按以下方式修改插件配置(将阶段从“验证”更改为“喜欢”):

mvn verify