001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util.test;
016    
017    import com.liferay.counter.service.CounterLocalServiceUtil;
018    import com.liferay.portal.kernel.util.LocaleUtil;
019    import com.liferay.portal.kernel.util.PwdGenerator;
020    import com.liferay.portal.kernel.util.UnicodeProperties;
021    import com.liferay.portal.test.randomizerbumpers.RandomizerBumper;
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    /**
034     * @author Manuel de la Pe??a
035     */
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 InputStream randomInputStream() {
065                    return new ByteArrayInputStream(randomBytes());
066            }
067    
068            public static int randomInt() {
069                    int value = _random.nextInt();
070    
071                    if (value > 0) {
072                            return value;
073                    }
074                    else if (value == 0) {
075                            return randomInt();
076                    }
077                    else {
078                            return -value;
079                    }
080            }
081    
082            public static Map<Locale, String> randomLocaleStringMap() {
083                    return randomLocaleStringMap(LocaleUtil.getDefault());
084            }
085    
086            public static Map<Locale, String> randomLocaleStringMap(Locale locale) {
087                    Map<Locale, String> map = new HashMap<Locale, String>();
088    
089                    map.put(LocaleUtil.getDefault(), randomString());
090    
091                    return map;
092            }
093    
094            public static long randomLong() {
095                    long value = _random.nextLong();
096    
097                    if (value > 0) {
098                            return value;
099                    }
100                    else if (value == 0) {
101                            return randomLong();
102                    }
103                    else {
104                            return -value;
105                    }
106            }
107    
108            @SafeVarargs
109            public static String randomString(
110                    int length, RandomizerBumper<String>... randomizerBumpers) {
111    
112                    generation:
113                    for (int i = 0; i < _RANDOMIZER_BUMPER_TRIES_MAX; i++) {
114                            String randomString = PwdGenerator.getPassword(length);
115    
116                            for (RandomizerBumper<String> randomizerBumper :
117                                            randomizerBumpers) {
118    
119                                    if (!randomizerBumper.accept(randomString)) {
120                                            continue generation;
121                                    }
122                            }
123    
124                            return randomString;
125                    }
126    
127                    throw new IllegalStateException(
128                            "Unable to generate a random string that is acceptable by all " +
129                                    "randomizer bumpers " + Arrays.toString(randomizerBumpers) +
130                                            " after " + _RANDOMIZER_BUMPER_TRIES_MAX + " tries");
131            }
132    
133            @SafeVarargs
134            public static String randomString(
135                    RandomizerBumper<String>... randomizerBumpers) {
136    
137                    return randomString(8, randomizerBumpers);
138            }
139    
140            @SafeVarargs
141            public static String[] randomStrings(
142                    int count, RandomizerBumper<String>... randomizerBumpers) {
143    
144                    String[] strings = new String[count];
145    
146                    for (int i = 0; i < count; i++) {
147                            strings[i] = randomString(randomizerBumpers);
148                    }
149    
150                    return strings;
151            }
152    
153            public static UnicodeProperties randomUnicodeProperties(
154                    int propertyCount, int keyLength, int valueLength) {
155    
156                    UnicodeProperties unicodeProperties = new UnicodeProperties();
157    
158                    for (int i = 0; i < propertyCount; i++) {
159                            unicodeProperties.put(
160                                    RandomTestUtil.randomString(keyLength),
161                                    RandomTestUtil.randomString(valueLength));
162                    }
163    
164                    return unicodeProperties;
165            }
166    
167            private static final int _RANDOMIZER_BUMPER_TRIES_MAX = 100;
168    
169            private static Random _random = new Random();
170    
171    }