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.plugin.testing;
20  
21  import java.io.File;
22  
23  import org.apache.maven.artifact.repository.MavenArtifactRepository;
24  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
25  import org.codehaus.plexus.PlexusTestCase;
26  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
27  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
28  
29  /**
30   * Stub for {@link ExpressionEvaluator}
31   *
32   * @author jesse
33   */
34  public class ResolverExpressionEvaluatorStub implements ExpressionEvaluator {
35      /** {@inheritDoc} */
36      @Override
37      public Object evaluate(String expr) throws ExpressionEvaluationException {
38  
39          Object value = null;
40  
41          if (expr == null) {
42              return null;
43          }
44  
45          String expression = stripTokens(expr);
46  
47          if (expression.equals(expr)) {
48              int index = expr.indexOf("${");
49              if (index >= 0) {
50                  int lastIndex = expr.indexOf("}", index);
51                  if (lastIndex >= 0) {
52                      String retVal = expr.substring(0, index);
53  
54                      if (index > 0 && expr.charAt(index - 1) == '$') {
55                          retVal += expr.substring(index + 1, lastIndex + 1);
56                      } else {
57                          retVal += evaluate(expr.substring(index, lastIndex + 1));
58                      }
59  
60                      retVal += evaluate(expr.substring(lastIndex + 1));
61                      return retVal;
62                  }
63              }
64  
65              // Was not an expression
66              if (expression.indexOf("$$") > -1) {
67                  return expression.replaceAll("\\$\\$", "\\$");
68              }
69          }
70  
71          if ("basedir".equals(expression) || "project.basedir".equals(expression)) {
72              return PlexusTestCase.getBasedir();
73          } else if (expression.startsWith("basedir") || expression.startsWith("project.basedir")) {
74              int pathSeparator = expression.indexOf("/");
75  
76              if (pathSeparator > 0) {
77                  value = PlexusTestCase.getBasedir() + expression.substring(pathSeparator);
78              } else {
79                  System.out.println("Got expression '" + expression + "' that was not recognised");
80              }
81              return value;
82          } else if ("localRepository".equals(expression)) {
83              File localRepo = new File(PlexusTestCase.getBasedir(), "target/local-repo");
84              return new MavenArtifactRepository(
85                      "localRepository",
86                      "file://" + localRepo.getAbsolutePath(),
87                      new DefaultRepositoryLayout(),
88                      null,
89                      null);
90          } else {
91              return expr;
92          }
93      }
94  
95      private String stripTokens(String expr) {
96          if (expr.startsWith("${") && expr.indexOf("}") == expr.length() - 1) {
97              expr = expr.substring(2, expr.length() - 1);
98          }
99  
100         return expr;
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public File alignToBaseDirectory(File file) {
106         if (file.getAbsolutePath().startsWith(PlexusTestCase.getBasedir())) {
107             return file;
108         } else if (file.isAbsolute()) {
109             return file;
110         } else {
111             return new File(PlexusTestCase.getBasedir(), file.getPath());
112         }
113     }
114 }