Archive for July, 2007

Fake it!

Friday, July 13th, 2007

Sometimes you need test data to... test some class or jsp page. Here is the java class that can be of help to someone.



JAVA:

  1. import org.apache.commons.lang.RandomStringUtils;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.apache.commons.lang.math.RandomUtils;
  4.  
  5. public class Fake {
  6.  
  7.     public static String getAddress() {
  8.         String str = RandomStringUtils.randomNumeric(2) + "/"
  9.                 + RandomStringUtils.randomNumeric(3);
  10.         String streetName = get(7) + 8;
  11.         streetName = StringUtils.capitalise(streetName);
  12.         str += " ";
  13.         str += streetName;
  14.         return str;
  15.     }
  16.  
  17.     public static String getWord() {
  18.         String streetName = RandomStringUtils.randomAlphabetic(7).toLowerCase();
  19.         streetName = StringUtils.capitalise(streetName);
  20.         return streetName;
  21.     }
  22.  
  23.     public static String get(int maxLength) {
  24.         int l = RandomUtils.nextInt(maxLength);
  25.         return RandomStringUtils.randomAlphabetic(maxLength + 3).toLowerCase();
  26.     }
  27.    
  28.     public static String getSentence(int numberOfWords) {
  29.         StringBuffer sb = new StringBuffer();
  30.         for (int i = 0; i <numberOfWords; i++) {
  31.             sb.append(get(16));
  32.             sb.append(" ");
  33.         }
  34.         return StringUtils.capitalise(sb.toString());
  35.     }
  36.  
  37.     public static String getEmail() {
  38.         StringBuilder sb = new StringBuilder(get(4));
  39.         sb.append("@");
  40.         sb.append(get(4));
  41.         sb.append(".");
  42.         sb.append(RandomStringUtils.randomAlphabetic(3).toLowerCase());
  43.         return sb.toString();
  44.  
  45.     }
  46.  
  47.     public static int getId() {
  48.         return RandomUtils.nextInt(99999);
  49.     }
  50.  
  51.     public static String getPhoneNumber() {
  52.         return RandomStringUtils.randomNumeric(12);
  53.     }
  54.  
  55.     public static String getPostalCode() {
  56.         return RandomStringUtils.randomNumeric(7);
  57.     }
  58.  
  59.     public static String getState() {
  60.         return RandomStringUtils.randomAlphabetic(3).toUpperCase();
  61.     }
  62. }