commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r828915 - in /commons/sandbox/validator2/branches/alternative/validation-api: ./ src/main/java/javax/validation/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/commons/ src/test/java/org/apach...
Date Fri, 23 Oct 2009 02:03:03 GMT
Author: niallp
Date: Fri Oct 23 02:03:01 2009
New Revision: 828915

URL: http://svn.apache.org/viewvc?rev=828915&view=rev
Log:
Implement Validation methods

Added:
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/AnotherMockConfiguration.java
  (with props)
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/AnotherMockValidationProvider.java
  (with props)
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockConfiguration.java
  (with props)
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockValidationProvider.java
  (with props)
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockValidatorFactory.java
  (with props)
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/ValidationTest.java
  (with props)
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/resources/
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/resources/META-INF/
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/resources/META-INF/services/
    commons/sandbox/validator2/branches/alternative/validation-api/src/test/resources/META-INF/services/javax.validation.spi.ValidationProvider
  (with props)
Modified:
    commons/sandbox/validator2/branches/alternative/validation-api/pom.xml
    commons/sandbox/validator2/branches/alternative/validation-api/src/main/java/javax/validation/Validation.java

Modified: commons/sandbox/validator2/branches/alternative/validation-api/pom.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/validator2/branches/alternative/validation-api/pom.xml?rev=828915&r1=828914&r2=828915&view=diff
==============================================================================
--- commons/sandbox/validator2/branches/alternative/validation-api/pom.xml (original)
+++ commons/sandbox/validator2/branches/alternative/validation-api/pom.xml Fri Oct 23 02:03:01
2009
@@ -36,7 +36,7 @@
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
-            <version>3.8.2</version>
+            <version>4.7</version>
             <scope>test</scope>
         </dependency>
     </dependencies>

Modified: commons/sandbox/validator2/branches/alternative/validation-api/src/main/java/javax/validation/Validation.java
URL: http://svn.apache.org/viewvc/commons/sandbox/validator2/branches/alternative/validation-api/src/main/java/javax/validation/Validation.java?rev=828915&r1=828914&r2=828915&view=diff
==============================================================================
--- commons/sandbox/validator2/branches/alternative/validation-api/src/main/java/javax/validation/Validation.java
(original)
+++ commons/sandbox/validator2/branches/alternative/validation-api/src/main/java/javax/validation/Validation.java
Fri Oct 23 02:03:01 2009
@@ -16,8 +16,20 @@
  */
 package javax.validation;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
 import javax.validation.bootstrap.GenericBootstrap;
 import javax.validation.bootstrap.ProviderSpecificBootstrap;
