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   * @deprecated This stub is for deprecated JUnit 4 style tests.
34   */
35  @Deprecated
36  public class ResolverExpressionEvaluatorStub implements ExpressionEvaluator {
37      /** {@inheritDoc} */
38      @Override
39      public Object evaluate(String expr) throws ExpressionEvaluationException {
40  
41          Object value = null;
42  
43          if (expr == null) {
44              return null;
45          }
46  
47          String expression = stripTokens(expr);
48  
49          if (expression.equals(expr)) {
50              int index = expr.indexOf("${");
51              if (index >= 0) {
52                  int lastIndex = expr.indexOf("}", index);
53                  if (lastIndex >= 0) {
54                      String retVal = expr.substring(0, index);
55  
56                      if (index > 0 && expr.charAt(index - 1) == '$') {
57                          retVal += expr.substring(index + 1, lastIndex + 1);
58                      } else {
59                          retVal += evaluate(expr.substring(index, lastIndex + 1));
60                      }
61  
62                      retVal += evaluate(expr.substring(lastIndex + 1));
63                      return retVal;
64                  }
65              }
66  
67              // Was not an expression
68              if (expression.indexOf("$$") > -1) {
69                  return expression.replaceAll("\\$\\$", "\\$");
70              }
71          }
72  
73          if ("basedir".equals(expression) || "project.basedir".equals(expression)) {
74              return PlexusTestCase.getBasedir();
75          } else if (expression.startsWith("basedir") || expression.startsWith("project.basedir")) {
76              int pathSeparator = expression.indexOf("/");
77  
78              if (pathSeparator > 0) {
79                  value = PlexusTestCase.getBasedir() + expression.substring(pathSeparator);
80              } else {
81                  System.out.println("Got expression '" + expression + "' that was not recognised");
82              }
83              return value;
84          } else if ("localRepository".equals(expression)) {
85              File localRepo = new File(PlexusTestCase.getBasedir(), "target/local-repo");
86              return new MavenArtifactRepository(
87                      "localRepository",
88                      "file://" + localRepo.getAbsolutePath(),
89                      new DefaultRepositoryLayout(),
90                      null,
91                      null);
92          } else {
93              return expr;
94          }
95      }
96  
97      private String stripTokens(String expr) {
98          if (expr.startsWith("${") && expr.indexOf("}") == expr.length() - 1) {
99              expr = expr.substring(2, expr.length() - 1);
100         }
101 
102         return expr;
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     public File alignToBaseDirectory(File file) {
108         if (file.getAbsolutePath().startsWith(PlexusTestCase.getBasedir())) {
109             return file;
110         } else if (file.isAbsolute()) {
111             return file;
112         } else {
113             return new File(PlexusTestCase.getBasedir(), file.getPath());
114         }
115     }
116 }