forked from gradle/gradle-java-modules
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
334 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/test/groovy/com/zyxist/chainsaw/builder/factory/JUnit5SampleResourceTestFactory.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.zyxist.chainsaw.builder.factory | ||
|
||
import com.zyxist.chainsaw.builder.JavaCodeFactory | ||
import com.zyxist.chainsaw.builder.JigsawProjectBuilder | ||
|
||
class JUnit5SampleResourceTestFactory implements JavaCodeFactory { | ||
private boolean useModuleLoader | ||
|
||
static def junit5TestAccessingResources() { | ||
def factory = new JUnit5SampleResourceTestFactory() | ||
factory.useModuleLoader = false | ||
return factory | ||
} | ||
|
||
static def junit5TestAccessingModularResources() { | ||
def factory = new JUnit5SampleResourceTestFactory() | ||
factory.useModuleLoader = true | ||
return factory | ||
} | ||
|
||
@Override | ||
String getFilename(JigsawProjectBuilder builder) { | ||
return builder.getPackageName().replace(".", "/") + "/SampleResourceTest.java" | ||
} | ||
|
||
@Override | ||
String generateCode(JigsawProjectBuilder builder) { | ||
def loadingCode = useModuleLoader ? getModularLoadingCode() : getClasspathLoadingCode() | ||
return """ | ||
package ${builder.getPackageName()}; | ||
import java.util.Properties; | ||
import java.io.IOException; | ||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
public class SampleResourceTest { | ||
@Test | ||
public void readsTestResources() throws IOException { | ||
Properties props = new Properties(); | ||
props.load($loadingCode); | ||
assertEquals("Hello world", props.get("property")); | ||
} | ||
} | ||
""" | ||
} | ||
|
||
private String getClasspathLoadingCode() { | ||
return "getClass().getClassLoader().getResourceAsStream(\"text-resource.properties\")" | ||
} | ||
|
||
private String getModularLoadingCode() { | ||
return "getClass().getModule().getResourceAsStream(\"text-resource.properties\")" | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/test/groovy/com/zyxist/chainsaw/builder/factory/RunnableResourceClassFactory.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.zyxist.chainsaw.builder.factory | ||
|
||
import com.zyxist.chainsaw.builder.JavaCodeFactory | ||
import com.zyxist.chainsaw.builder.JigsawProjectBuilder | ||
|
||
class RunnableResourceClassFactory implements JavaCodeFactory { | ||
private boolean useModuleLoader | ||
|
||
static def runnableResourceClasspathClass() { | ||
def factory = new RunnableResourceClassFactory() | ||
factory.useModuleLoader = false | ||
return factory | ||
} | ||
|
||
static def runnableResourceModularClass() { | ||
def factory = new RunnableResourceClassFactory() | ||
factory.useModuleLoader = true | ||
return factory | ||
} | ||
|
||
@Override | ||
String getFilename(JigsawProjectBuilder builder) { | ||
return builder.getPackageName().replace(".", "/") + "/ResourceClass.java" | ||
} | ||
|
||
@Override | ||
String generateCode(JigsawProjectBuilder builder) { | ||
def loadingCode = useModuleLoader ? getModularLoadingCode() : getClasspathLoadingCode() | ||
return """ | ||
package ${builder.getPackageName()}; | ||
import java.util.Properties; | ||
import java.io.IOException; | ||
public class ResourceClass { | ||
public static void main(String... args) throws IOException { | ||
Properties props = new Properties(); | ||
props.load($loadingCode); | ||
System.out.println(props.get("property")); | ||
} | ||
} | ||
""" | ||
} | ||
|
||
private String getClasspathLoadingCode() { | ||
return "ResourceClass.class.getClassLoader().getResourceAsStream(\"text-resource.properties\")" | ||
} | ||
|
||
private String getModularLoadingCode() { | ||
return "ResourceClass.class.getModule().getResourceAsStream(\"text-resource.properties\")" | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/test/groovy/com/zyxist/chainsaw/builder/factory/SampleTextResourceFactory.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.zyxist.chainsaw.builder.factory | ||
|
||
import com.zyxist.chainsaw.builder.JavaCodeFactory | ||
import com.zyxist.chainsaw.builder.JigsawProjectBuilder | ||
|
||
class SampleTextResourceFactory implements JavaCodeFactory { | ||
private boolean inPackage = false | ||
|
||
static SampleTextResourceFactory sampleTextResource() { | ||
return new SampleTextResourceFactory() | ||
} | ||
|
||
static SampleTextResourceFactory samplePackagedTextResource() { | ||
def factory = new SampleTextResourceFactory() | ||
factory.inPackage = true | ||
return factory | ||
} | ||
|
||
@Override | ||
String getFilename(JigsawProjectBuilder builder) { | ||
return (inPackage ? builder.getPackageName().replace(".", "/") + "/" : "") + "text-resource.properties" | ||
} | ||
|
||
@Override | ||
String generateCode(JigsawProjectBuilder builder) { | ||
return """ | ||
property=Hello world | ||
""" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.