bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From carlosv...@apache.org
Subject svn commit: r956027 - in /incubator/bval/sandbox/jsr303-impl-bench/src/main: java/org/apache/bval/bench/ java/org/apache/bval/bench/jsr303/ java/org/apache/bval/bench/metatree/ resources/
Date Fri, 18 Jun 2010 15:46:57 GMT
Author: carlosvara
Date: Fri Jun 18 15:46:57 2010
New Revision: 956027

URL: http://svn.apache.org/viewvc?rev=956027&view=rev
Log:
Nested constraints and inheritance

Modified:
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/BasicType.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Config.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Generator.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/jsr303/Jsr303Annotator.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/MetaJavaBean.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/resources/generator.default.properties

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/BasicType.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/BasicType.java?rev=956027&r1=956026&r2=956027&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/BasicType.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/BasicType.java
Fri Jun 18 15:46:57 2010
@@ -39,13 +39,13 @@ public enum BasicType {
         public Class<?> getTypeClass() {
             return Integer.class;
         }
-    }/*,
+    },
     INT {
         @Override
         public Class<?> getTypeClass() {
             return int.class;
         }
-    }*/;
+    };
     
     
     public abstract Class<?> getTypeClass();

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Config.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Config.java?rev=956027&r1=956026&r2=956027&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Config.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Config.java
Fri Jun 18 15:46:57 2010
@@ -26,8 +26,6 @@ import java.util.Random;
 
 
 // TODO
-// % of valid basic fields values
-// % of ref fields annotated with @NotNull
 // % of ref beans that reference already existing beans (circular deps)
 
 
