1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.util.lang;
17
18 import static org.hamcrest.MatcherAssert.assertThat;
19 import static org.hamcrest.Matchers.allOf;
20 import static org.hamcrest.Matchers.containsString;
21 import static org.hamcrest.Matchers.is;
22
23 import org.junit.Test;
24 import org.junit.experimental.categories.Category;
25
26 import de.smartics.testdoc.annotations.Uut;
27 import de.smartics.testdoc.categories.type.Coverage;
28 import de.smartics.util.test.lang.LangTestUtils;
29
30
31
32
33 @Uut(type = Arg.class)
34 public class ArgTest
35 {
36
37
38
39
40 private static final String ARGUMENT_NAME = "arg";
41
42 private static final Object OBJECT_VALUE = "object";
43
44 private static final String STRING_VALUE = "string";
45
46 private static final String MESSAGE = "message";
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 @Test(expected = NullPointerException.class)
63 public void rejectsNullValueWithNullPointerException()
64 {
65 Arg.checkNotNull(ARGUMENT_NAME, null);
66 }
67
68 @Test
69 public void acceptsNonNullValue()
70 {
71 Arg.checkNotNull(ARGUMENT_NAME, OBJECT_VALUE);
72 }
73
74 @SuppressWarnings("unchecked")
75 @Test(expected = NullPointerException.class)
76 public void allowsToPassAnOptionalMessageForNullCheck()
77 {
78 try
79 {
80 Arg.checkNotNull(ARGUMENT_NAME, null, MESSAGE);
81 }
82 catch (final NullArgumentException e)
83 {
84 assertThat(e.getMessage(),
85 allOf(containsString(ARGUMENT_NAME), containsString(MESSAGE)));
86 throw e;
87 }
88 }
89
90 @Test
91 @Category(Coverage.class)
92 public void allowsToPassAnOptionalMessageForNullCheckEvenIfNoExceptionIsThrown()
93 {
94 Arg.checkNotNull(ARGUMENT_NAME, OBJECT_VALUE, MESSAGE);
95 }
96
97
98
99 @Test(expected = NullPointerException.class)
100 public void rejectsBlankValueWithNullPointerException()
101 {
102 Arg.checkNotBlank(ARGUMENT_NAME, null);
103 }
104
105 @Test
106 public void acceptsNonBlankValue()
107 {
108 Arg.checkNotBlank(ARGUMENT_NAME, STRING_VALUE);
109 }
110
111 @SuppressWarnings("unchecked")
112 @Test(expected = NullArgumentException.class)
113 public void allowsToPassAnOptionalMessageForBlankCheck()
114 {
115 try
116 {
117 Arg.checkNotBlank(ARGUMENT_NAME, null, MESSAGE);
118 }
119 catch (final NullArgumentException e)
120 {
121 assertThat(e.getMessage(),
122 allOf(containsString(ARGUMENT_NAME), containsString(MESSAGE)));
123 throw e;
124 }
125 }
126
127 @Test
128 @Category(Coverage.class)
129 public void allowsToPassAnOptionalMessageForBlankCheckEvenIfNoExceptionIsThrown()
130 {
131 final Object object =
132 Arg.checkNotNull(ARGUMENT_NAME, OBJECT_VALUE, MESSAGE);
133 assertThat(object, is(OBJECT_VALUE));
134 }
135
136
137
138 @Test
139 @Category(Coverage.class)
140 public void isAnUtilityClass()
141 {
142 LangTestUtils.runPrivateConstructorTest(Arg.class);
143 }
144 }