001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.corpus;
015
016import java.util.Random;
017
018import javax.management.remote.JMXProviderException;
019
020public class ExceptionBuilder {
021
022    static Throwable build(Random r, double nestingProbability) {
023        double rn = r.nextDouble();
024        boolean nested = false;
025        if (rn < nestingProbability) {
026            nested = true;
027        }
028
029        Throwable cause = null;
030        if (nested) {
031            cause = makeThrowable(r, null);
032        }
033        return makeThrowable(r, cause);
034    }
035
036    private static Throwable makeThrowable(Random r, Throwable cause) {
037        int exType = r.nextInt(4);
038        switch (exType) {
039        case 0:
040            return new IllegalArgumentException("an illegal argument was passed", cause);
041        case 1:
042            return new Exception("this is a test", cause);
043        case 2:
044            return new JMXProviderException("jmx provider exception error occured", cause);
045        case 3:
046            return new OutOfMemoryError("ran out of memory");
047        }
048        return null;
049    }
050
051}