Skip to Content.
Sympa Menu

notify-dpml - svn commit: r1343 - in development/main/metro: composition/api/src/main/net/dpml/composition/model composition/impl/src/main/net/dpml/composition/model/impl composition/impl/src/main/net/dpml/composition/util composition/test/etc/test composition/test/src/test/net/dpml/composition/util testing/testschema/src/main/net/dpml/test/testschema

notify-dpml AT lists.ibiblio.org

Subject: DPML Notify

List archive

Chronological Thread  
  • From: pneubauer AT netcompartner.com
  • To: notify-dpml AT lists.ibiblio.org
  • Subject: svn commit: r1343 - in development/main/metro: composition/api/src/main/net/dpml/composition/model composition/impl/src/main/net/dpml/composition/model/impl composition/impl/src/main/net/dpml/composition/util composition/test/etc/test composition/test/src/test/net/dpml/composition/util testing/testschema/src/main/net/dpml/test/testschema
  • Date: Sun, 02 Jan 2005 23:07:07 +0100

Author: pneubauer
Date: Sun Jan 2 23:07:07 2005
New Revision: 1343

Added:
development/main/metro/composition/test/etc/test/schemavalidation.xml

development/main/metro/composition/test/etc/test/schemavalidation_failing.xml
Modified:

development/main/metro/composition/api/src/main/net/dpml/composition/model/ComponentModel.java

development/main/metro/composition/api/src/main/net/dpml/composition/model/ContainmentModel.java

development/main/metro/composition/impl/src/main/net/dpml/composition/model/impl/DefaultComponentModel.java

development/main/metro/composition/impl/src/main/net/dpml/composition/model/impl/DefaultContainmentModel.java

development/main/metro/composition/impl/src/main/net/dpml/composition/util/ConfigurationValidator.java

development/main/metro/composition/impl/src/main/net/dpml/composition/util/XercesSchemaValidator.java

development/main/metro/composition/test/src/test/net/dpml/composition/util/SchemaValidationTestCase.java

development/main/metro/testing/testschema/src/main/net/dpml/test/testschema/ConfigurableB.java
Log:
first working validation on components. set the @metro.configuration
schema="true" and provide an .xschema for your component if you want to
validate


Modified:
development/main/metro/composition/api/src/main/net/dpml/composition/model/ComponentModel.java
==============================================================================
---
development/main/metro/composition/api/src/main/net/dpml/composition/model/ComponentModel.java
(original)
+++
development/main/metro/composition/api/src/main/net/dpml/composition/model/ComponentModel.java
Sun Jan 2 23:07:07 2005
@@ -97,8 +97,10 @@
* configuration will replace the existing configuration.
*
* @param config the supplied configuration
+ * @throws ModelException
+ * @throws IllegalStateException
*/
- void setConfiguration( Configuration config );
+ void setConfiguration( Configuration config ) throws
IllegalStateException, ModelException;

/**
* Set the configuration to the supplied value. The supplied
@@ -109,8 +111,11 @@
* configuration otherwise the resoved configuration shall be layed
above
* the configuration supplied with the profile which in turn is layer
above
* the type default configuration (if any)
+ * @throws ModelException if the supplied configuraiton was null, or if
the Configuration
+ * should been but could not be validated
+ * @throws IllegalStateException
*/
- void setConfiguration( Configuration config, boolean policy );
+ void setConfiguration( Configuration config, boolean policy ) throws
IllegalStateException, ModelException;

/**
* Return the configuration to be applied to the component.

Modified:
development/main/metro/composition/api/src/main/net/dpml/composition/model/ContainmentModel.java
==============================================================================
---
development/main/metro/composition/api/src/main/net/dpml/composition/model/ContainmentModel.java
(original)
+++
development/main/metro/composition/api/src/main/net/dpml/composition/model/ContainmentModel.java
Sun Jan 2 23:07:07 2005
@@ -198,8 +198,9 @@
/**
* Apply a set of override targets.
* @param targets a set of target directives
+ * @throws ModelException if an error occures
*/
- void applyTargets( TargetDirective[]targets );
+ void applyTargets( TargetDirective[]targets ) throws ModelException;

