View Javadoc

1   /*
2    * Copyright 2011-2013 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  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   * Tests {@link Arg}.
32   */
33  @Uut(type = Arg.class)
34  public class ArgTest
35  {
36    // ********************************* Fields *********************************
37  
38    // --- constants ------------------------------------------------------------
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    // --- members --------------------------------------------------------------
49  
50    // ****************************** Inner Classes *****************************
51  
52    // ********************************* Methods ********************************
53  
54    // --- prepare --------------------------------------------------------------
55  
56    // --- helper ---------------------------------------------------------------
57  
58    // --- tests ----------------------------------------------------------------
59  
60    // ... null .................................................................
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    // ... blank ................................................................
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   // ... basics ...............................................................
137 
138   @Test
139   @Category(Coverage.class)
140   public void isAnUtilityClass()
141   {
142     LangTestUtils.runPrivateConstructorTest(Arg.class);
143   }
144 }