1 package org.apache.maven.plugin.testing;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
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.apache.maven.artifact.repository.DefaultArtifactRepository;
28 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
29
30
31
32
33
34
35
36 public class ResolverExpressionEvaluatorStub
37 implements ExpressionEvaluator
38 {
39
40 public Object evaluate( String expr )
41 throws ExpressionEvaluationException
42 {
43
44 Object value = null;
45
46 if ( expr == null )
47 {
48 return null;
49 }
50
51 String expression = stripTokens( expr );
52
53 if ( expression.equals( expr ) )
54 {
55 int index = expr.indexOf( "${" );
56 if ( index >= 0 )
57 {
58 int lastIndex = expr.indexOf( "}", index );
59 if ( lastIndex >= 0 )
60 {
61 String retVal = expr.substring( 0, index );
62
63 if ( index > 0 && expr.charAt( index - 1 ) == '$' )
64 {
65 retVal += expr.substring( index + 1, lastIndex + 1 );
66 }
67 else
68 {
69 retVal += evaluate( expr.substring( index, lastIndex + 1 ) );
70 }
71
72 retVal += evaluate( expr.substring( lastIndex + 1 ) );
73 return retVal;
74 }
75 }
76
77
78 if ( expression.indexOf( "$$" ) > -1 )
79 {
80 return expression.replaceAll( "\\$\\$", "\\$" );
81 }
82 }
83
84 if ( "basedir".equals( expression ) )
85 {
86 return PlexusTestCase.getBasedir();
87 }
88 else if ( expression.startsWith( "basedir" ) )
89 {
90 int pathSeparator = expression.indexOf( "/" );
91
92 if ( pathSeparator > 0 )
93 {
94 value = PlexusTestCase.getBasedir() + expression.substring( pathSeparator );
95 }
96 else
97 {
98 System.out.println( "Got expression '" + expression + "' that was not recognised" );
99 }
100 return value;
101 }
102 else if ( "localRepository".equals( expression ) )
103 {
104 File localRepo = new File( PlexusTestCase.getBasedir(), "target/local-repo" );
105 return new DefaultArtifactRepository( "localRepository", "file://" + localRepo.getAbsolutePath(),
106 new DefaultRepositoryLayout() );
107 }
108 else
109 {
110 return expr;
111 }
112 }
113
114 private String stripTokens( String expr )
115 {
116 if ( expr.startsWith( "${" ) && expr.indexOf( "}" ) == expr.length() - 1 )
117 {
118 expr = expr.substring( 2, expr.length() - 1 );
119 }
120
121 return expr;
122 }
123
124
125 public File alignToBaseDirectory( File file )
126 {
127 if ( file.getAbsolutePath().startsWith( PlexusTestCase.getBasedir() ) )
128 {
129 return file;
130 }
131 else if ( file.isAbsolute() )
132 {
133 return file;
134 }
135 else
136 {
137 return new File( PlexusTestCase.getBasedir(), file.getPath() );
138 }
139 }
140 }