View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.corpus;
15  
16  import java.util.Random;
17  
18  import javax.management.remote.JMXProviderException;
19  
20  public class ExceptionBuilder {
21  
22      static Throwable build(Random r, double nestingProbability) {
23          double rn = r.nextDouble();
24          boolean nested = false;
25          if (rn < nestingProbability) {
26              nested = true;
27          }
28  
29          Throwable cause = null;
30          if (nested) {
31              cause = makeThrowable(r, null);
32          }
33          return makeThrowable(r, cause);
34      }
35  
36      private static Throwable makeThrowable(Random r, Throwable cause) {
37          int exType = r.nextInt(4);
38          switch (exType) {
39          case 0:
40              return new IllegalArgumentException("an illegal argument was passed", cause);
41          case 1:
42              return new Exception("this is a test", cause);
43          case 2:
44              return new JMXProviderException("jmx provider exception error occured", cause);
45          case 3:
46              return new OutOfMemoryError("ran out of memory");
47          }
48          return null;
49      }
50  
51  }