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.util;
15  
16  public class TestHelper {
17  
18      static public Throwable makeNestedException(int level) {
19          if (level == 0) {
20              return new Exception("nesting level=" + level);
21          }
22          Throwable cause = makeNestedException(level - 1);
23          return new Exception("nesting level =" + level, cause);
24      }
25  
26      /**
27       * Usage:
28       * 
29       * <pre>
30       * String s = "123";
31       * positionOf("1").in(s) < positionOf("3").in(s)
32       * </pre>
33       *
34       * @param pattern Plain text to be found
35       * @return StringPosition fluent interface
36       */
37      public static StringPosition positionOf(String pattern) {
38          return new StringPosition(pattern);
39      }
40  
41      public static class StringPosition {
42          private final String pattern;
43  
44          public StringPosition(String pattern) {
45              this.pattern = pattern;
46          }
47  
48          public int in(String s) {
49              final int position = s.indexOf(pattern);
50              if (position < 0)
51                  throw new IllegalArgumentException("String '" + pattern + "' not found in: '" + s + "'");
52              return position;
53          }
54  
55      }
56  
57  }