wissel.net

Usability - Productivity - Business - The web - Singapore & Twins

Generating Test data


You have seen that over and over: Test1, qwertyyui, asdfgh as data entered in development to test an application. Short of borrowing a copy of production data, having useful test data is a pain in the neck. For my currently limited exposure to development (after all I work as pre Sales engineer) I use a Java class that helps me to generate random result from a set of selection values. To make this work I used the following libraries:
  • JodaTime: takes the headache out of date calculation
  • gson: save and load JSON, in this case the random data
  • Lorem Ipsum: generate blocks of text that look good
  • Here you go:
    packagecom.notessensei.randomdata;

    importjava.io.InputStream;
    importjava.io.InputStreamReader;
    importjava.io.OutputStream;
    importjava.io.PrintWriter;
    importjava.util.Date;
    importjava.util.HashMap;
    importjava.util.List;
    importjava.util.Map;
    importjava.util.Random;

    importorg.joda.time.DateTime;

    importcom.google.gson.Gson;

    importde.svenjacobs.loremipsum.LoremIpsum;

    /**
     * Source of random data to generate test data for anything before use you need
     * either load lists of your data using addRandomStringSource or load a JSON
     * file from a previous run using loadDataFromJson
     *
     * @author NotesSensei
     *
     */

    publicclass RandomLoader {

        /*
         * How often should the random generator try for getRandomString with
         * exclusion before it gives up
         */

        publicstaticfinalint             MAX_RANDOM_TRIES    = 100;
        privateMap<String, List<String>>   randomStrings ;
        privateRandom                      randomGenerator ;

        /**
         * Initialize all things random
         */

        public RandomLoader (){
            this. randomStrings = newHashMap<String, List<String>>();
            this. randomGenerator = newRandom(newDate(). getTime());
        }

        /**
         * Adds or ammends a collection of values to draw from
         */

        publicvoid addRandomStringSource (String sourceName, List<String> sourceMembers ){
            if(!this. randomStrings. containsKey(sourceName )){
                this. randomStrings. put(sourceName, sourceMembers );
            }else{
                // We have a list of this name, so we add the values
                List<String> existingList = this. randomStrings. get(sourceName );
                for(String newMember : sourceMembers ){
                    existingList. add(newMember );
                }
            }
        }

        /**
         * Get rid of a list we don't need anymore
         *
         * @param sourceName
         */

        publicvoid dropRandomStringSource (String sourceName ){
            if(this. randomStrings. containsKey(sourceName )){
                this. randomStrings. remove(sourceName );
            }
        }

        /**
         * Gets a random value from a predefined list
         */

        publicString getRandomString (String sourceName ){
            if(this. randomStrings. containsValue(sourceName )){
                List<String> sourceCollection = this. randomStrings. get(sourceName );
                int whichValue = this. randomGenerator. nextInt(sourceCollection. size());
                return sourceCollection. get(whichValue );
            }
            // If we don't have that list we return the requested list name
            return sourceName ;
        }

        /**
         * Get a random String, but not the value specified Good for populating
         *travel to (exclude the from) or from/to message pairs etc
         *
         *@param sourceName
         *            from which list
         *@param excludedResult
         *            what not to return
         *@return
         */

        publicString getRandomStringButNot (String sourceName, String excludedResult ){
            String result = null;
            for(int i = 0; i < MAX_RANDOM_TRIES ; i++ ){
                result = this. getRandomString(sourceName );
                if(!result. equals(excludedResult )){
                    break;
                }
            }
            return result ;
        }

        /**
         * For populating whole paragraphs of random text LoremIpsum Style
         */

        publicString getRandomParagraph (int numberOfWords ){
            LoremIpsum li = new LoremIpsum ();
            return li. getWords(numberOfWords );
        }

        /**
         *Get a date in the future
         */

        publicDate getFutureDate (Date startDate, int maxDaysDistance ){
            int actualDayDistance = this. randomGenerator. nextInt(maxDaysDistance + 1);
            DateTime jdt = new org. joda. time. DateTime(startDate );
            DateTime jodaResult = jdt. plusDays(actualDayDistance );
            return jodaResult. toDate();
        }

        /**
         * Get a date in the past, good for approval simulation
         */

        publicDate getPastDate (Date startDate, int maxDaysDistance ){
            int actualDayDistance = this. randomGenerator. nextInt(maxDaysDistance + 1);
            DateTime jdt = new org. joda. time. DateTime(startDate );
            DateTime jodaResult = jdt. minusDays(actualDayDistance );
            return jodaResult. toDate();
        }

        /**
         *Lots of applications are about $$ approvals, so we need a generator
         */

        publicfloat getRandomAmount (float minimum, float maximum ){
            // between 0.0 and 1.0F
            float seedValue = this. randomGenerator. nextFloat();
            return(minimum + ((maximum - minimum )* seedValue ));
        }

        /**
         * Save the random strings to a JSON file for reuse
         */

        publicvoid saveDatatoJson (OutputStream out ){
            Gson gson = new Gson ();
            PrintWriter writer = newPrintWriter(out );
            gson. toJson(this, writer );
        }

        /**
         * Load a saved JSON file to populate the random strings
         */

        publicstatic RandomLoader loadDataFromJson (InputStream in ){
            InputStreamReader reader = newInputStreamReader(in );
            Gson gson = new Gson ();
            RandomLoader result = gson. fromJson(reader, RandomLoader. class);
            return result ;
        }
    }
    As usual YMMV

Posted by on 18 January 2013 | Comments (0) | categories: Software

Comments

  1. No comments yet, be the first to comment