Polish Maven profile support

This commit is contained in:
Stephane Nicoll
2021-01-19 16:30:57 +01:00
parent 50c1c28b1c
commit 6da6572ccc
4 changed files with 62 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@@ -19,6 +19,7 @@ package io.spring.initializr.generator.test.buildsystem.maven;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.util.function.Predicate;
import io.spring.initializr.generator.test.io.AbstractTextAssert;
import io.spring.initializr.generator.test.io.NodeAssert;
@@ -310,6 +311,27 @@ public class MavenBuildAssert extends AbstractTextAssert<MavenBuildAssert> {
return this;
}
/**
* Assert {@code pom.xml} defines a profile with the specified {@code id}.
* @param id the id of the profile
* @return {@code this} assertion object
*/
public MavenBuildAssert hasProfile(String id) {
this.pom.nodesAtPath("/project/profiles/profile").areExactly(1,
new Condition<>(profile(id), "matching profile"));
return this;
}
/**
* Assert {@code pom.xml} does not define a profile with the specified {@code id}.
* @param id the id of the profile
* @return {@code this} assertion object
*/
public MavenBuildAssert doesNotHaveProfile(String id) {
this.pom.nodesAtPath("/project/profiles/profile").noneMatch(profile(id));
return this;
}
/**
* Assert {@code pom.xml} does not define a node with the specified {@code path}.
* @param path the path of the node
@@ -421,4 +443,11 @@ public class MavenBuildAssert extends AbstractTextAssert<MavenBuildAssert> {
return repository;
}
private static Predicate<? super Node> profile(String id) {
return (candidate) -> {
String actualId = ((Element) candidate).getElementsByTagName("id").item(0).getTextContent();
return (actualId.equals(id));
};
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@@ -324,6 +324,17 @@ class MavenBuildAssertTests {
"Acme Milestones", "https://repo.example.com/milestone", true));
}
@Test
void hasProfile() {
assertThat(forMavenBuild("sample-profiles-pom.xml")).hasProfile("one");
}
@Test
void hasProfileWithUnknownId() {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(forMavenBuild("sample-profiles-pom.xml")).hasProfile("unknown"));
}
private AssertProvider<MavenBuildAssert> forSampleMavenBuild() {
return forMavenBuild("sample-pom.xml");
}

View File

@@ -0,0 +1,19 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<profiles>
<profile>
<id>one</id>
</profile>
<profile>
<id>two</id>
</profile>
</profiles>
</project>