View Javadoc

1   /*
2    * Copyright 1999-2002,2004 The Apache Software Foundation.
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  
17  package org.apache.commons.latka;
18  
19  /**
20   * This exception is thrown by <em>validators</em> when a validation has failed
21   *
22   * @author Doug Sale
23   * @author dIon Gillard (mainly javadoc)
24   * @version $Revision: 155424 $
25   */
26  public class ValidationException extends Exception {
27      
28      /**
29       * Create a validation exception with a null label and reason
30       */
31      public ValidationException() {
32          this(null, null);
33      }
34  
35      /**
36       * Create a validation exception with the given
37       * label and reason
38       * @param label the user's description of the assertion/test
39       * @param reason the reason it failed
40       */    
41      public ValidationException(String label, String reason) {
42          _label = label;
43          _reason = reason;
44      }
45  
46      /** User's description of the test/assertion */
47      private String _label = null;
48  
49      /** Why the test/assertion failed */
50      private String _reason = null;
51  
52      /**
53       * the message of the exception
54       * @return the message of the exception, built from
55       * the label and reason
56       */    
57      public String getMessage() {
58          StringBuffer buf = new StringBuffer();
59          if (null != getLabel()) {
60              buf.append(getLabel());
61              if (null != getReason()) {
62                  buf.append(": ");
63              }
64          }
65          if (null != getReason()) {
66              buf.append(getReason());
67          }
68          return buf.toString();
69      }
70  
71      /**
72       * User's description of the test/assertion
73       * @return User's description of the test/assertion
74       */
75      public String getLabel() {
76          return _label;
77      }
78  
79      /**
80       * Why the test/assertion failed
81       * @return Why the test/assertion failed
82       */
83      public String getReason() {
84          return _reason;
85      }
86  }