Skip to content

Commit

Permalink
Added test for pom profile filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Strum355 committed Sep 7, 2020
1 parent c886d1f commit 7e0ceca
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/spoon/support/compiler/SpoonPom.java
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ public File getFileSystemParent() {

@Override
public String getName() {
return "pom";
return model.getName();
}

@Override
Expand Down
23 changes: 22 additions & 1 deletion src/test/java/spoon/support/compiler/SpoonPomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import spoon.MavenLauncher;
import spoon.support.StandardEnvironment;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

import static org.junit.Assert.*;

Expand All @@ -30,4 +31,24 @@ public void checkVersion(String path, int expected) throws IOException, XmlPullP
//contract: Java version is read accurately from pom and does not trigger exceptions
assertEquals(expected, version);
}

@Test
public void getModuleNames() throws IOException, XmlPullParserException {
String[] expected = {"always"};
checkProfilesModules("src/test/resources/maven-launcher/profiles", expected, Pattern.compile("^$"));
String[] expected1 = {"profile-only", "always"};
checkProfilesModules("src/test/resources/maven-launcher/profiles", expected1, Pattern.compile(".+"));
}

public void checkProfilesModules(String path, String[] expected, Pattern profileFilter) throws IOException, XmlPullParserException {
SpoonPom pomModel = new SpoonPom(path, null, MavenLauncher.SOURCE_TYPE.APP_SOURCE, new StandardEnvironment(), profileFilter);
List<SpoonPom> modules = pomModel.getModules();

assertEquals(expected.length, modules.size());

// contract: modules declared in profiles that don't match the profileFilter are not included
for (int i = 0; i < modules.size(); i++) {
assertEquals(expected[i], modules.get(i).getName());
}
}
}
15 changes: 15 additions & 0 deletions src/test/resources/maven-launcher/profiles/always/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.foo.bar</groupId>
<artifactId>always</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>always</name>

<build>
<sourceDirectory>source/code</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
</build>
</project>
23 changes: 23 additions & 0 deletions src/test/resources/maven-launcher/profiles/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.foo.bar</groupId>
<artifactId>foobar</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>foobar</name>

<modules>
<module>always</module>
</modules>

<profiles>
<profile>
<id>test</id>
<modules>
<module>profile-only</module>
</modules>
</profile>
</profiles>
</project>
15 changes: 15 additions & 0 deletions src/test/resources/maven-launcher/profiles/profile-only/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.foo.bar</groupId>
<artifactId>profile-only</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>profile-only</name>

<build>
<sourceDirectory>source/code</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
</build>
</project>

0 comments on commit 7e0ceca

Please sign in to comment.