svn commit: rev 21129 - in avalon/trunk/tools/magic/engine: . src/java/org/apache/avalon/magic src/test/java/org/apache/avalon/magic src/test/resources/org/apache/avalon/magic

[email protected]
Newsgroups gmane.comp.jakarta.avalon.cvs
Message-ID <[email protected]>
Author: niclas
Date: Sat Jun 12 03:38:34 2004
New Revision: 21129

Added:
   avalon/trunk/tools/magic/engine/src/test/resources/org/apache/avalon/magic/test.properties
      - copied unchanged from rev 21125, avalon/trunk/tools/magic/engine/src/test/java/org/apache/avalon/magic/test.properties
Removed:
   avalon/trunk/tools/magic/engine/src/test/java/org/apache/avalon/magic/test.properties
Modified:
   avalon/trunk/tools/magic/engine/build.xml
   avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Builder.java
   avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/DefaultResolver.java
   avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginContext.java
   avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginProperties.java
   avalon/trunk/tools/magic/engine/src/test/java/org/apache/avalon/magic/PluginContextTestCase.java
Log:
Fixed the proeprty resolution problem, updated the unittests, and made unittesting part of the default build.

Modified: avalon/trunk/tools/magic/engine/build.xml
==============================================================================
--- avalon/trunk/tools/magic/engine/build.xml	(original)
+++ avalon/trunk/tools/magic/engine/build.xml	Sat Jun 12 03:38:34 2004
@@ -62,7 +62,7 @@
   
   </target>
   
-  <target name="dist" depends="install" >
+  <target name="dist" depends="install,test" >
     <copy todir="${build.dist.dir}" >
       <fileset dir="${build.src.dir}/dist" includes="**" />
     </copy>

Modified: avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Builder.java
==============================================================================
--- avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Builder.java	(original)
+++ avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/Builder.java	Sat Jun 12 03:38:34 2004
@@ -151,14 +151,14 @@
     
     private void loadGlobalProperties( PluginProperties props )
     {
-        props.put( "magic.home.dir", m_SystemDir.toString() );
-        props.put( "magic.plugins.dir", m_PluginsDir.getAbsolutePath() );
-        props.put( "magic.repository.dir", new File( m_SystemDir, "repository" ).toString() );
-        props.put( "magic.project.dir", m_ProjectDir.getAbsolutePath() );
-        props.put( "magic.temp.dir", m_TempDir.getAbsolutePath() );
-        props.put( "user.home", System.getProperty( "user.home" ) );
-        props.put( "java.home", System.getProperty( "java.home" ) );
-        props.put( "java.version", System.getProperty( "java.version" ) );
+        props.setProperty( "magic.home.dir", m_SystemDir.toString() );
+        props.setProperty( "magic.plugins.dir", m_PluginsDir.getAbsolutePath() );
+        props.setProperty( "magic.repository.dir", new File( m_SystemDir, "repository" ).toString() );
+        props.setProperty( "magic.project.dir", m_ProjectDir.getAbsolutePath() );
+        props.setProperty( "magic.temp.dir", m_TempDir.getAbsolutePath() );
+        props.setProperty( "user.home", System.getProperty( "user.home" ) );
+        props.setProperty( "java.home", System.getProperty( "java.home" ) );
+        props.setProperty( "java.version", System.getProperty( "java.version" ) );
         populateDateTimes( props );        
     }
     
@@ -268,12 +268,12 @@
     {
         Calendar cal = Calendar.getInstance();
         
-        p.put( "magic.year", "" + cal.get( Calendar.YEAR ) );
-        p.put( "magic.month", "" + cal.get( Calendar.MONTH ) );
-        p.put( "magic.date", "" + cal.get( Calendar.DATE ) );
-        p.put( "magic.hour", "" + cal.get( Calendar.HOUR_OF_DAY ) );
-        p.put( "magic.minute", "" + cal.get( Calendar.MINUTE ) );
-        p.put( "magic.second", "" + cal.get( Calendar.SECOND ) );
+        p.setProperty( "magic.year", "" + cal.get( Calendar.YEAR ) );
+        p.setProperty( "magic.month", "" + cal.get( Calendar.MONTH ) );
+        p.setProperty( "magic.date", "" + cal.get( Calendar.DATE ) );
+        p.setProperty( "magic.hour", "" + cal.get( Calendar.HOUR_OF_DAY ) );
+        p.setProperty( "magic.minute", "" + cal.get( Calendar.MINUTE ) );
+        p.setProperty( "magic.second", "" + cal.get( Calendar.SECOND ) );
     }
 } 
  

