View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Stub for {@link ExpressionEvaluator}
33   *
34   * @author jesse
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      /** {@inheritDoc} */
45      @Override
46      public Object evaluate(String expr) throws ExpressionEvaluationException {
47          return evaluate(expr, null);
48      }
49  
50      /** {@inheritDoc} */
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              // Was not an expression
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     /** {@inheritDoc} */
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 }