1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.api.plugin.testing;
20
21 import java.io.File;
22 import java.util.Map;
23
24 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
25 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
26 import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
27 import org.eclipse.aether.repository.LocalRepository;
28
29
30
31
32
33
34 public class ResolverExpressionEvaluatorStub implements TypeAwareExpressionEvaluator {
35
36 private final Map<String, Object> properties;
37
38 public ResolverExpressionEvaluatorStub(Map<String, Object> properties) {
39 this.properties = properties;
40 }
41
42
43 @Override
44 public Object evaluate(String expr) throws ExpressionEvaluationException {
45 return evaluate(expr, null);
46 }
47
48
49 @Override
50 public Object evaluate(String expr, Class<?> type) throws ExpressionEvaluationException {
51
52 Object value = null;
53
54 if (expr == null) {
55 return null;
56 }
57
58 String expression = stripTokens(expr);
59
60 if (expression.equals(expr)) {
61 int index = expr.indexOf("${");
62 if (index >= 0) {
63 int lastIndex = expr.indexOf("}", index);
64 if (lastIndex >= 0) {
65 String retVal = expr.substring(0, index);
66
67 if (index > 0 && expr.charAt(index - 1) == '$') {
68 retVal += expr.substring(index + 1, lastIndex + 1);
69 } else {
70 retVal += evaluate(expr.substring(index, lastIndex + 1));
71 }
72
73 retVal += evaluate(expr.substring(lastIndex + 1));
74 return retVal;
75 }
76 }
77
78
79 return expression.contains("$$") ? expression.replaceAll("\\$\\$", "\\$") : expression;
80 } else {
81 if ("basedir".equals(expression) || "project.basedir".equals(expression)) {
82 value = MojoExtension.getBasedir();
83 } else if (expression.startsWith("basedir") || expression.startsWith("project.basedir")) {
84 int pathSeparator = expression.indexOf("/");
85 if (pathSeparator > 0) {
86 value = MojoExtension.getBasedir() + expression.substring(pathSeparator);
87 }
88 } else if ("localRepository".equals(expression)) {
89 File localRepo = new File(MojoExtension.getBasedir(), "target/local-repo");
90 return new LocalRepository("file://" + localRepo.getAbsolutePath());
91 }
92 if (value == null && properties != null && properties.containsKey(expression)) {
93 value = properties.get(expression);
94 }
95 return value;
96 }
97 }
98
99 private String stripTokens(String expr) {
100 if (expr.startsWith("${") && expr.indexOf("}") == expr.length() - 1) {
101 expr = expr.substring(2, expr.length() - 1);
102 }
103
104 return expr;
105 }
106
107
108 @Override
109 public File alignToBaseDirectory(File file) {
110 if (file.getAbsolutePath().startsWith(MojoExtension.getBasedir())) {
111 return file;
112 } else if (file.isAbsolute()) {
113 return file;
114 } else {
115 return new File(MojoExtension.getBasedir(), file.getPath());
116 }
117 }
118 }