at.spardat.xma.test.TestUtil Maven / Gradle / Ivy
/******************************************************************************* * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH . * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * s IT Solutions AT Spardat GmbH - initial API and implementation *******************************************************************************/ // @(#) $Id: TestUtil.java 2089 2007-11-28 13:56:13Z s3460 $ package at.spardat.xma.test; import java.io.FileReader; import java.io.FileWriter; import java.util.Random; /** * Provides some utility-Methods to generate test-data. Contains a random * number generator, which is able to reproduce the same sequence of random numbers * over multiple runs of an application, dependent on a system property. * * @author YSD, 02.05.2003 21:32:50 */ public class TestUtil { /** * Generiert einen String einer besimmten L?nge mit zuf?lligen * Ziffern-Inhalt. * * @param len die L?nge des Returnwerts * @return String der L?nge
aus [a-z]. */ public static String randomString (int len) { StringBuffer b = new StringBuffer(len); for (int i=0; ilen
der nur aus Ziffern besteht. */ public static String randomDigitString (int len) { StringBuffer b = new StringBuffer(len); for (int i=0; ilen minLen up to maxLen. */ public static String randomString (int minLen, int maxLen) { return randomString (randomInt (minLen, maxLen)); } /** * Returns a random String whose length is also random and * ranges from minLen up to maxLen. This * method also returns null with a probability of 0.1. */ public static String randomNullString (int minLen, int maxLen) { if (draw(0.1)) return null; return randomString (randomInt (minLen, maxLen)); } /** * Returns random number in the range [low, high] */ public static int randomInt (int low, int high) { if (high < low) throw new IllegalArgumentException(); if (low == high) return low; return getRandom().nextInt(high-low+1)+low; } /** * Returns a random number in the range [low, high] */ public static double randomDouble (double low, double high) { if (high < low) throw new IllegalArgumentException(); if (low == high) return low; return getRandom().nextDouble() * (high-low) + low; } /** * Liefert Zufallsboolean */ public static boolean randomBool () { return getRandom().nextBoolean(); } /** * Returns a boolean that is true with a provided * probability. * * @param probality value ranging from 0.0 to 1.0. * @return true with the specified probability. */ public static boolean draw (double probality) { return getRandom().nextDouble() < probality; } private static Random random_; /** * Get the singleton random number generator. Dependent upon the * system-property LASTSEED, it is seeded by an number read * from the file lastseed.txt or the seed is taken from the current * time and written to the file. * * Thereby we are able to reproduce a sequence of random numbers * over multiple runs of a JUnit testRunner. */ public synchronized static Random getRandom () { if (random_ == null) { // look up system property LASTSEED if (System.getProperty("LASTSEED") == null) { // create Random-Generator based on current time long time = System.currentTimeMillis(); try { writeSeedToFile(time); } catch (Exception ex) { ex.printStackTrace(); } random_ = new Random(time); } else { // get the last seed from file long seed = 0; try { seed = getLastSeedFromFile(); } catch (Exception ex) { ex.printStackTrace(); } random_ = new Random(seed); } } return random_; } private static String SEED_FILE = "lastSeed.txt"; /** * Returns the last seed written to the file
lastSeed.txt
*/ private static long getLastSeedFromFile () throws Exception { FileReader fr = new FileReader(SEED_FILE); StringBuffer buf = new StringBuffer(); int lastChar; while ((lastChar = fr.read()) != -1) buf.append((char)lastChar); fr.close(); return Long.parseLong(buf.toString()); } /** * Writes the seed to the filelastSeed.txt
*/ private static void writeSeedToFile(long seed) throws Exception { FileWriter fw = new FileWriter(SEED_FILE); fw.write(String.valueOf(seed)); fw.close(); } }