Modified: avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/DefaultResolver.java
==============================================================================
--- avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/DefaultResolver.java	(original)
+++ avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/DefaultResolver.java	Sat Jun 12 03:38:34 2004
@@ -24,7 +24,13 @@
 public class DefaultResolver
     implements PropertyResolver
 {
-
+    private PluginContext m_Context;
+    
+    DefaultResolver( PluginContext context )
+    {
+        m_Context = context;
+    }
+    
     public String resolve( PluginProperties props, String value )
     {
         // optimization for common case.
@@ -44,7 +50,10 @@
                 String open = (String) stack.pop();
                 if( open.equals( "${" ) )
                 {
-                    String propValue = props.getProperty( name );
+                    String propValue = props.getProperty( name, this );
+                    if( propValue == null )
+                        propValue = m_Context.getProperty( name );
+                        
                     if( propValue == null )
                         push( stack, "${" + name + "}" );
                     else

Modified: avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginContext.java
==============================================================================
--- avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginContext.java	(original)
+++ avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginContext.java	Sat Jun 12 03:38:34 2004
@@ -45,6 +45,7 @@
     private File m_TempDir;
    
     private Project  m_AntProject;
+    private PropertyResolver m_Resolver;
     
     
     PluginContext( File scriptDir )
@@ -74,6 +75,8 @@
         if( ant == null )
             throw new IllegalArgumentException( "Null argument: ant" );
             
+        m_Resolver = new DefaultResolver( this );
+        
         m_ProjectName = projectName.trim();
         m_ProjectDir = projectDir;
         m_ProjectProperties = projectProps;
@@ -162,7 +165,8 @@
     
     public String getProperty( String name )
     {
-        String value = m_ProjectProperties.getProperty( name );
+        name = name.trim();
+        String value = m_ProjectProperties.getProperty( name, m_Resolver );
         if( value == null )
         {
             if( name.equals( "plugin.dir" ) )
@@ -202,6 +206,6 @@
     
     public String resolve( String value )
     {
-        return new DefaultResolver().resolve( m_ProjectProperties, value );
+        return m_ProjectProperties.resolve( value, m_Resolver );
     }
 }

Modified: avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginProperties.java
==============================================================================
--- avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginProperties.java	(original)
+++ avalon/trunk/tools/magic/engine/src/java/org/apache/avalon/magic/PluginProperties.java	Sat Jun 12 03:38:34 2004
@@ -17,45 +17,82 @@
 
 package org.apache.avalon.magic;
 
+import java.io.IOException;
+import java.io.InputStream;
+
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Properties;
+import java.util.Set;
 
-public class PluginProperties extends Properties
+public class PluginProperties
 {
-    private PropertyResolver m_Resolver;
+    private Properties m_Properties;
     
     PluginProperties()
     {
-        m_Resolver = new DefaultResolver();
+        m_Properties = new Properties();
     }
     
-    PluginProperties( Properties content )
+    PluginProperties( PluginProperties content )
     {
-        super();
+        this();
+        putAll( content );
+    }
+    
+    void putAll( PluginProperties content )
+    {    
         Iterator list = content.entrySet().iterator();
         while( list.hasNext() )
         {
             Map.Entry entry = (Map.Entry) list.next();
             Object key = entry.getKey();
             Object value = entry.getValue();
-            put( key, value );
+            m_Properties.put( key, value );
         }
     }
     
+    public String getProperty( String name )
+    {
+        String value = m_Properties.getProperty( name ).trim();
+        if( value.indexOf( "${" ) >= 0 )
+            throw new IllegalArgumentException( "The value of '" + name + "' contains a variable, and not supported to resolve with this method:" + value );
+        return value;
+    }
+    
     public String getProperty( String name, PropertyResolver resolver )
     {
         name = name.trim();
-        String value = super.getProperty( name );
+        String value = m_Properties.getProperty( name );
         if( value == null )
             return null;
         value = value.trim();
         return resolver.resolve( this, value );
     }
     
-    public String getProperty( String name )
+    public String resolve( String data, PropertyResolver resolver  )
     {
-        return getProperty( name, m_Resolver );
+        return resolver.resolve( this, data ).trim();
+    }
+
+    public void setProperty( String name, String value )
+    {
+        m_Properties.setProperty( name, value );
     }
     
+    void load( InputStream in )
+        throws IOException
+    {
+        m_Properties.load( in );
+    }
+    
+    Set entrySet()
+    {
+        return m_Properties.entrySet();
+    }
+
+    Set keySet()
+    {
+        return m_Properties.keySet();
+    }
 }

Modified: avalon/trunk/tools/magic/engine/src/test/java/org/apache/avalon/magic/PluginContextTestCase.java
==============================================================================
--- avalon/trunk/tools/magic/engine/src/test/java/org/apache/avalon/magic/PluginContextTestCase.java	(original)
+++ avalon/trunk/tools/magic/engine/src/test/java/org/apache/avalon/magic/PluginContextTestCase.java	Sat Jun 12 03:38:34 2004
@@ -27,19 +27,30 @@
     protected void setUp() throws Exception
     {
         super.setUp();
-        m_ProjectDir = new File( "target/projectdir");
-        m_ProjectDir.mkdir();
+        m_ProjectDir = new File( "target/test/projectdir");
+        m_ProjectDir.mkdirs();
         
         PluginProperties projectProps = new PluginProperties();
         InputStream in = getClass().getResourceAsStream( "test.properties");
         projectProps.load( in );
         
-        m_PluginDir = new File( "target/plugins");
-        m_PluginDir.mkdir();
+        m_PluginDir = new File( "target/test/plugins");
+        m_PluginDir.mkdirs();
         
-        m_SystemDir = new File( "target/system");
-        m_SystemDir.mkdir();
+        m_SystemDir = new File( "target/test/system");
+        m_SystemDir.mkdirs();
         
+        m_TempDir = new File( "target/test/temp");
+        m_TempDir.mkdirs();
+        
+        m_ProjectSystemDir = new File( "../../../central/system");
+
+        m_Project = new Project();
+        m_Project.setBaseDir( m_ProjectDir );
+        m_Project.setCoreLoader( this.getClass().getClassLoader() );
+        m_Project.setName( "testcase project" );
+        m_Project.init();
+                
         m_Context = new PluginContext
         ( 
             " testcase project ", 
@@ -89,26 +100,43 @@
         assertEquals( "Plugin ClassName failed.", "TestCasePlugin", m_Context.getPluginClassname());
     }
 
-    public void testGetProperty()
+    public void testGetProperty1()
     {
         String p1 = "niclas${abc.def}hedhman";
         String value = m_Context.resolve( p1 );
+        System.out.println( "1:" + value );
         assertEquals( "Unresolvable failed.", p1, value );
+    }
 
-        p1 = "niclas ${a.property } hedhman";
-        value = m_Context.resolve( p1 );
+    public void testGetProperty2()
+    {
+        String p1 = "niclas ${a.property } hedhman";
+        String value = m_Context.resolve( p1 );
+        System.out.println( "2:" + value );
         assertEquals( "Single Level resolution failed.", "niclas has the surname of hedhman", value );
+    }
 
-        p1 = "${a2.this}";
-        value = m_Context.resolve( p1 );
+    public void testGetProperty3()
+    {
+        String p1 = "${a2.this}";
+        String value = m_Context.resolve( p1 );
+        System.out.println( "3:" + value );
         assertEquals( "Property resolution failed.", "this is", value );
+    }
 
-        p1 = "Hey, ${a2.${a1}} ${a2.${a4}} ${a3}";
-        value = m_Context.resolve( p1 );
+    public void testGetProperty4()
+    {
+        String p1 = "Hey, ${a2.${a1}} ${a2.${a4}} ${a3}";
+        String value = m_Context.resolve( p1 );
+        System.out.println( "4:" + value );
         assertEquals( "Nested resolution failed.", "Hey, this is this is not this is funky", value );
+    }
 
-        p1 = "${${${${${${b1}}}}}}";
-        value = m_Context.resolve( p1 );
+    public void testGetProperty5()
+    {
+        String p1 = "${${${${${${b1}}}}}}";
+        String value = m_Context.resolve( p1 );
+        System.out.println( "5:" + value );
         assertEquals( "Nested resolution failed.", "YEAH!!!!", value );
     }
 
@@ -116,5 +144,11 @@
     {
         assertTrue( "Ant Project failed.", m_Context.getAntProject() != null );
         assertEquals( "Ant Project failed.", "testcase project", m_Context.getAntProject().getName() );
+    }
+    
+    public void testResolvePluginDir()
+    {
+        String expected = "This is " + m_PluginDir.getAbsolutePath() + " right.";
+        assertEquals( "PluginDir not retrievable.", expected, m_Context.resolve( "This is ${plugin.dir} right." ) );
     }
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.