├── .gitignore ├── .travis.yml ├── LICENSE.txt ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main └── java │ └── com │ └── zyxist │ └── chainsaw │ ├── ChainsawPlugin.java │ ├── ConfigurableTask.java │ ├── JavaModule.java │ ├── JavaModuleHacks.java │ ├── TaskConfigurationOrchestrator.java │ ├── TaskConfigurator.java │ ├── algorithms │ ├── FileRewriter.java │ ├── ModuleNameChecker.java │ ├── ModuleNameDetector.java │ ├── ModulePatcher.java │ └── RewritingOutput.java │ ├── compilation │ ├── CompileJavaConfigurator.java │ └── GenerateJavadocConfigurator.java │ ├── exec │ ├── CreateStartScriptsConfigurator.java │ └── RunTaskConfigurator.java │ ├── jigsaw │ ├── JigsawCLI.java │ ├── JigsawFlags.java │ └── cli │ │ ├── AddModuleFlag.java │ │ ├── ArgProducer.java │ │ ├── ExportItem.java │ │ ├── ExportListFlag.java │ │ ├── ModuleFlag.java │ │ ├── ModuleVersionFlag.java │ │ ├── OpenItem.java │ │ ├── OpenListFlag.java │ │ ├── PatchItem.java │ │ ├── PatchListFlag.java │ │ ├── ReadItem.java │ │ └── ReadListFlag.java │ ├── tasks │ └── VerifyModuleNameTask.java │ └── tests │ ├── CompileTestJavaConfigurator.java │ ├── JUnit4.java │ ├── JUnit5.java │ ├── NoTestEngine.java │ ├── TestEngine.java │ ├── TestNG.java │ └── TestTaskConfigurator.java └── test └── groovy └── com └── zyxist └── chainsaw ├── JavaModuleHacksSpec.groovy ├── TaskConfigurationOrchestratorSpec.groovy ├── algorithms └── ModuleNameDetectorSpec.groovy ├── builder ├── Dependencies.java ├── JavaCodeFactory.java ├── JigsawProjectBuilder.java └── factory │ ├── DaggerComponentFactory.groovy │ ├── DaggerModuleFactory.groovy │ ├── DocumentedJavaClassFactory.groovy │ ├── JUnit4SampleTestFactory.groovy │ ├── JUnit5SampleResourceTestFactory.groovy │ ├── JUnit5SampleTestFactory.groovy │ ├── RegularJavaClassFactory.groovy │ ├── RunnableDaggerClassFactory.groovy │ ├── RunnableGuavaClassFactory.groovy │ ├── RunnableHackingClassFactory.groovy │ ├── RunnableJavaClassFactory.groovy │ ├── RunnableResourceClassFactory.groovy │ ├── SampleTextResourceFactory.groovy │ ├── TestNGExportedTestPackageTestFactory.groovy │ └── TestNGSampleTestFactory.groovy ├── integration ├── AnnotationProcessingTestSpec.groovy ├── ChainsawPluginSpec.groovy ├── CustomFlagsSpec.groovy ├── IncrementalCompilationSpec.groovy ├── JUnit4TestSpec.groovy ├── JUnit5TestSpec.groovy ├── JavadocTestSpec.groovy ├── ModulePatchTestSpec.groovy ├── MultiprojectSetupSpec.groovy ├── StartScriptsSpec.groovy └── TestNGTestSpec.groovy ├── jigsaw ├── JigsawCLISpec.groovy └── cli │ ├── AddModuleFlagSpec.groovy │ ├── ExportItemSpec.groovy │ ├── OpenItemSpec.groovy │ ├── PatchItemSpec.groovy │ └── ReadItemSpec.groovy └── tasks └── VerifyModuleNameTaskSpec.groovy /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /build/ 3 | /out 4 | 5 | # Ignore Gradle GUI config 6 | gradle-app.setting 7 | 8 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 9 | !gradle-wrapper.jar 10 | 11 | # Cache of project 12 | .gradletasknamecache 13 | 14 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 15 | # gradle/wrapper/gradle-wrapper.properties 16 | 17 | # IntelliJ IDEA 18 | .idea 19 | classes 20 | *.iml 21 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk9 4 | 5 | install: echo "skip 'gradle assemble' step" 6 | script: 7 | - ./gradlew clean build --stacktrace 8 | 9 | after_success: 10 | - bash <(curl -s https://codecov.io/bash) -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/zyxist/chainsaw.svg?branch=master)](https://travis-ci.org/zyxist/chainsaw) 2 | [![Codecov](https://img.shields.io/codecov/c/github/zyxist/chainsaw.svg)]() 3 | [![GitHub release](https://img.shields.io/github/release/zyxist/chainsaw.svg)]() 4 | 5 | # Gradle Chainsaw Plugin 6 | 7 | A plugin for [Gradle](http://gradle.org) that adds a support for Java Platform Module System, also known as Jigsaw, 8 | introduced by Java 9. 9 | 10 | Gradle doesn't currently support building Java 9 modules in a first-class way, except for a extremely limited 11 | `experimental-jigsaw` plugin. This plugin aims to provide the necessary support by reconfiguring common Gradle 12 | tasks to use modules, so that you could use modules in your project right now. 13 | 14 | The full documentation and some tips, how to solve common problems with Jigsaw, can be found on wiki. 15 | 16 | ## Basic usage 17 | 18 | If you already have a working Java project, applying the plugin is just one step: 19 | 20 | ```groovy 21 | plugins { 22 | id 'com.zyxist.chainsaw' version 'x.y.z' 23 | } 24 | ``` 25 | 26 | 27 | Chainsaw will automatically detect your Jigsaw module descriptor and reconfigure Gradle tasks 28 | to use modules instead of classpath. 29 | 30 | ## Features 31 | 32 | Every release brings improvements and support for more and more corner cases and use cases, based on 33 | real-world projects. The plugin is already quite usable. All of the features: 34 | 35 | * support for modular compilation of main sources and unit tests, 36 | * support for running unit tests (JUnit 4/JUnit 5 with possibility to add additional test engines and test libraries), 37 | * support for javadocs, 38 | * support for `run` task, 39 | * support for custom Jigsaw flags and module patching, 40 | * partial support for `installDist` task. 41 | 42 | The long-term vision includes providing support for building modular runtime images, and multirelease JAR 43 | archives. To learn more about using Chainsaw, visit the project wiki. 44 | 45 | ## How to help? 46 | 47 | Chainsaw is a community project. You can help in various ways: 48 | 49 | * give the plugin a star on Github or make a fork, 50 | * write about Chainsaw on your blog, 51 | * create a Pull Request. 52 | 53 | When creating a Pull Request, remember to write integration tests for the new functionality. Jigsaw tends to be 54 | very tricky and these tests are important to ensure that new features don't break anything that has previously worked. 55 | 56 | ## License 57 | 58 | Apache License 2.0 -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'groovy' 3 | id 'idea' 4 | id 'jacoco' 5 | id 'maven-publish' 6 | id 'java-gradle-plugin' 7 | id 'com.gradle.plugin-publish' version '0.9.10' 8 | } 9 | 10 | group = 'com.zyxist' 11 | version = '0.3.1' 12 | 13 | targetCompatibility = '1.8' 14 | sourceCompatibility = '1.8' 15 | 16 | // Fix a bad interaction with IntelliJ and Gradle > 4.0 17 | idea.module.inheritOutputDirs = true 18 | 19 | repositories { 20 | jcenter() 21 | } 22 | 23 | dependencies { 24 | compile 'com.github.javaparser:javaparser-core:3.5.15' 25 | 26 | testCompile('org.spockframework:spock-core:1.1-groovy-2.4') { 27 | exclude module: 'groovy-all' 28 | } 29 | testCompile('org.mockito:mockito-core:2.15.0') 30 | } 31 | 32 | jacocoTestReport { 33 | reports { 34 | xml.enabled = true 35 | html.enabled = true 36 | } 37 | } 38 | 39 | check.dependsOn jacocoTestReport 40 | 41 | test { 42 | testLogging.showStandardStreams = true 43 | } 44 | 45 | gradlePlugin { 46 | plugins { 47 | chainsawPlugin { 48 | id = "com.zyxist.chainsaw" 49 | implementationClass = "com.zyxist.chainsaw.ChainsawPlugin" 50 | } 51 | } 52 | } 53 | 54 | pluginBundle { 55 | website = 'https://github.com/zyxist/chainsaw' 56 | vcsUrl = 'https://github.com/zyxist/chainsaw' 57 | plugins { 58 | chainsawPlugin { 59 | id = 'com.zyxist.chainsaw' 60 | displayName = 'Chainsaw: Java 9 Jigsaw support' 61 | description = 'Adds a support for building and testing modular JDK9 applications with Gradle. ' 62 | tags = ['jigsaw', 'modules', 'java9', 'java'] 63 | version = project.version 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyxist/chainsaw/028e7265f0a5513563f5a677717b8afa934226ef/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'chainsaw' 2 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/ChainsawPlugin.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw; 17 | 18 | import com.zyxist.chainsaw.algorithms.ModuleNameDetector; 19 | import com.zyxist.chainsaw.compilation.CompileJavaConfigurator; 20 | import com.zyxist.chainsaw.compilation.GenerateJavadocConfigurator; 21 | import com.zyxist.chainsaw.exec.CreateStartScriptsConfigurator; 22 | import com.zyxist.chainsaw.exec.RunTaskConfigurator; 23 | import com.zyxist.chainsaw.tasks.VerifyModuleNameTask; 24 | import com.zyxist.chainsaw.tests.*; 25 | import org.gradle.api.*; 26 | import org.gradle.api.artifacts.Configuration; 27 | import org.gradle.api.logging.Logger; 28 | import org.gradle.api.logging.Logging; 29 | import org.gradle.api.plugins.AppliedPlugin; 30 | import org.gradle.api.plugins.JavaPlugin; 31 | import org.gradle.api.plugins.JavaPluginConvention; 32 | import org.gradle.api.tasks.SourceSet; 33 | import org.gradle.api.tasks.SourceSetContainer; 34 | import org.gradle.internal.impldep.org.bouncycastle.math.raw.Mod; 35 | 36 | import java.util.LinkedHashMap; 37 | import java.util.Map; 38 | 39 | public class ChainsawPlugin implements Plugin { 40 | private static final Logger LOGGER = Logging.getLogger(ChainsawPlugin.class); 41 | 42 | private static final String APPLICATION_PLUGIN = "application"; 43 | 44 | public static final String PATCH_CONFIGURATION_NAME = "patch"; 45 | 46 | private static final String EXTENSION_NAME = "javaModule"; 47 | 48 | private static final String VERIFY_MODULE_NAME_TASK_NAME = "verifyModuleName"; 49 | 50 | private static final TestEngine[] TEST_ENGINES = { 51 | new JUnit4(), 52 | new JUnit5(), 53 | new TestNG(), 54 | new NoTestEngine() 55 | }; 56 | 57 | @Override 58 | public void apply(Project project) { 59 | LOGGER.debug("Applying ChainsawPlugin to " + project.getName()); 60 | JavaPlugin plugin = project.getPlugins().apply(JavaPlugin.class); 61 | Configuration cfg = project.getConfigurations().create(PATCH_CONFIGURATION_NAME); 62 | cfg.setDescription("Dependencies that break Java Module System rules and need to be added as patches to other modules."); 63 | 64 | project.getExtensions().create(EXTENSION_NAME, JavaModule.class); 65 | JavaPluginConvention javaConfig = project.getConvention().getPlugin(JavaPluginConvention.class); 66 | ModuleNameDetector detector = new ModuleNameDetector(javaConfig.getSourceCompatibility()); 67 | 68 | VerifyModuleNameTask vmnTask = project.getTasks().create(VERIFY_MODULE_NAME_TASK_NAME, VerifyModuleNameTask.class); 69 | vmnTask.dependsOn("compileJava"); 70 | 71 | removePatchedDependencies(project, cfg); 72 | configureJavaTasks(project, detector); 73 | } 74 | 75 | private void removePatchedDependencies(Project project, Configuration patchConfig) { 76 | project.getConfigurations().all(cfg -> { 77 | if (!cfg.getName().equals(PATCH_CONFIGURATION_NAME)) { 78 | patchConfig.getDependencies().all(dependency -> { 79 | Map exclusion = new LinkedHashMap<>(); 80 | exclusion.put("group", dependency.getGroup()); 81 | exclusion.put("module", dependency.getName()); 82 | cfg.exclude(exclusion); 83 | }); 84 | } 85 | }); 86 | } 87 | 88 | private TestEngine selectTestEngine(Project project) { 89 | for (TestEngine testEngine : TEST_ENGINES) { 90 | if (testEngine.accepts(project)) { 91 | return testEngine; 92 | } 93 | } 94 | throw new GradleException("Unknown test engine."); 95 | } 96 | 97 | private void inferModuleName(Project project, JavaModule moduleConfig, ModuleNameDetector detector) { 98 | if (moduleConfig.getName() == null) { 99 | final SourceSet mainSourceSet = ((SourceSetContainer) project.getProperties().get("sourceSets")).getByName("main"); 100 | moduleConfig.setName(detector.findModuleName(mainSourceSet.getJava().getSrcDirs().iterator().next())); 101 | LOGGER.info("Inferred module name: " + moduleConfig.getName()); 102 | } 103 | } 104 | 105 | private void configureJavaTasks(final Project project, final ModuleNameDetector detector) { 106 | project.afterEvaluate(evaluatedProject -> { 107 | final JavaModule moduleConfig = (JavaModule) evaluatedProject.getExtensions().getByName(EXTENSION_NAME); 108 | inferModuleName(evaluatedProject, moduleConfig, detector); 109 | TestEngine testEngine = selectTestEngine(evaluatedProject); 110 | final TaskConfigurationOrchestrator orchestrator = new TaskConfigurationOrchestrator(); 111 | orchestrator 112 | .use(ConfigurableTask.JAVA_COMPILE, new CompileJavaConfigurator(moduleConfig)) 113 | .use(ConfigurableTask.JAVA_TEST_COMPILE, new CompileTestJavaConfigurator(moduleConfig, testEngine)) 114 | .use(ConfigurableTask.JAVADOC, new GenerateJavadocConfigurator(moduleConfig)) 115 | .use(ConfigurableTask.TEST, new TestTaskConfigurator(moduleConfig, testEngine)) 116 | .use(ConfigurableTask.RUN, new RunTaskConfigurator(moduleConfig)) 117 | .use(ConfigurableTask.START_SCRIPTS, new CreateStartScriptsConfigurator(moduleConfig)) 118 | .configureTasks(evaluatedProject, "java"); 119 | 120 | evaluatedProject.getPluginManager() 121 | .withPlugin(APPLICATION_PLUGIN, appliedPlugin -> orchestrator.configureTasks(evaluatedProject, "application")); 122 | }); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/ConfigurableTask.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw; 17 | 18 | import org.gradle.api.plugins.ApplicationPlugin; 19 | import org.gradle.api.plugins.JavaPlugin; 20 | import org.gradle.api.tasks.Exec; 21 | import org.gradle.api.tasks.application.CreateStartScripts; 22 | import org.gradle.api.tasks.compile.JavaCompile; 23 | import org.gradle.api.tasks.javadoc.Javadoc; 24 | import org.gradle.api.tasks.testing.Test; 25 | 26 | public enum ConfigurableTask { 27 | JAVA_COMPILE("java", JavaCompile.class, JavaPlugin.COMPILE_JAVA_TASK_NAME), 28 | JAVA_TEST_COMPILE("java", JavaCompile.class, JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME), 29 | JAVADOC("java", Javadoc.class, JavaPlugin.JAVADOC_TASK_NAME), 30 | TEST("java", Test.class, JavaPlugin.TEST_TASK_NAME), 31 | RUN("application", Exec.class, ApplicationPlugin.TASK_RUN_NAME), 32 | START_SCRIPTS("application", CreateStartScripts.class, ApplicationPlugin.TASK_START_SCRIPTS_NAME); 33 | 34 | private final String category; 35 | private final Class taskType; 36 | private final String taskName; 37 | 38 | ConfigurableTask(String category, Class taskType, String taskName) { 39 | this.taskType = taskType; 40 | this.taskName = taskName; 41 | this.category = category; 42 | } 43 | 44 | public boolean hasCategory(String category) { 45 | return category.equals(this.category); 46 | } 47 | 48 | public Class taskType() { 49 | return taskType; 50 | } 51 | 52 | public String taskName() { 53 | return taskName; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/JavaModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw; 17 | 18 | import org.gradle.api.Action; 19 | 20 | import java.util.*; 21 | 22 | /** 23 | * Represents information about a Java 9 module. 24 | */ 25 | public class JavaModule { 26 | private String moduleName; 27 | private boolean allowModuleNamingViolations = false; 28 | private List addTestModules = new ArrayList<>(); 29 | private List exportTestPackages = new ArrayList<>(); 30 | private JavaModuleHacks hacks = new JavaModuleHacks(); 31 | 32 | public String getName() { 33 | return moduleName; 34 | } 35 | 36 | public void setName(String moduleName) { 37 | this.moduleName = moduleName; 38 | } 39 | 40 | public boolean isAllowModuleNamingViolations() { 41 | return allowModuleNamingViolations; 42 | } 43 | 44 | public void setAllowModuleNamingViolations(boolean allowModuleNamingViolations) { 45 | this.allowModuleNamingViolations = allowModuleNamingViolations; 46 | } 47 | 48 | public List getExtraTestModules() { 49 | return addTestModules; 50 | } 51 | 52 | public void setExtraTestModules(List testModules) { 53 | this.addTestModules = testModules; 54 | } 55 | 56 | public List getExportedTestPackages() { 57 | return exportTestPackages; 58 | } 59 | 60 | public void setExportedTestPackages(List testPackages) { 61 | this.exportTestPackages = testPackages; 62 | } 63 | 64 | public void hacks(Action action) { 65 | action.execute(hacks); 66 | } 67 | 68 | public JavaModuleHacks getHacks() { 69 | return hacks; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/JavaModuleHacks.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw; 17 | 18 | import com.zyxist.chainsaw.jigsaw.JigsawCLI; 19 | import com.zyxist.chainsaw.jigsaw.cli.ExportItem; 20 | import com.zyxist.chainsaw.jigsaw.cli.OpenItem; 21 | import com.zyxist.chainsaw.jigsaw.cli.ReadItem; 22 | 23 | import java.util.*; 24 | 25 | /** 26 | * This DSL element allows generating additional custom Jigsaw flags, such as --add-opens or 27 | * --patch-module, which are necessary to hack various older libraries which break Jigsaw 28 | * rules. 29 | */ 30 | public class JavaModuleHacks { 31 | private List customOpenItems = new ArrayList<>(); 32 | private List customReadItems = new ArrayList<>(); 33 | private List customExportItems = new ArrayList<>(); 34 | private Map patchedDependencies = new LinkedHashMap<>(); 35 | 36 | public void opens(String openedModule, String openedPackage, String destinationModule) { 37 | this.customOpenItems.add(new OpenItem(openedModule, openedPackage, destinationModule)); 38 | } 39 | 40 | public void reads(String srcModule, String dstModule) { 41 | this.customReadItems.add(new ReadItem(srcModule).to(dstModule)); 42 | } 43 | 44 | public void reads(String srcModule, Collection modules) { 45 | this.customReadItems.add(new ReadItem(srcModule).toAll(modules)); 46 | } 47 | 48 | public void exports(String module, String exportedPackage, String dstModule) { 49 | this.customExportItems.add(new ExportItem(module, exportedPackage).to(dstModule)); 50 | } 51 | 52 | public void exports(String module, String exportedPackage, Collection dstModules) { 53 | this.customExportItems.add(new ExportItem(module, exportedPackage).toAll(dstModules)); 54 | } 55 | 56 | public void patches(String patchingDependency, String patchedModule) { 57 | this.patchedDependencies.put(patchingDependency, patchedModule); 58 | } 59 | 60 | public List getCustomOpenItems() { 61 | return customOpenItems; 62 | } 63 | 64 | public List getCustomReadItems() { 65 | return customReadItems; 66 | } 67 | 68 | public List getCustomExportItems() { 69 | return customExportItems; 70 | } 71 | 72 | public Map getPatchedDependencies() { 73 | return patchedDependencies; 74 | } 75 | 76 | public void applyHacks(JigsawCLI cli) { 77 | getCustomOpenItems().forEach(openDef -> cli.openList().open(openDef)); 78 | getCustomReadItems().forEach(readDef -> cli.readList().read(readDef)); 79 | getCustomExportItems().forEach(exportDef -> cli.exportList().export(exportDef)); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/TaskConfigurationOrchestrator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw; 17 | 18 | import org.gradle.api.Project; 19 | import org.gradle.api.Task; 20 | 21 | import java.util.Collection; 22 | import java.util.EnumMap; 23 | import java.util.Objects; 24 | 25 | /** 26 | * Orchestrates the re-configuration of various Java tasks into Jigsaw mode. The supported tasks 27 | * are specified by {@link ConfigurableTask} enumerator. 28 | */ 29 | public class TaskConfigurationOrchestrator { 30 | private final EnumMap configurators = new EnumMap<>(ConfigurableTask.class); 31 | 32 | public TaskConfigurationOrchestrator use(ConfigurableTask type, TaskConfigurator configurator) { 33 | this.configurators.put(type, Objects.requireNonNull(configurator)); 34 | return this; 35 | } 36 | 37 | public Collection getConfigurators() { 38 | return this.configurators.values(); 39 | } 40 | 41 | public void configureTasks(final Project project, String category) { 42 | configurators.forEach((type, configurator) -> { 43 | if (type.hasCategory(category)) { 44 | configureTask(project, type, configurator); 45 | } 46 | }); 47 | } 48 | 49 | private void configureTask(Project project, ConfigurableTask taskType, TaskConfigurator configurator) { 50 | final Task task = project.getTasks().findByName(taskType.taskName()); 51 | configurator.updateConfiguration(project, task); 52 | configurator 53 | .doFirst(project, task) 54 | .ifPresent(action -> task.doFirst(action)); 55 | configurator 56 | .doLast(project, task) 57 | .ifPresent(action -> task.doLast(action)); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/TaskConfigurator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw; 17 | 18 | import org.gradle.api.Action; 19 | import org.gradle.api.Project; 20 | import org.gradle.api.Task; 21 | 22 | import java.util.Optional; 23 | 24 | /** 25 | * Interface for reconfiguring existing Java-related tasks into Jigsaw mode. 26 | * 27 | * @param Type of task to reconfigure 28 | */ 29 | public interface TaskConfigurator { 30 | default void updateConfiguration(Project project, T task) { 31 | } 32 | 33 | default Optional> doFirst(final Project project, final T task) { 34 | return Optional.empty(); 35 | } 36 | 37 | default Optional> doLast(final Project project, final T task) { 38 | return Optional.empty(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/algorithms/FileRewriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.algorithms; 17 | 18 | import org.gradle.api.GradleException; 19 | 20 | import java.io.BufferedWriter; 21 | import java.io.File; 22 | import java.io.IOException; 23 | import java.nio.charset.StandardCharsets; 24 | import java.nio.file.Files; 25 | import java.nio.file.StandardOpenOption; 26 | import java.util.ArrayList; 27 | import java.util.List; 28 | import java.util.Objects; 29 | 30 | /** 31 | * Utility for safe rewriting text files, used for modifying startup scripts. 32 | * If preserves the platform line endings, allows replacing, removing and inserting 33 | * new lines. 34 | */ 35 | public class FileRewriter { 36 | private final File transformedFile; 37 | private final LineEndings eol; 38 | 39 | public FileRewriter(File transformedFile, LineEndings eol) { 40 | this.transformedFile = Objects.requireNonNull(transformedFile); 41 | this.eol = Objects.requireNonNull(eol); 42 | } 43 | 44 | public void rewrite(RewritingAlgorithm algorithm) { 45 | try { 46 | List fileContent = new ArrayList<>(Files.readAllLines(transformedFile.toPath(), StandardCharsets.UTF_8)); 47 | transformedFile.delete(); 48 | try (BufferedWriter writer = Files.newBufferedWriter(transformedFile.toPath(), StandardCharsets.UTF_8, StandardOpenOption.CREATE)) { 49 | for (String srcLine : fileContent) { 50 | algorithm.rewrite(truncateEOL(srcLine), outLine -> { 51 | writer.write(outLine); 52 | writer.write(eol.getEnding()); 53 | }); 54 | } 55 | } 56 | } catch (IOException exception) { 57 | throw new GradleException("Cannot modify the file '" + transformedFile.getPath() + "'", exception); 58 | } 59 | } 60 | 61 | private String truncateEOL(String srcLine) { 62 | if (srcLine.endsWith("\r\n")) { 63 | return srcLine.substring(0, srcLine.length() - 2); 64 | } else if (srcLine.endsWith("\n")) { 65 | return srcLine.substring(0, srcLine.length() - 1); 66 | } 67 | return srcLine; 68 | } 69 | 70 | @FunctionalInterface 71 | public interface RewritingAlgorithm { 72 | void rewrite(String line, RewritingOutput output) throws IOException; 73 | } 74 | 75 | public enum LineEndings { 76 | WINDOWS("\r\n"), 77 | UNIX("\n"); 78 | 79 | private final String ending; 80 | 81 | LineEndings(String ending) { 82 | this.ending = ending; 83 | } 84 | 85 | public String getEnding() { 86 | return ending; 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/algorithms/ModuleNameChecker.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.algorithms; 17 | 18 | import org.gradle.api.logging.Logger; 19 | import org.gradle.api.logging.Logging; 20 | 21 | import java.io.File; 22 | 23 | /** 24 | * Tests that the module name is derived from the root package 25 | * (the official recommendation for module naming). 26 | */ 27 | public class ModuleNameChecker { 28 | private static final Logger LOGGER = Logging.getLogger(ModuleNameChecker.class); 29 | private final String mainDirectory; 30 | 31 | public ModuleNameChecker(String mainDirectory) { 32 | this.mainDirectory = mainDirectory; 33 | } 34 | 35 | public boolean verifyModuleName(String moduleName) { 36 | String pkgDirName = mainDirectory + "/" + asPackageDirectory(moduleName); 37 | File pkgDir = new File(pkgDirName); 38 | LOGGER.info("Testing module name '"+moduleName+"' against package '" + pkgDirName + "'"); 39 | return pkgDir.exists() && pkgDir.isDirectory(); 40 | } 41 | 42 | private String asPackageDirectory(String moduleName) { 43 | return moduleName.replace(".", "/"); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/algorithms/ModuleNameDetector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.algorithms; 17 | 18 | import com.github.javaparser.JavaParser; 19 | import com.github.javaparser.ParseResult; 20 | import com.github.javaparser.ParseStart; 21 | import com.github.javaparser.ParserConfiguration; 22 | import com.github.javaparser.ast.CompilationUnit; 23 | import org.gradle.api.GradleException; 24 | import org.gradle.api.JavaVersion; 25 | 26 | import java.io.File; 27 | import java.io.FileInputStream; 28 | import java.io.FileNotFoundException; 29 | 30 | import static com.github.javaparser.Providers.provider; 31 | 32 | public class ModuleNameDetector { 33 | private final JavaParser parser; 34 | private final ParserConfiguration.LanguageLevel languageLevel; 35 | 36 | public ModuleNameDetector(JavaVersion javaVersion) { 37 | this.languageLevel = translateJavaVersion(javaVersion); 38 | this.parser = new JavaParser(new ParserConfiguration() 39 | .setLanguageLevel(languageLevel)); 40 | } 41 | 42 | public ParserConfiguration.LanguageLevel getLanguageLevel() { 43 | return languageLevel; 44 | } 45 | 46 | public String findModuleName(File sourceDir) { 47 | try { 48 | if (!sourceDir.exists()) { 49 | return null; 50 | } 51 | File moduleDescriptor = new File(sourceDir, "module-info.java"); 52 | if (!moduleDescriptor.exists()) { 53 | throw new GradleException("The project is lacking a Java module descriptor in '" + sourceDir.getPath() + "' directory."); 54 | } 55 | ParseResult result = parser.parse(ParseStart.COMPILATION_UNIT, provider(new FileInputStream(moduleDescriptor))); 56 | if (result.isSuccessful()) { 57 | return result.getResult().get().getModule() 58 | .map(module -> module.getName().asString()) 59 | .orElseThrow(() -> new GradleException("Extracting Java module name failed.")); 60 | } 61 | throw new GradleException("Unable to parse the Java module descriptor."); 62 | } catch (FileNotFoundException exception) { 63 | throw new GradleException("Cannot find the module descriptor.", exception); 64 | } 65 | } 66 | private ParserConfiguration.LanguageLevel translateJavaVersion(JavaVersion version) { 67 | switch (version) { 68 | case VERSION_1_9: 69 | return ParserConfiguration.LanguageLevel.JAVA_9; 70 | case VERSION_1_10: 71 | return ParserConfiguration.LanguageLevel.JAVA_10; 72 | default: 73 | return ParserConfiguration.LanguageLevel.JAVA_9; 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/algorithms/ModulePatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.algorithms; 17 | 18 | import com.zyxist.chainsaw.jigsaw.cli.PatchItem; 19 | import org.gradle.api.Project; 20 | import org.gradle.api.artifacts.Configuration; 21 | import org.gradle.api.artifacts.ResolvedArtifact; 22 | import org.gradle.api.logging.LogLevel; 23 | import org.gradle.api.logging.Logger; 24 | import org.gradle.api.logging.Logging; 25 | 26 | import java.util.LinkedHashMap; 27 | import java.util.Map; 28 | import java.util.Set; 29 | 30 | /** 31 | * Generates the '--patch-module' flag configuration from dependencies, and 32 | * patch settings set in 'javaModule' section. 33 | */ 34 | public class ModulePatcher { 35 | private static final Logger LOGGER = Logging.getLogger(ModulePatcher.class); 36 | private final Map patches; 37 | 38 | public ModulePatcher(Map patches) { 39 | this.patches = patches; 40 | } 41 | 42 | public Map patchFrom(Project project, String ... dependencySets) { 43 | Map patchList = new LinkedHashMap<>(); 44 | for (String depSet: dependencySets) { 45 | Configuration cfg = project.getConfigurations().findByName(depSet); 46 | if (null != cfg && cfg.isCanBeResolved()) { 47 | scanDependencySet(patchList, cfg.getResolvedConfiguration().getResolvedArtifacts()); 48 | } 49 | } 50 | return patchList; 51 | } 52 | 53 | private void scanDependencySet(Map patchCommands, Set resolvedArtifacts) { 54 | for (ResolvedArtifact artifact : resolvedArtifacts) { 55 | for (Map.Entry patched : patches.entrySet()) { 56 | LOGGER.info("'"+patched.getKey()+"' vs '" + toGenericName(artifact)+"'"); 57 | if (patched.getKey().equals(toGenericName(artifact))) { 58 | if (LOGGER.isEnabled(LogLevel.INFO)) { 59 | LOGGER.info("Module '" + patched.getValue() + "' will be patched with '" + toGenericName(artifact) + "'"); 60 | } 61 | toList(patchCommands, patched.getValue(), artifact.getFile().getAbsolutePath()); 62 | } 63 | } 64 | } 65 | } 66 | 67 | private void toList(Map patchCommands, String patchedModule, String patchingJar) { 68 | PatchItem item = patchCommands.get(patchedModule); 69 | if (null == item) { 70 | item = new PatchItem(patchedModule); 71 | patchCommands.put(patchedModule, item); 72 | } 73 | item.with(patchingJar); 74 | } 75 | 76 | private String toGenericName(ResolvedArtifact artifact) { 77 | return artifact.getModuleVersion().getId().getGroup()+":"+artifact.getModuleVersion().getId().getName(); 78 | } 79 | 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/algorithms/RewritingOutput.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.algorithms; 17 | 18 | import java.io.IOException; 19 | 20 | @FunctionalInterface 21 | public interface RewritingOutput { 22 | void emitLine(String line) throws IOException; 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/compilation/CompileJavaConfigurator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.compilation; 17 | 18 | import com.zyxist.chainsaw.JavaModule; 19 | import com.zyxist.chainsaw.TaskConfigurator; 20 | import com.zyxist.chainsaw.algorithms.ModulePatcher; 21 | import com.zyxist.chainsaw.jigsaw.JigsawCLI; 22 | import org.gradle.api.Project; 23 | import org.gradle.api.tasks.compile.JavaCompile; 24 | 25 | import static com.zyxist.chainsaw.ChainsawPlugin.PATCH_CONFIGURATION_NAME; 26 | 27 | public class CompileJavaConfigurator implements TaskConfigurator { 28 | private final JavaModule moduleConfig; 29 | 30 | public CompileJavaConfigurator(JavaModule moduleConfig) { 31 | this.moduleConfig = moduleConfig; 32 | } 33 | 34 | @Override 35 | public void updateConfiguration(Project project, JavaCompile compileJava) { 36 | JigsawCLI cli = new JigsawCLI(compileJava.getClasspath().getAsPath()); 37 | ModulePatcher patcher = new ModulePatcher(moduleConfig.getHacks().getPatchedDependencies()); 38 | patcher 39 | .patchFrom(project, PATCH_CONFIGURATION_NAME) 40 | .forEach((k, patchedModule) -> cli.patchList().patch(patchedModule)); 41 | 42 | compileJava.getOptions().getCompilerArgs().addAll(cli.generateArgs()); 43 | compileJava.setClasspath(project.files()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/compilation/GenerateJavadocConfigurator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.compilation; 17 | 18 | import com.zyxist.chainsaw.JavaModule; 19 | import com.zyxist.chainsaw.TaskConfigurator; 20 | import com.zyxist.chainsaw.jigsaw.JigsawCLI; 21 | import com.zyxist.chainsaw.jigsaw.JigsawFlags; 22 | import org.gradle.api.Action; 23 | import org.gradle.api.Project; 24 | import org.gradle.api.Task; 25 | import org.gradle.api.tasks.javadoc.Javadoc; 26 | import org.gradle.external.javadoc.StandardJavadocDocletOptions; 27 | 28 | import java.util.Optional; 29 | 30 | public class GenerateJavadocConfigurator implements TaskConfigurator { 31 | private final JavaModule moduleConfig; 32 | 33 | public GenerateJavadocConfigurator(JavaModule moduleConfig) { 34 | this.moduleConfig = moduleConfig; 35 | } 36 | 37 | @Override 38 | public Optional> doFirst(Project project, Javadoc javadoc) { 39 | return Optional.of(task -> { 40 | JigsawCLI cli = new JigsawCLI(javadoc.getClasspath().getAsPath()); 41 | StandardJavadocDocletOptions options = (StandardJavadocDocletOptions) javadoc.getOptions(); 42 | options.addStringOption(JigsawFlags.JAVADOC_MODULE_PATH, cli.getModulePath()); 43 | }); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/exec/CreateStartScriptsConfigurator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.exec; 17 | 18 | import com.zyxist.chainsaw.JavaModule; 19 | import com.zyxist.chainsaw.TaskConfigurator; 20 | import com.zyxist.chainsaw.algorithms.FileRewriter; 21 | import com.zyxist.chainsaw.algorithms.RewritingOutput; 22 | import com.zyxist.chainsaw.jigsaw.JigsawCLI; 23 | import org.gradle.api.Action; 24 | import org.gradle.api.Project; 25 | import org.gradle.api.Task; 26 | import org.gradle.api.tasks.application.CreateStartScripts; 27 | 28 | import java.io.File; 29 | import java.io.IOException; 30 | import java.util.Optional; 31 | 32 | public class CreateStartScriptsConfigurator implements TaskConfigurator { 33 | private static final String LIBS_PLACEHOLDER = "APP_HOME_LIBS_PLACEHOLDER"; 34 | private final JavaModule moduleConfig; 35 | 36 | public CreateStartScriptsConfigurator(JavaModule moduleConfig) { 37 | this.moduleConfig = moduleConfig; 38 | } 39 | 40 | @Override 41 | public void updateConfiguration(Project project, CreateStartScripts task) { 42 | task.getInputs().property("moduleName", moduleConfig.getName()); 43 | } 44 | 45 | @Override 46 | public Optional> doFirst(Project project, CreateStartScripts startScripts) { 47 | return Optional.of(task -> { 48 | JigsawCLI cli = new JigsawCLI(LIBS_PLACEHOLDER); 49 | cli.module(moduleConfig.getName(), startScripts.getMainClassName()); 50 | moduleConfig.getHacks().applyHacks(cli); 51 | 52 | startScripts.setClasspath(project.files()); 53 | startScripts.setDefaultJvmOpts(cli.generateArgs()); 54 | }); 55 | } 56 | 57 | @Override 58 | public Optional> doLast(Project project, CreateStartScripts startScripts) { 59 | return Optional.of(task -> { 60 | FileRewriter bashRewriter = new FileRewriter(new File(startScripts.getOutputDir(), startScripts.getApplicationName()), FileRewriter.LineEndings.UNIX); 61 | FileRewriter batRewriter = new FileRewriter(new File(startScripts.getOutputDir(), startScripts.getApplicationName() + ".bat"), FileRewriter.LineEndings.WINDOWS); 62 | 63 | bashRewriter.rewrite(this::transformUnixFile); 64 | batRewriter.rewrite(this::transformWindowsFile); 65 | }); 66 | } 67 | 68 | void transformUnixFile(String line, RewritingOutput output) throws IOException { 69 | if (line.contains(LIBS_PLACEHOLDER)) { 70 | output.emitLine(line.replaceFirst(LIBS_PLACEHOLDER, "\\$APP_HOME/lib")); 71 | } else { 72 | output.emitLine(line); 73 | } 74 | } 75 | 76 | void transformWindowsFile(String line, RewritingOutput output) throws IOException { 77 | if (line.contains(LIBS_PLACEHOLDER)) { 78 | output.emitLine(line.replaceFirst(LIBS_PLACEHOLDER, "%APP_HOME%\\\\\\\\lib")); 79 | } else { 80 | output.emitLine(line); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/exec/RunTaskConfigurator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.exec; 17 | 18 | import com.zyxist.chainsaw.JavaModule; 19 | import com.zyxist.chainsaw.TaskConfigurator; 20 | import com.zyxist.chainsaw.algorithms.ModulePatcher; 21 | import com.zyxist.chainsaw.jigsaw.JigsawCLI; 22 | import com.zyxist.chainsaw.jigsaw.cli.PatchItem; 23 | import org.gradle.api.Action; 24 | import org.gradle.api.Project; 25 | import org.gradle.api.Task; 26 | import org.gradle.api.tasks.JavaExec; 27 | import org.gradle.api.tasks.SourceSet; 28 | import org.gradle.api.tasks.SourceSetContainer; 29 | 30 | import java.io.File; 31 | import java.util.ArrayList; 32 | import java.util.List; 33 | import java.util.Optional; 34 | 35 | import static com.zyxist.chainsaw.ChainsawPlugin.PATCH_CONFIGURATION_NAME; 36 | 37 | public class RunTaskConfigurator implements TaskConfigurator { 38 | private final JavaModule moduleConfig; 39 | 40 | public RunTaskConfigurator(JavaModule moduleConfig) { 41 | this.moduleConfig = moduleConfig; 42 | } 43 | 44 | @Override 45 | public void updateConfiguration(Project project, JavaExec task) { 46 | task.getInputs().property("moduleName", moduleConfig.getName()); 47 | } 48 | 49 | @Override 50 | public Optional> doFirst(Project project, final JavaExec run) { 51 | return Optional.of(task -> { 52 | final SourceSet mainSourceSet = ((SourceSetContainer) project.getProperties().get("sourceSets")).getByName("main"); 53 | 54 | File resourceOutDir = mainSourceSet.getOutput().getResourcesDir(); 55 | JigsawCLI cli = new JigsawCLI(stripResources(run.getClasspath().getAsPath(), resourceOutDir)); 56 | cli.module(moduleConfig.getName(), run.getMain()); 57 | ModulePatcher patcher = new ModulePatcher(moduleConfig.getHacks().getPatchedDependencies()); 58 | patcher 59 | .patchFrom(project, PATCH_CONFIGURATION_NAME) 60 | .forEach((k, patchedModule) -> cli.patchList().patch(patchedModule)); 61 | 62 | cli.patchList().patch(new PatchItem(moduleConfig.getName()).with(resourceOutDir.getAbsolutePath())); 63 | moduleConfig.getHacks().applyHacks(cli); 64 | 65 | List jvmArgs = new ArrayList<>(); 66 | jvmArgs.addAll(run.getJvmArgs()); 67 | jvmArgs.addAll(cli.generateArgs()); 68 | run.setJvmArgs(jvmArgs); 69 | run.setClasspath(project.files()); 70 | }); 71 | } 72 | 73 | private String stripResources(String classpath, File resourceOutputDir) { 74 | String outPath = ":" + resourceOutputDir.getAbsolutePath(); 75 | if (classpath.contains(outPath)) { 76 | return classpath.replace(outPath, ""); 77 | } 78 | return classpath; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/jigsaw/JigsawCLI.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.jigsaw; 17 | 18 | import com.zyxist.chainsaw.jigsaw.cli.*; 19 | 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | import java.util.stream.Collectors; 23 | 24 | /** 25 | * A model of Jigsaw command-line interface, used for generating the necessary 26 | * JVM and compiler CLI flags. 27 | */ 28 | public class JigsawCLI { 29 | private final String modulePath; 30 | private ModuleFlag moduleFlag = new ModuleFlag(); 31 | private ModuleVersionFlag moduleVersion = new ModuleVersionFlag(); 32 | private final AddModuleFlag addModuleFlag = new AddModuleFlag(); 33 | private final PatchListFlag patchList = new PatchListFlag(); 34 | private final ExportListFlag exportList = new ExportListFlag(); 35 | private final ReadListFlag readList = new ReadListFlag(); 36 | private final OpenListFlag openList = new OpenListFlag(); 37 | 38 | public JigsawCLI(String modulePath) { 39 | this.modulePath = modulePath; 40 | } 41 | 42 | public JigsawCLI module(String moduleName, String mainClassName) { 43 | this.moduleFlag = new ModuleFlag(moduleName, mainClassName); 44 | return this; 45 | } 46 | 47 | public JigsawCLI version(String version) { 48 | this.moduleVersion = new ModuleVersionFlag(version); 49 | return this; 50 | } 51 | 52 | public AddModuleFlag addModules() { 53 | return this.addModuleFlag; 54 | } 55 | 56 | public PatchListFlag patchList() { 57 | return this.patchList; 58 | } 59 | 60 | public ExportListFlag exportList() { 61 | return this.exportList; 62 | } 63 | 64 | public ReadListFlag readList() { 65 | return this.readList; 66 | } 67 | 68 | public OpenListFlag openList() { 69 | return openList; 70 | } 71 | 72 | public void toArgs(List args) { 73 | generateModulePath(args); 74 | moduleVersion.toArgs(args); 75 | addModuleFlag.toArgs(args); 76 | exportList.toArgs(args); 77 | openList.toArgs(args); 78 | readList.toArgs(args); 79 | patchList.toArgs(args); 80 | moduleFlag.toArgs(args); 81 | } 82 | 83 | public List generateArgs() { 84 | List args = new ArrayList<>(); 85 | toArgs(args); 86 | return args; 87 | } 88 | 89 | @Override 90 | public String toString() { 91 | return generateArgs().stream().collect(Collectors.joining(" ")); 92 | } 93 | 94 | public String getModulePath() { 95 | return modulePath; 96 | } 97 | 98 | private void generateModulePath(List args) { 99 | args.add(JigsawFlags.MODULE_PATH); 100 | args.add(modulePath); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/jigsaw/JigsawFlags.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.jigsaw; 17 | 18 | public final class JigsawFlags { 19 | public static final String MODULE_PATH = "--module-path"; 20 | public static final String MODULE = "--module"; 21 | public static final String MODULE_VERSION = "--module-version"; 22 | public static final String ADD_EXPORTS = "--add-exports"; 23 | public static final String ADD_READS = "--add-reads"; 24 | public static final String ADD_OPENS = "--add-opens"; 25 | public static final String ADD_MODULES = "--add-modules"; 26 | public static final String PATCH_MODULE = "--patch-module"; 27 | 28 | public static final String JAVADOC_MODULE_PATH = "-module-path"; 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/jigsaw/cli/AddModuleFlag.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.jigsaw.cli; 17 | 18 | import java.util.Collection; 19 | import java.util.LinkedList; 20 | import java.util.List; 21 | import java.util.stream.Collectors; 22 | 23 | import static com.zyxist.chainsaw.jigsaw.JigsawFlags.ADD_MODULES; 24 | 25 | public class AddModuleFlag implements ArgProducer { 26 | private final List extraModules = new LinkedList<>(); 27 | 28 | public AddModuleFlag addAllModulePath() { 29 | this.add("ALL-MODULE-PATH"); 30 | return this; 31 | } 32 | 33 | public AddModuleFlag add(String module) { 34 | this.extraModules.add(module); 35 | return this; 36 | } 37 | 38 | public AddModuleFlag addAll(Collection modules) { 39 | this.extraModules.addAll(modules); 40 | return this; 41 | } 42 | 43 | public AddModuleFlag merge(AddModuleFlag otherFlag) { 44 | this.extraModules.addAll(otherFlag.extraModules); 45 | return this; 46 | } 47 | 48 | @Override 49 | public void toArgs(List args) { 50 | if (!extraModules.isEmpty()) { 51 | args.add(ADD_MODULES); 52 | args.add(extraModules.stream().collect(Collectors.joining(","))); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/jigsaw/cli/ArgProducer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.jigsaw.cli; 17 | 18 | import java.util.List; 19 | 20 | public interface ArgProducer { 21 | void toArgs(List args); 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/jigsaw/cli/ExportItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.jigsaw.cli; 17 | 18 | import com.zyxist.chainsaw.jigsaw.JigsawFlags; 19 | 20 | import java.util.ArrayList; 21 | import java.util.Collection; 22 | import java.util.List; 23 | import java.util.StringJoiner; 24 | import java.util.stream.Collectors; 25 | 26 | public class ExportItem implements ArgProducer { 27 | private final String srcModule; 28 | private final String exportedPackage; 29 | 30 | private final List dstModules = new ArrayList<>(); 31 | 32 | public ExportItem(String srcModule, String exportedPackage) { 33 | this.srcModule = srcModule; 34 | this.exportedPackage = exportedPackage; 35 | } 36 | 37 | public ExportItem to(String module) { 38 | this.dstModules.add(module); 39 | return this; 40 | } 41 | 42 | public ExportItem toAll(Collection modules) { 43 | this.dstModules.addAll(modules); 44 | return this; 45 | } 46 | 47 | @Override 48 | public void toArgs(List args) { 49 | if (!dstModules.isEmpty()) { 50 | args.add(JigsawFlags.ADD_EXPORTS); 51 | args.add(srcModule+"/"+exportedPackage+"="+dstModules.stream().collect(Collectors.joining(","))); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/jigsaw/cli/ExportListFlag.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.jigsaw.cli; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | public class ExportListFlag implements ArgProducer { 22 | private List exports = new ArrayList<>(); 23 | 24 | public ExportListFlag export(ExportItem item) { 25 | this.exports.add(item); 26 | return this; 27 | } 28 | 29 | @Override 30 | public void toArgs(List args) { 31 | for (ExportItem item: exports) { 32 | item.toArgs(args); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/jigsaw/cli/ModuleFlag.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.jigsaw.cli; 17 | 18 | import com.zyxist.chainsaw.jigsaw.JigsawFlags; 19 | 20 | import java.util.List; 21 | 22 | public class ModuleFlag implements ArgProducer { 23 | private final String moduleName; 24 | private final String mainClassName; 25 | 26 | public ModuleFlag(String moduleName, String mainClassName) { 27 | this.moduleName = moduleName; 28 | this.mainClassName = mainClassName; 29 | } 30 | 31 | public ModuleFlag() { 32 | this(null, null); 33 | } 34 | 35 | public String getModuleName() { 36 | return moduleName; 37 | } 38 | 39 | public String getMainClassName() { 40 | return mainClassName; 41 | } 42 | 43 | @Override 44 | public void toArgs(List args) { 45 | if (null != moduleName && null != mainClassName) { 46 | args.add(JigsawFlags.MODULE); 47 | args.add(moduleName + "/" + mainClassName); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/jigsaw/cli/ModuleVersionFlag.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.jigsaw.cli; 17 | 18 | import com.zyxist.chainsaw.jigsaw.JigsawFlags; 19 | 20 | import java.util.List; 21 | 22 | public class ModuleVersionFlag implements ArgProducer { 23 | private final String version; 24 | 25 | public ModuleVersionFlag(String version) { 26 | this.version = version; 27 | } 28 | 29 | public ModuleVersionFlag() { 30 | this(null); 31 | } 32 | 33 | @Override 34 | public void toArgs(List args) { 35 | if (null != version) { 36 | args.add(JigsawFlags.MODULE_VERSION); 37 | args.add(version); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/jigsaw/cli/OpenItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.jigsaw.cli; 17 | 18 | import com.zyxist.chainsaw.jigsaw.JigsawFlags; 19 | 20 | import java.util.List; 21 | 22 | public class OpenItem implements ArgProducer { 23 | private final String openedModule; 24 | private final String openedPackage; 25 | private final String destinationModule; 26 | 27 | public OpenItem(String openedModule, String openedPackage, String destinationModule) { 28 | this.openedModule = openedModule; 29 | this.openedPackage = openedPackage; 30 | this.destinationModule = destinationModule; 31 | } 32 | 33 | public String getOpenedPackage() { 34 | return openedPackage; 35 | } 36 | 37 | public String getOpenedModule() { 38 | return openedModule; 39 | } 40 | 41 | public String getDestinationModule() { 42 | return destinationModule; 43 | } 44 | 45 | @Override 46 | public void toArgs(List args) { 47 | args.add(JigsawFlags.ADD_OPENS); 48 | args.add(openedModule+"/"+openedPackage+"="+destinationModule); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/jigsaw/cli/OpenListFlag.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.jigsaw.cli; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | public class OpenListFlag implements ArgProducer { 22 | private List items = new ArrayList<>(); 23 | 24 | public OpenListFlag open(OpenItem item) { 25 | this.items.add(item); 26 | return this; 27 | } 28 | 29 | @Override 30 | public void toArgs(List args) { 31 | for (OpenItem item: items) { 32 | item.toArgs(args); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/jigsaw/cli/PatchItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.jigsaw.cli; 17 | 18 | import com.zyxist.chainsaw.jigsaw.JigsawFlags; 19 | 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | import java.util.stream.Collectors; 23 | 24 | public class PatchItem implements ArgProducer { 25 | private final String patchedModule; 26 | private final List patchingJars = new ArrayList<>(); 27 | 28 | public PatchItem(String patchedModule) { 29 | this.patchedModule = patchedModule; 30 | } 31 | 32 | public PatchItem with(String jarFile) { 33 | this.patchingJars.add(jarFile); 34 | return this; 35 | } 36 | 37 | @Override 38 | public void toArgs(List args) { 39 | if (!patchingJars.isEmpty()) { 40 | args.add(JigsawFlags.PATCH_MODULE); 41 | args.add(patchedModule+"="+patchingJars.stream().collect(Collectors.joining(":"))); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/jigsaw/cli/PatchListFlag.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.jigsaw.cli; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | import java.util.Objects; 21 | 22 | public class PatchListFlag implements ArgProducer { 23 | private List items = new ArrayList<>(); 24 | 25 | public PatchListFlag patch(PatchItem item) { 26 | this.items.add(Objects.requireNonNull(item)); 27 | return this; 28 | } 29 | 30 | 31 | @Override 32 | public void toArgs(List args) { 33 | for (PatchItem item: items) { 34 | item.toArgs(args); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/jigsaw/cli/ReadItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.jigsaw.cli; 17 | 18 | import com.zyxist.chainsaw.jigsaw.JigsawFlags; 19 | 20 | import java.util.ArrayList; 21 | import java.util.Collection; 22 | import java.util.List; 23 | import java.util.stream.Collectors; 24 | 25 | public class ReadItem implements ArgProducer { 26 | private final String srcModule; 27 | 28 | private final List dstModules = new ArrayList<>(); 29 | 30 | public ReadItem(String srcModule) { 31 | this.srcModule = srcModule; 32 | } 33 | 34 | public ReadItem to(String module) { 35 | this.dstModules.add(module); 36 | return this; 37 | } 38 | 39 | public ReadItem toAll(Collection modules) { 40 | this.dstModules.addAll(modules); 41 | return this; 42 | } 43 | 44 | @Override 45 | public void toArgs(List args) { 46 | if (!dstModules.isEmpty()) { 47 | args.add(JigsawFlags.ADD_READS); 48 | args.add(srcModule+"="+dstModules.stream().collect(Collectors.joining(","))); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/jigsaw/cli/ReadListFlag.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.jigsaw.cli; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | public class ReadListFlag implements ArgProducer { 22 | private List items = new ArrayList<>(); 23 | 24 | public ReadListFlag read(ReadItem item) { 25 | this.items.add(item); 26 | return this; 27 | } 28 | 29 | @Override 30 | public void toArgs(List args) { 31 | for (ReadItem item: items) { 32 | item.toArgs(args); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/tasks/VerifyModuleNameTask.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.tasks; 17 | 18 | import com.zyxist.chainsaw.JavaModule; 19 | import com.zyxist.chainsaw.algorithms.ModuleNameChecker; 20 | import org.gradle.api.DefaultTask; 21 | import org.gradle.api.GradleException; 22 | import org.gradle.api.Project; 23 | import org.gradle.api.logging.Logger; 24 | import org.gradle.api.logging.Logging; 25 | import org.gradle.api.plugins.JavaPluginConvention; 26 | import org.gradle.api.tasks.SourceSet; 27 | import org.gradle.api.tasks.TaskAction; 28 | 29 | import java.io.File; 30 | 31 | public class VerifyModuleNameTask extends DefaultTask { 32 | private static final Logger LOGGER = Logging.getLogger(VerifyModuleNameTask.class); 33 | 34 | private static final String ERROR_TEMPLATE = "The module name '%s' does not follow the official " + 35 | "module naming convention for Java (reverse-DNS style, derived from the root package)."; 36 | 37 | @TaskAction 38 | public void verifyModuleName() { 39 | Project project = getProject(); 40 | SourceSet mainSourceSet = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().findByName("main"); 41 | JavaModule javaModule = project.getExtensions().getByType(JavaModule.class); 42 | 43 | for (File srcDir: mainSourceSet.getAllJava().getSourceDirectories()) { 44 | checkSourceSet(srcDir, javaModule); 45 | } 46 | } 47 | 48 | private void checkSourceSet(File srcDir, JavaModule javaModule) { 49 | ModuleNameChecker checker = new ModuleNameChecker(srcDir.getAbsolutePath()); 50 | if (!checker.verifyModuleName(javaModule.getName())) { 51 | if (javaModule.isAllowModuleNamingViolations()) { 52 | LOGGER.lifecycle(String.format(ERROR_TEMPLATE, javaModule.getName())); 53 | } else { 54 | throw new GradleException(String.format(ERROR_TEMPLATE, javaModule.getName())); 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/tests/CompileTestJavaConfigurator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.tests; 17 | 18 | import com.zyxist.chainsaw.JavaModule; 19 | import com.zyxist.chainsaw.TaskConfigurator; 20 | import com.zyxist.chainsaw.algorithms.ModulePatcher; 21 | import com.zyxist.chainsaw.jigsaw.JigsawCLI; 22 | import com.zyxist.chainsaw.jigsaw.cli.PatchItem; 23 | import com.zyxist.chainsaw.jigsaw.cli.ReadItem; 24 | import org.gradle.api.Project; 25 | import org.gradle.api.tasks.SourceSet; 26 | import org.gradle.api.tasks.SourceSetContainer; 27 | import org.gradle.api.tasks.compile.JavaCompile; 28 | 29 | import java.util.Objects; 30 | 31 | import static com.zyxist.chainsaw.ChainsawPlugin.PATCH_CONFIGURATION_NAME; 32 | 33 | public class CompileTestJavaConfigurator implements TaskConfigurator { 34 | private final JavaModule moduleConfig; 35 | private final TestEngine testEngine; 36 | 37 | public CompileTestJavaConfigurator(JavaModule moduleConfig, TestEngine testEngine) { 38 | this.moduleConfig = Objects.requireNonNull(moduleConfig); 39 | this.testEngine = Objects.requireNonNull(testEngine); 40 | } 41 | 42 | @Override 43 | public void updateConfiguration(Project project, JavaCompile compileJava) { 44 | compileJava.getInputs().property("moduleName", moduleConfig.getName()); 45 | JigsawCLI cli = new JigsawCLI(compileJava.getClasspath().getAsPath()); 46 | ModulePatcher patcher = new ModulePatcher(moduleConfig.getHacks().getPatchedDependencies()); 47 | final SourceSet test = ((SourceSetContainer) project.getProperties().get("sourceSets")).getByName("test"); 48 | 49 | cli.addModules() 50 | .addAll(testEngine.getTestEngineModules()) 51 | .addAll(moduleConfig.getExtraTestModules()); 52 | cli.readList() 53 | .read(new ReadItem(moduleConfig.getName()) 54 | .toAll(testEngine.getTestEngineModules()) 55 | .toAll(moduleConfig.getExtraTestModules())); 56 | patcher 57 | .patchFrom(project, PATCH_CONFIGURATION_NAME) 58 | .forEach((k, patchedModule) -> cli.patchList().patch(patchedModule)); 59 | cli.patchList().patch( 60 | new PatchItem(moduleConfig.getName()) 61 | .with(test.getJava().getSourceDirectories().getAsPath()) 62 | ); 63 | 64 | compileJava.getOptions().getCompilerArgs().addAll(cli.generateArgs()); 65 | compileJava.setClasspath(project.files()); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/tests/JUnit4.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.tests; 17 | 18 | import com.zyxist.chainsaw.ChainsawPlugin; 19 | import org.gradle.api.Project; 20 | import org.gradle.api.logging.Logger; 21 | import org.gradle.api.logging.Logging; 22 | 23 | import java.util.Arrays; 24 | import java.util.List; 25 | 26 | public class JUnit4 implements TestEngine { 27 | private static final Logger LOGGER = Logging.getLogger(ChainsawPlugin.class); 28 | 29 | private static final String JUNIT_GROUP = "junit"; 30 | private static final String JUNIT_ARTIFACT = "junit"; 31 | 32 | private static final String JUNIT_MODULE_NAME = "junit"; 33 | 34 | @Override 35 | public boolean accepts(Project project) { 36 | return TestEngine.checkTestDependencies(project, dep -> dep.getGroup().equals(JUNIT_GROUP) && dep.getName().equals(JUNIT_ARTIFACT)); 37 | } 38 | 39 | @Override 40 | public List getTestEngineModules() { 41 | return Arrays.asList(JUNIT_MODULE_NAME); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/tests/JUnit5.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.tests; 17 | 18 | import com.zyxist.chainsaw.ChainsawPlugin; 19 | import org.gradle.api.Project; 20 | import org.gradle.api.artifacts.Configuration; 21 | import org.gradle.api.artifacts.Dependency; 22 | import org.gradle.api.logging.Logger; 23 | import org.gradle.api.logging.Logging; 24 | 25 | import java.util.Arrays; 26 | import java.util.List; 27 | 28 | public class JUnit5 implements TestEngine { 29 | private static final Logger LOGGER = Logging.getLogger(ChainsawPlugin.class); 30 | 31 | private static final String JUNIT_GROUP = "org.junit.jupiter"; 32 | private static final String JUNIT_ARTIFACT = "junit-jupiter-api"; 33 | 34 | private static final String JUNIT_MODULE_NAME = "org.junit.jupiter.api"; 35 | 36 | @Override 37 | public boolean accepts(Project project) { 38 | return TestEngine.checkTestDependencies(project, dep -> dep.getGroup().equals(JUNIT_GROUP) && dep.getName().equals(JUNIT_ARTIFACT)); 39 | } 40 | 41 | @Override 42 | public List getTestEngineModules() { 43 | return Arrays.asList(JUNIT_MODULE_NAME); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/tests/NoTestEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.tests; 17 | 18 | import org.gradle.api.Project; 19 | 20 | import java.util.Collections; 21 | import java.util.List; 22 | 23 | public class NoTestEngine implements TestEngine { 24 | 25 | @Override 26 | public boolean accepts(Project project) { 27 | return true; 28 | } 29 | 30 | @Override 31 | public List getTestEngineModules() { 32 | return Collections.EMPTY_LIST; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/tests/TestEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.tests; 17 | 18 | import org.gradle.api.Project; 19 | import org.gradle.api.artifacts.Configuration; 20 | import org.gradle.api.artifacts.Dependency; 21 | 22 | import java.util.List; 23 | import java.util.function.Predicate; 24 | 25 | public interface TestEngine { 26 | 27 | boolean accepts(Project project); 28 | 29 | List getTestEngineModules(); 30 | 31 | static boolean checkTestDependencies(Project project, Predicate acceptor) { 32 | Configuration configuration = project.getConfigurations().getByName("testCompile"); 33 | for (Dependency dependency : configuration.getDependencies()) { 34 | if (acceptor.test(dependency)) { 35 | return true; 36 | } 37 | } 38 | configuration = project.getConfigurations().getByName("testImplementation"); 39 | for (Dependency dependency : configuration.getDependencies()) { 40 | if (acceptor.test(dependency)) { 41 | return true; 42 | } 43 | } 44 | return false; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/tests/TestNG.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.tests; 17 | 18 | import com.zyxist.chainsaw.ChainsawPlugin; 19 | import org.gradle.api.Project; 20 | import org.gradle.api.logging.Logger; 21 | import org.gradle.api.logging.Logging; 22 | 23 | import java.util.Arrays; 24 | import java.util.List; 25 | 26 | public class TestNG implements TestEngine { 27 | private static final Logger LOGGER = Logging.getLogger(ChainsawPlugin.class); 28 | 29 | private static final String TESTNG_GROUP = "org.testng"; 30 | private static final String TESTNG_ARTIFACT = "testng"; 31 | 32 | private static final String TESTNG_MODULE_NAME = "testng"; 33 | 34 | @Override 35 | public boolean accepts(Project project) { 36 | return TestEngine.checkTestDependencies(project, dep -> dep.getGroup().equals(TESTNG_GROUP) && dep.getName().equals(TESTNG_ARTIFACT)); 37 | } 38 | 39 | @Override 40 | public List getTestEngineModules() { 41 | return Arrays.asList(TESTNG_MODULE_NAME); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/zyxist/chainsaw/tests/TestTaskConfigurator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.tests; 17 | 18 | import com.zyxist.chainsaw.JavaModule; 19 | import com.zyxist.chainsaw.TaskConfigurator; 20 | import com.zyxist.chainsaw.algorithms.ModulePatcher; 21 | import com.zyxist.chainsaw.jigsaw.JigsawCLI; 22 | import com.zyxist.chainsaw.jigsaw.cli.ExportItem; 23 | import com.zyxist.chainsaw.jigsaw.cli.PatchItem; 24 | import com.zyxist.chainsaw.jigsaw.cli.ReadItem; 25 | import org.gradle.api.Action; 26 | import org.gradle.api.Project; 27 | import org.gradle.api.Task; 28 | import org.gradle.api.tasks.SourceSet; 29 | import org.gradle.api.tasks.SourceSetContainer; 30 | import org.gradle.api.tasks.testing.Test; 31 | 32 | import java.util.ArrayList; 33 | import java.util.List; 34 | import java.util.Objects; 35 | import java.util.Optional; 36 | 37 | import static com.zyxist.chainsaw.ChainsawPlugin.PATCH_CONFIGURATION_NAME; 38 | 39 | public class TestTaskConfigurator implements TaskConfigurator { 40 | private final JavaModule moduleConfig; 41 | private final TestEngine testEngine; 42 | 43 | public TestTaskConfigurator(JavaModule moduleConfig, TestEngine testEngine) { 44 | this.moduleConfig = Objects.requireNonNull(moduleConfig); 45 | this.testEngine = Objects.requireNonNull(testEngine); 46 | } 47 | 48 | @Override 49 | public void updateConfiguration(Project project, Test task) { 50 | task.getInputs().property("moduleName", moduleConfig.getName()); 51 | } 52 | 53 | @Override 54 | public Optional> doFirst(Project project, final Test testTask) { 55 | return Optional.of(task -> { 56 | JigsawCLI cli = new JigsawCLI(testTask.getClasspath().getAsPath()); 57 | ModulePatcher patcher = new ModulePatcher(moduleConfig.getHacks().getPatchedDependencies()); 58 | final SourceSet test = ((SourceSetContainer) project.getProperties().get("sourceSets")).getByName("test"); 59 | 60 | cli.addModules() 61 | .addAllModulePath(); 62 | cli.readList() 63 | .read(new ReadItem(moduleConfig.getName()) 64 | .toAll(testEngine.getTestEngineModules()) 65 | .toAll(moduleConfig.getExtraTestModules())); 66 | moduleConfig.getExportedTestPackages().forEach(exportTestPackage -> cli.exportList() 67 | .export(new ExportItem(moduleConfig.getName(), exportTestPackage) 68 | .toAll(testEngine.getTestEngineModules()))); 69 | 70 | moduleConfig.getHacks().applyHacks(cli); 71 | patcher 72 | .patchFrom(project, PATCH_CONFIGURATION_NAME) 73 | .forEach((k, patchedModule) -> cli.patchList().patch(patchedModule)); 74 | cli.patchList().patch( 75 | new PatchItem(moduleConfig.getName()) 76 | .with(test.getJava().getOutputDir().getAbsolutePath()) 77 | ); 78 | 79 | List jvmArgs = new ArrayList<>(); 80 | jvmArgs.addAll(testTask.getJvmArgs()); 81 | jvmArgs.addAll(cli.generateArgs()); 82 | testTask.setJvmArgs(jvmArgs); 83 | testTask.setClasspath(project.files()); 84 | }); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/JavaModuleHacksSpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw 17 | 18 | import com.zyxist.chainsaw.jigsaw.JigsawCLI 19 | import spock.lang.Specification 20 | 21 | class JavaModuleHacksSpec extends Specification { 22 | 23 | def "it should apply all customizations to the Jigsaw CLI"() { 24 | given: 25 | JigsawCLI cli = new JigsawCLI(); 26 | JavaModuleHacks hacks = new JavaModuleHacks(); 27 | hacks.opens("a", "b", "c") 28 | hacks.reads("a", "b") 29 | hacks.exports("mod", "pkg", "other.mod") 30 | 31 | when: 32 | hacks.applyHacks(cli) 33 | def args = cli.generateArgs() 34 | 35 | then: 36 | args.contains("--add-opens") 37 | args.contains("a/b=c") 38 | args.contains("--add-reads") 39 | args.contains("a=b") 40 | args.contains("--add-exports") 41 | args.contains("mod/pkg=other.mod") 42 | } 43 | 44 | def "it is possible to add multiple destinations to export"() { 45 | given: 46 | JigsawCLI cli = new JigsawCLI(); 47 | JavaModuleHacks hacks = new JavaModuleHacks(); 48 | hacks.exports("mod", "pkg", ["abc", "def"]) 49 | 50 | when: 51 | hacks.applyHacks(cli) 52 | def args = cli.generateArgs() 53 | 54 | then: 55 | args.contains("--add-exports") 56 | args.contains("mod/pkg=abc,def") 57 | } 58 | 59 | def "it is possible to add multiple destinations to reads"() { 60 | given: 61 | JigsawCLI cli = new JigsawCLI(); 62 | JavaModuleHacks hacks = new JavaModuleHacks() 63 | hacks.reads("mod", ["abc", "def"]) 64 | 65 | when: 66 | hacks.applyHacks(cli) 67 | def args = cli.generateArgs() 68 | 69 | then: 70 | args.contains("--add-reads") 71 | args.contains("mod=abc,def") 72 | } 73 | 74 | def "it is possible to add patch dependencies"() { 75 | given: 76 | JavaModuleHacks hacks = new JavaModuleHacks() 77 | 78 | when: 79 | hacks.patches("com.example.group:artifactId", "com.example.else:something") 80 | 81 | then: 82 | hacks.patchedDependencies.size() == 1 83 | hacks.patchedDependencies['com.example.group:artifactId'] == 'com.example.else:something' 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/TaskConfigurationOrchestratorSpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw 17 | 18 | import org.gradle.api.Project 19 | import org.gradle.api.Task 20 | import org.gradle.api.tasks.TaskContainer 21 | import org.mockito.ArgumentMatchers 22 | import spock.lang.Specification 23 | 24 | import static org.mockito.ArgumentMatchers.eq 25 | import static org.mockito.Mockito.mock 26 | import static org.mockito.Mockito.never 27 | import static org.mockito.Mockito.verify 28 | import static org.mockito.Mockito.when 29 | 30 | class TaskConfigurationOrchestratorSpec extends Specification { 31 | 32 | def "should register configurators with use() method"() { 33 | given: 34 | def orchestrator = new TaskConfigurationOrchestrator() 35 | def firstTask = mock(TaskConfigurator.class) 36 | def secondTask = mock(TaskConfigurator.class) 37 | 38 | when: 39 | orchestrator.use(ConfigurableTask.JAVA_COMPILE, firstTask) 40 | orchestrator.use(ConfigurableTask.JAVA_TEST_COMPILE, secondTask) 41 | 42 | then: 43 | orchestrator.getConfigurators().size() == 2 44 | orchestrator.getConfigurators().contains(firstTask) 45 | orchestrator.getConfigurators().contains(secondTask) 46 | } 47 | 48 | def "should configure all tasks from a single category"() { 49 | given: 50 | def orchestrator = new TaskConfigurationOrchestrator() 51 | 52 | def firstTask = mock(Task.class) 53 | def secondTask = mock(Task.class) 54 | 55 | def firstConfigurator = mock(TaskConfigurator.class) 56 | def secondConfigurator = mock(TaskConfigurator.class) 57 | def thirdConfigurator = mock(TaskConfigurator.class) 58 | 59 | def taskContainer = mock(TaskContainer.class) 60 | def project = mock(Project.class) 61 | when(project.getTasks()).thenReturn(taskContainer) 62 | when(taskContainer.findByName(ConfigurableTask.JAVA_COMPILE.taskName())).thenReturn(firstTask) 63 | when(taskContainer.findByName(ConfigurableTask.JAVA_TEST_COMPILE.taskName())).thenReturn(secondTask) 64 | 65 | orchestrator.use(ConfigurableTask.JAVA_COMPILE, firstConfigurator) 66 | .use(ConfigurableTask.JAVA_TEST_COMPILE, secondConfigurator) 67 | .use(ConfigurableTask.RUN, thirdConfigurator) 68 | 69 | when: 70 | orchestrator.configureTasks(project, "java") 71 | 72 | then: 73 | verify(firstConfigurator).doFirst(project, firstTask) || true 74 | verify(secondConfigurator).doFirst(project, secondTask) || true 75 | verify(thirdConfigurator, never()).doFirst(eq(project), ArgumentMatchers.any(Task.class)) || true 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/algorithms/ModuleNameDetectorSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.zyxist.chainsaw.algorithms 2 | 3 | import com.github.javaparser.ParserConfiguration 4 | import org.gradle.api.GradleException 5 | import org.gradle.api.JavaVersion 6 | import org.junit.Rule 7 | import org.junit.rules.TemporaryFolder 8 | import spock.lang.Specification 9 | 10 | class ModuleNameDetectorSpec extends Specification { 11 | @Rule 12 | final TemporaryFolder tmpDir = new TemporaryFolder() 13 | 14 | def "it should be able to parse a Java module descriptor and extract the name"() { 15 | given: 16 | tmpDir.newFile("module-info.java").text = """ 17 | module com.example.foo { 18 | } 19 | """ 20 | 21 | ModuleNameDetector detector = new ModuleNameDetector(JavaVersion.VERSION_1_9) 22 | 23 | when: 24 | def result = detector.findModuleName(tmpDir.root) 25 | 26 | then: 27 | result == 'com.example.foo' 28 | } 29 | 30 | def "it should report a missing module descriptor"() { 31 | given: 32 | ModuleNameDetector detector = new ModuleNameDetector(JavaVersion.VERSION_1_9) 33 | 34 | when: 35 | detector.findModuleName(tmpDir.root) 36 | 37 | then: 38 | GradleException ex = thrown() 39 | ex.getMessage() == 'The project is lacking a Java module descriptor in \'' + tmpDir.root.getPath() + '\' directory.' 40 | } 41 | 42 | def "it recognizes Java 9"() { 43 | given: 44 | ModuleNameDetector detector = new ModuleNameDetector(JavaVersion.VERSION_1_9) 45 | 46 | when: 47 | def result = detector.getLanguageLevel() 48 | 49 | then: 50 | result == ParserConfiguration.LanguageLevel.JAVA_9 51 | } 52 | 53 | def "it recognizes Java 10"() { 54 | given: 55 | ModuleNameDetector detector = new ModuleNameDetector(JavaVersion.VERSION_1_10) 56 | 57 | when: 58 | def result = detector.getLanguageLevel() 59 | 60 | then: 61 | result == ParserConfiguration.LanguageLevel.JAVA_10 62 | } 63 | 64 | def "other (future) versions of Java default to Java 9"() { 65 | given: 66 | // using Java 8, because there is nothing newer than 10 in Gradle API 67 | ModuleNameDetector detector = new ModuleNameDetector(JavaVersion.VERSION_1_8) 68 | 69 | when: 70 | def result = detector.getLanguageLevel() 71 | 72 | then: 73 | result == ParserConfiguration.LanguageLevel.JAVA_9 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/Dependencies.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder; 17 | 18 | public class Dependencies { 19 | public static final String JUNIT4_DEPENDENCY = "junit:junit:4.12"; 20 | public static final String JUNIT5_PLUGIN_DEPENDENCY = "org.junit.platform:junit-platform-gradle-plugin:1.0.0"; 21 | public static final String JUNIT5_API_DEPENDENCY = "org.junit.jupiter:junit-jupiter-api:5.0.0"; 22 | public static final String JUNIT5_ENGINE_DEPENDENCY = "org.junit.jupiter:junit-jupiter-engine:5.0.0"; 23 | public static final String TESTNG_DEPENDENCY = "org.testng:testng:6.14.2"; 24 | public static final String MOCKITO_DEPENDENCY = "org.mockito:mockito-core:2.11.0"; 25 | public static final String GUAVA_DEPENDENCY = "com.google.guava:guava:23.2-jre"; 26 | public static final String GUICE_DEPENDENCY = "com.google.inject:guice:4.2.0"; 27 | public static final String DAGGER_DEPENDENCY = "com.google.dagger:dagger:2.11"; 28 | public static final String DAGGER_COMPILER_DEPENDENCY = "com.google.dagger:dagger-compiler:2.11"; 29 | 30 | public static final String JSR250_DEPENDENCY = "javax.annotation:jsr250-api:1.0"; 31 | public static final String JSR305_DEPENDENCY = "com.google.code.findbugs:jsr305:1.3.9"; 32 | 33 | public static final String JSR250_PATCH = "javax.annotation:jsr250-api"; 34 | public static final String JSR305_PATCH = "com.google.code.findbugs:jsr305"; 35 | 36 | public static final String DAGGER_MODULE = "dagger"; 37 | public static final String GUAVA_MODULE = "com.google.common"; 38 | public static final String JSR250_MODULE = "jsr250.api"; 39 | public static final String MOCKITO_MODULE = "org.mockito"; 40 | 41 | public static final String JUNIT5_PLUGIN_NAME = "org.junit.platform.gradle.plugin"; 42 | 43 | public static final String APT_PLUGIN_VERSION = "0.15"; 44 | } 45 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/JavaCodeFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder; 17 | 18 | public interface JavaCodeFactory { 19 | String getFilename(JigsawProjectBuilder builder); 20 | String generateCode(JigsawProjectBuilder builder); 21 | } 22 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/factory/DaggerComponentFactory.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder.factory 17 | 18 | import com.zyxist.chainsaw.builder.JavaCodeFactory 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | 21 | class DaggerComponentFactory implements JavaCodeFactory { 22 | static DaggerComponentFactory daggerComponent() { 23 | return new DaggerComponentFactory() 24 | } 25 | 26 | @Override 27 | String getFilename(JigsawProjectBuilder builder) { 28 | return builder.getPackageName().replace(".", "/") + "/ApplicationComponent.java" 29 | } 30 | 31 | @Override 32 | String generateCode(JigsawProjectBuilder builder) { 33 | return """ 34 | package ${builder.getPackageName()}; 35 | 36 | import dagger.Component; 37 | import javax.inject.Singleton; 38 | 39 | @Singleton 40 | @Component( 41 | modules = { ApplicationModule.class } 42 | ) 43 | public interface ApplicationComponent { 44 | SomeClass someClass(); 45 | } 46 | """ 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/factory/DaggerModuleFactory.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder.factory 17 | 18 | import com.zyxist.chainsaw.builder.JavaCodeFactory 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | 21 | class DaggerModuleFactory implements JavaCodeFactory { 22 | static DaggerModuleFactory daggerModule() { 23 | return new DaggerModuleFactory() 24 | } 25 | 26 | @Override 27 | String getFilename(JigsawProjectBuilder builder) { 28 | return builder.getPackageName().replace(".", "/") + "/ApplicationModule.java" 29 | } 30 | 31 | @Override 32 | String generateCode(JigsawProjectBuilder builder) { 33 | return """ 34 | package ${builder.getPackageName()}; 35 | 36 | import dagger.Module; 37 | import dagger.Provides; 38 | 39 | @Module 40 | public class ApplicationModule { 41 | @Provides 42 | public SomeClass providesSomeClass() { 43 | return new SomeClass(); 44 | } 45 | } 46 | """ 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/factory/DocumentedJavaClassFactory.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder.factory; 17 | 18 | import com.zyxist.chainsaw.builder.JavaCodeFactory; 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder; 20 | 21 | class DocumentedJavaClassFactory implements JavaCodeFactory { 22 | static DocumentedJavaClassFactory documentedJavaClass() { 23 | return new DocumentedJavaClassFactory(); 24 | } 25 | 26 | @Override 27 | String getFilename(JigsawProjectBuilder builder) { 28 | return builder.getPackageName().replace(".", "/") + "/DocumentedClass.java" 29 | } 30 | 31 | @Override 32 | String generateCode(JigsawProjectBuilder builder) { 33 | return """ 34 | package ${builder.getPackageName()}; 35 | 36 | /** 37 | * This class is documented. 38 | */ 39 | public class DocumentedClass { 40 | /** 41 | * This is a documented method 42 | * 43 | * @param aString Some argument 44 | */ 45 | public void documentedMethod(String aString) { 46 | System.out.println(aString); 47 | } 48 | } 49 | """ 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/factory/JUnit4SampleTestFactory.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder.factory 17 | 18 | import com.zyxist.chainsaw.builder.JavaCodeFactory 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | 21 | class JUnit4SampleTestFactory implements JavaCodeFactory { 22 | boolean mocks; 23 | 24 | static def junit4Test() { 25 | def factory = new JUnit4SampleTestFactory() 26 | factory.mocks = false 27 | return factory 28 | } 29 | 30 | static def junit4TestWithMocks() { 31 | def factory = new JUnit4SampleTestFactory() 32 | factory.mocks = true 33 | return factory 34 | } 35 | 36 | @Override 37 | String getFilename(JigsawProjectBuilder builder) { 38 | return builder.getPackageName().replace(".", "/") + "/AClassTest.java" 39 | } 40 | 41 | @Override 42 | String generateCode(JigsawProjectBuilder builder) { 43 | def mockitoImport = "" 44 | def mockitoUsage = "" 45 | if (mocks) { 46 | mockitoImport = "import static org.mockito.Mockito.mock;" 47 | mockitoUsage = "Object obj = mock(Object.class);" 48 | } 49 | return """ 50 | package ${builder.getPackageName()}; 51 | 52 | ${mockitoImport} 53 | import org.junit.Test; 54 | import static org.junit.Assert.assertTrue; 55 | 56 | public class AClassTest { 57 | @Test 58 | public void isAnInstanceOfAClass() { 59 | ${mockitoUsage} 60 | assertTrue(new AImplementation() instanceof AClass); 61 | } 62 | 63 | static class AImplementation extends AClass { 64 | @Override 65 | public void aMethod(String aString) { 66 | // Do nothing 67 | } 68 | } 69 | } 70 | """ 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/factory/JUnit5SampleResourceTestFactory.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder.factory 17 | 18 | import com.zyxist.chainsaw.builder.JavaCodeFactory 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | 21 | class JUnit5SampleResourceTestFactory implements JavaCodeFactory { 22 | private boolean useModuleLoader 23 | 24 | static def junit5TestAccessingResources() { 25 | def factory = new JUnit5SampleResourceTestFactory() 26 | factory.useModuleLoader = false 27 | return factory 28 | } 29 | 30 | static def junit5TestAccessingModularResources() { 31 | def factory = new JUnit5SampleResourceTestFactory() 32 | factory.useModuleLoader = true 33 | return factory 34 | } 35 | 36 | @Override 37 | String getFilename(JigsawProjectBuilder builder) { 38 | return builder.getPackageName().replace(".", "/") + "/SampleResourceTest.java" 39 | } 40 | 41 | @Override 42 | String generateCode(JigsawProjectBuilder builder) { 43 | def loadingCode = useModuleLoader ? getModularLoadingCode() : getClasspathLoadingCode() 44 | return """ 45 | package ${builder.getPackageName()}; 46 | 47 | import java.util.Properties; 48 | import java.io.IOException; 49 | import org.junit.jupiter.api.Test; 50 | import static org.junit.jupiter.api.Assertions.assertEquals; 51 | 52 | public class SampleResourceTest { 53 | @Test 54 | public void readsTestResources() throws IOException { 55 | Properties props = new Properties(); 56 | props.load($loadingCode); 57 | 58 | assertEquals("Hello world", props.get("property")); 59 | } 60 | } 61 | """ 62 | } 63 | 64 | private String getClasspathLoadingCode() { 65 | return "getClass().getClassLoader().getResourceAsStream(\"text-resource.properties\")" 66 | } 67 | 68 | private String getModularLoadingCode() { 69 | return "getClass().getModule().getResourceAsStream(\"text-resource.properties\")" 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/factory/JUnit5SampleTestFactory.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder.factory 17 | 18 | import com.zyxist.chainsaw.builder.JavaCodeFactory 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | 21 | class JUnit5SampleTestFactory implements JavaCodeFactory { 22 | boolean mocks; 23 | 24 | static def junit5Test() { 25 | def factory = new JUnit5SampleTestFactory() 26 | factory.mocks = false 27 | return factory 28 | } 29 | 30 | static def junit5TestWithMocks() { 31 | def factory = new JUnit5SampleTestFactory() 32 | factory.mocks = true 33 | return factory 34 | } 35 | 36 | @Override 37 | String getFilename(JigsawProjectBuilder builder) { 38 | return builder.getPackageName().replace(".", "/") + "/AClassTest.java" 39 | } 40 | 41 | @Override 42 | String generateCode(JigsawProjectBuilder builder) { 43 | def mockitoImport = "" 44 | def mockitoUsage = "" 45 | if (mocks) { 46 | mockitoImport = "import static org.mockito.Mockito.mock;" 47 | mockitoUsage = "Object obj = mock(Object.class);" 48 | } 49 | return """ 50 | package ${builder.getPackageName()}; 51 | 52 | ${mockitoImport} 53 | import org.junit.jupiter.api.Test; 54 | import static org.junit.jupiter.api.Assertions.assertTrue; 55 | 56 | public class AClassTest { 57 | @Test 58 | public void isAnInstanceOfAClass() { 59 | ${mockitoUsage} 60 | assertTrue(new AImplementation() instanceof AClass); 61 | } 62 | 63 | static class AImplementation extends AClass { 64 | @Override 65 | public void aMethod(String aString) { 66 | // Do nothing 67 | } 68 | } 69 | } 70 | """ 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/factory/RegularJavaClassFactory.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder.factory 17 | 18 | import com.zyxist.chainsaw.builder.JavaCodeFactory 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | 21 | class RegularJavaClassFactory implements JavaCodeFactory { 22 | def name 23 | 24 | static RegularJavaClassFactory regularJavaClass(String name) { 25 | def factory = new RegularJavaClassFactory() 26 | factory.name = name 27 | return factory 28 | } 29 | 30 | @Override 31 | String getFilename(JigsawProjectBuilder builder) { 32 | return builder.getPackageName().replace(".", "/") + "/" + name + ".java" 33 | } 34 | 35 | @Override 36 | String generateCode(JigsawProjectBuilder builder) { 37 | return """ 38 | package ${builder.getPackageName()}; 39 | 40 | public class ${name} { 41 | private String foo; 42 | 43 | public void setFoo(String foo) { 44 | this.foo = foo; 45 | } 46 | 47 | public void someMethod() { 48 | System.out.println(foo); 49 | } 50 | 51 | public void aMethod(String aString) { 52 | System.out.println(aString); 53 | } 54 | } 55 | """ 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/factory/RunnableDaggerClassFactory.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder.factory 17 | 18 | import com.zyxist.chainsaw.builder.JavaCodeFactory 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | 21 | class RunnableDaggerClassFactory implements JavaCodeFactory { 22 | static RunnableDaggerClassFactory runnableDaggerClass() { 23 | return new RunnableDaggerClassFactory(); 24 | } 25 | 26 | @Override 27 | String getFilename(JigsawProjectBuilder builder) { 28 | return builder.getPackageName().replace(".", "/") + "/AClass.java" 29 | } 30 | 31 | @Override 32 | String generateCode(JigsawProjectBuilder builder) { 33 | return """ 34 | package ${builder.getPackageName()}; 35 | public class AClass { 36 | public void aMethod(String aString) { 37 | SomeClass sc = DaggerApplicationComponent.create().someClass(); 38 | sc.setFoo(aString); 39 | sc.someMethod(); 40 | 41 | } 42 | 43 | public static void main(String... args) { 44 | new AClass().aMethod("Hello World!"); 45 | } 46 | } 47 | """ 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/factory/RunnableGuavaClassFactory.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder.factory 17 | 18 | import com.zyxist.chainsaw.builder.JavaCodeFactory 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | 21 | class RunnableGuavaClassFactory implements JavaCodeFactory { 22 | static RunnableGuavaClassFactory runnableGuavaJsr250Class() { 23 | return new RunnableGuavaClassFactory(); 24 | } 25 | 26 | @Override 27 | String getFilename(JigsawProjectBuilder builder) { 28 | return builder.getPackageName().replace(".", "/") + "/AClass.java" 29 | } 30 | 31 | @Override 32 | String generateCode(JigsawProjectBuilder builder) { 33 | return """ 34 | package ${builder.getPackageName()}; 35 | 36 | import javax.annotation.Resource; 37 | import com.google.common.collect.ImmutableList; 38 | 39 | @Resource 40 | public class AClass { 41 | public void aMethod(String aString) { 42 | ImmutableList list = ImmutableList.of(aString); 43 | System.out.println(list.get(0)); 44 | } 45 | 46 | public static void main(String... args) { 47 | new AClass().aMethod("Hello World!"); 48 | } 49 | } 50 | """ 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/factory/RunnableHackingClassFactory.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder.factory 17 | 18 | import com.zyxist.chainsaw.builder.JavaCodeFactory 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | 21 | class RunnableHackingClassFactory implements JavaCodeFactory { 22 | static RunnableHackingClassFactory runnableHackingJavaClass() { 23 | return new RunnableHackingClassFactory(); 24 | } 25 | 26 | @Override 27 | String getFilename(JigsawProjectBuilder builder) { 28 | return builder.getPackageName().replace(".", "/") + "/AClass.java" 29 | } 30 | 31 | @Override 32 | String generateCode(JigsawProjectBuilder builder) { 33 | return """ 34 | package ${builder.getPackageName()}; 35 | import java.lang.ClassLoader; 36 | import java.lang.reflect.Method; 37 | import java.lang.NoSuchMethodException; 38 | public class AClass { 39 | public static void main(String... args) throws NoSuchMethodException { 40 | Method method = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class); 41 | method.setAccessible(true); 42 | System.out.println("Success - old cglib will work"); 43 | } 44 | } 45 | """ 46 | } 47 | 48 | 49 | } 50 | 51 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/factory/RunnableJavaClassFactory.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder.factory 17 | 18 | import com.zyxist.chainsaw.builder.JavaCodeFactory 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | 21 | class RunnableJavaClassFactory implements JavaCodeFactory { 22 | private final String textToDisplay; 23 | 24 | RunnableJavaClassFactory(String textToDisplay) { 25 | this.textToDisplay = textToDisplay 26 | } 27 | 28 | static RunnableJavaClassFactory runnableJavaClass() { 29 | return new RunnableJavaClassFactory("Hello World!") 30 | } 31 | 32 | static RunnableJavaClassFactory runnableJavaClass(String textToDisplay) { 33 | return new RunnableJavaClassFactory(textToDisplay) 34 | } 35 | 36 | @Override 37 | String getFilename(JigsawProjectBuilder builder) { 38 | return builder.getPackageName().replace(".", "/") + "/AClass.java" 39 | } 40 | 41 | @Override 42 | String generateCode(JigsawProjectBuilder builder) { 43 | return """ 44 | package ${builder.getPackageName()}; 45 | public class AClass { 46 | public void aMethod(String aString) { 47 | System.out.println(aString); 48 | } 49 | 50 | public static void main(String... args) { 51 | new AClass().aMethod("${this.textToDisplay}"); 52 | } 53 | } 54 | """ 55 | } 56 | 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/factory/RunnableResourceClassFactory.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder.factory 17 | 18 | import com.zyxist.chainsaw.builder.JavaCodeFactory 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | 21 | class RunnableResourceClassFactory implements JavaCodeFactory { 22 | private boolean useModuleLoader 23 | 24 | static def runnableResourceClasspathClass() { 25 | def factory = new RunnableResourceClassFactory() 26 | factory.useModuleLoader = false 27 | return factory 28 | } 29 | 30 | static def runnableResourceModularClass() { 31 | def factory = new RunnableResourceClassFactory() 32 | factory.useModuleLoader = true 33 | return factory 34 | } 35 | 36 | @Override 37 | String getFilename(JigsawProjectBuilder builder) { 38 | return builder.getPackageName().replace(".", "/") + "/ResourceClass.java" 39 | } 40 | 41 | @Override 42 | String generateCode(JigsawProjectBuilder builder) { 43 | def loadingCode = useModuleLoader ? getModularLoadingCode() : getClasspathLoadingCode() 44 | return """ 45 | package ${builder.getPackageName()}; 46 | import java.util.Properties; 47 | import java.io.IOException; 48 | 49 | public class ResourceClass { 50 | public static void main(String... args) throws IOException { 51 | Properties props = new Properties(); 52 | props.load($loadingCode); 53 | System.out.println(props.get("property")); 54 | } 55 | } 56 | """ 57 | } 58 | 59 | private String getClasspathLoadingCode() { 60 | return "ResourceClass.class.getClassLoader().getResourceAsStream(\"text-resource.properties\")" 61 | } 62 | 63 | private String getModularLoadingCode() { 64 | return "ResourceClass.class.getModule().getResourceAsStream(\"text-resource.properties\")" 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/factory/SampleTextResourceFactory.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder.factory 17 | 18 | import com.zyxist.chainsaw.builder.JavaCodeFactory 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | 21 | class SampleTextResourceFactory implements JavaCodeFactory { 22 | private boolean inPackage = false 23 | 24 | static SampleTextResourceFactory sampleTextResource() { 25 | return new SampleTextResourceFactory() 26 | } 27 | 28 | static SampleTextResourceFactory samplePackagedTextResource() { 29 | def factory = new SampleTextResourceFactory() 30 | factory.inPackage = true 31 | return factory 32 | } 33 | 34 | @Override 35 | String getFilename(JigsawProjectBuilder builder) { 36 | return (inPackage ? builder.getPackageName().replace(".", "/") + "/" : "") + "text-resource.properties" 37 | } 38 | 39 | @Override 40 | String generateCode(JigsawProjectBuilder builder) { 41 | return """ 42 | property=Hello world 43 | """ 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/factory/TestNGExportedTestPackageTestFactory.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder.factory 17 | 18 | import com.zyxist.chainsaw.builder.JavaCodeFactory 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | 21 | class TestNGExportedTestPackageTestFactory implements JavaCodeFactory { 22 | boolean mocks; 23 | 24 | static def exportedTestPackageTest() { 25 | def factory = new TestNGExportedTestPackageTestFactory() 26 | factory.mocks = false 27 | return factory 28 | } 29 | 30 | static def exportedTestPackageTestWithMocks() { 31 | def factory = new TestNGExportedTestPackageTestFactory() 32 | factory.mocks = true 33 | return factory 34 | } 35 | 36 | @Override 37 | String getFilename(JigsawProjectBuilder builder) { 38 | return builder.getPackageName().replace(".", "/") + "/test/AClassTest.java" 39 | } 40 | 41 | @Override 42 | String generateCode(JigsawProjectBuilder builder) { 43 | def mockitoImport = "" 44 | def mockitoUsage = "" 45 | if (mocks) { 46 | mockitoImport = "import static org.mockito.Mockito.mock;" 47 | mockitoUsage = "Object obj = mock(Object.class);" 48 | } 49 | return """ 50 | package ${builder.getPackageName()}.test; 51 | 52 | import ${builder.getPackageName()}.AClass; 53 | ${mockitoImport} 54 | import org.testng.annotations.Test; 55 | import static org.testng.Assert.assertTrue; 56 | 57 | public class AClassTest { 58 | @Test 59 | public void isAnInstanceOfAClass() { 60 | ${mockitoUsage} 61 | assertTrue(new AImplementation() instanceof AClass); 62 | } 63 | 64 | static class AImplementation extends AClass { 65 | @Override 66 | public void aMethod(String aString) { 67 | // Do nothing 68 | } 69 | } 70 | } 71 | """ 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/builder/factory/TestNGSampleTestFactory.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.builder.factory 17 | 18 | import com.zyxist.chainsaw.builder.JavaCodeFactory 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | 21 | class TestNGSampleTestFactory implements JavaCodeFactory { 22 | boolean mocks; 23 | 24 | static def testngTest() { 25 | def factory = new TestNGSampleTestFactory() 26 | factory.mocks = false 27 | return factory 28 | } 29 | 30 | static def testngTestWithMocks() { 31 | def factory = new TestNGSampleTestFactory() 32 | factory.mocks = true 33 | return factory 34 | } 35 | 36 | @Override 37 | String getFilename(JigsawProjectBuilder builder) { 38 | return builder.getPackageName().replace(".", "/") + "/AClassTest.java" 39 | } 40 | 41 | @Override 42 | String generateCode(JigsawProjectBuilder builder) { 43 | def mockitoImport = "" 44 | def mockitoUsage = "" 45 | if (mocks) { 46 | mockitoImport = "import static org.mockito.Mockito.mock;" 47 | mockitoUsage = "Object obj = mock(Object.class);" 48 | } 49 | return """ 50 | package ${builder.getPackageName()}; 51 | 52 | ${mockitoImport} 53 | import org.testng.annotations.Test; 54 | import static org.testng.Assert.assertTrue; 55 | 56 | public class AClassTest { 57 | @Test 58 | public void isAnInstanceOfAClass() { 59 | ${mockitoUsage} 60 | assertTrue(new AImplementation() instanceof AClass); 61 | } 62 | 63 | static class AImplementation extends AClass { 64 | @Override 65 | public void aMethod(String aString) { 66 | // Do nothing 67 | } 68 | } 69 | } 70 | """ 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/integration/AnnotationProcessingTestSpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.integration 17 | 18 | import com.zyxist.chainsaw.builder.Dependencies 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | import org.gradle.testkit.runner.GradleRunner 21 | import org.junit.Rule 22 | import org.junit.rules.TemporaryFolder 23 | import spock.lang.IgnoreIf 24 | import spock.lang.Specification 25 | 26 | import static com.zyxist.chainsaw.builder.factory.DaggerComponentFactory.daggerComponent 27 | import static com.zyxist.chainsaw.builder.factory.DaggerModuleFactory.daggerModule 28 | import static com.zyxist.chainsaw.builder.factory.RegularJavaClassFactory.regularJavaClass 29 | import static com.zyxist.chainsaw.builder.factory.RunnableDaggerClassFactory.runnableDaggerClass 30 | import static org.gradle.testkit.runner.TaskOutcome.SUCCESS 31 | 32 | class AnnotationProcessingTestSpec extends Specification { 33 | static final NOT_JAVA_9 = !System.getProperty("java.version").startsWith("9") 34 | @Rule 35 | final TemporaryFolder tmpDir = new TemporaryFolder() 36 | 37 | JigsawProjectBuilder project 38 | 39 | def setup() { 40 | project = new JigsawProjectBuilder(tmpDir) 41 | project.moduleName("com.example") 42 | .packageName("com.example") 43 | .useAnnotationProcessor() 44 | .gradleJavaPlugin("application") 45 | .requiredModule(Dependencies.JSR250_MODULE) 46 | .requiredModule(Dependencies.DAGGER_MODULE) 47 | .exportedPackage("com.example") 48 | .patchDependency(Dependencies.JSR305_DEPENDENCY) 49 | .patchedModule(Dependencies.JSR305_PATCH, Dependencies.JSR250_PATCH) 50 | .compileDependency(Dependencies.JSR250_DEPENDENCY) 51 | .compileDependency(Dependencies.DAGGER_DEPENDENCY) 52 | .aptDependency(Dependencies.JSR250_DEPENDENCY) 53 | .aptDependency(Dependencies.DAGGER_COMPILER_DEPENDENCY) 54 | .createJavaFile(regularJavaClass("SomeClass")) 55 | .createJavaFile(daggerComponent()) 56 | .createJavaFile(daggerModule()) 57 | .createJavaFile(runnableDaggerClass()) 58 | .mainClass("com.example.AClass") 59 | } 60 | 61 | @IgnoreIf({NOT_JAVA_9}) 62 | def "chainsaw is able to build projects with annotation processors: Dagger scenario"() { 63 | given: 64 | project 65 | .createModuleDescriptor() 66 | .createGradleBuild() 67 | 68 | when: 69 | def result = GradleRunner.create() 70 | .withProjectDir(project.root) 71 | .withDebug(true) 72 | .forwardOutput() 73 | .withArguments("--stacktrace", "assemble") 74 | .withPluginClasspath().build() 75 | 76 | then: 77 | result.task(":compileJava").outcome == SUCCESS 78 | result.task(":jar").outcome == SUCCESS 79 | new File(tmpDir.root, "build/libs/modular.jar").exists() 80 | new File(tmpDir.root, "build/classes/java/main/module-info.class").exists() 81 | new File(tmpDir.root, "build/classes/java/main/com/example/SomeClass.class").exists() 82 | new File(tmpDir.root, "build/classes/java/main/com/example/AClass.class").exists() 83 | new File(tmpDir.root, "build/classes/java/main/com/example/DaggerApplicationComponent.class").exists() 84 | } 85 | 86 | @IgnoreIf({NOT_JAVA_9}) 87 | def "chainsaw is able to run projects with annotation processors: Dagger scenario"() { 88 | given: 89 | project 90 | .createModuleDescriptor() 91 | .createGradleBuild() 92 | 93 | when: 94 | def result = GradleRunner.create() 95 | .withProjectDir(project.root) 96 | .withDebug(true) 97 | .forwardOutput() 98 | .withArguments("--stacktrace", "run") 99 | .withPluginClasspath() 100 | .build() 101 | 102 | then: 103 | result.output.contains("Hello World!") 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/integration/ChainsawPluginSpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.integration 17 | 18 | import com.zyxist.chainsaw.builder.Dependencies 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | import org.gradle.testkit.runner.GradleRunner 21 | import org.junit.Rule 22 | import org.junit.rules.TemporaryFolder 23 | import spock.lang.IgnoreIf 24 | import spock.lang.Specification 25 | 26 | import static com.zyxist.chainsaw.builder.factory.JUnit4SampleTestFactory.junit4Test 27 | import static com.zyxist.chainsaw.builder.factory.RunnableJavaClassFactory.runnableJavaClass 28 | import static com.zyxist.chainsaw.builder.factory.RunnableResourceClassFactory.runnableResourceClasspathClass 29 | import static com.zyxist.chainsaw.builder.factory.RunnableResourceClassFactory.runnableResourceModularClass 30 | import static com.zyxist.chainsaw.builder.factory.SampleTextResourceFactory.samplePackagedTextResource 31 | import static com.zyxist.chainsaw.builder.factory.SampleTextResourceFactory.sampleTextResource 32 | import static org.gradle.testkit.runner.TaskOutcome.SUCCESS 33 | 34 | class ChainsawPluginSpec extends Specification { 35 | @Rule 36 | final TemporaryFolder tmpDir = new TemporaryFolder() 37 | static final NOT_JAVA_9 = !System.getProperty("java.version").startsWith("9") 38 | 39 | JigsawProjectBuilder project 40 | 41 | def setup() { 42 | project = new JigsawProjectBuilder(tmpDir) 43 | project 44 | .moduleName("com.example") 45 | .packageName("com.example") 46 | .exportedPackage("com.example") 47 | .gradleJavaPlugin("application") 48 | .testCompileDependency(Dependencies.JUNIT4_DEPENDENCY) 49 | 50 | } 51 | 52 | @IgnoreIf({NOT_JAVA_9}) 53 | def "can assemble a module"() { 54 | given: 55 | project 56 | .mainClass("com.example.AClass") 57 | .createJavaFile(runnableJavaClass()) 58 | .createJavaTestFile(junit4Test()) 59 | .createModuleDescriptor() 60 | .createGradleBuild() 61 | 62 | when: 63 | def result = GradleRunner.create() 64 | .withProjectDir(project.root) 65 | .withDebug(true) 66 | .forwardOutput() 67 | .withArguments("assemble") 68 | .withPluginClasspath().build() 69 | 70 | then: 71 | result.task(":compileJava").outcome == SUCCESS 72 | result.task(":jar").outcome == SUCCESS 73 | new File(tmpDir.root, "build/libs/modular.jar").exists() 74 | new File(tmpDir.root, "build/classes/java/main/module-info.class").exists() 75 | new File(tmpDir.root, "build/classes/java/main/com/example/AClass.class").exists() 76 | } 77 | 78 | @IgnoreIf({NOT_JAVA_9}) 79 | def "can detect module name from sources"() { 80 | given: 81 | project 82 | .mainClass("com.example.AClass") 83 | .createJavaFile(runnableJavaClass()) 84 | .dontUseExplicitModuleName() 85 | .createModuleDescriptor() 86 | .createGradleBuild() 87 | 88 | when: 89 | def result = GradleRunner.create() 90 | .withProjectDir(project.root) 91 | .withDebug(true) 92 | .forwardOutput() 93 | .withArguments("assemble", "--info") 94 | .withPluginClasspath().build() 95 | 96 | then: 97 | result.task(":compileJava").outcome == SUCCESS 98 | result.task(":jar").outcome == SUCCESS 99 | result.output.contains("Inferred module name: com.example") 100 | new File(tmpDir.root, "build/libs/modular.jar").exists() 101 | new File(tmpDir.root, "build/classes/java/main/module-info.class").exists() 102 | new File(tmpDir.root, "build/classes/java/main/com/example/AClass.class").exists() 103 | } 104 | 105 | @IgnoreIf({NOT_JAVA_9}) 106 | def "can check a module"() { 107 | given: 108 | project 109 | .mainClass("com.example.AClass") 110 | .createJavaFile(runnableJavaClass()) 111 | .createJavaTestFile(junit4Test()) 112 | .createModuleDescriptor() 113 | .createGradleBuild() 114 | 115 | when: 116 | def result = GradleRunner.create() 117 | .withProjectDir(project.root) 118 | .withDebug(true) 119 | .forwardOutput() 120 | .withArguments("check") 121 | .withPluginClasspath().build() 122 | 123 | then: 124 | result.task(":test").outcome == SUCCESS 125 | } 126 | 127 | @IgnoreIf({NOT_JAVA_9}) 128 | def "can run with a module"() { 129 | given: 130 | project 131 | .mainClass("com.example.AClass") 132 | .createJavaFile(runnableJavaClass()) 133 | .createJavaTestFile(junit4Test()) 134 | .createModuleDescriptor() 135 | .createGradleBuild() 136 | 137 | when: 138 | def result = GradleRunner.create() 139 | .withProjectDir(project.root) 140 | .withDebug(true) 141 | .forwardOutput() 142 | .withArguments("run", "--info", "--stacktrace") 143 | .withPluginClasspath().build() 144 | 145 | then: 146 | result.output.contains("--module-path") 147 | result.output.contains("Hello World!") 148 | } 149 | 150 | @IgnoreIf({NOT_JAVA_9}) 151 | def "resources loaded with classloader are visible in runtime"() { 152 | project 153 | .mainClass("com.example.ResourceClass") 154 | .createJavaFile(runnableResourceClasspathClass()) 155 | .createJavaResourceFile(sampleTextResource()) 156 | .createModuleDescriptor() 157 | .createGradleBuild() 158 | 159 | when: 160 | def result = GradleRunner.create() 161 | .withProjectDir(project.root) 162 | .withDebug(true) 163 | .forwardOutput() 164 | .withArguments("run", "--info") 165 | .withPluginClasspath().build() 166 | 167 | then: 168 | result.output.contains("--module-path") 169 | result.output.contains("Hello world") 170 | } 171 | 172 | @IgnoreIf({NOT_JAVA_9}) 173 | def "resources loaded with module loader are visible in runtime"() { 174 | project 175 | .mainClass("com.example.ResourceClass") 176 | .createJavaFile(runnableResourceModularClass()) 177 | .createJavaResourceFile(sampleTextResource()) 178 | .createModuleDescriptor() 179 | .createGradleBuild() 180 | 181 | when: 182 | def result = GradleRunner.create() 183 | .withProjectDir(project.root) 184 | .withDebug(true) 185 | .forwardOutput() 186 | .withArguments("run", "--info", "--stacktrace") 187 | .withPluginClasspath().build() 188 | 189 | then: 190 | result.output.contains("--module-path") 191 | result.output.contains("Hello world") 192 | } 193 | } -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/integration/CustomFlagsSpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.integration 17 | 18 | import com.zyxist.chainsaw.builder.Dependencies 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | import org.gradle.testkit.runner.GradleRunner 21 | import org.junit.Rule 22 | import org.junit.rules.TemporaryFolder 23 | import spock.lang.IgnoreIf 24 | import spock.lang.Specification 25 | 26 | import static com.zyxist.chainsaw.builder.factory.JUnit4SampleTestFactory.junit4Test 27 | import static com.zyxist.chainsaw.builder.factory.RunnableHackingClassFactory.runnableHackingJavaClass 28 | import static com.zyxist.chainsaw.builder.factory.RunnableJavaClassFactory.runnableJavaClass 29 | import static org.gradle.testkit.runner.TaskOutcome.SUCCESS 30 | import static org.gradle.testkit.runner.TaskOutcome.SUCCESS 31 | 32 | class CustomFlagsSpec extends Specification { 33 | @Rule 34 | final TemporaryFolder tmpDir = new TemporaryFolder() 35 | static final NOT_JAVA_9 = !System.getProperty("java.version").startsWith("9") 36 | 37 | JigsawProjectBuilder project 38 | 39 | def setup() { 40 | project = new JigsawProjectBuilder(tmpDir) 41 | project 42 | .moduleName("com.example") 43 | .packageName("com.example") 44 | .exportedPackage("com.example") 45 | .gradleJavaPlugin("application") 46 | 47 | } 48 | 49 | @IgnoreIf({NOT_JAVA_9}) 50 | def "it is possible to apply custom --add-opens flags to the runner"() { 51 | given: 52 | project 53 | .mainClass("com.example.AClass") 54 | .createJavaFile(runnableHackingJavaClass()) 55 | .openedModule("java.base", "java.lang", "com.example") 56 | .createModuleDescriptor() 57 | .createGradleBuild() 58 | 59 | when: 60 | def result = GradleRunner.create() 61 | .withProjectDir(project.root) 62 | .withDebug(true) 63 | .forwardOutput() 64 | .withArguments("run", "--info", "--stacktrace") 65 | .withPluginClasspath().build() 66 | 67 | then: 68 | result.task(":run").outcome == SUCCESS 69 | result.output.contains("--module-path") 70 | result.output.contains("--add-opens java.base/java.lang=com.example") 71 | result.output.contains("Success - old cglib will work") 72 | } 73 | 74 | @IgnoreIf({NOT_JAVA_9}) 75 | def "it is possible to apply compiler arguments"() { 76 | given: 77 | project 78 | .mainClass("com.example.AClass") 79 | .createJavaFile(runnableJavaClass()) 80 | .withCompilerArgs("-proc:none") 81 | .createModuleDescriptor() 82 | .createGradleBuild() 83 | 84 | when: 85 | def result = GradleRunner.create() 86 | .withProjectDir(project.root) 87 | .withDebug(true) 88 | .forwardOutput() 89 | .withArguments("assemble", "--debug") 90 | .withPluginClasspath().build() 91 | 92 | then: 93 | result.task(":compileJava").outcome == SUCCESS 94 | result.task(":jar").outcome == SUCCESS 95 | result.output.contains("--module-path") 96 | result.output.contains("-proc:none") 97 | new File(tmpDir.root, "build/libs/modular.jar").exists() 98 | } 99 | 100 | @IgnoreIf({NOT_JAVA_9}) 101 | def "it is possible to apply custom JVM arguments"() { 102 | given: 103 | project 104 | .mainClass("com.example.AClass") 105 | .createJavaFile(runnableJavaClass()) 106 | .withJvmArgs("-XX:NewSize=64M") 107 | .createModuleDescriptor() 108 | .createGradleBuild() 109 | 110 | when: 111 | def result = GradleRunner.create() 112 | .withProjectDir(project.root) 113 | .withDebug(true) 114 | .forwardOutput() 115 | .withArguments("run", "--debug") 116 | .withPluginClasspath().build() 117 | 118 | then: 119 | result.task(":run").outcome == SUCCESS 120 | result.output.contains("--module-path") 121 | result.output.contains("-XX:NewSize=64M") 122 | } 123 | 124 | @IgnoreIf({NOT_JAVA_9}) 125 | def "it is possible to apply custom JVM arguments for tests"() { 126 | given: 127 | project 128 | .testCompileDependency(Dependencies.JUNIT4_DEPENDENCY) 129 | .mainClass("com.example.AClass") 130 | .createJavaFile(runnableJavaClass()) 131 | .createJavaTestFile(junit4Test()) 132 | .withTestJvmArgs("-XX:NewSize=128M") 133 | .createModuleDescriptor() 134 | .createGradleBuild() 135 | 136 | when: 137 | def result = GradleRunner.create() 138 | .withProjectDir(project.root) 139 | .withDebug(true) 140 | .forwardOutput() 141 | .withArguments("check", "--debug") 142 | .withPluginClasspath().build() 143 | 144 | then: 145 | result.task(":test").outcome == SUCCESS 146 | result.output.contains("--module-path") 147 | result.output.contains("-XX:NewSize=128M") 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/integration/IncrementalCompilationSpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.integration 17 | 18 | import com.zyxist.chainsaw.builder.Dependencies 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | import org.gradle.testkit.runner.GradleRunner 21 | import org.junit.Rule 22 | import org.junit.rules.TemporaryFolder 23 | import spock.lang.Specification 24 | 25 | import static com.zyxist.chainsaw.builder.factory.JUnit4SampleTestFactory.junit4Test 26 | import static com.zyxist.chainsaw.builder.factory.RegularJavaClassFactory.regularJavaClass 27 | import static com.zyxist.chainsaw.builder.factory.RunnableJavaClassFactory.runnableJavaClass 28 | import static org.gradle.testkit.runner.TaskOutcome.* 29 | 30 | class IncrementalCompilationSpec extends Specification { 31 | @Rule 32 | final TemporaryFolder tmpDir = new TemporaryFolder() 33 | static final NOT_JAVA_9 = !System.getProperty("java.version").startsWith("9") 34 | 35 | JigsawProjectBuilder project 36 | 37 | def setup() { 38 | project = new JigsawProjectBuilder(tmpDir) 39 | project 40 | .enableIncrementalCompilation() 41 | .moduleName("com.example") 42 | .packageName("com.example") 43 | .exportedPackage("com.example") 44 | .gradleJavaPlugin("application") 45 | .testCompileDependency(Dependencies.JUNIT4_DEPENDENCY) 46 | 47 | } 48 | 49 | 50 | def "the plugin does not break incremental compilation in compileJava task, when one of classes is changed"() { 51 | given: 52 | project 53 | .mainClass("com.example.AClass") 54 | .createJavaFile(runnableJavaClass()) 55 | .createJavaFile(regularJavaClass("Foo")) 56 | .createJavaTestFile(junit4Test()) 57 | .createModuleDescriptor() 58 | .createGradleBuild() 59 | 60 | when: 61 | def firstRun = GradleRunner.create() 62 | .withProjectDir(project.root) 63 | .withDebug(true) 64 | .forwardOutput() 65 | .withArguments("assemble") 66 | .withPluginClasspath().build() 67 | project.replaceJavaFile(runnableJavaClass("Changed text.")) 68 | def secondRun = GradleRunner.create() 69 | .withProjectDir(project.root) 70 | .withDebug(true) 71 | .forwardOutput() 72 | .withArguments("assemble", "--info") 73 | .withPluginClasspath().build() 74 | 75 | then: 76 | firstRun.task(":compileJava").outcome == SUCCESS 77 | secondRun.task(":compileJava").outcome == SUCCESS 78 | secondRun.output.contains("Incremental compilation of 1 classes completed") 79 | new File(tmpDir.root, "build/libs/modular.jar").exists() 80 | new File(tmpDir.root, "build/classes/java/main/module-info.class").exists() 81 | new File(tmpDir.root, "build/classes/java/main/com/example/AClass.class").exists() 82 | } 83 | 84 | def "the plugin does not break incremental compilation in compileJava task, when there are no changes"() { 85 | given: 86 | project 87 | .mainClass("com.example.AClass") 88 | .createJavaFile(runnableJavaClass()) 89 | .createJavaFile(regularJavaClass("Foo")) 90 | .createJavaTestFile(junit4Test()) 91 | .createModuleDescriptor() 92 | .createGradleBuild() 93 | 94 | when: 95 | def firstRun = GradleRunner.create() 96 | .withProjectDir(project.root) 97 | .withDebug(true) 98 | .forwardOutput() 99 | .withArguments("assemble") 100 | .withPluginClasspath().build() 101 | def secondRun = GradleRunner.create() 102 | .withProjectDir(project.root) 103 | .withDebug(true) 104 | .forwardOutput() 105 | .withArguments("assemble", "--info") 106 | .withPluginClasspath().build() 107 | 108 | then: 109 | firstRun.task(":compileJava").outcome == SUCCESS 110 | secondRun.task(":compileJava").outcome == UP_TO_DATE 111 | new File(tmpDir.root, "build/libs/modular.jar").exists() 112 | new File(tmpDir.root, "build/classes/java/main/module-info.class").exists() 113 | new File(tmpDir.root, "build/classes/java/main/com/example/AClass.class").exists() 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/integration/JUnit4TestSpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.integration 17 | 18 | import com.zyxist.chainsaw.builder.Dependencies 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | import org.gradle.testkit.runner.GradleRunner 21 | import org.junit.Rule 22 | import org.junit.rules.TemporaryFolder 23 | import spock.lang.IgnoreIf 24 | import spock.lang.Specification 25 | 26 | import static com.zyxist.chainsaw.builder.factory.JUnit4SampleTestFactory.junit4TestWithMocks 27 | import static com.zyxist.chainsaw.builder.factory.RegularJavaClassFactory.regularJavaClass 28 | import static org.gradle.testkit.runner.TaskOutcome.SUCCESS 29 | 30 | class JUnit4TestSpec extends Specification { 31 | static final NOT_JAVA_9 = !System.getProperty("java.version").startsWith("9") 32 | @Rule 33 | final TemporaryFolder tmpDir = new TemporaryFolder() 34 | 35 | JigsawProjectBuilder project 36 | 37 | def setup() { 38 | project = new JigsawProjectBuilder(tmpDir) 39 | project.moduleName("com.example") 40 | .packageName("com.example") 41 | .exportedPackage("com.example") 42 | .testCompileDependency(Dependencies.JUNIT4_DEPENDENCY) 43 | .testCompileDependency(Dependencies.MOCKITO_DEPENDENCY) 44 | .extraTestModule(Dependencies.MOCKITO_MODULE) 45 | .createJavaFile(regularJavaClass("AClass")) 46 | .createJavaTestFile(junit4TestWithMocks()) 47 | } 48 | 49 | @IgnoreIf({NOT_JAVA_9}) 50 | def "run JUnit4 tests with Mockito - java plugin way"() { 51 | given: 52 | project 53 | .gradleJavaPlugin("java") 54 | .createGradleBuild() 55 | .createModuleDescriptor() 56 | 57 | when: 58 | def result = GradleRunner.create() 59 | .withProjectDir(project.root) 60 | .withDebug(true) 61 | .forwardOutput() 62 | .withArguments("check") 63 | .withPluginClasspath().build() 64 | 65 | then: 66 | result.task(":test").outcome == SUCCESS 67 | } 68 | 69 | @IgnoreIf({NOT_JAVA_9}) 70 | def "run JUnit4 tests with Mockito - new way"() { 71 | given: 72 | project 73 | .gradleJavaPlugin("application") 74 | .createGradleBuild() 75 | .createModuleDescriptor() 76 | 77 | when: 78 | def result = GradleRunner.create() 79 | .withProjectDir(tmpDir.root) 80 | .withDebug(true) 81 | .forwardOutput() 82 | .withArguments("check") 83 | .withPluginClasspath().build() 84 | 85 | then: 86 | result.task(":test").outcome == SUCCESS 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/integration/JUnit5TestSpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.integration 17 | 18 | import com.zyxist.chainsaw.builder.Dependencies 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | import org.gradle.testkit.runner.GradleRunner 21 | import org.junit.Rule 22 | import org.junit.rules.TemporaryFolder 23 | import spock.lang.IgnoreIf 24 | import spock.lang.Specification 25 | 26 | import static com.zyxist.chainsaw.builder.factory.JUnit5SampleResourceTestFactory.junit5TestAccessingModularResources 27 | import static com.zyxist.chainsaw.builder.factory.JUnit5SampleResourceTestFactory.junit5TestAccessingResources 28 | import static com.zyxist.chainsaw.builder.factory.JUnit5SampleTestFactory.junit5TestWithMocks 29 | import static com.zyxist.chainsaw.builder.factory.RegularJavaClassFactory.regularJavaClass 30 | import static com.zyxist.chainsaw.builder.factory.SampleTextResourceFactory.sampleTextResource 31 | import static org.gradle.testkit.runner.TaskOutcome.SUCCESS 32 | 33 | class JUnit5TestSpec extends Specification { 34 | static final NOT_JAVA_9 = !System.getProperty("java.version").startsWith("9") 35 | 36 | @Rule 37 | final TemporaryFolder tmpDir = new TemporaryFolder() 38 | JigsawProjectBuilder project 39 | 40 | def setup() { 41 | project = new JigsawProjectBuilder(tmpDir) 42 | project.moduleName("com.example") 43 | .packageName("com.example") 44 | .exportedPackage("com.example") 45 | .applyPlugin(Dependencies.JUNIT5_PLUGIN_DEPENDENCY, Dependencies.JUNIT5_PLUGIN_NAME) 46 | .testCompileDependency(Dependencies.JUNIT5_API_DEPENDENCY) 47 | .testCompileDependency(Dependencies.MOCKITO_DEPENDENCY) 48 | .testRuntimeDependency(Dependencies.JUNIT5_ENGINE_DEPENDENCY) 49 | .extraTestModule(Dependencies.MOCKITO_MODULE) 50 | .createJavaFile(regularJavaClass("AClass")) 51 | } 52 | 53 | @IgnoreIf({NOT_JAVA_9}) 54 | def "run JUnit5 tests with Mockito - java plugin way"() { 55 | given: 56 | project 57 | .gradleJavaPlugin("java") 58 | .createJavaTestFile(junit5TestWithMocks()) 59 | .createGradleBuild() 60 | .createModuleDescriptor() 61 | 62 | when: 63 | def result = GradleRunner.create() 64 | .withProjectDir(tmpDir.root) 65 | .withDebug(true) 66 | .forwardOutput() 67 | .withArguments("check") 68 | .withPluginClasspath().build() 69 | 70 | then: 71 | result.task(":junitPlatformTest").outcome == SUCCESS 72 | } 73 | 74 | @IgnoreIf({NOT_JAVA_9}) 75 | def "run JUnit5 tests with Mockito - new way"() { 76 | given: 77 | project 78 | .gradleJavaPlugin("application") 79 | .createJavaTestFile(junit5TestWithMocks()) 80 | .createGradleBuild() 81 | .createModuleDescriptor() 82 | 83 | when: 84 | def result = GradleRunner.create() 85 | .withProjectDir(tmpDir.root) 86 | .withDebug(true) 87 | .forwardOutput() 88 | .withArguments("check") 89 | .withPluginClasspath().build() 90 | 91 | then: 92 | result.task(":junitPlatformTest").outcome == SUCCESS 93 | } 94 | 95 | @IgnoreIf({NOT_JAVA_9}) 96 | def "JUnit5 tests shall see the test resources loaded via classpath"() { 97 | given: 98 | project 99 | .gradleJavaPlugin("java") 100 | .createTestResourceFile(sampleTextResource()) 101 | .createJavaTestFile(junit5TestAccessingResources()) 102 | .createGradleBuild() 103 | .createModuleDescriptor() 104 | 105 | when: 106 | def result = GradleRunner.create() 107 | .withProjectDir(tmpDir.root) 108 | .withDebug(true) 109 | .forwardOutput() 110 | .withArguments("check") 111 | .withPluginClasspath().build() 112 | 113 | then: 114 | result.task(":junitPlatformTest").outcome == SUCCESS 115 | } 116 | 117 | @IgnoreIf({NOT_JAVA_9}) 118 | def "JUnit5 tests shall see the test resources loaded via modular path"() { 119 | given: 120 | project 121 | .gradleJavaPlugin("java") 122 | .createTestResourceFile(sampleTextResource()) 123 | .createJavaTestFile(junit5TestAccessingModularResources()) 124 | .createGradleBuild() 125 | .createModuleDescriptor() 126 | 127 | when: 128 | def result = GradleRunner.create() 129 | .withProjectDir(tmpDir.root) 130 | .withDebug(true) 131 | .forwardOutput() 132 | .withArguments("check") 133 | .withPluginClasspath().build() 134 | 135 | then: 136 | result.task(":junitPlatformTest").outcome == SUCCESS 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/integration/JavadocTestSpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.integration 17 | 18 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 19 | import com.zyxist.chainsaw.jigsaw.JigsawFlags 20 | import org.gradle.testkit.runner.GradleRunner 21 | import org.junit.Rule 22 | import org.junit.rules.TemporaryFolder 23 | import spock.lang.IgnoreIf 24 | import spock.lang.Specification 25 | 26 | import static com.zyxist.chainsaw.builder.factory.DocumentedJavaClassFactory.documentedJavaClass 27 | import static org.gradle.testkit.runner.TaskOutcome.SUCCESS 28 | 29 | class JavadocTestSpec extends Specification { 30 | @Rule 31 | final TemporaryFolder tmpDir = new TemporaryFolder() 32 | static final NOT_JAVA_9 = !System.getProperty("java.version").startsWith("9") 33 | 34 | JigsawProjectBuilder project 35 | 36 | def setup() { 37 | project = new JigsawProjectBuilder(tmpDir) 38 | project 39 | .moduleName("com.example") 40 | .packageName("com.example") 41 | .exportedPackage("com.example") 42 | .createJavaFile(documentedJavaClass()) 43 | } 44 | 45 | @IgnoreIf({ NOT_JAVA_9 }) 46 | def "it is possible to generate javadocs for modular project"() { 47 | given: 48 | project 49 | .gradleJavaPlugin("java") 50 | .createGradleBuild() 51 | .createModuleDescriptor() 52 | 53 | when: 54 | def result = GradleRunner.create() 55 | .withProjectDir(project.root) 56 | .withDebug(true) 57 | .forwardOutput() 58 | .withArguments("javadoc") 59 | .withPluginClasspath().build() 60 | 61 | then: 62 | result.task(":javadoc").outcome == SUCCESS 63 | new File(tmpDir.root, "build/tmp/javadoc/javadoc.options").exists() 64 | new File(tmpDir.root, "build/docs/javadoc/com/example/DocumentedClass.html").exists() 65 | new File(tmpDir.root, "build/tmp/javadoc/javadoc.options").text.contains(JigsawFlags.JAVADOC_MODULE_PATH) 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/integration/ModulePatchTestSpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.integration 17 | 18 | import com.zyxist.chainsaw.builder.Dependencies 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | import org.gradle.testkit.runner.GradleRunner 21 | import org.junit.Rule 22 | import org.junit.rules.TemporaryFolder 23 | import spock.lang.IgnoreIf 24 | import spock.lang.Specification 25 | 26 | import static com.zyxist.chainsaw.builder.factory.JUnit4SampleTestFactory.junit4Test 27 | import static com.zyxist.chainsaw.builder.factory.RegularJavaClassFactory.regularJavaClass 28 | import static com.zyxist.chainsaw.builder.factory.RunnableGuavaClassFactory.runnableGuavaJsr250Class 29 | import static com.zyxist.chainsaw.builder.factory.RunnableJavaClassFactory.runnableJavaClass 30 | import static org.gradle.testkit.runner.TaskOutcome.SUCCESS 31 | 32 | class ModulePatchTestSpec extends Specification { 33 | static final NOT_JAVA_9 = !System.getProperty("java.version").startsWith("9") 34 | @Rule 35 | final TemporaryFolder tmpDir = new TemporaryFolder() 36 | 37 | JigsawProjectBuilder project 38 | 39 | def setup() { 40 | project = new JigsawProjectBuilder(tmpDir) 41 | project.moduleName("com.example") 42 | .packageName("com.example") 43 | .requiredModule(Dependencies.JSR250_MODULE) 44 | .requiredModule(Dependencies.GUAVA_MODULE) 45 | .exportedPackage("com.example") 46 | .patchDependency(Dependencies.JSR305_DEPENDENCY) 47 | .patchedModule(Dependencies.JSR305_PATCH, Dependencies.JSR250_PATCH) 48 | .compileDependency(Dependencies.JSR250_DEPENDENCY) 49 | .compileDependency(Dependencies.GUAVA_DEPENDENCY) 50 | .createJavaFile(regularJavaClass("SomeClass")) 51 | } 52 | 53 | @IgnoreIf({NOT_JAVA_9}) 54 | def "patch rouge jsr305 during compilation - java plugin"() { 55 | given: 56 | project 57 | .gradleJavaPlugin("java") 58 | .createModuleDescriptor() 59 | .createGradleBuild() 60 | 61 | when: 62 | def result = GradleRunner.create() 63 | .withProjectDir(project.root) 64 | .withDebug(true) 65 | .forwardOutput() 66 | .withArguments("assemble", '--info') 67 | .withPluginClasspath().build() 68 | 69 | then: 70 | result.task(":compileJava").outcome == SUCCESS 71 | result.task(":jar").outcome == SUCCESS 72 | new File(tmpDir.root, "build/libs/modular.jar").exists() 73 | new File(tmpDir.root, "build/classes/java/main/module-info.class").exists() 74 | new File(tmpDir.root, "build/classes/java/main/com/example/SomeClass.class").exists() 75 | } 76 | 77 | @IgnoreIf({NOT_JAVA_9}) 78 | def "patch rouge jsr305 during compilation - library plugin"() { 79 | given: 80 | project 81 | .gradleJavaPlugin("java-library") 82 | .createModuleDescriptor() 83 | .createGradleBuild() 84 | 85 | when: 86 | def result = GradleRunner.create() 87 | .withProjectDir(project.root) 88 | .withDebug(true) 89 | .forwardOutput() 90 | .withArguments("assemble") 91 | .withPluginClasspath().build() 92 | 93 | then: 94 | result.task(":compileJava").outcome == SUCCESS 95 | result.task(":jar").outcome == SUCCESS 96 | new File(tmpDir.root, "build/libs/modular.jar").exists() 97 | new File(tmpDir.root, "build/classes/java/main/module-info.class").exists() 98 | new File(tmpDir.root, "build/classes/java/main/com/example/SomeClass.class").exists() 99 | } 100 | 101 | @IgnoreIf({NOT_JAVA_9}) 102 | def "patch rouge jsr305 during compilation - application plugin"() { 103 | given: 104 | project 105 | .gradleJavaPlugin("application") 106 | .createJavaFile(runnableJavaClass()) 107 | .mainClass("com.example.AClass") 108 | .createModuleDescriptor() 109 | .createGradleBuild() 110 | 111 | when: 112 | def result = GradleRunner.create() 113 | .withProjectDir(project.root) 114 | .withDebug(true) 115 | .forwardOutput() 116 | .withArguments("assemble") 117 | .withPluginClasspath().build() 118 | 119 | then: 120 | result.task(":compileJava").outcome == SUCCESS 121 | result.task(":jar").outcome == SUCCESS 122 | new File(tmpDir.root, "build/libs/modular.jar").exists() 123 | new File(tmpDir.root, "build/classes/java/main/module-info.class").exists() 124 | new File(tmpDir.root, "build/classes/java/main/com/example/SomeClass.class").exists() 125 | new File(tmpDir.root, "build/classes/java/main/com/example/AClass.class").exists() 126 | } 127 | 128 | @IgnoreIf({NOT_JAVA_9}) 129 | def "patch rogue jsr305 in unit tests - application plugin"() { 130 | given: 131 | project 132 | .testCompileDependency(Dependencies.JUNIT4_DEPENDENCY) 133 | .gradleJavaPlugin("application") 134 | .createJavaFile(runnableGuavaJsr250Class()) 135 | .createJavaTestFile(junit4Test()) 136 | .mainClass("com.example.AClass") 137 | .createModuleDescriptor() 138 | .createGradleBuild() 139 | 140 | when: 141 | def result = GradleRunner.create() 142 | .withProjectDir(project.root) 143 | .withDebug(true) 144 | .forwardOutput() 145 | .withArguments("check") 146 | .withPluginClasspath().build() 147 | 148 | then: 149 | result.task(":test").outcome == SUCCESS 150 | } 151 | 152 | @IgnoreIf({NOT_JAVA_9}) 153 | def "patch rogue jsr305 in runtime"() { 154 | given: 155 | project 156 | .gradleJavaPlugin("application") 157 | .createJavaFile(runnableGuavaJsr250Class()) 158 | .mainClass("com.example.AClass") 159 | .createModuleDescriptor() 160 | .createGradleBuild() 161 | 162 | when: 163 | def result = GradleRunner.create() 164 | .withProjectDir(project.root) 165 | .withDebug(true) 166 | .forwardOutput() 167 | .withArguments("run") 168 | .withPluginClasspath() 169 | .build() 170 | 171 | then: 172 | result.output.contains("Hello World!") 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/integration/MultiprojectSetupSpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.integration 17 | 18 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 19 | import org.gradle.testkit.runner.GradleRunner 20 | import org.gradle.testkit.runner.TaskOutcome 21 | import org.junit.Rule 22 | import org.junit.rules.TemporaryFolder 23 | import spock.lang.Specification 24 | 25 | class MultiprojectSetupSpec extends Specification { 26 | static final NOT_JAVA_9 = !System.getProperty("java.version").startsWith("9") 27 | 28 | @Rule 29 | final TemporaryFolder tmpDir = new TemporaryFolder() 30 | JigsawProjectBuilder project 31 | 32 | def "don't fail, if there is no /src/main/java directory due to module name autodetection"() { 33 | given: 34 | project = new JigsawProjectBuilder(tmpDir) 35 | project 36 | .moduleName("com.example") 37 | .gradleJavaPlugin("java") 38 | .dontUseExplicitModuleName() 39 | .createGradleBuild() 40 | 41 | when: 42 | def result = GradleRunner.create() 43 | .withProjectDir(project.root) 44 | .withDebug(true) 45 | .forwardOutput() 46 | .withArguments("compileJava", '--info') 47 | .withPluginClasspath().build() 48 | 49 | then: 50 | result.task(":compileJava").outcome == TaskOutcome.NO_SOURCE 51 | } 52 | } -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/integration/StartScriptsSpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.integration 17 | 18 | import com.zyxist.chainsaw.builder.Dependencies 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | import org.gradle.testkit.runner.GradleRunner 21 | import org.junit.Rule 22 | import org.junit.rules.TemporaryFolder 23 | import spock.lang.IgnoreIf 24 | import spock.lang.Specification 25 | 26 | import static com.zyxist.chainsaw.builder.factory.RunnableJavaClassFactory.runnableJavaClass 27 | import static org.gradle.testkit.runner.TaskOutcome.SUCCESS 28 | 29 | class StartScriptsSpec extends Specification { 30 | static final NOT_IMPLEMENTED = true 31 | static final NOT_JAVA_9 = !System.getProperty("java.version").startsWith("9") 32 | @Rule 33 | final TemporaryFolder tmpDir = new TemporaryFolder() 34 | 35 | JigsawProjectBuilder project 36 | 37 | def setup() { 38 | project = new JigsawProjectBuilder(tmpDir) 39 | project 40 | .moduleName("com.example") 41 | .packageName("com.example") 42 | .exportedPackage("com.example") 43 | .compileDependency(Dependencies.GUICE_DEPENDENCY) 44 | .gradleJavaPlugin("application") 45 | .mainClass("com.example.AClass") 46 | .createJavaFile(runnableJavaClass()) 47 | } 48 | 49 | @IgnoreIf({NOT_JAVA_9}) 50 | def "plugin adds --module-path to the generated Unix and Windows script"() { 51 | given: 52 | project 53 | .createModuleDescriptor() 54 | .createGradleBuild() 55 | 56 | when: 57 | def result = GradleRunner.create() 58 | .withProjectDir(project.root) 59 | .withDebug(true) 60 | .forwardOutput() 61 | .withArguments("installDist", "--stacktrace") 62 | .withPluginClasspath().build() 63 | 64 | then: 65 | result.task(":installDist").outcome == SUCCESS 66 | new File(tmpDir.root, "build/install/modular/bin/modular").exists() 67 | new File(tmpDir.root, "build/install/modular/bin/modular.bat").exists() 68 | new File(tmpDir.root, "build/install/modular/bin/modular").text.contains("'\"--module-path\" \"\$APP_HOME/lib\"") 69 | new File(tmpDir.root, "build/install/modular/bin/modular.bat").text.contains("\"--module-path\" \"%APP_HOME%\\\\lib\"") 70 | } 71 | 72 | @IgnoreIf({NOT_JAVA_9}) 73 | def "plugin adds custom --add-something flags to the generated Unix and Windows script"() { 74 | given: 75 | project 76 | .openedModule("com.example", "com.example", "com.google.guice") 77 | .createModuleDescriptor() 78 | .createGradleBuild() 79 | 80 | when: 81 | def result = GradleRunner.create() 82 | .withProjectDir(project.root) 83 | .withDebug(true) 84 | .forwardOutput() 85 | .withArguments("installDist", "--stacktrace") 86 | .withPluginClasspath().build() 87 | 88 | then: 89 | result.task(":installDist").outcome == SUCCESS 90 | new File(tmpDir.root, "build/install/modular/bin/modular").text.contains("\"--add-opens\" \"com.example/com.example=com.google.guice\"") 91 | new File(tmpDir.root, "build/install/modular/bin/modular.bat").text.contains("\"--add-opens\" \"com.example/com.example=com.google.guice\"") 92 | } 93 | 94 | @IgnoreIf({NOT_JAVA_9; NOT_IMPLEMENTED}) 95 | def "plugin adds custom patch config to the generated Unix and Windows script"() { 96 | 97 | } 98 | 99 | @IgnoreIf({NOT_JAVA_9}) 100 | def "Windows script retains line endings characteristic for that platform"() { 101 | given: 102 | project 103 | .openedModule("com.example", "com.example", "com.google.guice") 104 | .createModuleDescriptor() 105 | .createGradleBuild() 106 | 107 | when: 108 | def result = GradleRunner.create() 109 | .withProjectDir(project.root) 110 | .withDebug(true) 111 | .forwardOutput() 112 | .withArguments("installDist", "--stacktrace") 113 | .withPluginClasspath().build() 114 | 115 | then: 116 | new File(tmpDir.root, "build/install/modular/bin/modular.bat").text.contains("\r\n") 117 | } 118 | 119 | @IgnoreIf({NOT_JAVA_9}) 120 | def "Unix script retains line endings characteristic for that platform"() { 121 | given: 122 | project 123 | .openedModule("com.example", "com.example", "com.google.guice") 124 | .createModuleDescriptor() 125 | .createGradleBuild() 126 | 127 | when: 128 | def result = GradleRunner.create() 129 | .withProjectDir(project.root) 130 | .withDebug(true) 131 | .forwardOutput() 132 | .withArguments("installDist", "--stacktrace") 133 | .withPluginClasspath().build() 134 | 135 | then: 136 | !new File(tmpDir.root, "build/install/modular/bin/modular").text.contains("\r\n") 137 | new File(tmpDir.root, "build/install/modular/bin/modular").text.contains("\n") 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/integration/TestNGTestSpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.integration 17 | 18 | import com.zyxist.chainsaw.builder.Dependencies 19 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 20 | import org.gradle.testkit.runner.GradleRunner 21 | import org.junit.Rule 22 | import org.junit.rules.TemporaryFolder 23 | import spock.lang.IgnoreIf 24 | import spock.lang.Specification 25 | 26 | import static com.zyxist.chainsaw.builder.factory.TestNGSampleTestFactory.testngTestWithMocks 27 | import static com.zyxist.chainsaw.builder.factory.TestNGExportedTestPackageTestFactory.exportedTestPackageTestWithMocks 28 | import static com.zyxist.chainsaw.builder.factory.RegularJavaClassFactory.regularJavaClass 29 | import static org.gradle.testkit.runner.TaskOutcome.SUCCESS 30 | 31 | class TestNGTestSpec extends Specification { 32 | static final NOT_JAVA_9 = !System.getProperty("java.version").startsWith("9") 33 | @Rule 34 | final TemporaryFolder tmpDir = new TemporaryFolder() 35 | 36 | JigsawProjectBuilder project 37 | 38 | def setup() { 39 | project = new JigsawProjectBuilder(tmpDir) 40 | project.moduleName("com.example") 41 | .packageName("com.example") 42 | .exportedPackage("com.example") 43 | .testCompileDependency(Dependencies.TESTNG_DEPENDENCY) 44 | .testCompileDependency(Dependencies.MOCKITO_DEPENDENCY) 45 | .extraTestModule(Dependencies.MOCKITO_MODULE) 46 | .createJavaFile(regularJavaClass("AClass")) 47 | .createJavaTestFile(testngTestWithMocks()) 48 | } 49 | 50 | @IgnoreIf({NOT_JAVA_9}) 51 | def "run TestNG tests with Mockito - java plugin way"() { 52 | given: 53 | project 54 | .gradleJavaPlugin("java") 55 | .createGradleBuild() 56 | .createModuleDescriptor() 57 | 58 | when: 59 | def result = GradleRunner.create() 60 | .withProjectDir(project.root) 61 | .withDebug(true) 62 | .forwardOutput() 63 | .withArguments("check") 64 | .withPluginClasspath().build() 65 | 66 | then: 67 | result.task(":test").outcome == SUCCESS 68 | } 69 | 70 | @IgnoreIf({NOT_JAVA_9}) 71 | def "run TestNG tests with Mockito - new way"() { 72 | given: 73 | project 74 | .gradleJavaPlugin("application") 75 | .createGradleBuild() 76 | .createModuleDescriptor() 77 | 78 | when: 79 | def result = GradleRunner.create() 80 | .withProjectDir(tmpDir.root) 81 | .withDebug(true) 82 | .forwardOutput() 83 | .withArguments("check") 84 | .withPluginClasspath().build() 85 | 86 | then: 87 | result.task(":test").outcome == SUCCESS 88 | } 89 | 90 | @IgnoreIf({NOT_JAVA_9}) 91 | def "run TestNG with explicitly exported test packages"() { 92 | given: 93 | project 94 | .gradleJavaPlugin("application") 95 | .createJavaTestFile(exportedTestPackageTestWithMocks()) 96 | .exportedTestPackage("com.example.test") 97 | .createGradleBuild() 98 | .createModuleDescriptor() 99 | 100 | when: 101 | def result = GradleRunner.create() 102 | .withProjectDir(tmpDir.root) 103 | .withDebug(true) 104 | .forwardOutput() 105 | .withArguments("check") 106 | .withPluginClasspath().build() 107 | 108 | then: 109 | result.task(":test").outcome == SUCCESS 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/jigsaw/JigsawCLISpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.jigsaw 17 | 18 | import com.zyxist.chainsaw.jigsaw.cli.ExportItem 19 | import com.zyxist.chainsaw.jigsaw.cli.OpenItem 20 | import com.zyxist.chainsaw.jigsaw.cli.PatchItem 21 | import com.zyxist.chainsaw.jigsaw.cli.ReadItem 22 | import spock.lang.Specification 23 | 24 | class JigsawCLISpec extends Specification { 25 | 26 | def "should generate --module-path"() { 27 | given: 28 | def cli = new JigsawCLI('/foo/bar/joe') 29 | 30 | when: 31 | def result = cli.toString() 32 | 33 | then: 34 | result == '--module-path /foo/bar/joe' 35 | } 36 | 37 | def "should generate --module with module name and main class"() { 38 | given: 39 | def cli = new JigsawCLI('/foo/bar/joe') 40 | .module('com.example.foo', 'com.example.foo.Main') 41 | 42 | when: 43 | def result = cli.toString() 44 | 45 | then: 46 | result == '--module-path /foo/bar/joe --module com.example.foo/com.example.foo.Main' 47 | } 48 | 49 | def "should generate --module-version"() { 50 | given: 51 | def cli = new JigsawCLI('/foo/bar/joe') 52 | .version("1.2.3") 53 | 54 | when: 55 | def result = cli.toString() 56 | 57 | then: 58 | result == '--module-path /foo/bar/joe --module-version 1.2.3' 59 | } 60 | 61 | def "should generate --add-modules"() { 62 | given: 63 | def cli = new JigsawCLI('/foo/bar/joe'); 64 | cli.addModules() 65 | .add('com.example.foo') 66 | .add('com.example.bar') 67 | 68 | when: 69 | def result = cli.toString() 70 | 71 | then: 72 | result == '--module-path /foo/bar/joe --add-modules com.example.foo,com.example.bar' 73 | } 74 | 75 | def "should generate --add-exports"() { 76 | given: 77 | def cli = new JigsawCLI('/foo/bar/joe') 78 | cli.exportList() 79 | .export(new ExportItem("some.module", "some.pkg").to("other.module")) 80 | 81 | 82 | when: 83 | def result = cli.toString() 84 | 85 | then: 86 | result == '--module-path /foo/bar/joe --add-exports some.module/some.pkg=other.module' 87 | } 88 | 89 | def "should generate --add-reads"() { 90 | given: 91 | def cli = new JigsawCLI('/foo/bar/joe') 92 | cli.readList() 93 | .read(new ReadItem("some.module").to("other.module")) 94 | 95 | 96 | when: 97 | def result = cli.toString() 98 | 99 | then: 100 | result == '--module-path /foo/bar/joe --add-reads some.module=other.module' 101 | } 102 | 103 | def "should generate --add-opens"() { 104 | given: 105 | def cli = new JigsawCLI('/foo/bar/joe') 106 | cli.openList() 107 | .open(new OpenItem("some.module", "opened.module", "dst.module")) 108 | 109 | 110 | when: 111 | def result = cli.toString() 112 | 113 | then: 114 | result == '--module-path /foo/bar/joe --add-opens some.module/opened.module=dst.module' 115 | } 116 | 117 | def "should generate --patch-module"() { 118 | given: 119 | def cli = new JigsawCLI('/foo/bar/joe') 120 | cli.patchList() 121 | .patch(new PatchItem("some.module").with("/path/to/some.jar")) 122 | 123 | 124 | when: 125 | def result = cli.toString() 126 | 127 | then: 128 | result == '--module-path /foo/bar/joe --patch-module some.module=/path/to/some.jar' 129 | } 130 | 131 | def "should return the provided module path"() { 132 | given: 133 | def cli = new JigsawCLI("/foo/bar/joe") 134 | 135 | when: 136 | def result = cli.getModulePath() 137 | 138 | then: 139 | result == '/foo/bar/joe' 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/jigsaw/cli/AddModuleFlagSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.zyxist.chainsaw.jigsaw.cli 2 | 3 | import spock.lang.Specification 4 | 5 | class AddModuleFlagSpec extends Specification { 6 | 7 | def "should not generate any CLI flag, if no extra modules are specified"() { 8 | given: 9 | def it = new AddModuleFlag() 10 | def args = [] 11 | 12 | when: 13 | it.toArgs(args) 14 | 15 | then: 16 | args.isEmpty() 17 | } 18 | 19 | def "should handle a case, when one extra module is provided"() { 20 | given: 21 | def it = new AddModuleFlag() 22 | .add('com.example.foo') 23 | def args = [] 24 | 25 | when: 26 | it.toArgs(args) 27 | 28 | then: 29 | args.size() == 2 30 | args.get(0) == '--add-modules' 31 | args.get(1) == 'com.example.foo' 32 | } 33 | 34 | def "should handle a case, when two or more extra modules are provided"() { 35 | given: 36 | def it = new AddModuleFlag() 37 | .add('com.example.foo') 38 | .add('com.example.bar') 39 | def args = [] 40 | 41 | when: 42 | it.toArgs(args) 43 | 44 | then: 45 | args.size() == 2 46 | args.get(0) == '--add-modules' 47 | args.get(1) == 'com.example.foo,com.example.bar' 48 | } 49 | 50 | def "should add multiple module names from a collection, using addAll()"() { 51 | given: 52 | def it = new AddModuleFlag() 53 | def args = [] 54 | 55 | when: 56 | it.addAll(['com.example.foo', 'com.example.bar']) 57 | it.toArgs(args) 58 | 59 | then: 60 | args.size() == 2 61 | args.get(0) == '--add-modules' 62 | args.get(1) == 'com.example.foo,com.example.bar' 63 | } 64 | 65 | def "should merge two AddModuleFlag instances, using merge()"() { 66 | given: 67 | def it = new AddModuleFlag().add('com.example.foo') 68 | def other = new AddModuleFlag().add('com.example.bar') 69 | def args = [] 70 | 71 | when: 72 | it.merge(other) 73 | it.toArgs(args) 74 | 75 | then: 76 | args.size() == 2 77 | args.get(0) == '--add-modules' 78 | args.get(1) == 'com.example.foo,com.example.bar' 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/jigsaw/cli/ExportItemSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.zyxist.chainsaw.jigsaw.cli 2 | 3 | import spock.lang.Specification 4 | 5 | class ExportItemSpec extends Specification { 6 | def "should not generate any CLI flag, if no exported modules are specified"() { 7 | given: 8 | def it = new ExportItem('com.example.foo', 'com.example.foo.pkg') 9 | def args = new LinkedList() 10 | 11 | when: 12 | it.toArgs(args) 13 | 14 | then: 15 | args.isEmpty() 16 | } 17 | 18 | def "should correctly handle a case, when one module is exported"() { 19 | given: 20 | def it = new ExportItem('com.example.foo', 'com.example.foo.pkg') 21 | .to('com.example.bar') 22 | def args = new LinkedList() 23 | 24 | when: 25 | it.toArgs(args) 26 | 27 | then: 28 | args.size() == 2 29 | args.get(0) == '--add-exports' 30 | args.get(1) == 'com.example.foo/com.example.foo.pkg=com.example.bar' 31 | } 32 | 33 | def "should correctly handle a case, when two or more modules are exported"() { 34 | given: 35 | def it = new ExportItem('com.example.foo', 'com.example.foo.pkg') 36 | .to('com.example.bar') 37 | .to('com.example.joe') 38 | def args = new LinkedList() 39 | 40 | when: 41 | it.toArgs(args) 42 | 43 | then: 44 | args.size() == 2 45 | args.get(0) == '--add-exports' 46 | args.get(1) == 'com.example.foo/com.example.foo.pkg=com.example.bar,com.example.joe' 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/jigsaw/cli/OpenItemSpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.jigsaw.cli 17 | 18 | import spock.lang.Specification 19 | 20 | class OpenItemSpec extends Specification { 21 | def "should correctly handle a case, when one module is opened"() { 22 | given: 23 | def it = new OpenItem('java.base', 'java.lang', 'my.module') 24 | def args = new LinkedList() 25 | 26 | when: 27 | it.toArgs(args) 28 | 29 | then: 30 | it.openedModule == 'java.base' 31 | it.openedPackage == 'java.lang' 32 | it.destinationModule == 'my.module' 33 | args.size() == 2 34 | args.get(0) == '--add-opens' 35 | args.get(1) == 'java.base/java.lang=my.module' 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/jigsaw/cli/PatchItemSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.zyxist.chainsaw.jigsaw.cli 2 | 3 | import spock.lang.Specification 4 | 5 | class PatchItemSpec extends Specification { 6 | 7 | def "should not generate a CLI flag, if no JARs are specified"() { 8 | given: 9 | def it = new PatchItem('com.example.foo') 10 | def args = new LinkedList() 11 | 12 | when: 13 | it.toArgs(args) 14 | 15 | then: 16 | args.isEmpty() 17 | } 18 | 19 | def "should correcly handle a case, when a single JAR is specified"() { 20 | given: 21 | def it = new PatchItem('com.example.foo').with('/path/to/some.jar') 22 | def args = new LinkedList() 23 | 24 | when: 25 | it.toArgs(args) 26 | 27 | then: 28 | args.size() == 2 29 | args.get(0) == '--patch-module' 30 | args.get(1) == 'com.example.foo=/path/to/some.jar' 31 | } 32 | 33 | def "should correctly handle a case, when two or more JARs are specified"() { 34 | given: 35 | def it = new PatchItem('com.example.foo') 36 | .with('/path/to/some.jar') 37 | .with('/path/to/another.jar') 38 | def args = new LinkedList() 39 | 40 | when: 41 | it.toArgs(args) 42 | 43 | then: 44 | args.size() == 2 45 | args.get(0) == '--patch-module' 46 | args.get(1) == 'com.example.foo=/path/to/some.jar:/path/to/another.jar' 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/jigsaw/cli/ReadItemSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.zyxist.chainsaw.jigsaw.cli 2 | 3 | import spock.lang.Specification 4 | 5 | class ReadItemSpec extends Specification { 6 | def "should not generate any CLI flag, if no modules opened for reading"() { 7 | given: 8 | def it = new ReadItem('com.example.foo') 9 | def args = new LinkedList() 10 | 11 | when: 12 | it.toArgs(args) 13 | 14 | then: 15 | args.isEmpty() 16 | } 17 | 18 | def "should correctly handle a case, when one module is opened for reading"() { 19 | given: 20 | def it = new ReadItem('com.example.foo') 21 | .to('com.example.bar') 22 | def args = new LinkedList() 23 | 24 | when: 25 | it.toArgs(args) 26 | 27 | then: 28 | args.size() == 2 29 | args.get(0) == '--add-reads' 30 | args.get(1) == 'com.example.foo=com.example.bar' 31 | } 32 | 33 | def "should correctly handle a case, when two or more modules are opened for reading"() { 34 | given: 35 | def it = new ReadItem('com.example.foo') 36 | .to('com.example.bar') 37 | .to('com.example.joe') 38 | def args = new LinkedList() 39 | 40 | when: 41 | it.toArgs(args) 42 | 43 | then: 44 | args.size() == 2 45 | args.get(0) == '--add-reads' 46 | args.get(1) == 'com.example.foo=com.example.bar,com.example.joe' 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/groovy/com/zyxist/chainsaw/tasks/VerifyModuleNameTaskSpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyxist.chainsaw.tasks 17 | 18 | import com.zyxist.chainsaw.builder.JigsawProjectBuilder 19 | import org.gradle.testkit.runner.GradleRunner 20 | import org.junit.Rule 21 | import org.junit.rules.TemporaryFolder 22 | import spock.lang.IgnoreIf 23 | import spock.lang.Specification 24 | 25 | import static com.zyxist.chainsaw.builder.factory.RunnableJavaClassFactory.runnableJavaClass 26 | import static org.gradle.testkit.runner.TaskOutcome.FAILED 27 | import static org.gradle.testkit.runner.TaskOutcome.SUCCESS 28 | 29 | class VerifyModuleNameTaskSpec extends Specification { 30 | 31 | static final NOT_JAVA_9 = !System.getProperty("java.version").startsWith("9") 32 | 33 | @Rule 34 | final TemporaryFolder tmpDir = new TemporaryFolder() 35 | 36 | JigsawProjectBuilder project 37 | 38 | def configureProjectWithModule(String moduleName, boolean violations) { 39 | project = new JigsawProjectBuilder(tmpDir) 40 | project 41 | .moduleName(moduleName) 42 | .allowNameViolations(violations) 43 | .packageName("com.example") 44 | .exportedPackage("com.example") 45 | .gradleJavaPlugin("application") 46 | .mainClass("com.example.AClass") 47 | .createJavaFile(runnableJavaClass()) 48 | .createModuleDescriptor() 49 | .createGradleBuild() 50 | } 51 | 52 | @IgnoreIf({NOT_JAVA_9}) 53 | def "succeeds for module named from the root package"() { 54 | given: 55 | configureProjectWithModule('com.example', false) 56 | 57 | when: 58 | def result = GradleRunner.create() 59 | .withProjectDir(project.root) 60 | .withDebug(true) 61 | .forwardOutput() 62 | .withArguments("verifyModuleName") 63 | .withPluginClasspath().build() 64 | 65 | then: 66 | result.task(":verifyModuleName").outcome == SUCCESS 67 | } 68 | 69 | @IgnoreIf({NOT_JAVA_9}) 70 | def "fails for module with custom naming"() { 71 | given: 72 | configureProjectWithModule('test.module', false) 73 | 74 | when: 75 | def result = GradleRunner.create() 76 | .withProjectDir(project.root) 77 | .withDebug(true) 78 | .forwardOutput() 79 | .withArguments("verifyModuleName") 80 | .withPluginClasspath() 81 | .buildAndFail() 82 | 83 | then: 84 | result.output.contains("The module name 'test.module' does not follow the official module naming convention for Java (reverse-DNS style, derived from the root package).") 85 | result.task(":verifyModuleName").outcome == FAILED 86 | } 87 | 88 | @IgnoreIf({NOT_JAVA_9}) 89 | def "prints a message, if violations have been allowed"() { 90 | given: 91 | configureProjectWithModule('test.module', true) 92 | 93 | when: 94 | def result = GradleRunner.create() 95 | .withProjectDir(project.root) 96 | .withDebug(true) 97 | .forwardOutput() 98 | .withArguments("verifyModuleName") 99 | .withPluginClasspath() 100 | .build() 101 | 102 | then: 103 | result.output.contains("The module name 'test.module' does not follow the official module naming convention for Java (reverse-DNS style, derived from the root package).") 104 | result.task(":verifyModuleName").outcome == SUCCESS 105 | } 106 | } 107 | --------------------------------------------------------------------------------