Coverage Report - org.apache.maven.surefire.assertion.ComparisonTool
 
Classes in this File Line Coverage Branch Coverage Complexity
ComparisonTool
0%
0/33
0%
0/32
5.333
 
 1  
 package org.apache.maven.surefire.assertion;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *     http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 /**
 23  
  * Utilities for displaying comparison failures.
 24  
  */
 25  
 public class ComparisonTool
 26  
 {
 27  
     private ComparisonTool()
 28  0
     {
 29  0
     }
 30  
 
 31  
     /**
 32  
      * Returns "..." in place of common prefix and "..." in
 33  
      * place of common suffix between expected and actual.
 34  
      * <p/>
 35  
      * Inspired by a patch from Alex Chaffee mailto:alex@purpletech.com
 36  
      *
 37  
      * @param message  the message to go along with the comparison
 38  
      * @param expected the expected value
 39  
      * @param actual   the actual value
 40  
      * @return the reduced comparison
 41  
      */
 42  
     static String trimComparison( String message, String expected, String actual )
 43  
     {
 44  
         String actualValue;
 45  
         String expectedValue;
 46  
 
 47  0
         if ( expected == null && actual == null )
 48  
         {
 49  0
             throw new IllegalArgumentException( "Cannot pass both expected and actual as null" );
 50  
         }
 51  0
         else if ( expected == null || actual == null )
 52  
         {
 53  0
             actualValue = actual;
 54  0
             expectedValue = expected;
 55  
         }
 56  
         else
 57  
         {
 58  0
             int end = Math.min( expected.length(), actual.length() );
 59  
 
 60  0
             int i = 0;
 61  0
             for ( ; i < end; i++ )
 62  
             {
 63  0
                 if ( expected.charAt( i ) != actual.charAt( i ) )
 64  
                 {
 65  0
                     break;
 66  
                 }
 67  
             }
 68  0
             int j = expected.length() - 1;
 69  0
             int k = actual.length() - 1;
 70  0
             for ( ; k >= i && j >= i; k--, j-- )
 71  
             {
 72  0
                 if ( expected.charAt( j ) != actual.charAt( k ) )
 73  
                 {
 74  0
                     break;
 75  
                 }
 76  
             }
 77  
 
 78  
             // equal strings
 79  0
             if ( j < i && k < i )
 80  
             {
 81  0
                 throw new IllegalArgumentException( "expected and actual cannot be the same" );
 82  
             }
 83  
             else
 84  
             {
 85  0
                 expectedValue = expected.substring( i, j + 1 );
 86  0
                 actualValue = actual.substring( i, k + 1 );
 87  0
                 if ( i <= end && i > 0 )
 88  
                 {
 89  0
                     expectedValue = "..." + expectedValue;
 90  0
                     actualValue = "..." + actualValue;
 91  
                 }
 92  
 
 93  0
                 if ( j < expected.length() - 1 )
 94  
                 {
 95  0
                     expectedValue = expectedValue + "...";
 96  
                 }
 97  0
                 if ( k < actual.length() - 1 )
 98  
                 {
 99  0
                     actualValue = actualValue + "...";
 100  
                 }
 101  
             }
 102  
         }
 103  0
         return formatMismatch( message, expectedValue, actualValue );
 104  
     }
 105  
 
 106  
     /**
 107  
      * Format a message for a comparison failure.
 108  
      *
 109  
      * @param message  the message to go with the failure
 110  
      * @param expected the expected value
 111  
      * @param actual   the actual value
 112  
      * @return the formatted string
 113  
      */
 114  
     static String formatMismatch( String message, Object expected, Object actual )
 115  
     {
 116  0
         String formatted = "";
 117  
 
 118  0
         if ( message != null )
 119  
         {
 120  0
             formatted = message + " ";
 121  
         }
 122  
 
 123  
         // TODO! i18n
 124  0
         return formatted + "expected:<" + expected + "> but was:<" + actual + ">";
 125  
     }
 126  
 }