View Javadoc
1   /*
2    *    Copyright 2010-2024 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  /*
17   * Licensed to the Apache Software Foundation (ASF) under one
18   * or more contributor license agreements.  See the NOTICE file
19   * distributed with this work for additional information
20   * regarding copyright ownership.  The ASF licenses this file
21   * to you under the Apache License, Version 2.0 (the
22   * "License"); you may not use this file except in compliance
23   * with the License.  You may obtain a copy of the License at
24   *
25   *   http://www.apache.org/licenses/LICENSE-2.0
26   *
27   * Unless required by applicable law or agreed to in writing,
28   * software distributed under the License is distributed on an
29   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
30   * KIND, either express or implied.  See the License for the
31   * specific language governing permissions and limitations
32   * under the License.
33   */
34  package org.mybatis.maven.testing;
35  
36  import java.io.File;
37  
38  import org.apache.maven.artifact.repository.MavenArtifactRepository;
39  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
40  import org.codehaus.plexus.PlexusTestCase;
41  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
42  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
43  
44  /**
45   * Mybatis: Borrowed from 'https://github.com/apache/maven-plugin-testing/blob/master/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/ConfiguationException.java'
46   * Reason: Removed from release 4.0.0-beta-1 and we need to have junit 5 support.  Maven dropped maven 3 support here!
47   * Git: From release 4.0.0-alpha-2
48   */
49  
50  /**
51   * Stub for {@link ExpressionEvaluator}
52   *
53   * @author jesse
54   */
55  public class ResolverExpressionEvaluatorStub implements ExpressionEvaluator {
56    /** {@inheritDoc} */
57    @Override
58    public Object evaluate(String expr) throws ExpressionEvaluationException {
59  
60      Object value = null;
61  
62      if (expr == null) {
63        return null;
64      }
65  
66      String expression = stripTokens(expr);
67  
68      if (expression.equals(expr)) {
69        int index = expr.indexOf("${");
70        if (index >= 0) {
71          int lastIndex = expr.indexOf("}", index);
72          if (lastIndex >= 0) {
73            String retVal = expr.substring(0, index);
74  
75            if (index > 0 && expr.charAt(index - 1) == '$') {
76              retVal += expr.substring(index + 1, lastIndex + 1);
77            } else {
78              retVal += evaluate(expr.substring(index, lastIndex + 1));
79            }
80  
81            retVal += evaluate(expr.substring(lastIndex + 1));
82            return retVal;
83          }
84        }
85  
86        // Was not an expression
87        if (expression.indexOf("$$") > -1) {
88          return expression.replaceAll("\\$\\$", "\\$");
89        }
90      }
91  
92      if ("basedir".equals(expression) || "project.basedir".equals(expression)) {
93        return PlexusTestCase.getBasedir();
94      } else if (expression.startsWith("basedir") || expression.startsWith("project.basedir")) {
95        int pathSeparator = expression.indexOf("/");
96  
97        if (pathSeparator > 0) {
98          value = PlexusTestCase.getBasedir() + expression.substring(pathSeparator);
99        } else {
100         System.out.println("Got expression '" + expression + "' that was not recognised");
101       }
102       return value;
103     } else if ("localRepository".equals(expression)) {
104       File localRepo = new File(PlexusTestCase.getBasedir(), "target/local-repo");
105       return new MavenArtifactRepository("localRepository", "file://" + localRepo.getAbsolutePath(),
106           new DefaultRepositoryLayout(), null, null);
107     } else {
108       return expr;
109     }
110   }
111 
112   private String stripTokens(String expr) {
113     if (expr.startsWith("${") && expr.indexOf("}") == expr.length() - 1) {
114       expr = expr.substring(2, expr.length() - 1);
115     }
116 
117     return expr;
118   }
119 
120   /** {@inheritDoc} */
121   @Override
122   public File alignToBaseDirectory(File file) {
123     if (file.getAbsolutePath().startsWith(PlexusTestCase.getBasedir())) {
124       return file;
125     } else if (file.isAbsolute()) {
126       return file;
127     } else {
128       return new File(PlexusTestCase.getBasedir(), file.getPath());
129     }
130   }
131 }