Skip to content

Commit

Permalink
validate all profile activation config strings
Browse files Browse the repository at this point in the history
  • Loading branch information
mbenson committed Apr 26, 2024
1 parent 9a30e2b commit 714ec6b
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,24 @@

import java.io.File;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.apache.maven.model.Activation;
import org.apache.maven.model.ActivationFile;
import org.apache.maven.model.Build;
import org.apache.maven.model.BuildBase;
import org.apache.maven.model.Dependency;
Expand Down Expand Up @@ -236,42 +243,97 @@ public void validateRawModel(Model m, ModelBuildingRequest request, ModelProblem
}

private void validate30RawProfileActivation(ModelProblemCollector problems, Activation activation, String prefix) {
if (activation == null || activation.getFile() == null) {
if (activation == null) {
return;
}
class ActivationFrame {
String location;
Optional<? extends InputLocationTracker> parent;

ActivationFile file = activation.getFile();

String path;
String location;

if (file.getExists() != null && !file.getExists().isEmpty()) {
path = file.getExists();
location = "exists";
} else if (file.getMissing() != null && !file.getMissing().isEmpty()) {
path = file.getMissing();
location = "missing";
} else {
return;
ActivationFrame(String location, Optional<? extends InputLocationTracker> parent) {
this.location = location;
this.parent = parent;
}
}

if (hasProjectExpression(path)) {
Matcher matcher = EXPRESSION_PROJECT_NAME_PATTERN.matcher(path);
while (matcher.find()) {
String propertyName = matcher.group(0);
if (!"${project.basedir}".equals(propertyName)) {
final Deque<ActivationFrame> stk = new LinkedList<>();

final Supplier<String> pathSupplier = () -> {
final boolean parallel = false;
return StreamSupport.stream(((Iterable<ActivationFrame>) stk::descendingIterator).spliterator(), parallel)
.map(f -> f.location)
.collect(Collectors.joining("."));
};
final Supplier<InputLocation> locationSupplier = () -> {
if (stk.size() < 2) {
return null;
}
Iterator<ActivationFrame> f = stk.iterator();

String location = f.next().location;
ActivationFrame parent = f.next();

return parent.parent.map(p -> p.getLocation(location)).orElse(null);
};
final Consumer<String> validator = s -> {
if (hasProjectExpression(s)) {
String path = pathSupplier.get();
Matcher matcher = EXPRESSION_PROJECT_NAME_PATTERN.matcher(s);
while (matcher.find()) {
String propertyName = matcher.group(0);

if (path.startsWith("activation.file.") && "${project.basedir}".equals(propertyName)) {
continue;
}
addViolation(
problems,
Severity.WARNING,
Version.V30,
prefix + "activation.file." + location,
prefix + path,
null,
"Failed to interpolate file location " + path + ": " + propertyName
"Failed to interpolate profile activation property " + s + ": " + propertyName
+ " expressions are not supported during profile activation.",
file.getLocation(location));
locationSupplier.get());
}
}
}
};
Optional<Activation> root = Optional.of(activation);
stk.push(new ActivationFrame("activation", root));
root.map(Activation::getFile).ifPresent(fa -> {
stk.push(new ActivationFrame("file", Optional.of(fa)));
stk.push(new ActivationFrame("exists", Optional.empty()));
validator.accept(fa.getExists());
stk.peek().location = "missing";
validator.accept(fa.getMissing());
stk.pop();
stk.pop();
});
root.map(Activation::getOs).ifPresent(oa -> {
stk.push(new ActivationFrame("os", Optional.of(oa)));
stk.push(new ActivationFrame("arch", Optional.empty()));
validator.accept(oa.getArch());
stk.peek().location = "family";
validator.accept(oa.getFamily());
stk.peek().location = "name";
validator.accept(oa.getName());
stk.peek().location = "version";
validator.accept(oa.getVersion());
stk.pop();
stk.pop();
});
root.map(Activation::getProperty).ifPresent(pa -> {
stk.push(new ActivationFrame("property", Optional.of(pa)));
stk.push(new ActivationFrame("name", Optional.empty()));
validator.accept(pa.getName());
stk.peek().location = "value";
validator.accept(pa.getValue());
stk.pop();
stk.pop();
});
root.map(Activation::getJdk).ifPresent(jdk -> {
stk.push(new ActivationFrame("jdk", Optional.empty()));
validator.accept(jdk);
stk.pop();
});
}

private void validate20RawPlugins(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import java.util.function.UnaryOperator;

import junit.framework.TestCase;
Expand Down Expand Up @@ -761,24 +762,64 @@ public void testParentVersionRELEASE() throws Exception {
result.getWarnings().get(0));
}

public void testProfileActivationWithAllowedExpression() throws Exception {
SimpleProblemCollector result = validateRaw("raw-model/profile-activation-file-with-allowed-expressions.xml");
public void repositoryWithExpression() throws Exception {
SimpleProblemCollector result = validateRaw("raw-model/repository-with-expression.xml");
assertViolations(result, 0, 1, 0);
assertEquals(
"'repositories.repository.[repo].url' contains an expression but should be a constant.",
result.getErrors().get(0));
}

public void repositoryWithBasedirExpression() throws Exception {
SimpleProblemCollector result = validateRaw("raw-model/repository-with-basedir-expression.xml");
assertViolations(result, 0, 0, 0);
}

public void testProfileActivationWithProjectExpression() throws Exception {
public void profileActivationWithAllowedExpression() throws Exception {
SimpleProblemCollector result = validateRaw(
"raw-model/profile-activation-file-with-allowed-expressions.xml",
mbr -> mbr.setUserProperties(new Properties() {
private static final long serialVersionUID = 1L;

{
setProperty("foo", "foo");
setProperty("bar", "foo");
}
}));
assertViolations(result, 0, 0, 0);
}

public void profileActivationFileWithProjectExpression() throws Exception {
SimpleProblemCollector result = validateRaw("raw-model/profile-activation-file-with-project-expressions.xml");
assertViolations(result, 0, 0, 2);

assertEquals(
"'profiles.profile[exists-project-version].activation.file.exists' "
+ "Failed to interpolate file location ${project.version}/test.txt: "
+ "Failed to interpolate profile activation property ${project.version}/test.txt: "
+ "${project.version} expressions are not supported during profile activation.",
result.getWarnings().get(0));

assertEquals(
"'profiles.profile[missing-project-version].activation.file.missing' "
+ "Failed to interpolate file location ${project.version}/test.txt: "
+ "Failed to interpolate profile activation property ${project.version}/test.txt: "
+ "${project.version} expressions are not supported during profile activation.",
result.getWarnings().get(1));
}

public void profileActivationPropertyWithProjectExpression() throws Exception {
SimpleProblemCollector result =
validateRaw("raw-model/profile-activation-property-with-project-expressions.xml");
assertViolations(result, 0, 0, 2);

assertEquals(
"'profiles.profile[property-name-project-version].activation.property.name' "
+ "Failed to interpolate profile activation property ${project.version}: "
+ "${project.version} expressions are not supported during profile activation.",
result.getWarnings().get(0));

assertEquals(
"'profiles.profile[property-value-project-version].activation.property.value' "
+ "Failed to interpolate profile activation property ${project.version}: "
+ "${project.version} expressions are not supported during profile activation.",
result.getWarnings().get(1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,23 @@ under the License.
</activation>
</profile>

<profile>
<id>dynamic-property-available</id>
<activation>
<property>
<name>${activationProperty}</name>
</property>
</activation>
</profile>

<profile>
<id>matches-another-property</id>
<activation>
<property>
<name>foo</name>
<value>${bar}</value>
</property>
</activation>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
-->
<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>
<artifactId>aid</artifactId>
<groupId>gid</groupId>
<version>0.1</version>
<packaging>pom</packaging>

<profiles>

<profile>
<id>property-name-project-version</id>
<activation>
<property>
<name>${project.version}</name>
</property>
</activation>
</profile>
<profile>
<id>property-value-project-version</id>
<activation>
<property>
<name>project.version</name>
<value>${project.version}</value>
</property>
</activation>
</profile>

</profiles>
</project>

0 comments on commit 714ec6b

Please sign in to comment.