├── testproject ├── options1 │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── sample │ │ │ ├── bar │ │ │ ├── Bar.java │ │ │ └── IgnoreClass.java │ │ │ └── foo │ │ │ ├── IgnoreClass.java │ │ │ ├── FooChild.java │ │ │ └── Foo.java │ ├── my_assert_class_template.txt │ └── build.gradle ├── basic │ ├── src │ │ ├── test │ │ │ └── java │ │ │ │ └── sample │ │ │ │ └── HogeTest.java │ │ └── main │ │ │ └── java │ │ │ └── sample │ │ │ ├── Fuga.java │ │ │ └── Hoge.java │ └── build.gradle ├── settings.gradle ├── options2_disable_assertions │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── sample │ │ │ └── Foo.java │ └── build.gradle ├── with_kotlin │ ├── src │ │ └── main │ │ │ └── kotlin │ │ │ └── sample │ │ │ └── Hoge.kt │ └── build.gradle ├── install_from_repository │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── sample │ │ │ └── Hoge.java │ └── build.gradle ├── custom_configurations │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── sample │ │ │ └── Hoge.java │ └── build.gradle ├── custom_sourceSets │ ├── src │ │ ├── api │ │ │ └── java │ │ │ │ └── sample │ │ │ │ └── Fuga.java │ │ └── main │ │ │ └── java │ │ │ └── sample │ │ │ └── Hoge.java │ └── build.gradle ├── use_no_maven_central │ ├── src │ │ ├── main │ │ │ └── java │ │ │ │ └── sample │ │ │ │ └── Hoge.java │ │ └── test │ │ │ └── java-gen │ │ │ └── foo │ │ │ └── HogeAssert.java │ └── build.gradle └── build.gradle ├── .gitignore ├── src └── main │ ├── resources │ └── META-INF │ │ └── gradle-plugins │ │ └── com.github.opengl-BOBO.assertjGen2.properties │ └── java │ └── com │ └── github │ └── opengl8080 │ └── gradle │ └── plugin │ └── assertj │ ├── DebugLogger.java │ ├── helper │ ├── Tasks.java │ ├── ConfigurationHelper.java │ ├── TaskHelper.java │ ├── SourceSetHelper.java │ └── ProjectHelper.java │ ├── MavenProjectWrapper.java │ ├── AssertjGen.java │ └── AssertjGenConfiguration.java ├── README.md └── LICENSE /testproject/options1/src/main/java/sample/bar/Bar.java: -------------------------------------------------------------------------------- 1 | package sample.bar; 2 | 3 | public class Bar { 4 | } 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .classpath 2 | .project 3 | .gradle/ 4 | build/ 5 | .settings/ 6 | .idea/ 7 | classes 8 | out/ 9 | *.iml -------------------------------------------------------------------------------- /testproject/options1/src/main/java/sample/bar/IgnoreClass.java: -------------------------------------------------------------------------------- 1 | package sample.bar; 2 | 3 | public class IgnoreClass { 4 | } -------------------------------------------------------------------------------- /testproject/options1/src/main/java/sample/foo/IgnoreClass.java: -------------------------------------------------------------------------------- 1 | package sample.foo; 2 | 3 | public class IgnoreClass { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/gradle-plugins/com.github.opengl-BOBO.assertjGen2.properties: -------------------------------------------------------------------------------- 1 | implementation-class=com.github.opengl8080.gradle.plugin.assertj.AssertjGen 2 | -------------------------------------------------------------------------------- /testproject/basic/src/test/java/sample/HogeTest.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | import org.junit.Test; 4 | 5 | public class HogeTest { 6 | 7 | @Test 8 | public void name() throws Exception { 9 | HogeAssert.assertThat(new Hoge()).hasAge(0); 10 | } 11 | } -------------------------------------------------------------------------------- /testproject/settings.gradle: -------------------------------------------------------------------------------- 1 | include ( 2 | 'basic', 3 | 'custom_configurations', 4 | 'use_no_maven_central', 5 | 'custom_sourceSets', 6 | 'with_kotlin', 7 | 'options1', 8 | 'options2_disable_assertions', 9 | 'install_from_repository' 10 | ) 11 | -------------------------------------------------------------------------------- /testproject/options1/src/main/java/sample/foo/FooChild.java: -------------------------------------------------------------------------------- 1 | package sample.foo; 2 | 3 | public class FooChild extends Foo { 4 | private int age; 5 | 6 | public int getAge() { 7 | return age; 8 | } 9 | 10 | public void setAge(int age) { 11 | this.age = age; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /testproject/options2_disable_assertions/src/main/java/sample/Foo.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | public class Foo { 4 | private String name; 5 | 6 | public String getName() { 7 | return name; 8 | } 9 | 10 | public void setName(String name) { 11 | this.name = name; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /testproject/options1/src/main/java/sample/foo/Foo.java: -------------------------------------------------------------------------------- 1 | package sample.foo; 2 | 3 | public class Foo { 4 | private String name; 5 | private int privateField; 6 | 7 | public String getName() { 8 | return name; 9 | } 10 | 11 | public void setName(String name) { 12 | this.name = name; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /testproject/with_kotlin/src/main/kotlin/sample/Hoge.kt: -------------------------------------------------------------------------------- 1 | package sample 2 | 3 | class Hoge { 4 | var name: String? = null 5 | var age: Int = 0 6 | 7 | override fun toString(): String { 8 | return "Hoge{" + 9 | "name='" + name + '\'' + 10 | ", age=" + age + 11 | '}' 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /testproject/basic/src/main/java/sample/Fuga.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | public class Fuga { 4 | private String name; 5 | private int age; 6 | 7 | public String getName() { 8 | return name; 9 | } 10 | 11 | public void setName(String name) { 12 | this.name = name; 13 | } 14 | 15 | public int getAge() { 16 | return age; 17 | } 18 | 19 | public void setAge(int age) { 20 | this.age = age; 21 | } 22 | } -------------------------------------------------------------------------------- /testproject/basic/src/main/java/sample/Hoge.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | public class Hoge { 4 | private String name; 5 | private int age; 6 | 7 | public String getName() { 8 | return name; 9 | } 10 | 11 | public void setName(String name) { 12 | this.name = name; 13 | } 14 | 15 | public int getAge() { 16 | return age; 17 | } 18 | 19 | public void setAge(int age) { 20 | this.age = age; 21 | } 22 | } -------------------------------------------------------------------------------- /testproject/install_from_repository/src/main/java/sample/Hoge.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | public class Hoge { 4 | private String name; 5 | private int age; 6 | 7 | public String getName() { 8 | return name; 9 | } 10 | 11 | public void setName(String name) { 12 | this.name = name; 13 | } 14 | 15 | public int getAge() { 16 | return age; 17 | } 18 | 19 | public void setAge(int age) { 20 | this.age = age; 21 | } 22 | } -------------------------------------------------------------------------------- /testproject/install_from_repository/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | maven { 4 | url "https://plugins.gradle.org/m2/" 5 | } 6 | } 7 | 8 | dependencies { 9 | classpath remoteDependencies 10 | } 11 | } 12 | 13 | apply plugin: "com.github.opengl-BOBO.assertjGen2" 14 | 15 | assertjGen { 16 | packages = ["sample"] 17 | debug = true 18 | } 19 | 20 | task('testAssertjGen') { 21 | dependsOn 'assertjGen' 22 | 23 | doLast { 24 | assertBasic(assertjGen.resolveTargetDir(project)) 25 | } 26 | } 27 | 28 | tasks.assertjGen.dependsOn(clean) 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/opengl8080/gradle/plugin/assertj/DebugLogger.java: -------------------------------------------------------------------------------- 1 | package com.github.opengl8080.gradle.plugin.assertj; 2 | 3 | import java.util.function.Supplier; 4 | 5 | public class DebugLogger { 6 | private static boolean debug = false; 7 | 8 | static void init(AssertjGenConfiguration config) { 9 | DebugLogger.debug = config.debug; 10 | } 11 | 12 | public static void debug(Supplier messageSupplier) { 13 | if (!debug) { 14 | return; 15 | } 16 | 17 | String message = messageSupplier.get(); 18 | System.out.println("[DEBUG] " + message); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /testproject/basic/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | dependencies { 3 | classpath files((String)localJarPath) 4 | } 5 | } 6 | 7 | apply plugin: "com.github.opengl-BOBO.assertjGen2" 8 | 9 | assertjGen { 10 | packages = ["sample"] 11 | debug = true 12 | } 13 | 14 | sourceSets { 15 | test { 16 | java { 17 | srcDirs assertjGen.resolveTargetDir(project) 18 | } 19 | } 20 | } 21 | 22 | task('testAssertjGen') { 23 | dependsOn 'assertjGen' 24 | 25 | doLast { 26 | assertBasic(assertjGen.resolveTargetDir(project)) 27 | } 28 | } 29 | 30 | tasks.assertjGen.dependsOn(clean) 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/opengl8080/gradle/plugin/assertj/helper/Tasks.java: -------------------------------------------------------------------------------- 1 | package com.github.opengl8080.gradle.plugin.assertj.helper; 2 | 3 | import org.gradle.api.Task; 4 | 5 | import java.util.Objects; 6 | import java.util.Set; 7 | 8 | public class Tasks { 9 | private final Set tasks; 10 | 11 | Tasks(Set tasks) { 12 | this.tasks = Objects.requireNonNull(tasks); 13 | } 14 | 15 | void areDependedFrom(Task task) { 16 | this.tasks.forEach(task::dependsOn); 17 | } 18 | 19 | public void dependsOn(Task dependent) { 20 | this.tasks.forEach(task -> task.dependsOn(dependent)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /testproject/custom_configurations/src/main/java/sample/Hoge.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | import com.github.javaparser.JavaParser; 4 | import org.antlr.v4.runtime.TokenSource; 5 | 6 | public class Hoge { 7 | private int testValue; 8 | 9 | public int getTestValue() { 10 | return testValue; 11 | } 12 | 13 | public void setTestValue(int testValue) { 14 | this.testValue = testValue; 15 | } 16 | 17 | public static void foo(TokenSource tokenSource) { 18 | System.out.println(tokenSource); 19 | } 20 | 21 | public static void fuga(JavaParser parser) throws Exception { 22 | JavaParser.parse(""); 23 | } 24 | } -------------------------------------------------------------------------------- /testproject/custom_sourceSets/src/api/java/sample/Fuga.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | public class Fuga { 4 | private String name; 5 | private int age; 6 | 7 | public String getName() { 8 | return name; 9 | } 10 | 11 | public int getAge() { 12 | return age; 13 | } 14 | 15 | public void setName(String name) { 16 | this.name = name; 17 | } 18 | 19 | public void setAge(int age) { 20 | this.age = age; 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return "Fuga{" + 26 | "name='" + name + '\'' + 27 | ", age=" + age + 28 | '}'; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /testproject/custom_sourceSets/src/main/java/sample/Hoge.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | public class Hoge { 4 | private String name; 5 | private int age; 6 | 7 | public String getName() { 8 | return name; 9 | } 10 | 11 | public int getAge() { 12 | return age; 13 | } 14 | 15 | public void setName(String name) { 16 | this.name = name; 17 | } 18 | 19 | public void setAge(int age) { 20 | this.age = age; 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return "Hoge{" + 26 | "name='" + name + '\'' + 27 | ", age=" + age + 28 | '}'; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /testproject/options1/my_assert_class_template.txt: -------------------------------------------------------------------------------- 1 | package ${package}; 2 | ${imports} 3 | 4 | /** 5 | * This file generated with custom template file. 6 | */ 7 | @javax.annotation.Generated(value="assertj-assertions-generator") 8 | public class ${custom_assertion_class} extends Abstract${custom_assertion_class}<${custom_assertion_class}, ${class_to_assert}> { 9 | 10 | public ${custom_assertion_class}(${class_to_assert} actual) { 11 | super(actual, ${custom_assertion_class}.class); 12 | } 13 | 14 | @org.assertj.core.util.CheckReturnValue 15 | public static ${custom_assertion_class} assertThat(${class_to_assert} actual) { 16 | return new ${custom_assertion_class}(actual); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /testproject/use_no_maven_central/src/main/java/sample/Hoge.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | public class Hoge { 4 | private String name; 5 | private int age; 6 | 7 | public String getName() { 8 | return name; 9 | } 10 | 11 | public int getAge() { 12 | return age; 13 | } 14 | 15 | public void setName(String name) { 16 | this.name = name; 17 | } 18 | 19 | public void setAge(int age) { 20 | this.age = age; 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return "Hoge{" + 26 | "name='" + name + '\'' + 27 | ", age=" + age + 28 | '}'; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /testproject/use_no_maven_central/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | dependencies { 3 | classpath files((String)localJarPath) 4 | } 5 | } 6 | 7 | apply plugin: "com.github.opengl-BOBO.assertjGen2" 8 | 9 | assertjGen { 10 | packages = ["sample"] 11 | debug = true 12 | } 13 | 14 | repositories { 15 | maven { url="http://download.osgeo.org/webdav/geotools" } 16 | mavenCentral() 17 | } 18 | 19 | dependencies { 20 | compile 'javax.media:jai_core:1.1.3' 21 | } 22 | 23 | task('testAssertjGen') { 24 | dependsOn 'assertjGen' 25 | 26 | doLast { 27 | assertBasic(assertjGen.resolveTargetDir(project)) 28 | } 29 | } 30 | 31 | tasks.assertjGen.dependsOn(clean) 32 | -------------------------------------------------------------------------------- /testproject/with_kotlin/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.1.51' 3 | 4 | repositories { 5 | mavenCentral() 6 | } 7 | 8 | dependencies { 9 | classpath files((String)localJarPath) 10 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 11 | } 12 | } 13 | 14 | apply plugin: "kotlin" 15 | apply plugin: "com.github.opengl-BOBO.assertjGen2" 16 | 17 | assertjGen { 18 | packages = ['sample'] 19 | debug = true 20 | } 21 | 22 | task('testAssertjGen') { 23 | dependsOn 'assertjGen' 24 | 25 | doLast { 26 | assertBasic(assertjGen.resolveTargetDir(project)) 27 | } 28 | } 29 | 30 | tasks.assertjGen.dependsOn(clean) 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/opengl8080/gradle/plugin/assertj/helper/ConfigurationHelper.java: -------------------------------------------------------------------------------- 1 | package com.github.opengl8080.gradle.plugin.assertj.helper; 2 | 3 | import org.gradle.api.artifacts.Configuration; 4 | 5 | import java.io.File; 6 | import java.util.List; 7 | import java.util.Objects; 8 | import java.util.stream.Collectors; 9 | 10 | class ConfigurationHelper { 11 | private final Configuration configuration; 12 | 13 | ConfigurationHelper(Configuration configuration) { 14 | this.configuration = Objects.requireNonNull(configuration); 15 | } 16 | 17 | List getFiles() { 18 | return this.configuration.getFiles() 19 | .stream() 20 | .map(File::getPath) 21 | .collect(Collectors.toList()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/opengl8080/gradle/plugin/assertj/helper/TaskHelper.java: -------------------------------------------------------------------------------- 1 | package com.github.opengl8080.gradle.plugin.assertj.helper; 2 | 3 | import org.gradle.api.Task; 4 | 5 | import java.util.Objects; 6 | 7 | public class TaskHelper { 8 | private final Task task; 9 | 10 | TaskHelper(Task task) { 11 | this.task = Objects.requireNonNull(task); 12 | } 13 | 14 | public void doLast(DoLast block) { 15 | this.task.doLast(task -> block.execute()); 16 | } 17 | 18 | public void dependsOn(Tasks tasks) { 19 | tasks.areDependedFrom(this.task); 20 | } 21 | 22 | public Task task() { 23 | return this.task; 24 | } 25 | 26 | @FunctionalInterface 27 | public interface DoLast { 28 | void execute(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /testproject/custom_configurations/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | dependencies { 3 | classpath files((String)localJarPath) 4 | } 5 | } 6 | 7 | apply plugin: "com.github.opengl-BOBO.assertjGen2" 8 | 9 | assertjGen { 10 | packages = ["sample"] 11 | configurations = ['implementation'] 12 | debug = true 13 | } 14 | 15 | configurations { 16 | implementation { 17 | canBeResolved = true 18 | } 19 | } 20 | 21 | dependencies { 22 | compile 'com.github.javaparser:javaparser-core:3.3.0' 23 | implementation "org.antlr:antlr4-runtime:4.7" 24 | } 25 | 26 | task('testAssertjGen') { 27 | dependsOn 'assertjGen' 28 | 29 | doLast { 30 | assertBasic(assertjGen.resolveTargetDir(project)) 31 | } 32 | } 33 | 34 | tasks.assertjGen.dependsOn(clean) 35 | -------------------------------------------------------------------------------- /testproject/custom_sourceSets/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | dependencies { 3 | classpath files((String)localJarPath) 4 | } 5 | } 6 | 7 | apply plugin: 'java' 8 | apply plugin: "com.github.opengl-BOBO.assertjGen2" 9 | 10 | sourceSets { 11 | api.java.srcDir { "src/api/java" } 12 | } 13 | 14 | assertjGen { 15 | packages = ['sample'] 16 | sourceSets = ["api"] // required 17 | debug = true 18 | } 19 | 20 | task('testAssertjGen') { 21 | dependsOn 'assertjGen' 22 | 23 | doLast { 24 | String targetDir = assertjGen.resolveTargetDir(project) 25 | assertBasic(targetDir) 26 | assertExistsFile(file("${targetDir}/sample/FugaAssert.java")) 27 | } 28 | } 29 | 30 | tasks.assertjGen.dependsOn(clean) 31 | tasks.assertjGen.dependsOn('compileApiJava') // required 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # assertjGen-gradle-plugin 2 | Gradle plugin that generate AssertJ assertion class. 3 | 4 | ## Example (v2.x.x) 5 | **build.gradle** 6 | 7 | ```groovy 8 | buildscript { 9 | repositories { 10 | maven { 11 | url "https://plugins.gradle.org/m2/" 12 | } 13 | } 14 | dependencies { 15 | classpath "gradle.plugin.com.github.opengl-8080:assertjGen-gradle-plugin:2.0.0" 16 | } 17 | } 18 | 19 | apply plugin: "com.github.opengl-BOBO.assertjGen2" 20 | 21 | repositories { 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | testCompile 'junit:junit:4.12' 27 | testCompile 'org.assertj:assertj-core:3.5.2' 28 | } 29 | 30 | assertjGen { 31 | packages = ['foo.bar'] 32 | generateAssertionsForAllFields = true 33 | includes = [/foo\.bar\.Fizz.*/] 34 | generateJUnitSoftAssertions = false 35 | writeReportInFile = "${buildDir}/report.txt" 36 | } 37 | ``` 38 | 39 | Please read [wiki](https://github.com/opengl-8080/assertjGen-gradle-plugin/wiki/Version2-in-English) for more details. 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 opengl-8080 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /testproject/options2_disable_assertions/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | dependencies { 3 | classpath files((String)localJarPath) 4 | } 5 | } 6 | 7 | apply plugin: "com.github.opengl-BOBO.assertjGen2" 8 | 9 | assertjGen { 10 | packages = ["sample"] 11 | generateAssertions = false 12 | generateBddAssertions = false 13 | generateSoftAssertions = false 14 | generateJUnitSoftAssertions = false 15 | 16 | debug = true 17 | } 18 | 19 | sourceSets { 20 | test { 21 | java { 22 | srcDirs assertjGen.resolveTargetDir(project) 23 | } 24 | } 25 | } 26 | 27 | task('testAssertjGen') { 28 | dependsOn 'assertjGen' 29 | 30 | doLast { 31 | String targetDir = assertjGen.resolveTargetDir(project) 32 | 33 | assertExistsFile(file("${targetDir}/sample/FooAssert.java")) 34 | 35 | assertNotExistsFile(file("${targetDir}/sample/Assertions.java")) 36 | assertNotExistsFile(file("${targetDir}/sample/BddAssertions.java")) 37 | assertNotExistsFile(file("${targetDir}/sample/JUnitSoftAssertions.java")) 38 | assertNotExistsFile(file("${targetDir}/sample/SoftAssertions.java")) 39 | } 40 | } 41 | 42 | tasks.assertjGen.dependsOn(clean) 43 | -------------------------------------------------------------------------------- /src/main/java/com/github/opengl8080/gradle/plugin/assertj/helper/SourceSetHelper.java: -------------------------------------------------------------------------------- 1 | package com.github.opengl8080.gradle.plugin.assertj.helper; 2 | 3 | import com.github.opengl8080.gradle.plugin.assertj.DebugLogger; 4 | import org.gradle.api.file.FileCollection; 5 | import org.gradle.api.tasks.SourceSet; 6 | 7 | import java.io.File; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | import java.util.Objects; 11 | 12 | class SourceSetHelper { 13 | private final SourceSet sourceSet; 14 | 15 | SourceSetHelper(SourceSet sourceSet) { 16 | this.sourceSet = Objects.requireNonNull(sourceSet); 17 | } 18 | 19 | void addSourceDirIfAbsent(String srcDirPath) { 20 | if (this.has(srcDirPath)) { 21 | DebugLogger.debug(() -> "skip to add source dir : " + srcDirPath); 22 | return; 23 | } 24 | 25 | this.addSourceDir(srcDirPath); 26 | } 27 | 28 | private boolean has(String path) { 29 | File file = new File(path); 30 | return this.sourceSet.getJava().getSrcDirs().contains(file); 31 | } 32 | 33 | private void addSourceDir(String srcDirPath) { 34 | this.sourceSet.getJava().srcDir(srcDirPath); 35 | DebugLogger.debug(() -> "add source dir : " + srcDirPath); 36 | } 37 | 38 | List getOutputDirs() { 39 | List list = new ArrayList<>(); 40 | 41 | FileCollection classesDirs = this.sourceSet.getOutput().getClassesDirs(); 42 | classesDirs.forEach(classesDir -> list.add(classesDir.getPath())); 43 | 44 | return list; 45 | } 46 | 47 | String getCompileJavaTaskName() { 48 | return this.sourceSet.getCompileJavaTaskName(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /testproject/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | assertjGenPluginVersion = '2.0.0' 4 | localJarPath = "../../build/libs/assertjGen-gradle-plugin-${assertjGenPluginVersion}.jar" 5 | remoteDependencies = "gradle.plugin.com.github.opengl-8080:assertjGen-gradle-plugin:${assertjGenPluginVersion}" 6 | } 7 | 8 | repositories { 9 | maven { 10 | url "https://plugins.gradle.org/m2/" 11 | } 12 | } 13 | dependencies { 14 | classpath 'org.assertj:assertj-assertions-generator-maven-plugin:2.1.0' 15 | // classpath 'org.assertj:assertj-assertions-generator:2.1.0' 16 | } 17 | } 18 | 19 | allprojects { 20 | apply plugin: 'java' 21 | 22 | repositories { 23 | if (project.name != "use_no_maven_central") { 24 | mavenCentral() 25 | } 26 | } 27 | 28 | dependencies { 29 | testCompile 'junit:junit:4.12' 30 | testCompile 'org.assertj:assertj-core:3.5.2' 31 | } 32 | } 33 | 34 | def assertExistsFile(File file) { 35 | assert file.exists() 36 | assert 0L < file.length() 37 | } 38 | 39 | def assertNotExistsFile(File file) { 40 | assert !file.exists() 41 | } 42 | 43 | def assertBasic(String targetDir) { 44 | assertExistsFile(file("${targetDir}/sample/Assertions.java")) 45 | assertExistsFile(file("${targetDir}/sample/BddAssertions.java")) 46 | assertExistsFile(file("${targetDir}/sample/JUnitSoftAssertions.java")) 47 | assertExistsFile(file("${targetDir}/sample/SoftAssertions.java")) 48 | assertExistsFile(file("${targetDir}/sample/HogeAssert.java")) 49 | } 50 | 51 | def assertFileContents(File file, String includedContents) { 52 | assert file.text.contains(includedContents) 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/github/opengl8080/gradle/plugin/assertj/MavenProjectWrapper.java: -------------------------------------------------------------------------------- 1 | package com.github.opengl8080.gradle.plugin.assertj; 2 | 3 | import com.github.opengl8080.gradle.plugin.assertj.helper.ProjectHelper; 4 | import org.apache.maven.artifact.DependencyResolutionRequiredException; 5 | import org.apache.maven.project.MavenProject; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | import java.util.Objects; 10 | 11 | public class MavenProjectWrapper extends MavenProject { 12 | private final ProjectHelper project; 13 | 14 | MavenProjectWrapper(ProjectHelper project) { 15 | this.project = Objects.requireNonNull(project); 16 | } 17 | 18 | @Override 19 | public List getCompileClasspathElements() throws DependencyResolutionRequiredException { 20 | AssertjGenConfiguration config = this.project.getAssertjGenConfiguration(); 21 | 22 | List classpathElements = new ArrayList<>(); 23 | 24 | classpathElements.addAll(this.project.getSourceSetOutputDirs("main")); 25 | classpathElements.addAll(this.project.getSourceSetOutputDirs(config.sourceSets)); 26 | classpathElements.addAll(this.project.getConfigurationFiles("compile")); 27 | classpathElements.addAll(this.project.getConfigurationFiles(config.configurations)); 28 | 29 | return classpathElements; 30 | } 31 | 32 | @Override 33 | public List getTestClasspathElements() throws DependencyResolutionRequiredException { 34 | List classpathElements = new ArrayList<>(); 35 | 36 | classpathElements.addAll(this.project.getSourceSetOutputDirs("test")); 37 | classpathElements.addAll(this.project.getConfigurationFiles("testCompile")); 38 | 39 | return classpathElements; 40 | } 41 | 42 | @Override 43 | public void addCompileSourceRoot(String path) { 44 | this.project.addSourceRootIfAbsent("main", path); 45 | } 46 | 47 | @Override 48 | public void addTestCompileSourceRoot(String path) { 49 | this.project.addSourceRootIfAbsent("test", path); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /testproject/use_no_maven_central/src/test/java-gen/foo/HogeAssert.java: -------------------------------------------------------------------------------- 1 | package foo; 2 | 3 | import org.assertj.core.api.AbstractAssert; 4 | import org.assertj.core.util.Objects; 5 | 6 | /** 7 | * {@link Hoge} specific assertions - Generated by CustomAssertionGenerator. 8 | */ 9 | public class HogeAssert extends AbstractAssert { 10 | 11 | /** 12 | * Creates a new {@link HogeAssert} to make assertions on actual Hoge. 13 | * @param actual the Hoge we want to make assertions on. 14 | */ 15 | public HogeAssert(Hoge actual) { 16 | super(actual, HogeAssert.class); 17 | } 18 | 19 | /** 20 | * An entry point for HogeAssert to follow AssertJ standard assertThat() statements.
21 | * With a static import, one can write directly: assertThat(myHoge) and get specific assertion with code completion. 22 | * @param actual the Hoge we want to make assertions on. 23 | * @return a new {@link HogeAssert} 24 | */ 25 | public static HogeAssert assertThat(Hoge actual) { 26 | return new HogeAssert(actual); 27 | } 28 | 29 | /** 30 | * Verifies that the actual Hoge's age is equal to the given one. 31 | * @param age the given age to compare the actual Hoge's age to. 32 | * @return this assertion object. 33 | * @throws AssertionError - if the actual Hoge's age is not equal to the given one. 34 | */ 35 | public HogeAssert hasAge(int age) { 36 | // check that actual Hoge we want to make assertions on is not null. 37 | isNotNull(); 38 | 39 | // overrides the default error message with a more explicit one 40 | String assertjErrorMessage = "\nExpecting age of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>"; 41 | 42 | // check 43 | int actualAge = actual.getAge(); 44 | if (actualAge != age) { 45 | failWithMessage(assertjErrorMessage, actual, age, actualAge); 46 | } 47 | 48 | // return the current assertion for method chaining 49 | return this; 50 | } 51 | 52 | /** 53 | * Verifies that the actual Hoge's name is equal to the given one. 54 | * @param name the given name to compare the actual Hoge's name to. 55 | * @return this assertion object. 56 | * @throws AssertionError - if the actual Hoge's name is not equal to the given one. 57 | */ 58 | public HogeAssert hasName(String name) { 59 | // check that actual Hoge we want to make assertions on is not null. 60 | isNotNull(); 61 | 62 | // overrides the default error message with a more explicit one 63 | String assertjErrorMessage = "\nExpecting name of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>"; 64 | 65 | // null safe check 66 | String actualName = actual.getName(); 67 | if (!Objects.areEqual(actualName, name)) { 68 | failWithMessage(assertjErrorMessage, actual, name, actualName); 69 | } 70 | 71 | // return the current assertion for method chaining 72 | return this; 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /testproject/options1/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | dependencies { 3 | classpath files((String)localJarPath) 4 | } 5 | } 6 | 7 | apply plugin: "com.github.opengl-BOBO.assertjGen2" 8 | 9 | assertjGen { 10 | packages = ["sample"] 11 | generateAssertionsInPackage = "sample.assertions" 12 | cleanTargetDir = true 13 | includes = [/sample\.foo\..*/] 14 | excludes = [".*IgnoreClass"] 15 | generateAssertionsForAllFields = true 16 | entryPointClassPackage = "sample.assertions.entries" 17 | writeReportInFile = file("${buildDir.getPath()}/report.txt") 18 | templates { 19 | templatesDirectory = "${project.projectDir.getPath()}" 20 | hierarchicalAssertionConcreteClass = "my_assert_class_template.txt" 21 | } 22 | 23 | debug = true 24 | } 25 | 26 | sourceSets { 27 | test { 28 | java { 29 | srcDirs assertjGen.resolveTargetDir(project) 30 | } 31 | } 32 | } 33 | 34 | ext { 35 | deleteTargetFileName = "delete-target" 36 | } 37 | 38 | task('testAssertjGen') { 39 | dependsOn 'assertjGen' 40 | 41 | doLast { 42 | String targetDir = assertjGen.resolveTargetDir(project) 43 | 44 | // test generateAssertionsInPackage (sample.assertions) 45 | assertExistsFile(file("${targetDir}/sample/assertions/FooAssert.java")) 46 | 47 | // test cleanTargetDir 48 | assertNotExistsFile(file("${targetDir}/${deleteTargetFileName}")) 49 | 50 | // test includes 51 | assertNotExistsFile(file("${targetDir}/sample/assertions/BarAssert.java")) 52 | 53 | // test excludes 54 | assertNotExistsFile(file("${targetDir}/sample/assertions/IgnoreClassAssert.java")) 55 | 56 | // test generateAssertionsForAllFields 57 | assertFileContents(file("${targetDir}/sample/assertions/AbstractFooAssert.java"), "hasPrivateField(int privateField)") 58 | 59 | // test entryPointClassPackage 60 | assertExistsFile(file("${targetDir}/sample/assertions/entries/Assertions.java")) 61 | assertExistsFile(file("${targetDir}/sample/assertions/entries/BddAssertions.java")) 62 | assertExistsFile(file("${targetDir}/sample/assertions/entries/JUnitSoftAssertions.java")) 63 | assertExistsFile(file("${targetDir}/sample/assertions/entries/SoftAssertions.java")) 64 | 65 | // test writeReportInFile 66 | assertExistsFile(file("${buildDir.getPath()}/report.txt")) 67 | 68 | // test templates 69 | assertFileContents(file("${targetDir}/sample/assertions/FooAssert.java"), "This file generated with custom template file.") 70 | } 71 | } 72 | 73 | task('createDeleteTargetFile') { 74 | doLast { 75 | String targetDir = assertjGen.resolveTargetDir(project) 76 | File deleteTarget = file("${targetDir}/${deleteTargetFileName}") 77 | deleteTarget.parentFile.mkdirs() 78 | deleteTarget << "this file should be deleted." 79 | } 80 | } 81 | 82 | task('deleteReportFile') { 83 | doLast { 84 | File report = file("${buildDir.getPath()}/report.txt") 85 | if (report.exists()) { 86 | report.delete() 87 | } 88 | } 89 | } 90 | 91 | tasks.assertjGen.dependsOn('deleteReportFile') 92 | tasks.assertjGen.dependsOn('createDeleteTargetFile') 93 | -------------------------------------------------------------------------------- /src/main/java/com/github/opengl8080/gradle/plugin/assertj/AssertjGen.java: -------------------------------------------------------------------------------- 1 | package com.github.opengl8080.gradle.plugin.assertj; 2 | 3 | import com.github.opengl8080.gradle.plugin.assertj.helper.ProjectHelper; 4 | import com.github.opengl8080.gradle.plugin.assertj.helper.TaskHelper; 5 | import org.apache.maven.plugin.MojoExecutionException; 6 | import org.apache.maven.plugin.MojoFailureException; 7 | import org.assertj.maven.AssertJAssertionsGeneratorMojo; 8 | import org.gradle.api.Plugin; 9 | import org.gradle.api.Project; 10 | import org.slf4j.Logger; 11 | import org.slf4j.LoggerFactory; 12 | 13 | public class AssertjGen implements Plugin { 14 | private static final Logger logger = LoggerFactory.getLogger(AssertjGen.class); 15 | 16 | @Override 17 | public void apply(Project originalProject) { 18 | ProjectHelper project = new ProjectHelper(originalProject); 19 | 20 | project.createExtension("assertjGen", AssertjGenConfiguration.class); 21 | project.applyPlugin("java"); 22 | 23 | this.defineAssertjGenTask(project); 24 | } 25 | 26 | private void defineAssertjGenTask(ProjectHelper project) { 27 | TaskHelper assertjGen = project.createTask("assertjGen"); 28 | 29 | assertjGen.doLast(() -> { 30 | AssertJAssertionsGeneratorMojo mojo = new AssertJAssertionsGeneratorMojo(); 31 | mojo.project = new MavenProjectWrapper(project); 32 | 33 | AssertjGenConfiguration config = project.getAssertjGenConfiguration(); 34 | DebugLogger.init(config); 35 | 36 | DebugLogger.debug(config::toString); 37 | 38 | mojo.targetDir = config.resolveTargetDir(project.project()); 39 | mojo.generateAssertionsInPackage = config.generateAssertionsInPackage; 40 | mojo.cleanTargetDir = config.cleanTargetDir; 41 | mojo.generatedSourcesScope = config.generatedSourcesScope; 42 | mojo.packages = config.packages; 43 | mojo.classes = config.classes; 44 | mojo.includes = config.includes; 45 | mojo.excludes = config.excludes; 46 | mojo.hierarchical = config.hierarchical; 47 | mojo.generateAssertionsForAllFields = config.generateAssertionsForAllFields; 48 | mojo.entryPointClassPackage = config.entryPointClassPackage; 49 | mojo.skip = config.skip; 50 | mojo.generateAssertions = config.generateAssertions; 51 | mojo.generateBddAssertions = config.generateBddAssertions; 52 | mojo.generateJUnitSoftAssertions = config.generateJUnitSoftAssertions; 53 | mojo.generateSoftAssertions = config.generateSoftAssertions; 54 | mojo.quiet = config.quiet; 55 | mojo.writeReportInFile = config.writeReportInFile; 56 | mojo.templates = config.templates; 57 | 58 | try { 59 | mojo.execute(); 60 | } catch (MojoFailureException | MojoExecutionException e) { 61 | throw new RuntimeException("failed to execute AssertJAssertionsGeneratorMojo", e); 62 | } 63 | }); 64 | 65 | 66 | // compileJava <- assertjGen 67 | assertjGen.dependsOn(project.getTasks("compileJava")); 68 | // assertjGen <- compileTestJava 69 | project.getTasks("compileTestJava").dependsOn(assertjGen.task()); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/github/opengl8080/gradle/plugin/assertj/helper/ProjectHelper.java: -------------------------------------------------------------------------------- 1 | package com.github.opengl8080.gradle.plugin.assertj.helper; 2 | 3 | import com.github.opengl8080.gradle.plugin.assertj.AssertjGenConfiguration; 4 | import org.gradle.api.Project; 5 | import org.gradle.api.Task; 6 | import org.gradle.api.artifacts.Configuration; 7 | import org.gradle.api.tasks.SourceSet; 8 | import org.gradle.api.tasks.SourceSetContainer; 9 | 10 | import java.util.HashMap; 11 | import java.util.List; 12 | import java.util.Map; 13 | import java.util.Objects; 14 | import java.util.Set; 15 | import java.util.stream.Collectors; 16 | 17 | public class ProjectHelper { 18 | private final Project project; 19 | 20 | public ProjectHelper(Project project) { 21 | this.project = Objects.requireNonNull(project); 22 | } 23 | 24 | public void createExtension(String name, Class clazz) { 25 | this.project.getExtensions().create(name, clazz); 26 | } 27 | 28 | public void applyPlugin(String name) { 29 | Map map = new HashMap<>(); 30 | map.put("plugin", name); 31 | this.project.apply(map); 32 | } 33 | 34 | public TaskHelper createTask(String name) { 35 | Task task = this.project.task(name); 36 | return new TaskHelper(task); 37 | } 38 | 39 | public Tasks getTasks(String name) { 40 | Set tasksByName = this.project.getTasksByName(name, false); 41 | return new Tasks(tasksByName); 42 | } 43 | // 44 | // public Tasks getCompileJavaTasks(List sourceSetNames) { 45 | // Set tasks = sourceSetNames.stream() 46 | // .map(this::getSourceSet) 47 | // .map(SourceSetHelper::getCompileJavaTaskName) 48 | // .map(taskName -> this.project.getTasksByName(taskName, false)) 49 | // .flatMap(Set::stream) 50 | // .collect(Collectors.toSet()); 51 | // 52 | // return new Tasks(tasks); 53 | // } 54 | 55 | public List getSourceSetOutputDirs(String sourceSetName) { 56 | SourceSetHelper sourceSet = this.getSourceSet(sourceSetName); 57 | return sourceSet.getOutputDirs(); 58 | } 59 | 60 | public List getSourceSetOutputDirs(List sourceSetNames) { 61 | return sourceSetNames 62 | .stream() 63 | .map(this::getSourceSetOutputDirs) 64 | .flatMap(List::stream) 65 | .collect(Collectors.toList()); 66 | } 67 | 68 | public List getConfigurationFiles(String configurationName) { 69 | ConfigurationHelper configuration = this.getConfiguration(configurationName); 70 | return configuration.getFiles(); 71 | } 72 | 73 | public List getConfigurationFiles(List configurationNames) { 74 | return configurationNames 75 | .stream() 76 | .map(this::getConfigurationFiles) 77 | .flatMap(List::stream) 78 | .collect(Collectors.toList()); 79 | } 80 | 81 | public void addSourceRootIfAbsent(String sourceSetName, String path) { 82 | if (path == null || path.trim().isEmpty()) { 83 | return; 84 | } 85 | 86 | SourceSetHelper sourceSet = this.getSourceSet(sourceSetName); 87 | sourceSet.addSourceDirIfAbsent(path); 88 | } 89 | 90 | public AssertjGenConfiguration getAssertjGenConfiguration() { 91 | return (AssertjGenConfiguration) this.project.getExtensions().getByName("assertjGen"); 92 | } 93 | 94 | private SourceSetHelper getSourceSet(String name) { 95 | SourceSetContainer sourceSets = (SourceSetContainer) this.project.getProperties().get("sourceSets"); 96 | SourceSet sourceSet = sourceSets.getByName(name); 97 | return new SourceSetHelper(sourceSet); 98 | } 99 | 100 | private ConfigurationHelper getConfiguration(String name) { 101 | Configuration configuration = this.project.getConfigurations().getByName(name); 102 | return new ConfigurationHelper(configuration); 103 | } 104 | 105 | public Project project() { 106 | return this.project; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/com/github/opengl8080/gradle/plugin/assertj/AssertjGenConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.github.opengl8080.gradle.plugin.assertj; 2 | 3 | import groovy.lang.Closure; 4 | import org.assertj.maven.Templates; 5 | import org.gradle.api.Project; 6 | 7 | import java.lang.reflect.Field; 8 | import java.util.ArrayList; 9 | import java.util.Arrays; 10 | import java.util.List; 11 | 12 | public class AssertjGenConfiguration { 13 | public String targetDir; 14 | public String generateAssertionsInPackage; 15 | public boolean cleanTargetDir = false; 16 | public String generatedSourcesScope = "test"; 17 | public String[] packages = {}; 18 | public String[] classes = {}; 19 | public String[] includes = {}; 20 | public String[] excludes = {}; 21 | public boolean hierarchical = true; 22 | public boolean generateAssertionsForAllFields = false; 23 | public String entryPointClassPackage; 24 | public boolean skip = false; 25 | public boolean generateAssertions = true; 26 | public boolean generateBddAssertions = true; 27 | public boolean generateJUnitSoftAssertions = true; 28 | public boolean generateSoftAssertions = true; 29 | public boolean quiet = false; 30 | public String writeReportInFile; 31 | Templates templates; 32 | 33 | public void setTemplates(Closure closure) { 34 | Templates templates = new Templates(); 35 | closure.setDelegate(templates); 36 | closure.call(); 37 | this.templates = templates; 38 | } 39 | 40 | public List configurations = new ArrayList<>(); 41 | public List sourceSets = new ArrayList<>(); 42 | 43 | public boolean debug = false; 44 | 45 | public String resolveTargetDir(Project project) { 46 | String targetDir = this.targetDir != null 47 | ? this.targetDir 48 | : project.getBuildDir().getPath() + "/generated-test-sources/assertj-assertions"; 49 | 50 | DebugLogger.debug(() -> "target = " + targetDir); 51 | String resolvedTargetDir = project.file(targetDir).getPath(); 52 | DebugLogger.debug(() -> "resolvedTargetDir = " + resolvedTargetDir); 53 | 54 | return resolvedTargetDir; 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return "AssertjGenConfiguration{" + 60 | "targetDir='" + targetDir + '\'' + 61 | ", generateAssertionsInPackage='" + generateAssertionsInPackage + '\'' + 62 | ", cleanTargetDir=" + cleanTargetDir + 63 | ", generatedSourcesScope='" + generatedSourcesScope + '\'' + 64 | ", packages=" + Arrays.toString(packages) + 65 | ", classes=" + Arrays.toString(classes) + 66 | ", includes=" + Arrays.toString(includes) + 67 | ", excludes=" + Arrays.toString(excludes) + 68 | ", hierarchical=" + hierarchical + 69 | ", generateAssertionsForAllFields=" + generateAssertionsForAllFields + 70 | ", entryPointClassPackage='" + entryPointClassPackage + '\'' + 71 | ", skip=" + skip + 72 | ", generateAssertions=" + generateAssertions + 73 | ", generateBddAssertions=" + generateBddAssertions + 74 | ", generateJUnitSoftAssertions=" + generateJUnitSoftAssertions + 75 | ", generateSoftAssertions=" + generateSoftAssertions + 76 | ", quiet=" + quiet + 77 | ", writeReportInFile='" + writeReportInFile + '\'' + 78 | ", templates=" + this.toString(this.templates) + 79 | ", configurations=" + configurations + 80 | ", sourceSets=" + sourceSets + 81 | ", debug=" + debug + 82 | '}'; 83 | } 84 | 85 | private String toString(Templates templates) { 86 | if (templates == null) { 87 | return "null"; 88 | } 89 | 90 | StringBuilder sb = new StringBuilder("Template["); 91 | try { 92 | Field[] fields = Templates.class.getFields(); 93 | for (int i = 0; i < fields.length; i++) { 94 | Field field = fields[i]; 95 | sb.append(field.getName()).append("=").append(field.get(templates)); 96 | 97 | if (i < fields.length - 1) { 98 | sb.append(", "); 99 | } 100 | } 101 | } catch (IllegalAccessException e) { 102 | throw new RuntimeException(e); 103 | } 104 | sb.append("]"); 105 | 106 | return sb.toString(); 107 | } 108 | } 109 | --------------------------------------------------------------------------------