4 Replies Latest reply: Nov 8, 2011 2:28 AM by jduprez RSS

    Populate java bean with random testdata using reflection.

    Chris
      Hello,

      I am trying to write a piece of code that wil fill any java bean with random test data.
      This piece of code is a proof of concept I created but it will only fill one class. I want it to fill classes recursively.
      Testclass2 is a is a property of testclass but it wont work this way. Any suggestions?

      public class RandomTestDataGenerator<CT> {
           public void polulateTestData(CT classImpl)
                     throws Exception {
      
                CT cl = (CT) classImpl.getClass();
      
                Method mtd[] = ((Class<? extends Object>) cl).getMethods();
                for (int i = 0; i < mtd.length; i++) {
                     if (mtd.getName().startsWith("set")) {

                          Class[] parameterTypes = mtd[i].getParameterTypes();
                          Object[] parameters = new Object[parameterTypes.length];
                          for (int f = 0; f < parameterTypes.length; f++) {

                               if (parameterTypes[f].getName().equals("java.lang.String")) {
                                    parameters[f] = new String("testvalue");
                               } else if (parameterTypes[f].getName().equals("int")) {
                                    parameters[f] = 10;
                               } else if (parameterTypes[f].getName().equals("java.lang.String")) {
                                    parameters[f] = new Integer(10);
                                    // TODO: fill more standard classes
                                    // TODO: use real random data..
                               } else if (parameterTypes[f] instanceof Object) {
                                    /* Populate subclasses. Won't work :-( */                              
                                    parameterTypes[f] = Class.forName(parameterTypes[f].getName());

                                    RandomTestDataGenerator generator = new RandomTestDataGenerator();
                                    generator.polulateTestData(parameterTypes[f]);

                               }
                          }

                          Method method = ((Class<Object>) cl).getMethod(mtd[i].getName(), mtd[i].getParameterTypes());
                          method.invoke(classImpl, parameters);
                     }
                }
           }
      }



      public class testclass {

           private String testStirng1;
           private String testStirng2;
           private int testInt1;
           private int testInt2;

           private testclass2 testclass;

           public String getTestStirng1() {
                return testStirng1;
           }

           public void setTestStirng1(String testStirng1) {
                this.testStirng1 = testStirng1;
           }

           public String getTestStirng2() {
                return testStirng2;
           }

           public void setTestStirng2(String testStirng2) {
                this.testStirng2 = testStirng2;
           }

           public int getTestInt1() {
                return testInt1;
           }

           public void setTestInt1(int testInt1) {
                this.testInt1 = testInt1;
           }

           public int getTestInt2() {
                return testInt2;
           }

           public void setTestInt2(int testInt2) {
                this.testInt2 = testInt2;
           }

           public void setTestclass(testclass2 testclass) {
                this.testclass = testclass;
           }

           public testclass2 getTestclass() {
                return testclass;
           }

      }


      public class testclass2 {

           private String testStirng1;
           private String testStirng2;
           private int testInt1;
           private int testInt2;

           public testclass2() {
                System.out.println("constructor testclass2");
           }

           public String getTestStirng1() {
                return testStirng1;
           }

           public void setTestStirng1(String testStirng1) {
                System.out.println("setTestStirng1 from testclass2");
                this.testStirng1 = testStirng1;
           }

           public String getTestStirng2() {
                return testStirng2;
           }

           public void setTestStirng2(String testStirng2) {
                this.testStirng2 = testStirng2;
           }

           public int getTestInt1() {
                return testInt1;
           }

           public void setTestInt1(int testInt1) {
                this.testInt1 = testInt1;
           }

           public int getTestInt2() {
                return testInt2;
           }

           public void setTestInt2(int testInt2) {
                this.testInt2 = testInt2;
           }

      }
      Edited by: Chris on 6-nov-2011 12:48                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        • 1. Re: Populate java bean with random testdata using reflection.
          jduprez
          <code>
          That's {noformat}
          {noformat}.                                                                                                                                                                                                                    
          • 2. Re: Populate java bean with random testdata using reflection.
            Chris
            Hello,

            I got it to work now. The solution whas "newInstance"

            import java.lang.reflect.Method;
            
            public class RandomTestDataGenerator<CT> {
                 public void polulateTestData(CT classImpl)
                           throws Exception {
            
                      CT cl = (CT) classImpl.getClass();
            
                      Method mtd[] = ((Class<? extends Object>) cl).getMethods();
                      for (int i = 0; i < mtd.length; i++) {
                           if (mtd.getName().startsWith("set")) {
                                // System.out.println(mtd[i].getName());
                                Class[] parameterTypes = mtd[i].getParameterTypes();
                                Object[] parameters = new Object[parameterTypes.length];
                                for (int f = 0; f < parameterTypes.length; f++) {

                                     if (parameterTypes[f].getName().equals("java.lang.String")) {
                                          parameters[f] = new String("testvalue");
                                     } else if (parameterTypes[f].getName().equals("int")) {
                                          parameters[f] = 10;
                                     } else if (parameterTypes[f].getName().equals("java.lang.String")) {
                                          parameters[f] = new Integer(10);
                                          // TODO... more standard classes
                                     } else if (parameterTypes[f] instanceof Object) {
                                          /* Populate subclasses. */
                                          // System.out.println(parameterTypes[f].getName());
                                          parameters[f] = parameterTypes[f].newInstance();
                                          RandomTestDataGenerator generator = new RandomTestDataGenerator();
                                          generator.polulateTestData(parameters[f]);
                                     }
                                }
                                Method method = ((Class<Object>) cl).getMethod(mtd[i].getName(), mtd[i].getParameterTypes());
                                method.invoke(classImpl, parameters);
                           }
                      }
                 }
            }


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
            • 3. Re: Populate java bean with random testdata using reflection.
              jduprez
              Chris wrote:
              I got it to work now. The solution whas "newInstance"
              I doubt this is enough: this only covers cases where at least both of this is true:
              - the class denoted by <TT>parameterTypes[f]</TT> has a public no-arg constructor.
              - the class is not abstract

              This is far from true for all classes. In those nasty cases the newInstance call will fail with various exceptions (see the javadoc for newInstance() ).
              If either of the condition is not true (which you can check by reflection before trying to invoke <TT>newInstance</TT>), your best bet is to shuffle a null as a value (but of course the receiving bean may not like it!).

              Brgds,

              J.
              • 4. Re: Populate java bean with random testdata using reflection.
                jduprez
                If either of the condition is not true (...), your best bet is to shuffle a null as a value (...)
                You can also try to find which constructors exist, and use whichever constructor has argument types that you know how to instantiate themselves (thus recursing until you stumble on a type that you don't know how to instantiate, and keep the null trick for those alone).

                J.