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.StringUtil;
020    import com.liferay.portal.kernel.util.UnicodeProperties;
021    
022    import java.util.Date;
023    import java.util.HashMap;
024    import java.util.Locale;
025    import java.util.Map;
026    import java.util.Random;
027    
028    /**
029     * @author Manuel de la Pe??a
030     */
031    public class RandomTestUtil {
032    
033            public static Date nextDate() {
034                    return new Date();
035            }
036    
037            public static double nextDouble() throws Exception {
038                    return CounterLocalServiceUtil.increment();
039            }
040    
041            public static int nextInt() throws Exception {
042                    return (int)CounterLocalServiceUtil.increment();
043            }
044    
045            public static long nextLong() throws Exception {
046                    return CounterLocalServiceUtil.increment();
047            }
048    
049            public static boolean randomBoolean() {
050                    return _random.nextBoolean();
051            }
052    
053            public static byte[] randomBytes() {
054                    String string = randomString();
055    
056                    return string.getBytes();
057            }
058    
059            public static int randomInt() {
060                    int value = _random.nextInt();
061    
062                    if (value > 0) {
063                            return value;
064                    }
065                    else if (value == 0) {
066                            return randomInt();
067                    }
068                    else {
069                            return -value;
070                    }
071            }
072    
073            public static Map<Locale, String> randomLocaleStringMap() {
074                    return randomLocaleStringMap(LocaleUtil.getDefault());
075            }
076    
077            public static Map<Locale, String> randomLocaleStringMap(Locale locale) {
078                    Map<Locale, String> map = new HashMap<Locale, String>();
079    
080                    map.put(LocaleUtil.getDefault(), randomString());
081    
082                    return map;
083            }
084    
085            public static long randomLong() {
086                    long value = _random.nextLong();
087    
088                    if (value > 0) {
089                            return value;
090                    }
091                    else if (value == 0) {
092                            return randomLong();
093                    }
094                    else {
095                            return -value;
096                    }
097            }
098    
099            public static String randomString() {
100                    return StringUtil.randomString();
101            }
102    
103            public static String randomString(int length) {
104                    return StringUtil.randomString(length);
105            }
106    
107            public static String[] randomStrings(int count) {
108                    String[] strings = new String[count];
109    
110                    for (int i = 0; i < count; i++) {
111                            strings[i] = StringUtil.randomString();
112                    }
113    
114                    return strings;
115            }
116    
117            public static UnicodeProperties randomUnicodeProperties(
118                    int propertyCount, int keyLength, int valueLength) {
119    
120                    UnicodeProperties unicodeProperties = new UnicodeProperties();
121    
122                    for (int i = 0; i < propertyCount; i++) {
123                            unicodeProperties.put(
124                                    RandomTestUtil.randomString(keyLength),
125                                    RandomTestUtil.randomString(valueLength));
126                    }
127    
128                    return unicodeProperties;
129            }
130    
131            private static Random _random = new Random();
132    
133    }