All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.enterprise.test.TestUtil Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
/*******************************************************************************
 * 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
 *******************************************************************************/

package at.spardat.enterprise.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.
 */
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 len der nur aus Ziffern besteht.
     */
    public static String randomDigitString (int len) {
        StringBuffer        b = new StringBuffer(len);
        for (int i=0; ilen aus [a-z].
     */
    public static String randomString (int len) {
        StringBuffer        b = new StringBuffer(len);
        for (int i=0; iLASTSEED, 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. */ private 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 file lastSeed.txt */ private static void writeSeedToFile(long seed) throws Exception { FileWriter fw = new FileWriter(SEED_FILE); fw.write(String.valueOf(seed)); fw.close(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy