View Javadoc
1   /*
2    * Copyright 2008 the original author or authors.
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 org.openehealth.ipf.commons.core.modules.api;
17  
18  import static org.junit.Assert.*;
19  
20  import org.junit.Test;
21  import org.openehealth.ipf.commons.core.modules.api.ValidationException;
22  
23  /**
24   * @author Dmytro Rud
25   */
26  public class ValidationExceptionTest {
27  
28      @Test
29      public void testMessages() {
30          String s;
31          String message = "exception message 12345";
32          Throwable[] causes;
33  
34          // ----------------- 
35          causes = new Throwable[] {new RuntimeException("12345"), new RuntimeException("abcd") };
36          
37          s = new ValidationException(message, causes).getMessage();
38          assertTrue(s.startsWith(message));
39          assertTrue(s.contains("\n12345"));
40          assertTrue(s.contains("\nabcd"));
41          
42          s = new ValidationException(causes).getMessage();
43          assertTrue(s.startsWith("12345\n"));
44          assertTrue(s.endsWith("\nabcd"));
45  
46          // -----------------
47          causes = new Throwable[] { new RuntimeException(), new RuntimeException() };
48  
49          s = new ValidationException(message, causes).getMessage();
50          assertTrue(s.startsWith(message));
51          assertTrue(s.indexOf("java.lang.RuntimeException") != s.lastIndexOf("java.lang.RuntimeException")); 
52          
53          s = new ValidationException(causes).getMessage();
54          assertEquals("java.lang.RuntimeException\njava.lang.RuntimeException", s); 
55  
56          // -----------------
57          causes = new Throwable[] { new RuntimeException(), new RuntimeException("67890") };
58  
59          s = new ValidationException(message, causes).getMessage();
60          assertTrue(s.startsWith(message));
61          assertTrue(s.contains("\njava.lang.RuntimeException"));
62          assertTrue(s.contains("\n67890"));
63  
64          s = new ValidationException(causes).getMessage();
65          assertTrue(s.startsWith("java.lang.RuntimeException\n"));
66          assertTrue(s.endsWith("\n67890"));
67  
68          // -----------------
69          causes = new Throwable[] {};
70          s = new ValidationException(message, causes).getMessage();
71          assertEquals(s, message);
72  
73          s = new ValidationException(causes).getMessage();
74          assertEquals(s, ValidationException.class.getName());
75  
76          // -----------------
77          causes = null;
78          s = new ValidationException(message, causes).getMessage();
79          assertEquals(s, message);
80  
81          s = new ValidationException(causes).getMessage();
82          assertEquals(s, ValidationException.class.getName());
83      
84          // -----------------
85          Throwable cause = null;
86          s = new ValidationException(message, cause).getMessage();
87          assertEquals(s, message);
88  
89          s = new ValidationException(cause).getMessage();
90          assertEquals(s, ValidationException.class.getName());
91      }
92  }