View Javadoc
1   /*
2    *    Copyright 2010-2025 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  import java.nio.file.Path;
38  
39  import org.apache.maven.artifact.repository.MavenArtifactRepository;
40  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
41  import org.codehaus.plexus.PlexusTestCase;
42  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
43  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
44  
45  /**
46   * 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'
47   * Reason: Removed from release 4.0.0-beta-1 and we need to have junit 5 support.  Maven dropped maven 3 support here!
48   * Git: From release 4.0.0-alpha-2
49   */
50  
51  /**
52   * Stub for {@link ExpressionEvaluator}
53   *
54   * @author jesse
55   */
56  public class ResolverExpressionEvaluatorStub implements ExpressionEvaluator {
57    /** {@inheritDoc} */
58    @Override
59    public Object evaluate(String expr) throws ExpressionEvaluationException {
60  
61      Object value = null;
62  
63      if (expr == null) {
64        return null;
65      }
66  
67      String expression = stripTokens(expr);
68  
69      if (expression.equals(expr)) {
70        int index = expr.indexOf("${");
71        if (index >= 0) {
72          int lastIndex = expr.indexOf("}", index);
73          if (lastIndex >= 0) {
74            String retVal = expr.substring(0, index);
75  
76            if (index > 0 && expr.charAt(index - 1) == '$') {
77              retVal += expr.substring(index + 1, lastIndex + 1);
78            } else {
79              retVal += evaluate(expr.substring(index, lastIndex + 1));
80            }
81  
82            retVal += evaluate(expr.substring(lastIndex + 1));
83            return retVal;
84          }
85        }
86  
87        // Was not an expression
88        if (expression.indexOf("$$") > -1) {
89          return expression.replaceAll("\\$\\$", "\\$");
90        }
91      }
92  
93      if ("basedir".equals(expression) || "project.basedir".equals(expression)) {
94        return PlexusTestCase.getBasedir();
95      } else if (expression.startsWith("basedir") || expression.startsWith("project.basedir")) {
96        int pathSeparator = expression.indexOf("/");
97  
98        if (pathSeparator > 0) {
99          value = PlexusTestCase.getBasedir() + expression.substring(pathSeparator);
100       } else {
101         System.out.println("Got expression '" + expression + "' that was not recognised");
102       }
103       return value;
104     } else if ("localRepository".equals(expression)) {
105       Path localRepo = Path.of(PlexusTestCase.getBasedir(), "target/local-repo");
106       return new MavenArtifactRepository("localRepository", "file://" + localRepo.toFile().getAbsolutePath(),
107           new DefaultRepositoryLayout(), null, null);
108     } else {
109       return expr;
110     }
111   }
112 
113   private String stripTokens(String expr) {
114     if (expr.startsWith("${") && expr.indexOf("}") == expr.length() - 1) {
115       expr = expr.substring(2, expr.length() - 1);
116     }
117 
118     return expr;
119   }
120 
121   /** {@inheritDoc} */
122   @Override
123   public File alignToBaseDirectory(File file) {
124     if (file.getAbsolutePath().startsWith(PlexusTestCase.getBasedir())) {
125       return file;
126     } else if (file.isAbsolute()) {
127       return file;
128     } else {
129       return Path.of(PlexusTestCase.getBasedir(), file.getPath()).toFile();
130     }
131   }
132 }