Coverage Report - org.truth0.subjects.ClassSubject
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassSubject
92%
12/13
100%
2/2
2.5
 
 1  
 /*
 2  
  * Copyright (c) 2011 David Saff
 3  
  * Copyright (c) 2011 Christian Gruber
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.truth0.subjects;
 18  
 
 19  
 
 20  
 import org.truth0.FailureStrategy;
 21  
 import org.truth0.util.ReflectionUtil;
 22  
 
 23  
 import com.google.common.annotations.GwtCompatible;
 24  
 import com.google.common.annotations.GwtIncompatible;
 25  
 
 26  
 @GwtCompatible
 27  
 public class ClassSubject extends Subject<ClassSubject, Class<?>> {
 28  
   public ClassSubject(FailureStrategy failureStrategy, Class<?> o) {
 29  5
     super(failureStrategy, o);
 30  5
   }
 31  
 
 32  
   // TODO(cgruber): Create an alternative implementation using JSNI.
 33  
   @GwtIncompatible("Reflection. ")
 34  
   public void declaresField(String fieldName) {
 35  4
     if (getSubject() == null) {
 36  1
       failureStrategy.fail("Cannot determine a field name from a null class.");
 37  0
       return; // not all failures throw exceptions.
 38  
     }
 39  
     try {
 40  3
       ReflectionUtil.getField(getSubject(), fieldName);
 41  1
     } catch (NoSuchFieldException e) {
 42  1
       StringBuilder message = new StringBuilder("Not true that ");
 43  1
       message.append("<").append(getSubject().getSimpleName()).append(">");
 44  1
       message.append(" has a field named <").append(fieldName).append(">");
 45  1
       failureStrategy.fail(message.toString());
 46  2
     }
 47  2
   }
 48  
 
 49  
 }