Bläddra i källkod

SimpleInjector Unit test

Change-Id: Ic73c78ad63bfbc2fcdef87c6c7cab70ab85a3131
Reviewed-on: http://gerrit.dmdirc.com/2447
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.7rc1
Greg Holmes 12 år sedan
förälder
incheckning
e9c30ea502
1 ändrade filer med 121 tillägg och 0 borttagningar
  1. 121
    0
      test/com/dmdirc/util/SimpleInjectorTest.java

+ 121
- 0
test/com/dmdirc/util/SimpleInjectorTest.java Visa fil

@@ -0,0 +1,121 @@
1
+/*
2
+ * Copyright (c) 2006-2012 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ * THE SOFTWARE.
21
+ */
22
+package com.dmdirc.util;
23
+
24
+import java.io.Serializable;
25
+
26
+import org.junit.Test;
27
+
28
+import static org.junit.Assert.*;
29
+
30
+public class SimpleInjectorTest {
31
+
32
+    @Test
33
+    public void testNoParams() {
34
+        SimpleInjector instance = new SimpleInjector();
35
+        assertTrue(instance.getParameters().isEmpty());
36
+    }
37
+
38
+    @Test
39
+    public void testAddParams() {
40
+        final Object object = new Object();
41
+        SimpleInjector instance = new SimpleInjector();
42
+        instance.addParameter(Object.class, object);
43
+        assertEquals(1, instance.getParameters().size());
44
+    }
45
+
46
+    @Test
47
+    public void testAddObjectsParams() {
48
+        final TestObject object = new TestObject("");
49
+        SimpleInjector instance = new SimpleInjector();
50
+        instance.addParameter(object);
51
+        assertEquals(3, instance.getParameters().size());
52
+    }
53
+
54
+    @Test
55
+    public void testParentsParams() {
56
+        final Object object = new Object();
57
+        SimpleInjector parent = new SimpleInjector();
58
+        parent.addParameter(Object.class, object);
59
+        SimpleInjector child = new SimpleInjector(parent);
60
+        assertEquals(1, child.getParameters().size());
61
+    }
62
+
63
+    @Test
64
+    public void testCreateInstanceNoParams() {
65
+        SimpleInjector instance = new SimpleInjector();
66
+        final Object result = instance.createInstance(Object.class);
67
+        assertNotNull(result);
68
+    }
69
+
70
+    @Test
71
+    public void testCreateInstanceParams() {
72
+        SimpleInjector instance = new SimpleInjector();
73
+        instance.addParameter(String.class, "test");
74
+        instance.addParameter(SimpleInjectorTest.class, this);
75
+        final TestObject result = instance.createInstance(TestObject.class);
76
+        assertNotNull(result);
77
+    }
78
+
79
+    @Test(expected=IllegalArgumentException.class)
80
+    public void testCreateUnsatisfiedConstructor() {
81
+        SimpleInjector instance = new SimpleInjector();
82
+        final Object result = instance.createInstance(TestObject.class);
83
+        assertNull(result);
84
+    }
85
+
86
+    @Test(expected=IllegalArgumentException.class)
87
+    public void testCreatePrivateConstructor() {
88
+        SimpleInjector instance = new SimpleInjector();
89
+        instance.addParameter(String.class, "");
90
+        final Object result = instance.createInstance(PrivateObject.class);
91
+        assertNull(result);
92
+    }
93
+
94
+    @Test(expected=IllegalArgumentException.class)
95
+    public void testCreateExceptionConstructor() {
96
+        SimpleInjector instance = new SimpleInjector();
97
+        instance.addParameter(String.class, "");
98
+        instance.addParameter(SimpleInjectorTest.class, this);
99
+        final Object result = instance.createInstance(ExceptionObject.class);
100
+        assertNull(result);
101
+    }
102
+
103
+    class TestObject implements Serializable {
104
+
105
+        public TestObject(final String test) {
106
+        }
107
+    }
108
+
109
+    class PrivateObject implements Serializable {
110
+
111
+        private PrivateObject() {
112
+        }
113
+    }
114
+
115
+    class ExceptionObject implements Serializable {
116
+
117
+        public ExceptionObject() {
118
+            throw new IndexOutOfBoundsException();
119
+        }
120
+    }
121
+}

Laddar…
Avbryt
Spara