package com.ibm.emb.junit;

/**
 * this class contains various static helper functions used in the testsuite for
 * Enterprise Media Beans
 */

public class EMBStaticHelper {

    /**
     * static Helper function for logging which should only be used when no EMB
     * TestConfig object is available, i.e. during initialisation of the
     * testsuite no writing to a configurable file is possible here
     */
    static public void testLog(String msg) {
        System.out.println("[LOG] :" + msg);
    }

    /**
     * static Helper function for error tracing which should only be used when
     * no EMB TestConfig object is available, i.e. during initialisation of the
     * testsuite no writing to a configurable file is possible here
     */
    static public void testError(String msg) {
        System.out.println("[ERROR] :" + msg);
    }

    /**
     * static Helper function for tracing which should only be used when no EMB
     * TestConfig object is available, i.e. during initialisation of the
     * testsuite no writing to a configurable file is possible here
     */
    static public void testTrace(String msg) {
        System.out.println("[TRACE] :" + msg);
    }

    /**
     * creates a String of length length with random content
     */
    static public String createRandomString(int length) {
        String result = "";
        for (int i = 0; i < length; i++) {
            int branch = randomInt(0, 2);
            switch (branch) {
                case 0:
                    result = result + (char) randomInt(65, 90);
                    break;
                case 1:
                    result = result + (char) randomInt(48, 57);
                    break;
                case 2:
                    result = result + (char) randomInt(97, 122);
                    break;

            }
        }
        return result;
    }

    /**
     * creates a byte Array String of length length with random content
     */
    static public byte[] createRandomByteArray(int length) {
        // 1024 byte array
        byte[] result = new byte[length];
        for (int i = 0; i < length; i++) {
            result[i] = (byte) Math.round(255 * Math.random());
        }
        return result;
    }

    /**
     * creates a random int in range between min and max
     */
    static public int randomInt(int min, int max) {
        int result = min + (Math.round((float) ((max - min) * Math.random())));
        return result;
    }

}