1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
public class Randomizer { //returns a random Integer public static Integer getRandomNumber(Integer size){ Double d = math.random() * size; return d.intValue(); } //returns either true or false randomly public static Boolean getRandomBoolean(){ if(math.mod(getRandomNumber(10),2) == 0){ return true; } else{ return false; } } //Get's a random value from a list of strings public static String getRandomString(List<String> strings){ List<Double> ranks = new List<Double>(); Map<Double,String> rankMap = new Map<Double,String>(); for(String s : strings){ Boolean isDup = true; Double rank; While(isDup){ Double x = getRandomNumber(100000); if(!rankMap.containsKey(x)){ rank = x; isDup = false; } } ranks.add(rank); rankMap.put(rank,s); } ranks.sort(); return rankMap.get(ranks.get(0)); } //Returns a random picklist value public static string getRandomPickListValue(Sobject s_object, String field_name, Boolean allow_blank){ List<String> Strings = new List<String>(); if(allow_blank){ String b = ''; Strings.add(b); } Schema.sObjectType sobject_type = s_object.getSObjectType(); Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); List<Schema.PicklistEntry> pick_list_values = field_map.get(field_name).getDescribe().getPickListValues(); for (Schema.PicklistEntry a : pick_list_values) { Strings.add(a.getValue()); } return getRandomString(Strings); } //returns a map of all picklists and multiselect picklists for a givien object //the keyset is the field name using proper case public static Map<String,List<String>> getPicVals(sObject s_object){ Map<String,List<String>> valueMap = new Map<String,List<String>>(); Schema.sObjectType sobject_type = s_object.getSObjectType(); Schema.DescribeSObjectResult r = sobject_type.getDescribe(); Map<String, Schema.SObjectField> field_map = r.fields.getMap(); for(String s : field_map.keyset()){ List<String> strings = new List<String>(); Schema.DescribeFieldResult F = field_map.get(s).getDescribe(); if(f.GetType() == Schema.DisplayType.Picklist || f.GetType() == Schema.DisplayType.MultiPicklist){ List<Schema.PicklistEntry> pick_list_values = field_map.get(s).getDescribe().getPickListValues(); for (Schema.PicklistEntry a : pick_list_values) { strings.add(a.getValue()); } valueMap.put(String.valueOf(field_map.get(s)),strings); } } return valueMap; } //Returns Lorem Ipsum placeholder text. public static String getPlaceholderText(Integer length){ String firstSentence = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '; List<String> sentenceList = new List<String>(); sentenceList.add('Vivamus nec lacus eget massa cursus pulvinar. '); sentenceList.add('Morbi vel odio eget nunc auctor posuere eget eget ante. '); sentenceList.add('Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. '); sentenceList.add('Pellentesque lacus eros. '); sentenceList.add('Sed suscipit tristique varius. '); sentenceList.add('Mauris ultricies, nibh eu fermentum accumsan, justo quam pulvinar tellus, sed tempor quam eros sit amet ante. '); sentenceList.add('Duis mi libero, cursus nec facilisis ut, commodo eget nunc. '); sentenceList.add('Nulla eros augue, iaculis sed volutpat et, sagittis quis sem. '); sentenceList.add('Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nulla placerat accumsan vulputate. '); sentenceList.add('Fusce placerat tellus eget tellus faucibus a gravida sapien fermentum. '); String s = firstSentence; while (s.length() < length+1){ s += getRandomString(sentenceList); } s = s.trim(); while (s.length() >= length-1){ s = s.substring(0,s.length()-1).trim(); } s = s.substring(0,s.length()-1).trim(); s += '.'; return s; } static testMethod void testRandomizer() { test.startTest(); Integer testInt = getRandomNumber(10); Boolean testBool = getRandomBoolean(); List<String> testStringList = new List<String>(); testStringList.add('one'); testStringList.add('two'); testStringList.add('three'); String testString = getRandomString(testStringList); String testString2 = getRandomPickListValue(new Account(), 'Industry', true); String testString3 = getRandomPickListValue(new Account(), 'Industry', false); Map<String,List<String>> testMap = getPicVals(new Account()); test.stopTest(); String testString4 = getPlaceholderText(300); } } |