/**
* Add a composition listener to the model.

Modified:
development/main/metro/composition/impl/src/main/net/dpml/composition/model/impl/DefaultComponentModel.java
==============================================================================
---
development/main/metro/composition/impl/src/main/net/dpml/composition/model/impl/DefaultComponentModel.java
(original)
+++
development/main/metro/composition/impl/src/main/net/dpml/composition/model/impl/DefaultComponentModel.java
Sun Jan 2 23:07:07 2005
@@ -18,6 +18,7 @@

package net.dpml.composition.model.impl;

+import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Enumeration;
@@ -37,6 +38,7 @@
import net.dpml.composition.model.StageModel;
import net.dpml.composition.provider.ComponentContext;
import net.dpml.composition.provider.SecurityModel;
+import net.dpml.composition.util.XercesSchemaValidator;
import net.dpml.configuration.Configurable;
import net.dpml.configuration.Configuration;
import net.dpml.configuration.impl.CascadingConfiguration;
@@ -145,11 +147,11 @@
consolidateConfigurations( explicit, defaults );
if( consolidated != null )
{
- m_config = consolidated;
+ setConfiguration( consolidated );
}
else
{
- m_config = EMPTY_CONFIGURATION;
+ setConfiguration( EMPTY_CONFIGURATION );
}
}

@@ -719,10 +721,10 @@
* @param config the supplied configuration
* @exception IllegalStateException if the component type backing the
* model does not implement the configurable interface
- * @exception NullPointerException if the supplied configuration is null
+ * @exception ModelException if the supplied configuration is null or not
successfully validated (if requested)
*/
public void setConfiguration( Configuration config )
- throws IllegalStateException, NullPointerException
+ throws IllegalStateException, ModelException
{
setConfiguration( config, true );
}
@@ -741,7 +743,7 @@
* @exception NullPointerException if the supplied configuration is null
*/
public void setConfiguration( Configuration config, boolean policy )
- throws IllegalStateException, NullPointerException
+ throws IllegalStateException, ModelException
{
if( !isConfigurable() )
{
@@ -757,15 +759,32 @@
{
throw new NullPointerException( "config" );
}
-
+ Configuration t_config = config;
if( policy )
{
- m_config = consolidateConfigurations( config, m_config );
+ t_config = consolidateConfigurations( config, m_config );
}
- else
+ String schema = this.getType().getInfo().getConfigurationSchema();
+ Boolean shouldWeValidate = Boolean.valueOf(schema);
+ if ( shouldWeValidate.booleanValue() )
{
- m_config = config;
+ //TODO: this is the only validator we have, this should be some
factory later on
+ try
+ {
+ XercesSchemaValidator validator = new XercesSchemaValidator();
+ //TODO: we have just one policy to locate schemas yet :
Colocation to the component class
+ String classname = this.getType().getInfo().getClassname();
+ String iSchemaLocation = classname.replace( '.', '/' )
+ + ".xschema";
+ InputStream schemaStream =
this.getClass().getClassLoader().getResourceAsStream( iSchemaLocation );
+ validator.validate(t_config, schemaStream );
+ }
+ catch (Exception e)
+ {
+ throw new ModelException ( "could not set configuration", e
);
+ }
}
+ m_config = t_config;
}

/**

Modified:
development/main/metro/composition/impl/src/main/net/dpml/composition/model/impl/DefaultContainmentModel.java
==============================================================================
---
development/main/metro/composition/impl/src/main/net/dpml/composition/model/impl/DefaultContainmentModel.java
(original)
+++
development/main/metro/composition/impl/src/main/net/dpml/composition/model/impl/DefaultContainmentModel.java
Sun Jan 2 23:07:07 2005
@@ -802,8 +802,9 @@
/**
* Apply a set of override targets.
* @param targets a set of target directives
+ * @throws ModelException
*/
- public void applyTargets( TargetDirective[]targets )
+ public void applyTargets( TargetDirective[]targets ) throws
ModelException
{
for( int i=0; i<targets.length; i++ )
{

Modified:
development/main/metro/composition/impl/src/main/net/dpml/composition/util/ConfigurationValidator.java
==============================================================================
---
development/main/metro/composition/impl/src/main/net/dpml/composition/util/ConfigurationValidator.java
(original)
+++
development/main/metro/composition/impl/src/main/net/dpml/composition/util/ConfigurationValidator.java
Sun Jan 2 23:07:07 2005
@@ -18,7 +18,7 @@

package net.dpml.composition.util;

-import java.io.IOException;
+import java.io.InputStream;

import net.dpml.composition.model.ModelException;
import net.dpml.configuration.Configuration;
@@ -29,10 +29,9 @@
{
/**
* @param iConfig the comfiguration to validate
- * @param iComponent the component class to validate for
+ * @param iSchema the schema to validate against
* @throws ModelException the validation failed
- * @throws IOException the resources could not be found
*/
- void validate( Configuration iConfig, Object iComponent )
- throws ModelException, IOException;
+ void validate( Configuration iConfig, InputStream iSchema )
+ throws ModelException;
}

Modified:
development/main/metro/composition/impl/src/main/net/dpml/composition/util/XercesSchemaValidator.java
==============================================================================
---
development/main/metro/composition/impl/src/main/net/dpml/composition/util/XercesSchemaValidator.java
(original)
+++
development/main/metro/composition/impl/src/main/net/dpml/composition/util/XercesSchemaValidator.java
Sun Jan 2 23:07:07 2005
@@ -168,36 +168,45 @@
* 3. write the contents of that resource into a temp File
* 4. validate the configuration via Schema validation, using Xerces
SAXParser and providing the schema file location
* 5. delete the temp file
- * @throws IOException
+ * @throws ModelException if validation goes wrong, even if no .xschema
could be found.
* @see
net.dpml.composition.util.ConfigurationValidator#validate(net.dpml.configuration.Configuration,
* net.dpml.meta.info.Type)
*/
- public void validate( Configuration iConfig, Object iComponent )
- throws ModelException, IOException
+ public void validate( Configuration iConfig, InputStream is )
+ throws ModelException
{
if ( iConfig == null )
{
throw new ModelException( "null configuration!" );
}

- Class t_class = iComponent.getClass();
- ClassLoader cl = t_class.getClassLoader();
- String iSchemaLocation = t_class.getName().replace( '.', '/' )
- + ".xschema";
- InputStream is = cl.getResourceAsStream( iSchemaLocation );
+
+ //did we get something?
+ if (null == is)
+ {
+ throw new ModelException( "Schema input stream was null" );
+ }
BufferedReader in = new BufferedReader( new InputStreamReader( is )
);
String inputLine;
+ File tempFile;
StringBuffer buffer = new StringBuffer();
- while ( ( inputLine = in.readLine() ) != null )
+ try
+ {
+ while ( ( inputLine = in.readLine() ) != null )
+ {
+ buffer.append( inputLine );
+ }
+ in.close();
+ tempFile = new File( "temp.xshcema" );
+ FileOutputStream os = new FileOutputStream( tempFile );
+ os.write( buffer.toString().getBytes() );
+ os.flush();
+ os.close();
+ }
+ catch ( Exception e )
{
- buffer.append( inputLine );
+ throw new ModelException( "got an exception", e );
}
- in.close();
- File tempFile = new File( "temp.xshcema" );
- FileOutputStream os = new FileOutputStream( tempFile );
- os.write( buffer.toString().getBytes() );
- os.flush();
- os.close();
try
{
validateConfiguration( tempFile.getAbsolutePath(), iConfig );

Added: development/main/metro/composition/test/etc/test/schemavalidation.xml
==============================================================================
--- (empty file)
+++ development/main/metro/composition/test/etc/test/schemavalidation.xml
Sun Jan 2 23:07:07 2005
@@ -0,0 +1,16 @@
+<container name="test">
+ <classloader>
+ <classpath>
+ <artifact>@AVALON-API-SPEC@</artifact>
+ <artifact>@AVALON-IMPL-SPEC@</artifact>
+ <artifact>@TEST-SCHEMA-SPEC@</artifact>
+ </classpath>
+ </classloader>
+ <container name="sub">
+ <component name="ConfigB"
class="net.dpml.test.testschema.ConfigurableB">
+ <configuration attr1="foo">
+ <child1/>
+ </configuration>
+ </component>
+ </container>
+</container>
\ No newline at end of file

Added:
development/main/metro/composition/test/etc/test/schemavalidation_failing.xml
==============================================================================
--- (empty file)
+++
development/main/metro/composition/test/etc/test/schemavalidation_failing.xml
Sun Jan 2 23:07:07 2005
@@ -0,0 +1,16 @@
+<container name="test">
+ <classloader>
+ <classpath>
+ <artifact>@AVALON-API-SPEC@</artifact>
+ <artifact>@AVALON-IMPL-SPEC@</artifact>
+ <artifact>@TEST-SCHEMA-SPEC@</artifact>
+ </classpath>
+ </classloader>
+ <container name="sub">
+ <component name="ConfigB"
class="net.dpml.test.testschema.ConfigurableB">
+ <configuration attr1="foo">
+ <child1/>
+ </configuration>
+ </component>
+ </container>
+</container>
\ No newline at end of file

Modified:
development/main/metro/composition/test/src/test/net/dpml/composition/util/SchemaValidationTestCase.java
==============================================================================
---
development/main/metro/composition/test/src/test/net/dpml/composition/util/SchemaValidationTestCase.java
(original)
+++
development/main/metro/composition/test/src/test/net/dpml/composition/util/SchemaValidationTestCase.java
Sun Jan 2 23:07:07 2005
@@ -19,13 +19,18 @@

import java.io.ByteArrayInputStream;
import java.io.File;
+import java.io.InputStream;
import java.util.Hashtable;

import junit.framework.TestCase;
+import net.dpml.composition.model.ContainmentModel;
import net.dpml.composition.model.ModelException;
+import net.dpml.composition.model.ModelRuntimeException;
+import net.dpml.composition.test.CompositionTestCase;
import net.dpml.configuration.Configuration;
+import net.dpml.configuration.impl.DefaultConfiguration;
import net.dpml.configuration.impl.DefaultConfigurationBuilder;
-import net.dpml.meta.info.builder.impl.TypeBuilder;
+import net.dpml.test.testschema.ConfigurableA;
import net.dpml.test.testschema.ConfigurableB;

import org.apache.xerces.parsers.SAXParser;
@@ -38,8 +43,10 @@
/**
* testing Schema validation
*/
-public class SchemaValidationTestCase extends TestCase
+public class SchemaValidationTestCase extends CompositionTestCase
{
+
+ private static final String PATH = "schemavalidation.xml";
static final String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";;

static final String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";;
@@ -88,6 +95,9 @@

private Configuration m_config;

+ private InputStream m_schema;
+
+ private ContainmentModel m_model;
/**
* @see junit.framework.TestCase#setUp()
*/
@@ -99,8 +109,43 @@
m_config = builder.build( new ByteArrayInputStream( XML_CONFIGURATION
.getBytes() ) );

+ ConfigurableB confB = new ConfigurableB();
+ String path = confB.getClass().getName().replace( '.', '/' )
+ + ".xschema";
+ m_schema = getClass().getClassLoader().getResourceAsStream(path);
+
+ }
+
+ public void testValidatingAssembly() throws Exception
+ {
+ m_model = createContainmentModel( PATH );
+ try
+ {
+ ContainmentModel root = (ContainmentModel) m_model.getModel( "/"
);
+ root.assemble();
+ }
+ catch ( ModelRuntimeException e )
+ {
+ fail();
+ }
+
}
+ public void testValidatingAssemblyWithoutConfig() throws Exception
+ {
+
+ String path = "schemavalidation_failing.xml";
+ m_model = createContainmentModel( path );
+ try
+ {
+ ContainmentModel root = (ContainmentModel) m_model.getModel( "/"
);
+ root.assemble();
+ }
+ catch ( ModelRuntimeException e )
+ {
+ assertTrue(true);
+ }

+ }
public void testParsingOfValidatingXMLOKWhenUsingFiles() throws Exception
{
SAXParser saxParser = new SAXParser();
@@ -137,23 +182,49 @@

public void testValidationOfComponentConfig() throws Exception
{
- ConfigurableB confB = new ConfigurableB();
try
{
- m_validator.validate( m_config, confB );
+ m_validator.validate( m_config, m_schema );
}
- catch (ModelException e) {
- fail("should bo ok");
+ catch ( ModelException e )
+ {
+ fail( "should bo ok" );
e.printStackTrace();
}
-
+ }

+ public void testValidationWrongComponentConfig() throws Exception
+ {
+ DefaultConfiguration config = new DefaultConfiguration( m_config );
+ config.setAttribute( "weird", "test" );
+ try
+ {
+ m_validator.validate( config, m_schema );
+ }
+ catch ( ModelException e )
+ {
+ assertTrue( true );
+ }
}

- public void testDummyAlwaysTrue()
+ public void testValidationWithNoSchema() throws Exception
{
- // System.out.println(System.getProperties());
- assertTrue( true );
+ try
+ {
+ m_validator.validate( m_config, new
ByteArrayInputStream("hej".getBytes()) );
+ }
+ catch ( ModelException e )
+ {
+ assertTrue( true );
+ }
+ try
+ {
+ m_validator.validate( m_config, null );
+ }
+ catch ( ModelException e )
+ {
+ assertTrue( true );
+ }
}

}

Modified:
development/main/metro/testing/testschema/src/main/net/dpml/test/testschema/ConfigurableB.java
==============================================================================
---
development/main/metro/testing/testschema/src/main/net/dpml/test/testschema/ConfigurableB.java
(original)
+++
development/main/metro/testing/testschema/src/main/net/dpml/test/testschema/ConfigurableB.java
Sun Jan 2 23:07:07 2005
@@ -31,6 +31,9 @@

private Logger m_logger;

+ /**
+ * @metro.configuration schema="true"
+ */
public void configure( Configuration configuration ) throws
ConfigurationException
{
m_logger.info( "inside configure" );



  • svn commit: r1343 - in development/main/metro: composition/api/src/main/net/dpml/composition/model composition/impl/src/main/net/dpml/composition/model/impl composition/impl/src/main/net/dpml/composition/util composition/test/etc/test composition/test/src/test/net/dpml/composition/util testing/testschema/src/main/net/dpml/test/testschema, pneubauer, 01/02/2005

Archive powered by MHonArc 2.6.24.

Top of Page