@@ -46,6 +44,7 @@ public enum Config {
 
     private final Random r;
     private final Properties p;
+    private int uniqueSeq = 0;
 
     private Config() {
 
@@ -56,6 +55,10 @@ public enum Config {
         r = new Random(new Long(p.getProperty("generator.rnd.seed")));
     }
 
+    public int nextUniqueNum() {
+        return uniqueSeq++;
+    }
+
     /**
      * @return The base directory where the generated classes will be output.
      */
@@ -101,18 +104,22 @@ public enum Config {
     }
 
     /**
-     * @param <T> The type of elements of the list.
-     * @param elements List of possible choices.
+     * @return The total number of base beans to generate.
+     */
+    public int getNumBaseBeans() {
+        return new Integer(p.getProperty("generator.numbasebeans"));
+    }
+
+    /**
+     * @param <T>
+     *            The type of elements of the list.
+     * @param elements
+     *            List of possible choices.
      * @return A random element from the list of elements.
      */
     public <T> T getRandom(List<T> elements) {
         return elements.get(r.nextInt(elements.size()));
     }
-    
-    // TODO: remove this method
-    public Random getRandomGen() {
-        return r;
-    }
 
     /**
      * @return The number of basic fields that a bean should have.
@@ -160,6 +167,22 @@ public enum Config {
         return r.nextDouble() < new Double(p.getProperty("generator.beanswithgroups"));
     }
 
+    /**
+     * @return <code>true</code> if it's determined that a valid value should
be
+     *         assigned to the current field, <code>false</code> otherwise.
+     */
+    public boolean shouldAssignValidValue() {
+        return r.nextDouble() < new Double(p.getProperty("generator.validvalues"));
+    }
+
+    /**
+     * @return <code>true</code> if it's determined that the current bean should
+     *         inherit from one of the base beans, <code>false</code> otherwise.
+     */
+    public boolean shouldInherit() {
+        return r.nextDouble() < new Double(p.getProperty("generator.inheritanceratio"));
+    }
+
 
     /**
      * @return A properties object with the default configuration values

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Generator.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Generator.java?rev=956027&r1=956026&r2=956027&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Generator.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Generator.java
Fri Jun 18 15:46:57 2010
@@ -54,6 +54,7 @@ public class Generator {
 
         // And the lists of beans
         List<MetaJavaBean> beans = Lists.newArrayListWithExpectedSize(Config.CFG.getNumBeans());
+        List<MetaJavaBean> baseBeans = Lists.newArrayList();
 
 
         // PHASE 1: Basic beans with basic type fields
@@ -61,20 +62,33 @@ public class Generator {
             MetaJavaBean mjb = new MetaJavaBean(cm, Config.CFG.getBeansPackageName() + ".Bean"
+ i, Config.CFG.rndNumFields());
             beans.add(mjb);
         }
+        for (int i = 0; i < Config.CFG.getNumBaseBeans(); ++i) {
+            MetaJavaBean mjb = new MetaJavaBean(cm, Config.CFG.getBeansPackageName() + ".BaseBean"
+ i, Config.CFG.rndNumFields());
+            baseBeans.add(mjb);
+        }
 
         // PHASE 2: Enrich beans with references to other beans
         for (MetaJavaBean mjb : beans) {
             mjb.interrelate(beans);
         }
 
-        // PHASE 3: Annotate the beans with the JSR-303 annotator (other
-        // annotators can be added)
+        // PHASE 3: Inheritance
+        for (MetaJavaBean mjb : beans) {
+            if (Config.CFG.shouldInherit()) {
+                mjb.setSuperClass(Config.CFG.getRandom(baseBeans));
+            }
+        }
+
+        // PHASE 4: Annotate the beans with the JSR-303 annotator (other annotators can be
added)
         Jsr303Annotator annotator = new Jsr303Annotator(cm);
         for (MetaJavaBean mjb : beans) {
             mjb.accept(annotator);
         }
+        for (MetaJavaBean bmjb : baseBeans) {
+            bmjb.accept(annotator);
+        }
 
-        // PHASE 4: Generate population code
+        // PHASE 5: Generate population code
         generateHolderClass(cm, beans);
 
 
@@ -102,27 +116,23 @@ public class Generator {
         }
     }
 
-    public static int uniqueCounter = 0;
-
     public static JVar generateBeanInitCode(MetaJavaBean mjb, JBlock body, int level) {
 
-        JVar beanDecl = body.decl(mjb.getGeneratedClass(), "lvl" + level + mjb.getName()
+ uniqueCounter++);
+        JVar beanDecl = body.decl(mjb.getGeneratedClass(), "lvl" + level + mjb.getName()
+ "_" + Config.CFG.nextUniqueNum());
         body.assign(beanDecl, JExpr._new(mjb.getGeneratedClass()));
 
         for (AbstractMetaField amf : mjb.getFields()) {
             if ((amf instanceof JavaBeanBasicField) && level == 0) {
-                
+
                 JavaBeanBasicField jbbf = (JavaBeanBasicField) amf;
                 Object value;
-                if (Config.CFG.getRandomGen().nextBoolean()) {
-                    int validIdx = Config.CFG.getRandomGen().nextInt(jbbf.getValidValues().size());
-                    value = jbbf.getValidValues().get(validIdx);
+                if (Config.CFG.shouldAssignValidValue()) {
+                    value = Config.CFG.getRandom(jbbf.getValidValues());
                 } else {
-                    int invalidIdx = Config.CFG.getRandomGen().nextInt(jbbf.getInvalidValues().size());
-                    value = jbbf.getInvalidValues().get(invalidIdx);
+                    value = Config.CFG.getRandom(jbbf.getInvalidValues());
                 }
                 jbbf.generateAssignCode(value);
-                
+
             } else if (amf instanceof JavaBeanRefField) {
 
                 JavaBeanRefField jbrf = (JavaBeanRefField) amf;

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/jsr303/Jsr303Annotator.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/jsr303/Jsr303Annotator.java?rev=956027&r1=956026&r2=956027&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/jsr303/Jsr303Annotator.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/jsr303/Jsr303Annotator.java
Fri Jun 18 15:46:57 2010
@@ -30,11 +30,13 @@ import javax.validation.Constraint;
 import javax.validation.ConstraintValidator;
 import javax.validation.ConstraintValidatorContext;
 import javax.validation.Payload;
+import javax.validation.ReportAsSingleViolation;
 import javax.validation.Valid;
 import javax.validation.constraints.Max;
 import javax.validation.constraints.Min;
 import javax.validation.constraints.NotNull;
 import javax.validation.constraints.Null;
+import javax.validation.constraints.Pattern;
 import javax.validation.constraints.Size;
 import org.apache.bval.bench.AnnotationType;
 import org.apache.bval.bench.AnnotatorVisitor;
@@ -51,6 +53,7 @@ import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import com.sun.codemodel.ClassType;
+import com.sun.codemodel.JAnnotationArrayMember;
 import com.sun.codemodel.JClass;
 import com.sun.codemodel.JClassAlreadyExistsException;
 import com.sun.codemodel.JCodeModel;
@@ -189,6 +192,7 @@ public class Jsr303Annotator implements 
 
         HashSet<MetaAnnotation> annotationsSet;
         HashMap<String, Object> annotParams;
+        Jsr303MetaAnnotationSet maSet;
 
         // ### STRING ----------------------------------------------------------
 
@@ -210,20 +214,30 @@ public class Jsr303Annotator implements 
         annotationsSet.add(new MetaAnnotation(codeModel, Size.class, AnnotationType.JSR_303,
annotParams));
         anns.put(BasicType.STRING, new Jsr303MetaAnnotationSet(annotationsSet, Lists.newArrayList((Object)
"goodgood"), Lists.newArrayList((Object) null, "bad")));
         
-        // TODO: A nested constraint
         // @GoodCode
-        //  -> @Pattern(regexp=".*")
-        //  -> @Size.List( {@Size(min=5), @Size(max=8)} )
-
+        annotationsSet = Sets.newHashSet();
+        annotParams = Maps.newHashMap();
+        JDefinedClass goodCode = buildTemplateConstraint("GoodCode");
+        goodCode.annotate(Constraint.class).paramArray("validatedBy");
+        goodCode.annotate(Pattern.class).param("regexp", ".*");
+        JAnnotationArrayMember sizes = goodCode.annotate(Size.List.class).paramArray("value");
+        sizes.annotate(Size.class).param("min", 5);
+        sizes.annotate(Size.class).param("max", 8);
+        goodCode.annotate(NotNull.class);
+        annotationsSet.add(new MetaAnnotation(goodCode, AnnotationType.JSR_303, annotParams));
+        anns.put(BasicType.STRING, new Jsr303MetaAnnotationSet(annotationsSet, Lists.newArrayList((Object)
"goodcode", "1234123"), Lists.newArrayList((Object) null, "bad", "acodetoolong")));
 
-        // ### INTEGER ---------------------------------------------------------
+        
+        // ### INTEGER & INT ---------------------------------------------------
 
         // @NotNull / 3 / null
         annotationsSet = Sets.newHashSet();
         annotParams = Maps.newHashMap();
         annotParams.put("message", "cannot be null");
         annotationsSet.add(new MetaAnnotation(codeModel, NotNull.class, AnnotationType.JSR_303,
annotParams));
-        anns.put(BasicType.INTEGER, new Jsr303MetaAnnotationSet(annotationsSet, Lists.newArrayList((Object)
3), Lists.newArrayList((Object) null)));
+        maSet = new Jsr303MetaAnnotationSet(annotationsSet, Lists.newArrayList((Object) 3),
Lists.newArrayList((Object) null));
+        anns.put(BasicType.INTEGER, maSet);
+        // This one makes no sense for int
 
         // @Min(100) @Max(200) / [ 155, null ] / [ -100, 4000 ]
         annotationsSet = Sets.newHashSet();
@@ -235,7 +249,24 @@ public class Jsr303Annotator implements 
         annotParams.put("message", "must be less than {value}");
         annotParams.put("value", 200);
         annotationsSet.add(new MetaAnnotation(codeModel, Max.class, AnnotationType.JSR_303,
annotParams));
-        anns.put(BasicType.INTEGER, new Jsr303MetaAnnotationSet(annotationsSet, Lists.newArrayList((Object)
155, null), Lists.newArrayList((Object) (-100), 4000)));
+        maSet =  new Jsr303MetaAnnotationSet(annotationsSet, Lists.newArrayList((Object)
155, null), Lists.newArrayList((Object) (-100), 4000));
+        anns.put(BasicType.INTEGER, maSet);
+        maSet.getValidValues().remove(null);
+        anns.put(BasicType.INT, maSet);
+        
+        // @RangedInt
+        annotationsSet = Sets.newHashSet();
+        annotParams = Maps.newHashMap();
+        JDefinedClass ranged = buildTemplateConstraint("RangedInt");
+        ranged.annotate(Constraint.class).paramArray("validatedBy");
+        ranged.annotate(Max.class).param("value", 3000);
+        ranged.annotate(Min.class).param("value", 1000);
+        ranged.annotate(ReportAsSingleViolation.class);
+        annotationsSet.add(new MetaAnnotation(ranged, AnnotationType.JSR_303, annotParams));
+        maSet = new Jsr303MetaAnnotationSet(annotationsSet, Lists.newArrayList((Object) 2500,
3000), Lists.newArrayList((Object) null, -20, 40000000));
+        anns.put(BasicType.INTEGER, maSet);
+        maSet.getInvalidValues().remove(null);
+        anns.put(BasicType.INT, maSet);
 
         return anns;
     }
@@ -321,22 +352,5 @@ public class Jsr303Annotator implements 
             throw new RuntimeException("Tried to create an already existing class: " + name,
e);
         }
     }
-    
-    /*
-     * Nested constraint
-     * 
-@Documented
-@Pattern(regexp=".*")
-@Size.List( {@Size(min=5), @Size(max=8)} )
-@Retention(RetentionPolicy.RUNTIME)
-@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
-public @interface NestedConstraint1 {
-    
-    Class<?>[] groups() default {};
-    String message() default "Invalid value";
-    Class<? extends Payload>[] payload() default {};
-    
-}
-     */
 
 }

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/MetaJavaBean.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/MetaJavaBean.java?rev=956027&r1=956026&r2=956027&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/MetaJavaBean.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/MetaJavaBean.java
Fri Jun 18 15:46:57 2010
@@ -78,7 +78,7 @@ public class MetaJavaBean extends Abstra
         // Init the simple fields
         this.fields = Maps.newHashMapWithExpectedSize(numFields);
         for (int i = 0; i < numFields; ++i) {
-            String fieldName = "field" + i;
+            String fieldName = "field" + Config.CFG.nextUniqueNum();
             JavaBeanBasicField field = new JavaBeanBasicField(this, fieldName);
             fields.put(fieldName, field);
         }
@@ -144,4 +144,8 @@ public class MetaJavaBean extends Abstra
         }
     }
 
+    public void setSuperClass(MetaJavaBean random) {
+        this.getGeneratedClass()._extends(random.getGeneratedClass());
+    }
+
 }

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/resources/generator.default.properties
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/resources/generator.default.properties?rev=956027&r1=956026&r2=956027&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/resources/generator.default.properties
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/resources/generator.default.properties
Fri Jun 18 15:46:57 2010
@@ -26,6 +26,7 @@ generator.pkg.base=org.apache.bval.bench
 generator.pkg.beans=org.apache.bval.bench.generated.beans
 generator.pkg.groups=org.apache.bval.bench.generated.groups
 generator.numbeans=200
+generator.numbasebeans=10
 generator.numgroups=5
 generator.basicfields.min=4
 generator.basicfields.max=7
@@ -34,4 +35,6 @@ generator.reffields.max=3
 generator.level1.fillrate=0.80
 generator.level2.fillrate=0.40
 generator.level3.fillrate=0.20
-generator.beanswithgroups=0.30
\ No newline at end of file
+generator.beanswithgroups=0.30
+generator.validvalues=0.80
+generator.inheritanceratio=0.30
\ No newline at end of file



Mime
View raw message