##### Coverage Report - org.jbehave.core.junit.AnnotatedEmbedderUtils

Classes in this File | Line Coverage | Branch Coverage | Complexity
AnnotatedEmbedderUtils |
68% | 11/16
50% | 1/2
| 2.2857142857142856;2.286
AnnotatedEmbedderUtils$AnnotatedEmbedderRunnerInstantiationFailed |
0% | 0/2
N/A | N/A
| 2.2857142857142856;2.286
AnnotatedEmbedderUtils$ClassLoadingFailed |
100% | 2/2
N/A | N/A
| 2.2857142857142856;2.286
AnnotatedEmbedderUtils$MissingAnnotatedEmbedderRunner |
0% | 0/2
N/A | N/A
| 2.2857142857142856;2.286

1 |  |
     package org.jbehave.core.junit;
2 |  |
3 |  |
     import org.jbehave.core.embedder.EmbedderClassLoader;
4 |  |
     import org.junit.runner.RunWith;
5 |  |
6 | 0 |
     public class AnnotatedEmbedderUtils {
7 |  |
8 |  |
         public static AnnotatedEmbedderRunner annotatedEmbedderRunner(String annotatedClassName,
9 |  |
                 EmbedderClassLoader classLoader) {
10 | 4 |
             Class<?> runnerClass = runnerClass(annotatedClassName, classLoader);
11 | 3 |
             return newAnnotatedEmbedderRunner(runnerClass, annotatedClassName, classLoader);
12 |  |
         }
13 |  |
14 |  |
         private static Class<?> runnerClass(String annotatedClassName, EmbedderClassLoader classLoader) {
15 | 4 |
             Class<?> annotatedClass = loadClass(annotatedClassName, classLoader);
16 | 3 |
             RunWith annotation = annotatedClass.getAnnotation(RunWith.class);
17 | 3 |
             if (annotation != null) {
18 | 3 |
                 return annotation.value();
19 |  |
             }
20 | 0 |
             throw new MissingAnnotatedEmbedderRunner(annotatedClass);
21 |  |
         }
22 |  |
23 |  |
         private static AnnotatedEmbedderRunner newAnnotatedEmbedderRunner(Class<?> runnerClass, String annotatedClassName,
24 |  |
                 EmbedderClassLoader classLoader) {
25 |  |
             try {
26 | 3 |
                 Class<?> annotatedClass = loadClass(annotatedClassName, classLoader);
27 | 3 |
                 return (AnnotatedEmbedderRunner) runnerClass.getConstructor(Class.class).newInstance(annotatedClass);
28 | 0 |
             } catch (Exception e) {
29 | 0 |
                 throw new AnnotatedEmbedderRunnerInstantiationFailed(runnerClass, annotatedClassName, classLoader, e);
30 |  |
             }
31 |  |
         }
32 |  |
33 |  |
         private static Class<?> loadClass(String className, EmbedderClassLoader classLoader) {
34 |  |
             try {
35 | 7 |
                 return classLoader.loadClass(className);
36 | 1 |
             } catch (ClassNotFoundException e) {
37 | 1 |
                 throw new ClassLoadingFailed(className, classLoader, e);
38 |  |
             }
39 |  |
         }
40 |  |
41 |  |
         @SuppressWarnings("serial")
42 |  |
         public static class ClassLoadingFailed extends RuntimeException {
43 |  |
44 |  |
             public ClassLoadingFailed(String className, EmbedderClassLoader classLoader, Throwable cause) {
45 | 1 |
                 super("Failed to load class " + className + " with classLoader " + classLoader, cause);
46 | 1 |
             }
47 |  |
48 |  |
         }
49 |  |
50 |  |
         @SuppressWarnings("serial")
51 |  |
         public static class AnnotatedEmbedderRunnerInstantiationFailed extends RuntimeException {
52 |  |
53 |  |
             public AnnotatedEmbedderRunnerInstantiationFailed(Class<?> runnerClass, String annotatedClassName,
54 |  |
                     EmbedderClassLoader classLoader, Throwable cause) {
55 | 0 |
                 super("Failed to instantiate annotated embedder runner " + runnerClass + " with annotatedClassName "
56 |  |
                         + annotatedClassName + " and classLoader " + classLoader, cause);
57 | 0 |
             }
58 |  |
59 |  |
         }
60 |  |
61 | 0 |
         @SuppressWarnings("serial")
62 |  |
         public static class MissingAnnotatedEmbedderRunner extends RuntimeException {
63 |  |
64 |  |
             public MissingAnnotatedEmbedderRunner(Class<?> annotatedClass) {
65 | 0 |
                 super("AnnotatedEmbedderRunner not specified via @RunWith annotation in annotatedClass " + annotatedClass);
66 | 0 |
             }
67 |  |
68 |  |
         }
69 |  |
     }