+import javax.validation.spi.BootstrapState;
 import javax.validation.spi.ValidationProvider;
 
 /**
@@ -28,16 +40,321 @@
  */
 public class Validation {
 
+    private static final Logger logger = Logger.getLogger(Validation.class.getName());
+
+    /**
+     * Creates a {@link ValidatorFactory} using the {@link GenericBootstrap}
+     * implementation and the first {@link ValidationProvider} found by
+     * the default {@link ValidationProviderResolver}.
+     *
+     * @return The validator factory
+     */
     public static ValidatorFactory buildDefaultValidatorFactory() {
         return byDefaultProvider().configure().buildValidatorFactory();
     }
 
+    /**
+     * Creates a new GenericBootstrap instance.
+     *
+     * @return a new GenericBootstrap instancee
+     */
     public static GenericBootstrap byDefaultProvider() {
-        throw new UnsupportedOperationException();
+        return new GenericBootstrapImpl();
     }
 
+    /**
+     * Creates a new ProviderSpecificBootstrap instance.
+     * 
+     * @param type The type of provider to find
+     * @return a new ProviderSpecificBootstrap instancee
+     * @throws ValidationException if the type is null
+     */
     public static <T extends Configuration<T>, U extends ValidationProvider<T>>
         ProviderSpecificBootstrap<T> byProvider(Class<U> type) {
-        throw new UnsupportedOperationException();
+        if (type == null) {
+            throw new ValidationException("Type is missing");
+        }
+        return new ProviderSpecificBootstrapImpl<T, U>(type);
+    }
+
+    /**
+     * {@link GenericBootstrap} implementation.
+     */
+    private static class GenericBootstrapImpl extends BaseBootstrapImpl implements GenericBootstrap
{
+
+        /**
+         * Set the validation provider resolver.
+         */
+        public GenericBootstrap providerResolver(ValidationProviderResolver resolver) {
+            setValidationProviderResolver(resolver);
+            return this;
+        }
+
+        /**
+         * Create configuration.
+         */
+        public Configuration<?> configure() {
+            
+            ValidationProvider<?> provider = getValidationProvider();
+
+            Configuration<?> configuration;
+            try {
+                configuration = provider.createGenericConfiguration(this);
+            } catch (Exception e) {
+                throw new ValidationException("Error creating configuration", e);
+            }
+
+            return configuration;
+        }
+    }
+
+    /**
+     * {@link ProviderSpecificBootstrap} implementation.
+     */
+    private static class ProviderSpecificBootstrapImpl<T extends Configuration<T>,
U extends ValidationProvider<T>>
+        extends BaseBootstrapImpl implements ProviderSpecificBootstrap<T> {
+
+        private final Class<U> providerClass;
+
+        /**
+         * Construct a new ProviderSpecificBootstrap} for the specified provider class.
+         */
+        private ProviderSpecificBootstrapImpl(Class<U> providerClass) {
+            this.providerClass = providerClass;
+        }
+
+        /**
+         * Set the validation provider resolver.
+         */
+        public ProviderSpecificBootstrap<T> providerResolver(ValidationProviderResolver
resolver) {
+            setValidationProviderResolver(resolver);
+            return this;
+        }
+
+        /**
+         * Create configuration.
+         */
+        @SuppressWarnings("unchecked")
+        public T configure() {
+
+            U provider = (U)getValidationProvider();
+
+            T configuration;
+            try {
+                configuration = provider.createSpecializedConfiguration(this);
+            } catch (Exception e) {
+                throw new ValidationException("Error creating configuration", e);
+            }
+
+            return configuration;
+        }
+
+        /**
+         * Returns the selected validation provider from the list.
+         */
+        ValidationProvider<?> getValidationProvider(List<ValidationProvider<?>>
providers) {
+
+            ValidationProvider<?> provider = null;
+            if (providers != null) {
+                for (ValidationProvider<?> validationProvider : providers) {
+                    if (providerClass.isAssignableFrom(validationProvider.getClass())) {
+                        provider = validationProvider;
+                        break;
+                    }
+                }
+            }
+            if (provider == null) {
+                throw new ValidationException("ValidationProvider '" + providerClass.getName()
+ "' Not Found");
+            }
+            return provider;
+        }
+    }
+
+    /**
+     * Base implementation for {@link GenericBootstrap} and {@link ProviderSpecificBootstrap}.
+     */
+    private static class BaseBootstrapImpl implements BootstrapState {
+
+        private ValidationProvider<?> provider;
+        private ValidationProviderResolver defaultResolver;
+        private ValidationProviderResolver resolver;
+
+        /**
+         * @see BootstrapState#getDefaultValidationProviderResolver()
+         */
+        public ValidationProviderResolver getDefaultValidationProviderResolver() {
+            if (defaultResolver == null) {
+                defaultResolver = new DefaultValidationProviderResolver();
+            }
+            return defaultResolver;
+        }
+
+        /**
+         * @see BootstrapState#getValidationProviderResolver()
+         */
+        public ValidationProviderResolver getValidationProviderResolver() {
+            return resolver;
+        }
+
+        /**
+         * Set the validation provider resolver.
+         */
+        void setValidationProviderResolver(ValidationProviderResolver resolver) {
+            this.resolver = resolver;
+            this.provider = null; // set to null to force re-intialization
+        }
+
+        /**
+         * Return the validation provider.
+         */
+        ValidationProvider<?> getValidationProvider() {
+
+            ValidationProvider<?> provider = this.provider;
+            if (provider != null) {
+                return provider;
+            }
+
+            // Find the resolver
+            ValidationProviderResolver resolver = getValidationProviderResolver();
+            if (resolver == null) {
+                resolver = getDefaultValidationProviderResolver();
+            }
+            if (resolver == null) {
+                throw new ValidationException("No validation provider found");
+            }
+
+            // Find the providers
+            List<ValidationProvider<?>> providers;
+            try {
+                providers = resolver.getValidationProviders();
+            } catch (Exception e) {
+                throw new ValidationException("Error getting validation providers", e);
+            }
+
+            provider = getValidationProvider(providers);
+
+            this.provider = provider;
+
+            return provider;
+        }
+
+        /**
+         * Returns the first validation provider in the list.
+         */
+        ValidationProvider<?> getValidationProvider(List<ValidationProvider<?>>
providers) {
+            ValidationProvider<?> provider = null;
+            if (providers != null && providers.size() > 0) {
+                provider = providers.get(0);
+            }
+            if (provider == null) {
+                throw new ValidationException("Default provider not found");
+            }
+            return provider;
+        }
+
+    }
+
+    /**
+     * Default {@link ValidationProviderResolver} implementation.
+     */
+    private static class DefaultValidationProviderResolver implements ValidationProviderResolver
{
+
+        private static final String LOCATION = "META-INF/services/" + ValidationProvider.class.getName();
+
+        /**
+         * Return the validation providers.
+         */
+        public List<ValidationProvider<?>> getValidationProviders() {
+            return loadProviders();
+        }
+
+        /**
+         * Load the validation providers
+         */
+        private List<ValidationProvider<?>> loadProviders() {
+
+            List<ValidationProvider<?>> providers = new ArrayList<ValidationProvider<?>>();
+
+            // Get the class loader
+            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+            if (classLoader == null) {
+                classLoader = Validation.class.getClassLoader();
+            }
+
+            // Get the resources
+            Enumeration<URL> resources;
+            try {
+                resources = classLoader.getResources(LOCATION);
+            } catch (IOException e) {
+                throw new ValidationException("Error loading resources from: " + LOCATION,
e);
+            }
+
+            // Load providers from the resources
+            while (resources.hasMoreElements()) {
+                loadProvidersFromResource(resources.nextElement(), classLoader, providers);
+            }
+
+            return providers;
+        }
+
+        /**
+         * Load the validation providers from the resource URL
+         */
+        private void loadProvidersFromResource(URL resource, ClassLoader classLoader, List<ValidationProvider<?>>
providers) {
+
+            InputStream stream = null;
+            try {
+                stream = resource.openStream();
+                BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
+                String line = reader.readLine();
+                while (line  != null) {
+                    if (!line.startsWith("#")) {
+                        line = line.trim();
+                        ValidationProvider<?> provider = null;
+                        if (line.length() > 0) {
+                            provider = loadProvider(line, classLoader, resource);
+                        }
+                        if (provider != null) {
+                            providers.add(provider);
+                        }
+                    }
+                    line = reader.readLine();
+                }
+            } catch (IOException e) {
+                logger.log(Level.SEVERE, "Error loading resources from: " + resource, e);
+                //throw new ValidationException("Error loading resources from: " + url, e);
+            } finally {
+                try {
+                    if (stream != null) {
+                        stream.close();
+                    }
+                } catch (Exception ignore) {
+                    // ignore errors on close
+                }
+            }
+        }
+
+        /**
+         * Load a validation provider.
+         */
+        private ValidationProvider<?> loadProvider(String className, ClassLoader classLoader,
URL resource) {
+
+            ValidationProvider<?> provider = null;
+            try {
+                Class<?> clazz = classLoader.loadClass(className);
+                if (ValidationProvider.class.isAssignableFrom(clazz)) {
+                    provider = (ValidationProvider<?>)clazz.newInstance();
+                } else {
+                    logger.severe("Class '" + className + "' is not a ValidationProvider
implementation  (from resource " + resource + ")");
+                }
+            } catch (ClassNotFoundException e) {
+                logger.severe("Validation provider class '" + className + "' not found (from
resource " + resource + ")");
+            } catch (Exception e) {
+                logger.log(Level.SEVERE, "Error creating validation provider instance '"
+ className + "'  (from resource " + resource + ")", e);
+            }
+
+            return provider;
+        }
+        
     }
 }

Added: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/AnotherMockConfiguration.java
URL: http://svn.apache.org/viewvc/commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/AnotherMockConfiguration.java?rev=828915&view=auto
==============================================================================
--- commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/AnotherMockConfiguration.java
(added)
+++ commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/AnotherMockConfiguration.java
Fri Oct 23 02:03:01 2009
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * 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.
+ */
+package org.apache.commons.validation.apitest;
+
+import java.io.InputStream;
+
+import javax.validation.Configuration;
+import javax.validation.ConstraintValidatorFactory;
+import javax.validation.MessageInterpolator;
+import javax.validation.TraversableResolver;
+import javax.validation.ValidatorFactory;
+
+/**
+ * Mock {@link Configuration} implementation.
+ *
+ * @version $Revision$ $Date$
+ */
+public class AnotherMockConfiguration implements Configuration<AnotherMockConfiguration>
{
+
+    public AnotherMockConfiguration addMapping(InputStream input) {
+        return this;
+    }
+
+    public AnotherMockConfiguration addProperty(String name, String value) {
+        return this;
+    }
+
+    public ValidatorFactory buildValidatorFactory() {
+        return new MockValidatorFactory();
+    }
+
+    public AnotherMockConfiguration constraintValidatorFactory(
+            ConstraintValidatorFactory factory) {
+        return this;
+    }
+
+    public ConstraintValidatorFactory getDefaultConstraintValidatorFactory() {
+        return null;
+    }
+
+    public MessageInterpolator getDefaultMessageInterpolator() {
+        return null;
+    }
+
+    public TraversableResolver getDefaultTraversableResolver() {
+        return null;
+    }
+
+    public AnotherMockConfiguration ignoreXmlConfiguration() {
+        return this;
+    }
+
+    public AnotherMockConfiguration messageInterpolator(
+            MessageInterpolator interpolator) {
+        return this;
+    }
+
+    public AnotherMockConfiguration traversableResolver(TraversableResolver resolver) {
+        return this;
+    }
+
+    
+}

Propchange: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/AnotherMockConfiguration.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/AnotherMockConfiguration.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/AnotherMockValidationProvider.java
URL: http://svn.apache.org/viewvc/commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/AnotherMockValidationProvider.java?rev=828915&view=auto
==============================================================================
--- commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/AnotherMockValidationProvider.java
(added)
+++ commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/AnotherMockValidationProvider.java
Fri Oct 23 02:03:01 2009
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * 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.
+ */
+package org.apache.commons.validation.apitest;
+
+import javax.validation.Configuration;
+import javax.validation.ValidatorFactory;
+import javax.validation.spi.BootstrapState;
+import javax.validation.spi.ConfigurationState;
+import javax.validation.spi.ValidationProvider;
+
+/**
+ * Mock {@link ValidationProvider} implementation.
+ *
+ * @version $Revision$ $Date$
+ */
+public class AnotherMockValidationProvider implements ValidationProvider<AnotherMockConfiguration>{
+
+    public ValidatorFactory buildValidatorFactory(ConfigurationState state) {
+        return new MockValidatorFactory();
+    }
+
+    public Configuration<?> createGenericConfiguration(BootstrapState state) {
+        return new AnotherMockConfiguration();
+    }
+
+    public AnotherMockConfiguration createSpecializedConfiguration(BootstrapState state)
{
+        return new AnotherMockConfiguration();
+    }
+
+    
+}

Propchange: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/AnotherMockValidationProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/AnotherMockValidationProvider.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockConfiguration.java
URL: http://svn.apache.org/viewvc/commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockConfiguration.java?rev=828915&view=auto
==============================================================================
--- commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockConfiguration.java
(added)
+++ commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockConfiguration.java
Fri Oct 23 02:03:01 2009
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * 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.
+ */
+package org.apache.commons.validation.apitest;
+
+import java.io.InputStream;
+
+import javax.validation.Configuration;
+import javax.validation.ConstraintValidatorFactory;
+import javax.validation.MessageInterpolator;
+import javax.validation.TraversableResolver;
+import javax.validation.ValidatorFactory;
+
+/**
+ * Mock {@link Configuration} implementation.
+ *
+ * @version $Revision$ $Date$
+ */
+public class MockConfiguration implements Configuration<MockConfiguration> {
+
+    public MockConfiguration addMapping(InputStream input) {
+        return this;
+    }
+
+    public MockConfiguration addProperty(String name, String value) {
+        return this;
+    }
+
+    public ValidatorFactory buildValidatorFactory() {
+        return new MockValidatorFactory();
+    }
+
+    public MockConfiguration constraintValidatorFactory(
+            ConstraintValidatorFactory factory) {
+        return this;
+    }
+
+    public ConstraintValidatorFactory getDefaultConstraintValidatorFactory() {
+        return null;
+    }
+
+    public MessageInterpolator getDefaultMessageInterpolator() {
+        return null;
+    }
+
+    public TraversableResolver getDefaultTraversableResolver() {
+        return null;
+    }
+
+    public MockConfiguration ignoreXmlConfiguration() {
+        return this;
+    }
+
+    public MockConfiguration messageInterpolator(
+            MessageInterpolator interpolator) {
+        return this;
+    }
+
+    public MockConfiguration traversableResolver(TraversableResolver resolver) {
+        return this;
+    }
+
+    
+}

Propchange: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockConfiguration.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockConfiguration.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockValidationProvider.java
URL: http://svn.apache.org/viewvc/commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockValidationProvider.java?rev=828915&view=auto
==============================================================================
--- commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockValidationProvider.java
(added)
+++ commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockValidationProvider.java
Fri Oct 23 02:03:01 2009
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * 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.
+ */
+package org.apache.commons.validation.apitest;
+
+import javax.validation.Configuration;
+import javax.validation.ValidatorFactory;
+import javax.validation.spi.BootstrapState;
+import javax.validation.spi.ConfigurationState;
+import javax.validation.spi.ValidationProvider;
+
+/**
+ * Mock {@link ValidationProvider} implementation.
+ *
+ * @version $Revision$ $Date$
+ */
+public class MockValidationProvider implements ValidationProvider<MockConfiguration>{
+
+    public ValidatorFactory buildValidatorFactory(ConfigurationState state) {
+        return new MockValidatorFactory();
+    }
+
+    public Configuration<?> createGenericConfiguration(BootstrapState state) {
+        return new MockConfiguration();
+    }
+
+    public MockConfiguration createSpecializedConfiguration(BootstrapState state) {
+        return new MockConfiguration();
+    }
+
+    
+}

Propchange: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockValidationProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockValidationProvider.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockValidatorFactory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockValidatorFactory.java?rev=828915&view=auto
==============================================================================
--- commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockValidatorFactory.java
(added)
+++ commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockValidatorFactory.java
Fri Oct 23 02:03:01 2009
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * 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.
+ */
+package org.apache.commons.validation.apitest;
+
+import javax.validation.ConstraintValidatorFactory;
+import javax.validation.MessageInterpolator;
+import javax.validation.TraversableResolver;
+import javax.validation.Validator;
+import javax.validation.ValidatorContext;
+import javax.validation.ValidatorFactory;
+
+/**
+ * Mock {@link ValidatorFactory} implementation.
+ *
+ * @version $Revision$ $Date$
+ */
+public class MockValidatorFactory implements ValidatorFactory {
+
+    public ConstraintValidatorFactory getConstraintValidatorFactory() {
+        return null;
+    }
+
+    public MessageInterpolator getMessageInterpolator() {
+        return null;
+    }
+
+    public TraversableResolver getTraversableResolver() {
+        return null;
+    }
+
+    public Validator getValidator() {
+        return null;
+    }
+
+    public <T> T unwrap(Class<T> type) {
+        return null;
+    }
+
+    public ValidatorContext usingContext() {
+        return null;
+    }
+
+    
+}

Propchange: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockValidatorFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/MockValidatorFactory.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/ValidationTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/ValidationTest.java?rev=828915&view=auto
==============================================================================
--- commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/ValidationTest.java
(added)
+++ commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/ValidationTest.java
Fri Oct 23 02:03:01 2009
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * 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.
+ */
+package org.apache.commons.validation.apitest;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+
+import javax.validation.Configuration;
+import javax.validation.Validation;
+import javax.validation.ValidationException;
+import javax.validation.bootstrap.GenericBootstrap;
+import javax.validation.bootstrap.ProviderSpecificBootstrap;
+
+
+/**
+ * Test case for {@link Validation}.
+ *
+ * @version $Revision$ $Date$
+ * @since 1.0
+ */
+public class ValidationTest {
+
+    /**
+     * Default Constructor.
+     */
+    public ValidationTest() {
+    }
+
+    @Test
+    public void testBuildDefaultValidatorFactory() {
+        assertNotNull(Validation.buildDefaultValidatorFactory());
+    }
+
+    @Test
+    public void testByDefaultProvider() {
+        assertNotNull(Validation.byDefaultProvider());
+        GenericBootstrap bootstrap = Validation.byDefaultProvider();
+        assertNotNull("GenericBootstrap is null",  bootstrap);
+
+        Configuration<?> configuration = bootstrap.configure();
+        assertNotNull("Configuration is null",  configuration);
+        assertEquals("MockConfiguration type",  MockConfiguration.class, configuration.getClass());
+    }
+
+    @Test
+    public void testByProvider_MockValidationProvider() {
+        ProviderSpecificBootstrap<MockConfiguration> bootstrap = Validation.byProvider(MockValidationProvider.class);
+        assertNotNull("ProviderSpecificBootstrap is null",  bootstrap);
+
+        MockConfiguration configuration = bootstrap.configure();
+        assertNotNull("Configuration is null",  configuration);
+    }
+
+    @Test
+    public void testByProvider_AnotherMockValidationProvider() {
+        ProviderSpecificBootstrap<AnotherMockConfiguration> bootstrap = Validation.byProvider(AnotherMockValidationProvider.class);
+        assertNotNull("ProviderSpecificBootstrap is null",  bootstrap);
+
+        AnotherMockConfiguration configuration = bootstrap.configure();
+        assertNotNull("Configuration is null",  configuration);
+    }
+
+    @Test
+    public void testByProvider_NullClass() {
+        try {
+            Validation.byProvider((Class<MockValidationProvider>)null);
+            fail("Expected ValidationException");
+        } catch (ValidationException e) {
+            // expected result
+        }
+    }
+}

Propchange: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/ValidationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/validator2/branches/alternative/validation-api/src/test/java/org/apache/commons/validation/apitest/ValidationTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: commons/sandbox/validator2/branches/alternative/validation-api/src/test/resources/META-INF/services/javax.validation.spi.ValidationProvider
URL: http://svn.apache.org/viewvc/commons/sandbox/validator2/branches/alternative/validation-api/src/test/resources/META-INF/services/javax.validation.spi.ValidationProvider?rev=828915&view=auto
==============================================================================
--- commons/sandbox/validator2/branches/alternative/validation-api/src/test/resources/META-INF/services/javax.validation.spi.ValidationProvider
(added)
+++ commons/sandbox/validator2/branches/alternative/validation-api/src/test/resources/META-INF/services/javax.validation.spi.ValidationProvider
Fri Oct 23 02:03:01 2009
@@ -0,0 +1,27 @@
+#   Licensed to the Apache Software Foundation (ASF) under one or more
+#   contributor license agreements.  See the NOTICE file distributed with
+#   this work for additional information regarding copyright ownership.
+#   The ASF licenses this file to You 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
+#
+#   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.
+
+# Default Test Provider
+org.apache.commons.validation.apitest.MockValidationProvider
+
+# An invalid class name
+foo.bar.InvalidClassName
+
+# Not a ValidationProvider implementation
+org.apache.commons.validation.apitest.MockValidatorFactory
+
+# Another valid provider implementation
+org.apache.commons.validation.apitest.AnotherMockValidationProvider
+

Propchange: commons/sandbox/validator2/branches/alternative/validation-api/src/test/resources/META-INF/services/javax.validation.spi.ValidationProvider
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message