Coverage Report - org.mockftpserver.core.util.Assert
 
Classes in this File Line Coverage Branch Coverage Complexity
Assert
93%
28/30
100%
16/16
2.778
 
 1  
 /*
 2  
  * Copyright 2007 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.mockftpserver.core.util;
 17  
 
 18  
 import java.util.Collection;
 19  
 import java.util.Map;
 20  
 
 21  
 /**
 22  
  * Provides static helper methods to make runtime assertions. Throws an
 23  
  * <code>AssertFailedException</code> when the assertion fails. All methods are static.
 24  
  * 
 25  
  * @version $Revision: 8 $ - $Date: 2007-12-18 22:42:32 -0500 (Tue, 18 Dec 2007) $
 26  
  * 
 27  
  * @author Chris Mair
 28  
  */
 29  
 public final class Assert {
 30  
     
 31  
     /**
 32  
      * Verify that arg is null. Throw an AssertFailedException if it is not null.
 33  
      * @param arg - the method parameter value
 34  
      * @param argName - the name of the parameter; used in the exception message
 35  
      * @throws AssertFailedException - if arg is not null
 36  
      */
 37  
     public static void isNull(Object arg, String argName) {
 38  2
         if (arg != null) {
 39  1
             throw new AssertFailedException("The value for \"" + argName + "\" must be null");
 40  
         }
 41  1
     }
 42  
 
 43  
         /**
 44  
          * Verify that arg is not null. Throw an AssertFailedException if it is null.
 45  
          * @param arg - the method parameter value
 46  
      * @param argName - the name of the parameter; used in the exception message
 47  
          * @throws AssertFailedException - if arg is null
 48  
          */
 49  
     public static void notNull(Object arg, String argName) {
 50  32840
                 if (arg == null) {
 51  186
             throw new AssertFailedException("The value of \"" + argName + "\" is null");
 52  
                 }
 53  32654
         }
 54  
 
 55  
         /**
 56  
          * Verify that condition is true. Throw an AssertFailedException if it is false.
 57  
          * @param condition - the condition that should be true
 58  
          * @throws AssertFailedException - if condition is false
 59  
          */
 60  
         public static void isTrue(boolean condition, String message) {
 61  3945
                 if (!condition) {
 62  30
                         throw new AssertFailedException(message);
 63  
                 }
 64  3915
         }
 65  
 
 66  
         /**
 67  
          * Verify that condition is false. Throw an AssertFailedException if it is true.
 68  
          * @param condition - the condition that should be false
 69  
          * @throws AssertFailedException - if condition is true
 70  
          */
 71  
         public static void isFalse(boolean condition, String message) {
 72  271
                 if (condition) {
 73  8
                         throw new AssertFailedException(message);
 74  
                 }
 75  263
         }
 76  
 
 77  
         /**
 78  
          * Verify that the collection is not null or empty. Throw an 
 79  
          * AssertFailedException if it is null or empty.
 80  
          * @param collection - the Collection
 81  
      * @param argName - the name of the parameter; used in the exception message
 82  
          * @throws AssertFailedException - if collection is null or empty
 83  
          */
 84  
     public static void notNullOrEmpty(Collection collection, String argName) {
 85  3
         notNull(collection, argName);
 86  2
                 if (collection.isEmpty()) {
 87  1
             throw new AssertFailedException("The \"" + argName + "\" Collection is empty");
 88  
                 }
 89  1
         }
 90  
 
 91  
         /**
 92  
          * Verify that the Map is not null or empty. Throw an AssertFailedException 
 93  
          * if it is null or empty.
 94  
          * @param map - the Map
 95  
      * @param argName - the name of the parameter; used in the exception message
 96  
          * @throws AssertFailedException - if map is null or empty
 97  
          */
 98  
     public static void notNullOrEmpty(Map map, String argName) {
 99  3
         notNull(map, argName);
 100  2
                 if (map.isEmpty()) {
 101  1
             throw new AssertFailedException("The \"" + argName + "\" Map is empty");
 102  
                 }
 103  1
         }
 104  
 
 105  
         /**
 106  
          * Verify that the array is not null or empty. Throw an 
 107  
          * AssertFailedException if it is null or empty.
 108  
          * @param array - the array
 109  
      * @param argName - the name of the parameter; used in the exception message
 110  
          * @throws AssertFailedException - if array is null or empty
 111  
          */
 112  
     public static void notNullOrEmpty(Object[] array, String argName) {
 113  3
         notNull(array, argName);
 114  2
                 if (array.length == 0) {
 115  1
             throw new AssertFailedException("The \"" + argName + "\" array is empty");
 116  
                 }
 117  1
         }
 118  
 
 119  
         /**
 120  
          * Verify that the String is not null or empty. Throw an 
 121  
          * AssertFailedException if it is null or empty.
 122  
          * @param string - the String
 123  
      * @param argName - the name of the parameter; used in the exception message
 124  
          * @throws AssertFailedException - if string is null or empty
 125  
          */
 126  
     public static void notNullOrEmpty(String string, String argName) {
 127  306
         notNull(string, argName);
 128  304
                 if (string.trim().length() == 0) {
 129  3
             throw new AssertFailedException("The \"" + argName + "\" String is empty");
 130  
                 }
 131  301
         }
 132  
 
 133  
         /**
 134  
          * Private constructor. All methods are static
 135  
          */
 136  0
         private Assert() {
 137  0
         }
 138  
 }