maven-m2-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tryg...@apache.org
Subject cvs commit: maven-components/maven-core/src/test/java/org/apache/maven/util/introspection ReflectionValueExtractorTest.java
Date Mon, 28 Mar 2005 14:37:41 GMT
trygvis     2005/03/28 06:37:41

  Modified:    maven-core/src/main/java/org/apache/maven/project/interpolation
                        RegexBasedModelInterpolator.java
               maven-core/src/main/java/org/apache/maven/util/introspection
                        ReflectionValueExtractor.java
               maven-core/src/test/java/org/apache/maven/project/interpolation
                        RegexBasedModelInterpolatorTest.java
               maven-core/src/test/java/org/apache/maven/util/introspection
                        ReflectionValueExtractorTest.java
  Log:
  o Fixing a NullPointerException in the value extractor if the specified getter
    didn't exists. The value extractor will now return null if the getter doesn't
    exists.
  o Properly implemented the method caching in the value exctrator.
  o Changed the RegexBasedModelInterpolator so it would properly handle null
    values. It used to convert null to "null" and then insert that, now it will
    leave the expression as is.
  
  Revision  Changes    Path
  1.4       +18 -19    maven-components/maven-core/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java
  
  Index: RegexBasedModelInterpolator.java
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-core/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- RegexBasedModelInterpolator.java	8 Mar 2005 06:06:22 -0000	1.3
  +++ RegexBasedModelInterpolator.java	28 Mar 2005 14:37:40 -0000	1.4
  @@ -1,20 +1,19 @@
   package org.apache.maven.project.interpolation;
   
  -/* ====================================================================
  - *   Copyright 2001-2004 The Apache Software Foundation.
  +/*
  + * Copyright 2001-2005 The Apache Software Foundation.
    *
  - *   Licensed under the Apache License, Version 2.0 (the "License");
  - *   you may not use this file except in compliance with the License.
  - *   You may obtain a copy of the License at
  + * Licensed under the Apache License, Version 2.0 (the "License");
  + * you may not use this file except in compliance with the License.
  + * You may obtain a copy of the License at
    *
  - *       http://www.apache.org/licenses/LICENSE-2.0
  + *      http://www.apache.org/licenses/LICENSE-2.0
    *
  - *   Unless required by applicable law or agreed to in writing, software
  - *   distributed under the License is distributed on an "AS IS" BASIS,
  - *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  - *   See the License for the specific language governing permissions and
  - *   limitations under the License.
  - * ====================================================================
  + * Unless required by applicable law or agreed to in writing, software
  + * distributed under the License is distributed on an "AS IS" BASIS,
  + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  + * See the License for the specific language governing permissions and
  + * limitations under the License.
    */
   
   import org.apache.maven.model.Model;
  @@ -32,12 +31,12 @@
   
   /**
    * @author jdcasey Created on Feb 3, 2005
  + * @version $Id$
    */
   public class RegexBasedModelInterpolator
       extends AbstractLogEnabled
       implements ModelInterpolator
   {
  -
       private static final Pattern EXPRESSION_PATTERN = Pattern.compile( "\\$\\{(pom|project\\.)?([^}]+)\\}"
);
   
       /**
  @@ -89,10 +88,11 @@
               String wholeExpr = matcher.group( 0 );
               String realExpr = matcher.group( 2 );
   
  -            String value = null;
  +            Object value = null;
  +
               try
               {
  -                value = String.valueOf( ReflectionValueExtractor.evaluate( realExpr, model
) );
  +                value = ReflectionValueExtractor.evaluate( realExpr, model );
               }
               catch ( Exception e )
               {
  @@ -105,15 +105,14 @@
   
               if ( value != null )
               {
  -                result = StringUtils.replace( result, wholeExpr, value );
  +                result = StringUtils.replace( result, wholeExpr, String.valueOf( value
) );
                   // could use:
  -                // result = matcher.replaceFirst( value );
  -                // but this could result in multiple lookups of value, and replaceAll is
not correct behaviour 
  +                // result = matcher.replaceFirst( stringValue );
  +                // but this could result in multiple lookups of stringValue, and replaceAll
is not correct behaviour
                   matcher.reset( result );
               }
           }
   
           return result;
       }
  -
   }
  \ No newline at end of file
  
  
  
  1.4       +27 -21    maven-components/maven-core/src/main/java/org/apache/maven/util/introspection/ReflectionValueExtractor.java
  
  Index: ReflectionValueExtractor.java
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-core/src/main/java/org/apache/maven/util/introspection/ReflectionValueExtractor.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ReflectionValueExtractor.java	7 Mar 2005 19:03:19 -0000	1.3
  +++ ReflectionValueExtractor.java	28 Mar 2005 14:37:41 -0000	1.4
  @@ -1,43 +1,41 @@
   package org.apache.maven.util.introspection;
   
  -/* ====================================================================
  - *   Copyright 2001-2004 The Apache Software Foundation.
  +/*
  + * Copyright 2001-2005 The Apache Software Foundation.
    *
  - *   Licensed under the Apache License, Version 2.0 (the "License");
  - *   you may not use this file except in compliance with the License.
  - *   You may obtain a copy of the License at
  + * Licensed under the Apache License, Version 2.0 (the "License");
  + * you may not use this file except in compliance with the License.
  + * You may obtain a copy of the License at
    *
  - *       http://www.apache.org/licenses/LICENSE-2.0
  + *      http://www.apache.org/licenses/LICENSE-2.0
    *
  - *   Unless required by applicable law or agreed to in writing, software
  - *   distributed under the License is distributed on an "AS IS" BASIS,
  - *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  - *   See the License for the specific language governing permissions and
  - *   limitations under the License.
  - * ====================================================================
  + * Unless required by applicable law or agreed to in writing, software
  + * distributed under the License is distributed on an "AS IS" BASIS,
  + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  + * See the License for the specific language governing permissions and
  + * limitations under the License.
    */
   
  -import org.codehaus.plexus.util.StringUtils;
  -
   import java.lang.reflect.Method;
   import java.util.HashMap;
   import java.util.Map;
   import java.util.StringTokenizer;
   
  +import org.codehaus.plexus.util.StringUtils;
  +
   /**
    * Using simple dotted expressions extract the values from a MavenProject
    * instance, For example we might want to extract a value like:
    * project.build.sourceDirectory
  - * 
  + *
    * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
  - * @version $Id: ReflectionValueExtractor.java,v 1.2 2005/03/01 07:05:33 brett
  - *          Exp $
  + * @version $Id$
    */
   public class ReflectionValueExtractor
   {
  -    private static Class[] args = new Class[0];
  +    private static Class[] args = new Class[ 0 ];
   
  -    private static Object[] params = new Object[0];
  +    private static Object[] params = new Object[ 0 ];
   
       private static ClassMap classMap;
   
  @@ -48,7 +46,8 @@
       }
   
       // TODO: don't throw Exception
  -    public static Object evaluate( String expression, Object root ) throws Exception
  +    public static Object evaluate( String expression, Object root )
  +        throws Exception
       {
           // ----------------------------------------------------------------------
           // Remove the leading "project" token
  @@ -75,6 +74,11 @@
   
               Method method = classMap.findMethod( methodName, args );
   
  +            if ( method == null )
  +            {
  +                return null;
  +            }
  +
               value = method.invoke( value, params );
           }
   
  @@ -88,8 +92,10 @@
           if ( classMap == null )
           {
               classMap = new ClassMap( clazz );
  +
  +            classMaps.put( clazz, classMap );
           }
   
           return classMap;
       }
  -}
  \ No newline at end of file
  +}
  
  
  
  1.2       +4 -4      maven-components/maven-core/src/test/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolatorTest.java
  
  Index: RegexBasedModelInterpolatorTest.java
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-core/src/test/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolatorTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- RegexBasedModelInterpolatorTest.java	24 Feb 2005 04:21:52 -0000	1.1
  +++ RegexBasedModelInterpolatorTest.java	28 Mar 2005 14:37:41 -0000	1.2
  @@ -16,17 +16,18 @@
    * limitations under the License.
    */
   
  -import junit.framework.TestCase;
   import org.apache.maven.model.Dependency;
   import org.apache.maven.model.Model;
   
  +import junit.framework.TestCase;
  +
   /**
    * @author jdcasey
  + * @version $Id$
    */
   public class RegexBasedModelInterpolatorTest
       extends TestCase
   {
  -
       public void testShouldInterpolateDependencyVersionToSetSameAsProjectVersion()
           throws ModelInterpolationException
       {
  @@ -75,5 +76,4 @@
   
           assertEquals( "foo-3.8.1", ( (Dependency) out.getDependencies().get( 0 ) ).getVersion()
);
       }
  -
  -}
  \ No newline at end of file
  +}
  
  
  
  1.4       +7 -5      maven-components/maven-core/src/test/java/org/apache/maven/util/introspection/ReflectionValueExtractorTest.java
  
  Index: ReflectionValueExtractorTest.java
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-core/src/test/java/org/apache/maven/util/introspection/ReflectionValueExtractorTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ReflectionValueExtractorTest.java	23 Mar 2005 06:52:52 -0000	1.3
  +++ ReflectionValueExtractorTest.java	28 Mar 2005 14:37:41 -0000	1.4
  @@ -33,15 +33,11 @@
   {
       private MavenProject project;
   
  -    private MavenProjectBuilder builder;
  -
       protected void setUp()
           throws Exception
       {
           super.setUp();
   
  -        builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE );
  -
           File f = getFileForClasspathResource( "pom.xml" );
   
           project = getProject( f );
  @@ -88,4 +84,10 @@
   
           assertNotNull( build );
       }
  +
  +    public void testValueExtractorWithAInvalidExpression()
  +        throws Exception
  +    {
  +        assertNull( ReflectionValueExtractor.evaluate( "project.foo", project ) );
  +    }
   }
  
  
  

Mime
View raw message