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