001
014
015 package com.liferay.portal.kernel.test.util;
016
017 import com.liferay.counter.service.CounterLocalServiceUtil;
018 import com.liferay.portal.kernel.test.randomizerbumpers.RandomizerBumper;
019 import com.liferay.portal.kernel.util.LocaleUtil;
020 import com.liferay.portal.kernel.util.PwdGenerator;
021 import com.liferay.portal.kernel.util.UnicodeProperties;
022
023 import java.io.ByteArrayInputStream;
024 import java.io.InputStream;
025
026 import java.util.Arrays;
027 import java.util.Date;
028 import java.util.HashMap;
029 import java.util.Locale;
030 import java.util.Map;
031 import java.util.Random;
032
033
036 public class RandomTestUtil {
037
038 public static Date nextDate() {
039 return new Date();
040 }
041
042 public static double nextDouble() throws Exception {
043 return CounterLocalServiceUtil.increment();
044 }
045
046 public static int nextInt() throws Exception {
047 return (int)CounterLocalServiceUtil.increment();
048 }
049
050 public static long nextLong() throws Exception {
051 return CounterLocalServiceUtil.increment();
052 }
053
054 public static boolean randomBoolean() {
055 return _random.nextBoolean();
056 }
057
058 public static byte[] randomBytes() {
059 String string = randomString();
060
061 return string.getBytes();
062 }
063
064 public static double randomDouble() {
065 double value = _random.nextDouble();
066
067 if (value > 0) {
068 return value;
069 }
070 else if (value == 0) {
071 return randomDouble();
072 }
073 else {
074 return -value;
075 }
076 }
077
078 public static InputStream randomInputStream() {
079 return new ByteArrayInputStream(randomBytes());
080 }
081
082 public static int randomInt() {
083 int value = _random.nextInt();
084
085 if (value > 0) {
086 return value;
087 }
088 else if (value == 0) {
089 return randomInt();
090 }
091 else {
092 return -value;
093 }
094 }
095
096 public static int randomInt(int min, int max)
097 throws IllegalArgumentException {
098
099 if ((min < 0) || (max < 0)) {
100 throw new IllegalArgumentException(
101 "Both min and max values must be positive");
102 }
103
104 if (max < min) {
105 throw new IllegalArgumentException(
106 "Max value must be greater than the min value");
107 }
108
109 int value = _random.nextInt(max - min + 1) + min;
110
111 if (value > 0) {
112 return value;
113 }
114 else if (value == 0) {
115 return randomInt(min, max);
116 }
117
118 return -value;
119 }
120
121 public static Map<Locale, String> randomLocaleStringMap() {
122 return randomLocaleStringMap(LocaleUtil.getDefault());
123 }
124
125 public static Map<Locale, String> randomLocaleStringMap(Locale locale) {
126 Map<Locale, String> map = new HashMap<>();
127
128 map.put(LocaleUtil.getDefault(), randomString());
129
130 return map;
131 }
132
133 public static long randomLong() {
134 long value = _random.nextLong();
135
136 if (value > 0) {
137 return value;
138 }
139 else if (value == 0) {
140 return randomLong();
141 }
142 else {
143 return -value;
144 }
145 }
146
147 @SafeVarargs
148 public static String randomString(
149 int length, RandomizerBumper<String>... randomizerBumpers) {
150
151 generation:
152 for (int i = 0; i < _RANDOMIZER_BUMPER_TRIES_MAX; i++) {
153 String randomString = PwdGenerator.getPassword(length);
154
155 for (RandomizerBumper<String> randomizerBumper :
156 randomizerBumpers) {
157
158 if (!randomizerBumper.accept(randomString)) {
159 continue generation;
160 }
161 }
162
163 return randomString;
164 }
165
166 throw new IllegalStateException(
167 "Unable to generate a random string that is acceptable by all " +
168 "randomizer bumpers " + Arrays.toString(randomizerBumpers) +
169 " after " + _RANDOMIZER_BUMPER_TRIES_MAX + " tries");
170 }
171
172 @SafeVarargs
173 public static String randomString(
174 RandomizerBumper<String>... randomizerBumpers) {
175
176 return randomString(8, randomizerBumpers);
177 }
178
179 @SafeVarargs
180 public static String[] randomStrings(
181 int count, RandomizerBumper<String>... randomizerBumpers) {
182
183 String[] strings = new String[count];
184
185 for (int i = 0; i < count; i++) {
186 strings[i] = randomString(randomizerBumpers);
187 }
188
189 return strings;
190 }
191
192 public static UnicodeProperties randomUnicodeProperties(
193 int propertyCount, int keyLength, int valueLength) {
194
195 UnicodeProperties unicodeProperties = new UnicodeProperties();
196
197 for (int i = 0; i < propertyCount; i++) {
198 unicodeProperties.put(
199 RandomTestUtil.randomString(keyLength),
200 RandomTestUtil.randomString(valueLength));
201 }
202
203 return unicodeProperties;
204 }
205
206 private static final int _RANDOMIZER_BUMPER_TRIES_MAX = 100;
207
208 private static final Random _random = new Random();
209
210 }