|
| 1 | +/* |
| 2 | + * Copyright (c) 2011-2024 Qulice.com |
| 3 | + * |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions |
| 8 | + * are met: 1) Redistributions of source code must retain the above |
| 9 | + * copyright notice, this list of conditions and the following |
| 10 | + * disclaimer. 2) Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following |
| 12 | + * disclaimer in the documentation and/or other materials provided |
| 13 | + * with the distribution. 3) Neither the name of the Qulice.com nor |
| 14 | + * the names of its contributors may be used to endorse or promote |
| 15 | + * products derived from this software without specific prior written |
| 16 | + * permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT |
| 20 | + * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 23 | + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 29 | + * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | +package com.qulice.checkstyle; |
| 32 | + |
| 33 | +import com.google.common.base.Joiner; |
| 34 | +import com.qulice.checkstyle.test.extensions.FileToUrl; |
| 35 | +import com.qulice.checkstyle.test.extensions.RunnerConstant; |
| 36 | +import com.qulice.checkstyle.test.extensions.ViolationMatcher; |
| 37 | +import com.qulice.spi.Environment; |
| 38 | +import com.qulice.spi.Violation; |
| 39 | +import java.io.File; |
| 40 | +import java.util.Collection; |
| 41 | +import org.hamcrest.MatcherAssert; |
| 42 | +import org.hamcrest.Matchers; |
| 43 | +import org.junit.jupiter.api.BeforeEach; |
| 44 | +import org.junit.jupiter.api.Test; |
| 45 | + |
| 46 | +/** |
| 47 | + * Test case for license. |
| 48 | + * |
| 49 | + * @since 0.3 |
| 50 | + */ |
| 51 | +final class CheckstyleValidatorLicenseTest { |
| 52 | + |
| 53 | + /** |
| 54 | + * Rule for testing. |
| 55 | + */ |
| 56 | + private License rule; |
| 57 | + |
| 58 | + @BeforeEach |
| 59 | + void setRule() { |
| 60 | + this.rule = new License(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * CheckstyleValidator can catch checkstyle violations. |
| 65 | + * @throws Exception If something wrong happens inside. |
| 66 | + */ |
| 67 | + @Test |
| 68 | + void catchesCheckstyleViolationsInLicense() throws Exception { |
| 69 | + final Environment.Mock mock = new Environment.Mock(); |
| 70 | + final File license = this.rule.savePackageInfo( |
| 71 | + new File(mock.basedir(), RunnerConstant.DIRECTORY.value()) |
| 72 | + ).withLines("License-1.", "", "License-2.") |
| 73 | + .withEol("\n") |
| 74 | + .file(); |
| 75 | + final String content = |
| 76 | + // @checkstyle StringLiteralsConcatenation (4 lines) |
| 77 | + "/" + "*\n * License-3.\n *\n * License-2.\n */\n" |
| 78 | + + "package foo;\n" |
| 79 | + + "public class Foo { }\n"; |
| 80 | + final String name = "Foo.java"; |
| 81 | + final Environment env = mock.withParam( |
| 82 | + RunnerConstant.LICENSE_PROP.value(), |
| 83 | + new FileToUrl(license).toUrl() |
| 84 | + ).withFile(String.format("src/main/java/foo/%s", name), content); |
| 85 | + final Collection<Violation> results = |
| 86 | + new CheckstyleValidator(env) |
| 87 | + .validate(env.files(name)); |
| 88 | + MatcherAssert.assertThat( |
| 89 | + "Header validation is expected", |
| 90 | + results, |
| 91 | + Matchers.hasItem( |
| 92 | + new ViolationMatcher( |
| 93 | + "Line does not match expected header line of", name |
| 94 | + ) |
| 95 | + ) |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * CheckstyleValidator does not produce errors when last thing |
| 101 | + * in file are imports. The only exception that should be thrown is |
| 102 | + * qulice ValidationException. |
| 103 | + * @throws Exception In case of error. |
| 104 | + */ |
| 105 | + @Test |
| 106 | + void doesNotThrowExceptionIfImportsOnly() throws Exception { |
| 107 | + final Environment.Mock mock = new Environment.Mock(); |
| 108 | + final File license = this.rule.savePackageInfo( |
| 109 | + new File(mock.basedir(), RunnerConstant.DIRECTORY.value()) |
| 110 | + ).withLines("License-1.", "", "License-2.") |
| 111 | + .withEol("\n") |
| 112 | + .file(); |
| 113 | + final String crlf = "\r\n"; |
| 114 | + final String content = Joiner.on(crlf).join( |
| 115 | + "package com.google;", |
| 116 | + crlf, |
| 117 | + "import java.util.*;" |
| 118 | + ); |
| 119 | + final String name = "Foo.java"; |
| 120 | + final Environment env = mock.withParam( |
| 121 | + RunnerConstant.LICENSE_PROP.value(), |
| 122 | + new FileToUrl(license).toUrl() |
| 123 | + ).withFile(String.format("src/main/java/foo/%s", name), content); |
| 124 | + final Collection<Violation> results = |
| 125 | + new CheckstyleValidator(env).validate(env.files(name)); |
| 126 | + MatcherAssert.assertThat( |
| 127 | + "Validation error should exist", |
| 128 | + results, |
| 129 | + Matchers.not(Matchers.<Violation>empty()) |
| 130 | + ); |
| 131 | + } |
| 132 | +} |
0 commit comments