001
014
015 package com.liferay.portal.kernel.test.util;
016
017 import com.liferay.counter.service.CounterLocalServiceUtil;
018 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
019 import com.liferay.portal.kernel.test.randomizerbumpers.RandomizerBumper;
020 import com.liferay.portal.kernel.util.LocaleUtil;
021 import com.liferay.portal.kernel.util.PwdGenerator;
022 import com.liferay.portal.kernel.util.UnicodeProperties;
023
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 @SafeVarargs
059 public static byte[] randomBytes(
060 int size, RandomizerBumper<byte[]>... randomizerBumpers) {
061
062 byte[] bytes = new byte[size];
063
064 generation:
065 for (int i = 0; i < _RANDOMIZER_BUMPER_TRIES_MAX; i++) {
066 _random.nextBytes(bytes);
067
068 for (RandomizerBumper<byte[]> randomizerBumper :
069 randomizerBumpers) {
070
071 if (!randomizerBumper.accept(bytes)) {
072 continue generation;
073 }
074 }
075
076 return bytes;
077 }
078
079 throw new IllegalStateException(
080 "Unable to generate a random byte array that is acceptable by " +
081 "all randomizer bumpers " + Arrays.toString(randomizerBumpers) +
082 " after " + _RANDOMIZER_BUMPER_TRIES_MAX + " tries");
083 }
084
085 @SafeVarargs
086 public static byte[] randomBytes(
087 RandomizerBumper<byte[]>... randomizerBumpers) {
088
089 return randomBytes(8, randomizerBumpers);
090 }
091
092 public static double randomDouble() {
093 double value = _random.nextDouble();
094
095 if (value > 0) {
096 return value;
097 }
098 else if (value == 0) {
099 return randomDouble();
100 }
101 else {
102 return -value;
103 }
104 }
105
106 @SafeVarargs
107 public static InputStream randomInputStream(
108 RandomizerBumper<byte[]>... randomizerBumpers) {
109
110 return new UnsyncByteArrayInputStream(randomBytes(randomizerBumpers));
111 }
112
113 public static int randomInt() {
114 int value = _random.nextInt();
115
116 if (value > 0) {
117 return value;
118 }
119 else if (value == 0) {
120 return randomInt();
121 }
122 else {
123 return -value;
124 }
125 }
126
127 public static int randomInt(int min, int max)
128 throws IllegalArgumentException {
129
130 if ((min < 0) || (max < 0)) {
131 throw new IllegalArgumentException(
132 "Both min and max values must be positive");
133 }
134
135 if (max < min) {
136 throw new IllegalArgumentException(
137 "Max value must be greater than the min value");
138 }
139
140 int value = _random.nextInt(max - min + 1) + min;
141
142 if (value > 0) {
143 return value;
144 }
145 else if (value == 0) {
146 return randomInt(min, max);
147 }
148
149 return -value;
150 }
151
152 public static Map<Locale, String> randomLocaleStringMap() {
153 return randomLocaleStringMap(LocaleUtil.getDefault());
154 }
155
156 public static Map<Locale, String> randomLocaleStringMap(Locale locale) {
157 Map<Locale, String> map = new HashMap<>();
158
159 map.put(LocaleUtil.getDefault(), randomString());
160
161 return map;
162 }
163
164 public static long randomLong() {
165 long value = _random.nextLong();
166
167 if (value > 0) {
168 return value;
169 }
170 else if (value == 0) {
171 return randomLong();
172 }
173 else {
174 return -value;
175 }
176 }
177
178 @SafeVarargs
179 public static String randomString(
180 int length, RandomizerBumper<String>... randomizerBumpers) {
181
182 generation:
183 for (int i = 0; i < _RANDOMIZER_BUMPER_TRIES_MAX; i++) {
184 String randomString = PwdGenerator.getPassword(length);
185
186 for (RandomizerBumper<String> randomizerBumper :
187 randomizerBumpers) {
188
189 if (!randomizerBumper.accept(randomString)) {
190 continue generation;
191 }
192 }
193
194 return randomString;
195 }
196
197 throw new IllegalStateException(
198 "Unable to generate a random string that is acceptable by all " +
199 "randomizer bumpers " + Arrays.toString(randomizerBumpers) +
200 " after " + _RANDOMIZER_BUMPER_TRIES_MAX + " tries");
201 }
202
203 @SafeVarargs
204 public static String randomString(
205 RandomizerBumper<String>... randomizerBumpers) {
206
207 return randomString(8, randomizerBumpers);
208 }
209
210 @SafeVarargs
211 public static String[] randomStrings(
212 int count, RandomizerBumper<String>... randomizerBumpers) {
213
214 String[] strings = new String[count];
215
216 for (int i = 0; i < count; i++) {
217 strings[i] = randomString(randomizerBumpers);
218 }
219
220 return strings;
221 }
222
223 public static UnicodeProperties randomUnicodeProperties(
224 int propertyCount, int keyLength, int valueLength) {
225
226 UnicodeProperties unicodeProperties = new UnicodeProperties();
227
228 for (int i = 0; i < propertyCount; i++) {
229 unicodeProperties.put(
230 RandomTestUtil.randomString(keyLength),
231 RandomTestUtil.randomString(valueLength));
232 }
233
234 return unicodeProperties;
235 }
236
237 private static final int _RANDOMIZER_BUMPER_TRIES_MAX = 100;
238
239 private static final Random _random = new Random();
240
241 }