Coverage Report - org.dozer.util.MappingValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
MappingValidator
82%
23/28
91%
11/12
2.6
 
 1  
 /**
 2  
  * Copyright 2005-2013 Dozer Project
 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.dozer.util;
 17  
 
 18  
 import org.dozer.config.BeanContainer;
 19  
 
 20  
 import java.io.IOException;
 21  
 import java.io.InputStream;
 22  
 import java.net.URL;
 23  
 
 24  
 /**
 25  
  * Internal class used to perform various validations. Validates mapping requests, field mappings, URL's, etc. Only
 26  
  * intended for internal use.
 27  
  *
 28  
  * @author tierney.matt
 29  
  * @author garsombke.franz
 30  
  */
 31  
 public final class MappingValidator {
 32  
 
 33  0
   private MappingValidator() {}
 34  
 
 35  
   public static void validateMappingRequest(Object srcObj) {
 36  132067
     if (srcObj == null) {
 37  3
       MappingUtils.throwMappingException("Source object must not be null");
 38  
     }
 39  132064
   }
 40  
 
 41  
   public static void validateMappingRequest(Object srcObj, Object destObj) {
 42  131205
     validateMappingRequest(srcObj);
 43  131205
     if (destObj == null) {
 44  1
       MappingUtils.throwMappingException("Destination object must not be null");
 45  
     }
 46  131204
   }
 47  
 
 48  
   public static void validateMappingRequest(Object srcObj, Class<?> destClass) {
 49  861
     validateMappingRequest(srcObj);
 50  859
     if (destClass == null) {
 51  2
       MappingUtils.throwMappingException("Destination class must not be null");
 52  
     }
 53  857
   }
 54  
 
 55  
   public static URL validateURL(String fileName) {
 56  602
     DozerClassLoader classLoader = BeanContainer.getInstance().getClassLoader();
 57  602
     if (fileName == null) {
 58  1
       MappingUtils.throwMappingException("File name is null");
 59  
     }
 60  
     
 61  601
     URL url = classLoader.loadResource(fileName);
 62  601
     if (url == null) {
 63  1
       MappingUtils.throwMappingException("Unable to locate dozer mapping file [" + fileName + "] in the classpath!");
 64  
     }
 65  
 
 66  600
     InputStream stream = null;
 67  
     try {
 68  600
       stream = url.openStream();
 69  0
     } catch (IOException e) {
 70  0
       MappingUtils.throwMappingException("Unable to open URL input stream for dozer mapping file [" + url + "]");
 71  
     } finally {
 72  600
       if (stream != null) {
 73  
         try {
 74  600
           stream.close();
 75  0
         } catch (IOException e) {
 76  0
           MappingUtils.throwMappingException("Unable to close input stream for dozer mapping file [" + url + "]");
 77  600
         }
 78  
       }
 79  
     }
 80  
 
 81  600
     return url;
 82  
   }
 83  
   
 84  
 }