Coverage Report - org.apache.maven.plugin.testing.ResolverExpressionEvaluatorStub
 
Classes in this File Line Coverage Branch Coverage Complexity
ResolverExpressionEvaluatorStub
65%
25/38
56%
17/30
8.667
 
 1  
 package org.apache.maven.plugin.testing;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 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  
  * Stub for {@link ExpressionEvaluator}
 32  
  *
 33  
  * @author jesse
 34  
  * @version $Id: ResolverExpressionEvaluatorStub.java 638332 2008-03-18 11:39:00Z bentmann $
 35  
  */
 36  10
 public class ResolverExpressionEvaluatorStub
 37  
     implements ExpressionEvaluator
 38  
 {
 39  
     /** {@inheritDoc} */
 40  
     public Object evaluate( String expr )
 41  
         throws ExpressionEvaluationException
 42  
     {
 43  
 
 44  26
         Object value = null;
 45  
 
 46  26
         if ( expr == null )
 47  
         {
 48  0
             return null;
 49  
         }
 50  
 
 51  26
         String expression = stripTokens( expr );
 52  
 
 53  26
         if ( expression.equals( expr ) )
 54  
         {
 55  20
             int index = expr.indexOf( "${" );
 56  20
             if ( index >= 0 )
 57  
             {
 58  2
                 int lastIndex = expr.indexOf( "}", index );
 59  2
                 if ( lastIndex >= 0 )
 60  
                 {
 61  2
                     String retVal = expr.substring( 0, index );
 62  
 
 63  2
                     if ( index > 0 && expr.charAt( index - 1 ) == '$' )
 64  
                     {
 65  0
                         retVal += expr.substring( index + 1, lastIndex + 1 );
 66  
                     }
 67  
                     else
 68  
                     {
 69  2
                         retVal += evaluate( expr.substring( index, lastIndex + 1 ) );
 70  
                     }
 71  
 
 72  2
                     retVal += evaluate( expr.substring( lastIndex + 1 ) );
 73  2
                     return retVal;
 74  
                 }
 75  
             }
 76  
 
 77  
             // Was not an expression
 78  18
             if ( expression.indexOf( "$$" ) > -1 )
 79  
             {
 80  0
                 return expression.replaceAll( "\\$\\$", "\\$" );
 81  
             }
 82  
         }
 83  
 
 84  24
         if ( "basedir".equals( expression ) )
 85  
         {
 86  4
             return PlexusTestCase.getBasedir();
 87  
         }
 88  20
         else if ( expression.startsWith( "basedir" ) )
 89  
         {
 90  0
             int pathSeparator = expression.indexOf( "/" );
 91  
 
 92  0
             if ( pathSeparator > 0 )
 93  
             {
 94  0
                 value = PlexusTestCase.getBasedir() + expression.substring( pathSeparator );
 95  
             }
 96  
             else
 97  
             {
 98  0
                 System.out.println( "Got expression '" + expression + "' that was not recognised" );
 99  
             }
 100  0
             return value;
 101  
         }
 102  20
         else if ( "localRepository".equals( expression ) )
 103  
         {
 104  2
             File localRepo = new File( PlexusTestCase.getBasedir(), "target/local-repo" );
 105  2
             return new DefaultArtifactRepository( "localRepository", "file://" + localRepo.getAbsolutePath(),
 106  
                                                   new DefaultRepositoryLayout() );
 107  
         }
 108  
         else
 109  
         {
 110  18
             return expr;
 111  
         }
 112  
     }
 113  
 
 114  
     private String stripTokens( String expr )
 115  
     {
 116  26
         if ( expr.startsWith( "${" ) && expr.indexOf( "}" ) == expr.length() - 1 )
 117  
         {
 118  6
             expr = expr.substring( 2, expr.length() - 1 );
 119  
         }
 120  
 
 121  26
         return expr;
 122  
     }
 123  
 
 124  
     /** {@inheritDoc} */
 125  
     public File alignToBaseDirectory( File file )
 126  
     {
 127  0
         if ( file.getAbsolutePath().startsWith( PlexusTestCase.getBasedir() ) )
 128  
         {
 129  0
             return file;
 130  
         }
 131  0
         else if ( file.isAbsolute() )
 132  
         {
 133  0
             return file;
 134  
         }
 135  
         else
 136  
         {
 137  0
             return new File( PlexusTestCase.getBasedir(), file.getPath() );
 138  
         }
 139  
     }
 140  
 }