Changeset 3095


Ignore:
Timestamp:
01/27/12 12:20:06 (4 months ago)
Author:
kasper
Message:

Improved support for @Validate by invoking validation methods at job creation time.

Location:
AnalyzerBeans/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • AnalyzerBeans/trunk/core/src/main/java/org/eobjects/analyzer/descriptors/AbstractMethodDescriptor.java

    r3031 r3095  
    2222import java.io.Serializable; 
    2323import java.lang.annotation.Annotation; 
     24import java.lang.reflect.InvocationTargetException; 
    2425import java.lang.reflect.Method; 
    2526import java.util.Arrays; 
     
    7172                } catch (RuntimeException e) { 
    7273                        throw e; 
     74                } catch (InvocationTargetException e) { 
     75                        Throwable targetException = e.getTargetException(); 
     76                        if (targetException instanceof RuntimeException) { 
     77                                throw (RuntimeException) targetException; 
     78                        } 
     79                        throw new IllegalStateException("Invoked method threw exception", targetException); 
    7380                } catch (Exception e) { 
    7481                        throw new IllegalStateException("Could not invoke method " + getMethod(), e); 
  • AnalyzerBeans/trunk/core/src/main/java/org/eobjects/analyzer/job/builder/AbstractBeanJobBuilder.java

    r2631 r3095  
    2828import org.eobjects.analyzer.descriptors.BeanDescriptor; 
    2929import org.eobjects.analyzer.descriptors.ConfiguredPropertyDescriptor; 
     30import org.eobjects.analyzer.lifecycle.LifeCycleHelper; 
    3031import org.eobjects.analyzer.result.renderer.Renderable; 
    3132import org.eobjects.analyzer.util.ReflectionUtils; 
     
    4445 */ 
    4546@SuppressWarnings("unchecked") 
    46 public class AbstractBeanJobBuilder<D extends BeanDescriptor<E>, E, B> 
    47                 implements Renderable { 
     47public class AbstractBeanJobBuilder<D extends BeanDescriptor<E>, E, B> implements Renderable { 
    4848 
    4949        private final Logger logger = LoggerFactory.getLogger(getClass()); 
     
    5454        private final AnalysisJobBuilder _analysisJobBuilder; 
    5555 
    56         public AbstractBeanJobBuilder(AnalysisJobBuilder analysisJobBuilder, 
    57                         D descriptor, Class<?> builderClass) { 
     56        public AbstractBeanJobBuilder(AnalysisJobBuilder analysisJobBuilder, D descriptor, Class<?> builderClass) { 
    5857                if (analysisJobBuilder == null) { 
    59                         throw new IllegalArgumentException( 
    60                                         "analysisJobBuilder cannot be null"); 
     58                        throw new IllegalArgumentException("analysisJobBuilder cannot be null"); 
    6159                } 
    6260                if (descriptor == null) { 
     
    6967                _descriptor = descriptor; 
    7068                if (!ReflectionUtils.is(getClass(), builderClass)) { 
    71                         throw new IllegalArgumentException( 
    72                                         "Builder class does not correspond to actual class of builder"); 
    73                 } 
    74  
    75                 _configurableBean = ReflectionUtils.newInstance(_descriptor 
    76                                 .getComponentClass()); 
     69                        throw new IllegalArgumentException("Builder class does not correspond to actual class of builder"); 
     70                } 
     71 
     72                _configurableBean = ReflectionUtils.newInstance(_descriptor.getComponentClass()); 
    7773        } 
    7874 
     
    8985        } 
    9086 
    91         public final boolean isConfigured(boolean throwException) 
    92                         throws IllegalStateException, 
     87        public final boolean isConfigured(boolean throwException) throws IllegalStateException, 
    9388                        UnconfiguredConfiguredPropertyException { 
    94                 for (ConfiguredPropertyDescriptor configuredProperty : _descriptor 
    95                                 .getConfiguredProperties()) { 
     89                for (ConfiguredPropertyDescriptor configuredProperty : _descriptor.getConfiguredProperties()) { 
    9690                        if (!isConfigured(configuredProperty, throwException)) { 
    9791                                if (throwException) { 
    98                                         throw new UnconfiguredConfiguredPropertyException(this, 
    99                                                         configuredProperty); 
     92                                        throw new UnconfiguredConfiguredPropertyException(this, configuredProperty); 
    10093                                } else { 
    10194                                        return false; 
     
    10396                        } 
    10497                } 
     98 
     99                try { 
     100                        LifeCycleHelper lifeCycleHelper = new LifeCycleHelper(null, null); 
     101                        lifeCycleHelper.validate(getDescriptor(), getConfigurableBean()); 
     102                } catch (RuntimeException e) { 
     103                        if (throwException) { 
     104                                throw e; 
     105                        } else { 
     106                                return false; 
     107                        } 
     108                } 
     109 
    105110                return true; 
    106111        } 
     
    119124        } 
    120125 
    121         public boolean isConfigured( 
    122                         ConfiguredPropertyDescriptor configuredProperty, 
    123                         boolean throwException) { 
     126        public boolean isConfigured(ConfiguredPropertyDescriptor configuredProperty, boolean throwException) { 
    124127                if (configuredProperty.isRequired()) { 
    125128                        Map<ConfiguredPropertyDescriptor, Object> configuredProperties = getConfiguredProperties(); 
     
    132135                        if (value == null) { 
    133136                                if (throwException) { 
    134                                         throw new UnconfiguredConfiguredPropertyException(this, 
    135                                                         configuredProperty); 
     137                                        throw new UnconfiguredConfiguredPropertyException(this, configuredProperty); 
    136138                                } else { 
    137                                         logger.debug("Configured property is not set: " 
    138                                                         + configuredProperty); 
     139                                        logger.debug("Configured property is not set: " + configuredProperty); 
    139140                                        return false; 
    140141                                } 
     
    145146 
    146147        public B setConfiguredProperty(String configuredName, Object value) { 
    147                 ConfiguredPropertyDescriptor configuredProperty = _descriptor 
    148                                 .getConfiguredProperty(configuredName); 
     148                ConfiguredPropertyDescriptor configuredProperty = _descriptor.getConfiguredProperty(configuredName); 
    149149                if (configuredProperty == null) { 
    150                         throw new IllegalArgumentException("No such configured property: " 
    151                                         + configuredName); 
     150                        throw new IllegalArgumentException("No such configured property: " + configuredName); 
    152151                } 
    153152                return setConfiguredProperty(configuredProperty, value); 
    154153        } 
    155154 
    156         public B setConfiguredProperty( 
    157                         ConfiguredPropertyDescriptor configuredProperty, Object value) { 
     155        public B setConfiguredProperty(ConfiguredPropertyDescriptor configuredProperty, Object value) { 
    158156                if (configuredProperty == null) { 
    159                         throw new IllegalArgumentException( 
    160                                         "configuredProperty cannot be null"); 
     157                        throw new IllegalArgumentException("configuredProperty cannot be null"); 
    161158                } 
    162159                if (value != null) { 
     
    168165                                                Object valuePart = Array.get(value, i); 
    169166                                                if (valuePart != null) { 
    170                                                         if (!ReflectionUtils.is(valuePart.getClass(), 
    171                                                                         configuredProperty.getBaseType())) { 
     167                                                        if (!ReflectionUtils.is(valuePart.getClass(), configuredProperty.getBaseType())) { 
    172168                                                                correctType = false; 
    173169                                                        } 
     
    175171                                        } 
    176172                                } else { 
    177                                         if (!ReflectionUtils.is(value.getClass(), 
    178                                                         configuredProperty.getBaseType())) { 
     173                                        if (!ReflectionUtils.is(value.getClass(), configuredProperty.getBaseType())) { 
    179174                                                correctType = false; 
    180175                                        } 
    181176                                } 
    182177                        } else { 
    183                                 if (!ReflectionUtils.is(value.getClass(), 
    184                                                 configuredProperty.getBaseType())) { 
     178                                if (!ReflectionUtils.is(value.getClass(), configuredProperty.getBaseType())) { 
    185179                                        correctType = false; 
    186180                                } 
    187181                        } 
    188182                        if (!correctType) { 
    189                                 throw new IllegalArgumentException("Invalid value type: " 
    190                                                 + value.getClass().getName() + ", expected: " 
     183                                throw new IllegalArgumentException("Invalid value type: " + value.getClass().getName() + ", expected: " 
    191184                                                + configuredProperty.getBaseType().getName()); 
    192185                        } 
     
    200193        public Map<ConfiguredPropertyDescriptor, Object> getConfiguredProperties() { 
    201194                Map<ConfiguredPropertyDescriptor, Object> map = new HashMap<ConfiguredPropertyDescriptor, Object>(); 
    202                 Set<ConfiguredPropertyDescriptor> configuredProperties = getDescriptor() 
    203                                 .getConfiguredProperties(); 
     195                Set<ConfiguredPropertyDescriptor> configuredProperties = getDescriptor().getConfiguredProperties(); 
    204196                for (ConfiguredPropertyDescriptor propertyDescriptor : configuredProperties) { 
    205197                        Object value = getConfiguredProperty(propertyDescriptor); 
     
    218210        } 
    219211 
    220         public Object getConfiguredProperty( 
    221                         ConfiguredPropertyDescriptor propertyDescriptor) { 
     212        public Object getConfiguredProperty(ConfiguredPropertyDescriptor propertyDescriptor) { 
    222213                return propertyDescriptor.getValue(getConfigurableBean()); 
    223214        } 
  • AnalyzerBeans/trunk/core/src/main/java/org/eobjects/analyzer/job/builder/AnalyzerJobBuilder.java

    r2786 r3095  
    179179                        if (_inputColumns.isEmpty()) { 
    180180                                if (throwException) { 
    181                                         throw new IllegalStateException("No input columns have been added!"); 
     181                                        throw new IllegalStateException("No input columns have been added to " + this); 
    182182                                } else { 
    183183                                        return false; 
  • AnalyzerBeans/trunk/core/src/test/java/org/eobjects/analyzer/job/builder/AnalysisJobBuilderTest.java

    r3053 r3095  
    3131import org.easymock.EasyMock; 
    3232import org.easymock.IArgumentMatcher; 
     33import org.eobjects.analyzer.beans.NumberAnalyzer; 
    3334import org.eobjects.analyzer.beans.StringAnalyzer; 
    3435import org.eobjects.analyzer.beans.convert.ConvertToStringTransformer; 
     
    3637import org.eobjects.analyzer.beans.filter.NullCheckFilter; 
    3738import org.eobjects.analyzer.beans.filter.NullCheckFilter.NullCheckCategory; 
     39import org.eobjects.analyzer.beans.filter.NumberRangeFilter; 
     40import org.eobjects.analyzer.beans.filter.RangeFilterCategory; 
    3841import org.eobjects.analyzer.beans.filter.ValidationCategory; 
    3942import org.eobjects.analyzer.beans.standardize.EmailStandardizerTransformer; 
     
    7982        } 
    8083 
     84        public void testValidate() throws Exception { 
     85                analysisJobBuilder.addSourceColumns("PUBLIC.EMPLOYEES.REPORTSTO"); 
     86                MetaModelInputColumn reportsToColumn = analysisJobBuilder.getSourceColumns().get(0); 
     87 
     88                FilterJobBuilder<NumberRangeFilter, RangeFilterCategory> filter = analysisJobBuilder 
     89                                .addFilter(NumberRangeFilter.class); 
     90                filter.addInputColumn(reportsToColumn); 
     91                filter.setConfiguredProperty("Lowest value", 2.0); 
     92                filter.setConfiguredProperty("Highest value", 1.0); 
     93                analysisJobBuilder.addAnalyzer(NumberAnalyzer.class).addInputColumn(reportsToColumn); 
     94 
     95                try { 
     96                        analysisJobBuilder.isConfigured(true); 
     97                        fail("Exception expected"); 
     98                } catch (Exception e) { 
     99                        assertEquals("Lowest value is greater than the highest value", e.getMessage()); 
     100                } 
     101        } 
     102 
    81103        public void testPreventCyclicFilterDependencies() throws Exception { 
    82104                analysisJobBuilder.addSourceColumns("PUBLIC.EMPLOYEES.REPORTSTO"); 
  • AnalyzerBeans/trunk/env/xml-config/src/main/java/org/eobjects/analyzer/job/JaxbJobReader.java

    r3046 r3095  
    481481                                                                .getOutput(); 
    482482 
    483                                                 assert outputColumns.size() == output.size(); 
     483                                                if (outputColumns.size() != output.size()) { 
     484                                                        throw new IllegalStateException("Expected " 
     485                                                                        + outputColumns.size() 
     486                                                                        + " output column, but found " 
     487                                                                        + output.size() + " (" 
     488                                                                        + transformerJobBuilder + ")"); 
     489                                                } 
    484490 
    485491                                                for (int i = 0; i < output.size(); i++) { 
Note: See TracChangeset for help on using the changeset viewer.