##### Coverage Report - org.jbehave.core.configuration.scala.ScalaContext

Classes in this File | Line Coverage | Branch Coverage | Complexity
ScalaContext |
100% | 19/19
83% | 5/6
| 2.0;2
ScalaContext$ScalaInstanceNotFound |
100% | 4/4
N/A | N/A
| 2.0;2

1 |  |
     package org.jbehave.core.configuration.scala;
2 |  |
3 |  |
     import java.util.ArrayList;
4 |  |
     import java.util.Arrays;
5 |  |
     import java.util.List;
6 |  |
7 |  |
     public class ScalaContext {
8 |  |
9 |  |
         private final ClassLoader classLoader;
10 |  |
         private final List<String> classNames;
11 |  |
         private List<Object> instances;
12 |  |
13 |  |
         public ScalaContext(String... classNames) {
14 | 3 |
             this(ScalaContext.class.getClassLoader(), classNames);
15 | 2 |
         }
16 |  |
17 | 3 |
         public ScalaContext(ClassLoader classLoader, String... classNames) {
18 | 3 |
             this.classLoader = classLoader;
19 | 3 |
             this.classNames = Arrays.asList(classNames);
20 | 3 |
             this.instances = createInstances();
21 | 2 |
         }
22 |  |
23 |  |
         public List<Object> getInstances() {
24 | 1 |
             return instances;
25 |  |
         }
26 |  |
27 |  |
         @SuppressWarnings("unchecked")
28 |  |
         public <T> T getInstanceOfType(Class<T> type) {
29 | 2 |
             for (Object instance : instances) {
30 | 1 |
                 if (type.isAssignableFrom(instance.getClass())) {
31 | 1 |
                     return (T) instance;
32 |  |
                 }
33 |  |
             }
34 | 1 |
             throw new ScalaInstanceNotFound(type);
35 |  |
         }
36 |  |
37 |  |
         /**
38 |  |
          * Creates an object instance from the Scala class name
39 |  |
          *
40 |  |
          * @param className the Scala class name
41 |  |
          * @return An Object instance
42 |  |
          */
43 |  |
         public Object newInstance(String className) {
44 |  |
             try {
45 | 4 |
                 return classLoader.loadClass(className).newInstance();
46 | 1 |
             } catch (Exception e) {
47 | 1 |
                 throw new ScalaInstanceNotFound(className);
48 |  |
             }
49 |  |
         }
50 |  |
51 |  |
         private List<Object> createInstances() {
52 | 3 |
             List<Object> instances = new ArrayList<Object>();
53 | 3 |
             for (String className : classNames) {
54 | 3 |
                 instances.add(newInstance(className));
55 |  |
             }
56 | 2 |
             return instances;
57 |  |
         }
58 |  |
59 |  |
         @SuppressWarnings("serial")
60 |  |
         public static final class ScalaInstanceNotFound extends RuntimeException {
61 |  |
62 |  |
             public ScalaInstanceNotFound(Class<?> type) {
63 | 1 |
                 super(type.toString());
64 | 1 |
             }
65 |  |
66 |  |
             public ScalaInstanceNotFound(String className) {
67 | 1 |
                 super(className);
68 | 1 |
             }
69 |  |
70 |  |
         }
71 |  |
72 |  |
     }