Skip to content

Commit 9606a2e

Browse files
committed
SHRINKRES-355 - Fix artifact selection in EmbeddedGradleImporter and add test for it.
1 parent 1c9d635 commit 9606a2e

File tree

5 files changed

+106
-4
lines changed

5 files changed

+106
-4
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apply plugin: 'war'
2+
apply plugin: 'java'
3+
4+
group = 'org.jboss.shrinkwrap.resolver.test'
5+
version = '1.0.0'
6+
description = "ShrinkWrap Resolver Gradle Test: Ambiguous Sample (JAR + WAR)"
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
// Minimal dependencies
13+
dependencies {
14+
testImplementation group: 'junit', name: 'junit', version: '4.12'
15+
}
16+
17+
// Force generation of the JAR to create the ambiguity (JAR vs WAR)
18+
jar {
19+
enabled = true
20+
}
21+
22+
// Ensure JAR is built alongside the WAR
23+
assemble.dependsOn jar
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<web-app>
2+
<distributable/>
3+
</web-app>

gradle/impl-gradle-embedded-archive/src/main/java/org/jboss/shrinkwrap/impl/gradle/archive/importer/embedded/EmbeddedGradleImporterImpl.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
import org.jboss.shrinkwrap.api.gradle.archive.importer.embedded.DistributionConfigurationStage;
3333
import org.jboss.shrinkwrap.api.gradle.archive.importer.embedded.EmbeddedGradleImporter;
3434
import org.jboss.shrinkwrap.api.importer.ZipImporter;
35+
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
36+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
37+
import org.jboss.shrinkwrap.api.spec.ResourceAdapterArchive;
38+
import org.jboss.shrinkwrap.api.spec.WebArchive;
3539
import org.jboss.shrinkwrap.impl.base.Validate;
3640

3741
/**
@@ -100,14 +104,14 @@ public DistributionConfigurationStage forThisProjectDirectory() {
100104
public <TYPE extends Assignable> TYPE as(final Class<TYPE> clazz) {
101105
final File result;
102106
if (buildResult == null) {
103-
result = importFromDefaultLibsDirectory();
107+
result = importFromDefaultLibsDirectory(clazz);
104108
} else {
105109
result = importFromConfiguredPath();
106110
}
107111
return ShrinkWrap.create(ZipImporter.class, archive.getName()).importFrom(result).as(clazz);
108112
}
109113

110-
private File importFromDefaultLibsDirectory() {
114+
private File importFromDefaultLibsDirectory(final Class<? extends Assignable> clazz) {
111115
final GradleProject currentGradleProject = findCurrentGradleProject();
112116
final File buildDir = currentGradleProject.getBuildDirectory();
113117
final File libsDir = new File(buildDir, "libs");
@@ -118,6 +122,27 @@ private File importFromDefaultLibsDirectory() {
118122
"Wrong project directory is used. Tests have to be run from working directory which is a current sub-module directory.");
119123
}
120124

125+
final String requiredExtension;
126+
if (WebArchive.class.isAssignableFrom(clazz)) {
127+
requiredExtension = ".war";
128+
} else if (EnterpriseArchive.class.isAssignableFrom(clazz)) {
129+
requiredExtension = ".ear";
130+
} else if (ResourceAdapterArchive.class.isAssignableFrom(clazz)) {
131+
requiredExtension = ".rar";
132+
} else if (JavaArchive.class.isAssignableFrom(clazz)) {
133+
requiredExtension = ".jar";
134+
} else {
135+
requiredExtension = null;
136+
}
137+
138+
if (requiredExtension != null) {
139+
for (File file : results) {
140+
if (file.getName().endsWith(requiredExtension)) {
141+
return file;
142+
}
143+
}
144+
}
145+
121146
return results[0];
122147
}
123148

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2014, Red Hat Middleware LLC, and individual contributors
4+
* by the @authors tag. See the copyright.txt in the distribution for a
5+
* full listing of individual contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.jboss.shrinkwrap.impl.gradle.archive.importer.embedded;
19+
20+
import org.jboss.shrinkwrap.api.ShrinkWrap;
21+
import org.jboss.shrinkwrap.api.gradle.archive.importer.embedded.EmbeddedGradleImporter;
22+
import org.jboss.shrinkwrap.api.spec.WebArchive;
23+
import org.junit.jupiter.api.Test;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* Regression test for SHRINKRES-355.
29+
* Verifies that the importer picks the correct artifact (.war) even when a .jar is present.
30+
*/
31+
public class MultipleArtifactsEmbeddedGradleImporterTestCase {
32+
33+
@Test
34+
void shouldImportWarWhenJarIsAlsoPresent() {
35+
final String dir = "src/it/multiple-artifacts-sample/";
36+
37+
final WebArchive webArchive = ShrinkWrap.create(EmbeddedGradleImporter.class)
38+
.forProjectDirectory(dir)
39+
.importBuildOutput()
40+
.as(WebArchive.class);
41+
42+
// DEBUG PRINT to see what's inside
43+
System.out.println("ARCHIVE CONTENT:\n" + webArchive.toString(true));
44+
45+
// CORRECT ASSERTION:
46+
// A WAR file must contain the WEB-INF directory.
47+
// A JAR file (which is the wrong file) will NOT contain WEB-INF.
48+
assertThat(webArchive.contains("WEB-INF"))
49+
.as("The imported archive should be a WAR and contain WEB-INF")
50+
.isTrue();
51+
}
52+
}

gradle/impl-gradle-embedded-archive/src/test/java/org/jboss/shrinkwrap/impl/gradle/archive/importer/embedded/WarEmbeddedGradleImporterTestCase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,5 @@ void should() {
4141
AssertArchive.assertContains(webArchive, "WEB-INF/classes/main.properties");
4242
AssertArchive.assertContains(webArchive, "WEB-INF/web.xml");
4343
AssertArchive.assertNotContains(webArchive, "file.toExclude");
44-
assertThat(webArchive.getContent().size()).isEqualTo(12);
45-
}
44+
assertThat(webArchive.getContent().size()).isEqualTo(12);}
4645
}

0 commit comments

Comments
 (0)