* Gradle supports only an internal API for this (@link{org.gradle.internal.hash.HashUtil}).
*/
public class HashUtil {
/**
* Calculates the SHA-256 to a given file.
*
* @param file the file
* @return the SHA-256
*/
public String calculateSha256(File file) {
try (RandomAccessFile inputStream = new RandomAccessFile(file, "r"); FileChannel channel = inputStream.getChannel()) {
// Calculate SHA-256
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
sha256.update(buffer);
// Convert to String
byte digest[] = sha256.digest();
StringBuilder result = new StringBuilder();
for (int i = 0; i < digest.length; i++) {
String hex = Integer.toHexString(0xff & digest[i]);
if (hex.length() == 1) {
result.append('0');
}
result.append(hex);
}
return result.toString();
} catch (NoSuchAlgorithmException | IOException e) {
throw new RuntimeException("Could not calculate SHA-256 hash of file: " + file, e);
}
}
}
================================================
FILE: gradle-lombok-plugin/src/main/resources/META-INF/gradle-plugins/io.franzbecker.gradle-lombok.properties
================================================
implementation-class=io.franzbecker.gradle.lombok.LombokPlugin
================================================
FILE: gradle-lombok-plugin/src/test/groovy/io/franzbecker/gradle/lombok/AbstractIntegrationTest.groovy
================================================
package io.franzbecker.gradle.lombok
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification
/**
* Abstract base class for integration tests.
*/
abstract class AbstractIntegrationTest extends Specification {
@Rule
TemporaryFolder testProjectDir
File projectDir
File buildFile
File propertiesFile
def setup() {
projectDir = testProjectDir.root
buildFile = createFile('build.gradle')
writeDefaultBuildFileContents()
propertiesFile = createFile('gradle.properties')
configureJacoco()
}
protected void writeDefaultBuildFileContents() {
buildFile << """\
plugins {
id 'java'
id '${LombokPlugin.NAME}'
}
repositories {
jcenter()
}
""".stripIndent()
}
/** see https://github.com/koral--/jacoco-gradle-testkit-plugin */
private void configureJacoco() {
propertiesFile << this.class.classLoader.getResourceAsStream('testkit-gradle.properties')
}
protected GradleRunner createGradleRunner() {
def runner = GradleRunner.create()
.withPluginClasspath()
.withProjectDir(projectDir)
return runner
}
BuildResult runBuild(String... arguments) {
def runner = createGradleRunner()
return runner.withArguments(arguments).build()
}
BuildResult runBuildAndFail(String... arguments) {
def runner = createGradleRunner()
return runner.withArguments(arguments).buildAndFail()
}
BuildResult runTaskAndFail(String task) {
def result = runBuildAndFail(task)
def buildTask = result.getTasks().first()
assert buildTask.path == ":$task"
assert buildTask.outcome == TaskOutcome.FAILED
return result
}
File createFile(String path) {
File file = new File(projectDir, path)
file.getParentFile().mkdirs()
file.createNewFile()
return file
}
protected void createSimpleTestCase() {
createSimpleTestCase("testCompile")
}
protected void createSimpleTestCase(String testConfigurationName) {
// build configuration supporting JUnit
buildFile << """
dependencies {
${testConfigurationName} 'junit:junit:4.12'
}
""".stripIndent()
// source code to compile and test
createJavaSource()
createTestSource()
}
private void createJavaSource() {
def file = createFile("src/main/java/com/example/HelloWorld.java")
file << """\
package com.example;
import lombok.Data;
@Data
public class HelloWorld {
private String id;
}
""".stripIndent()
}
private void createTestSource() {
def file = createFile("src/test/java/com/example/HelloWorldTest.java")
file << """\
package com.example;
import java.util.UUID;
import org.junit.Test;
import org.junit.Assert;
public class HelloWorldTest {
@Test
public void testHelloWorld() {
// Given
HelloWorld obj = new HelloWorld();
String id = UUID.randomUUID().toString();
// When
obj.setId(id);
// Then
Assert.assertEquals(id, obj.getId());
}
}
""".stripIndent()
}
}
================================================
FILE: gradle-lombok-plugin/src/test/groovy/io/franzbecker/gradle/lombok/CompatibilityIntegrationTest.groovy
================================================
package io.franzbecker.gradle.lombok
import org.gradle.testkit.runner.GradleRunner
import spock.lang.Unroll
@Unroll
class CompatibilityIntegrationTest extends AbstractIntegrationTest {
String theGradleVersion
@Override
protected GradleRunner createGradleRunner() {
def runner = super.createGradleRunner()
return runner.withGradleVersion(theGradleVersion)
}
def "Gradle #gradleVersion - can compile and test @Data annotation"() {
given:
theGradleVersion = gradleVersion
createSimpleTestCase(testConfigurationName)
when: "calling gradle test"
runBuild('test')
then: "build is successful and both class file exist"
noExceptionThrown()
new File(projectDir, "$classesPath/main/com/example/HelloWorld.class").exists()
new File(projectDir, "$classesPath/test/com/example/HelloWorldTest.class").exists()
where:
gradleVersion || classesPath || testConfigurationName
'2.12' || 'build/classes' || 'testCompile'
'2.14.1' || 'build/classes' || 'testCompile'
'3.5' || 'build/classes' || 'testCompile'
'4.2.1' || 'build/classes/java' || 'testCompile'
'4.7' || 'build/classes/java' || 'testCompile'
'5.4' || 'build/classes/java' || 'testCompile'
'6.4' || 'build/classes/java' || 'testCompile'
'6.4' || 'build/classes/java' || 'testImplementation'
'7.0' || 'build/classes/java' || 'testImplementation'
}
def "Gradle #gradleVersion - can run verifyLombok"() {
given:
theGradleVersion = gradleVersion
createSimpleTestCase(testConfigurationName)
when: "calling gradle verifyLombok"
runBuild('verifyLombok')
then: "no exception is thrown"
noExceptionThrown()
where:
gradleVersion || classesPath || testConfigurationName
'2.12' || 'build/classes' || 'testCompile'
'2.14.1' || 'build/classes' || 'testCompile'
'3.5' || 'build/classes' || 'testCompile'
'4.2.1' || 'build/classes/java' || 'testCompile'
'4.7' || 'build/classes/java' || 'testCompile'
'5.4' || 'build/classes/java' || 'testCompile'
'6.4' || 'build/classes/java' || 'testCompile'
'7.0' || 'build/classes/java' || 'testImplementation'
}
}
================================================
FILE: gradle-lombok-plugin/src/test/groovy/io/franzbecker/gradle/lombok/LombokPluginIntegrationTest.groovy
================================================
package io.franzbecker.gradle.lombok
import groovy.util.slurpersupport.GPathResult
/**
* Integration tests for {@link LombokPlugin}.
*/
class LombokPluginIntegrationTest extends AbstractIntegrationTest {
/**
* Creates a Java class with a method annotated with @SneakyThrows and tests if the
* annotation is applied properly.
*/
def "Can compile and test code with @SneakyThrows annotation."() {
given: "a valid build configuration"
buildFile << """
dependencies {
testCompile 'junit:junit:4.12'
}
""".stripIndent()
and: "source code with @SneakyThrows to compile and test"
createSneakyThrowsJavaSource()
createSneakyThrowsTestCode()
when: "calling gradle test"
runBuild('test')
then: "build is successful and both class file exist"
noExceptionThrown()
new File(projectDir, "build/classes/java/main/com/example/SneakyHelloWorld.class").exists()
new File(projectDir, "build/classes/java/test/com/example/SneakyHelloWorldTest.class").exists()
}
def "Can compile test code with Lombok depenceny"() {
given:
buildFile << """
dependencies {
testCompile 'junit:junit:4.12'
}
""".stripIndent()
and:
createTestCodeWithLombokDependency()
when:
runBuild('test')
then:
noExceptionThrown()
new File(projectDir, "build/classes/java/test/com/example/SneakyHelloWorldTest.class").exists()
}
def "Works with the java-library plugin"() {
given:
buildFile.text = buildFile.text.replace("id 'java'", "id 'java-library'")
createSimpleTestCase()
when:
runBuild('test')
then:
noExceptionThrown()
new File(projectDir, "build/classes/java/main/com/example/HelloWorld.class").exists()
new File(projectDir, "build/classes/java/test/com/example/HelloWorldTest.class").exists()
}
def "Dependency does not occur in generated POM using the 'maven' plugin"() {
given: "a valid build configuration"
buildFile << """
apply plugin: 'maven'
dependencies {
testCompile 'junit:junit:4.12'
}
""".stripIndent()
when: "calling gradle install"
runBuild('install')
then: "Parse POM and retrieve