View Javadoc

1   /*
2    * Copyright 2011-2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  
22  import org.junit.Test;
23  import org.junit.experimental.categories.Category;
24  
25  import de.smartics.testdoc.annotations.Uut;
26  import de.smartics.testdoc.categories.type.Coverage;
27  import de.smartics.util.test.lang.LangTestUtils;
28  
29  /**
30   * Tests {@link Arguments}.
31   */
32  @Uut(type = Arguments.class)
33  public class ArgumentsTest
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    private static final String ARGUMENT_NAME = "arg";
40  
41    private static final Object OBJECT_VALUE = "object";
42  
43    private static final String STRING_VALUE = "string";
44  
45    private static final String MESSAGE = "message";
46  
47    // --- members --------------------------------------------------------------
48  
49    // ****************************** Inner Classes *****************************
50  
51    // ********************************* Methods ********************************
52  
53    // --- prepare --------------------------------------------------------------
54  
55    // --- helper ---------------------------------------------------------------
56  
57    // --- tests ----------------------------------------------------------------
58  
59    // ... null .................................................................
60  
61    @Test(expected = NullPointerException.class)
62    public void rejectsNullValueWithNullPointerException()
63    {
64      Arguments.checkNotNull(ARGUMENT_NAME, null);
65    }
66  
67    @Test
68    public void acceptsNonNullValue()
69    {
70      Arguments.checkNotNull(ARGUMENT_NAME, OBJECT_VALUE);
71    }
72  
73    @SuppressWarnings("unchecked")
74    @Test(expected = NullPointerException.class)
75    public void allowsToPassAnOptionalMessageForNullCheck()
76    {
77      try
78      {
79        Arguments.checkNotNull(ARGUMENT_NAME, null, MESSAGE);
80      }
81      catch (final NullArgumentException e)
82      {
83        assertThat(e.getMessage(),
84            allOf(containsString(ARGUMENT_NAME), containsString(MESSAGE)));
85        throw e;
86      }
87    }
88  
89    @Test
90    @Category(Coverage.class)
91    public void allowsToPassAnOptionalMessageForNullCheckEvenIfNoExceptionIsThrown()
92    {
93      Arguments.checkNotNull(ARGUMENT_NAME, OBJECT_VALUE, MESSAGE);
94    }
95  
96    // ... blank ................................................................
97  
98    @Test(expected = BlankArgumentException.class)
99    public void rejectsBlankValueWithNullPointerException()
100   {
101     Arguments.checkNotBlank(ARGUMENT_NAME, null);
102   }
103 
104   @Test
105   public void acceptsNonBlankValue()
106   {
107     Arguments.checkNotBlank(ARGUMENT_NAME, STRING_VALUE);
108   }
109 
110   @SuppressWarnings("unchecked")
111   @Test(expected = BlankArgumentException.class)
112   public void allowsToPassAnOptionalMessageForBlankCheck()
113   {
114     try
115     {
116       Arguments.checkNotBlank(ARGUMENT_NAME, null, MESSAGE);
117     }
118     catch (final BlankArgumentException e)
119     {
120       assertThat(e.getMessage(),
121           allOf(containsString(ARGUMENT_NAME), containsString(MESSAGE)));
122       throw e;
123     }
124   }
125 
126   @Test
127   @Category(Coverage.class)
128   public void allowsToPassAnOptionalMessageForBlankCheckEvenIfNoExceptionIsThrown()
129   {
130     Arguments.checkNotNull(ARGUMENT_NAME, OBJECT_VALUE, MESSAGE);
131   }
132 
133   // ... basics ...............................................................
134 
135   @Test
136   @Category(Coverage.class)
137   public void isAnUtilityClass()
138   {
139     LangTestUtils.runPrivateConstructorTest(Arguments.class);
140   }
141 }