Coverage Report - net.sf.jmatchparser.util.ExpectException
 
Classes in this File Line Coverage Branch Coverage Complexity
ExpectException
0%
0/23
0%
0/8
2,6
 
 1  
 /*
 2  
  * Copyright (c) 2006 - 2011 Michael Schierl
 3  
  * 
 4  
  * All rights reserved.
 5  
  * 
 6  
  * Redistribution and use in source and binary forms, with or without
 7  
  * modification, are permitted provided that the following conditions
 8  
  * are met:
 9  
  * 
 10  
  * - Redistributions of source code must retain the above copyright notice,
 11  
  *   this list of conditions and the following disclaimer.
 12  
  *   
 13  
  * - Redistributions in binary form must reproduce the above copyright
 14  
  *   notice, this list of conditions and the following disclaimer in the
 15  
  *   documentation and/or other materials provided with the distribution.
 16  
  *   
 17  
  * - Neither name of the copyright holders nor the names of its
 18  
  *   contributors may be used to endorse or promote products derived from
 19  
  *   this software without specific prior written permission.
 20  
  *   
 21  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND THE CONTRIBUTORS
 22  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 23  
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 24  
  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 25  
  * HOLDERS OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 26  
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 27  
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 28  
  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 29  
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 30  
  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 31  
  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32  
  */
 33  
 package net.sf.jmatchparser.util;
 34  
 
 35  
 import java.util.regex.Matcher;
 36  
 import java.util.regex.Pattern;
 37  
 import java.util.regex.PatternSyntaxException;
 38  
 
 39  
 /**
 40  
  * This exception is thrown when an expectation fails. It tries to show useful
 41  
  * information (like actual and expected values) in its message.
 42  
  */
 43  
 public class ExpectException extends RuntimeException {
 44  
 
 45  
         private static final long serialVersionUID = 4350118922979233122L;
 46  
 
 47  
         /**
 48  
          * Create a new Expect exception
 49  
          * 
 50  
          * @param actual
 51  
          *            Actual value
 52  
          * @param expected
 53  
          *            Expected value
 54  
          */
 55  
         public ExpectException(String actual, String expected) {
 56  0
                 super("Unexpected string\nActual:   '" + actual + "'\nExpected: '" + expected + "'");
 57  0
         }
 58  
 
 59  
         private ExpectException(String actual, Pattern pattern) {
 60  0
                 super("Nonmatching regex\n\nActual: '" + actual + "'\nRegex:  '" + pattern.pattern() + "'");
 61  0
         }
 62  
 
 63  
         private ExpectException(String actual, Pattern pattern, String debugActual, String debugPattern) {
 64  0
                 super("Nonmatching regex\n\nActual: '" + actual + "'\nRegex:  '" + pattern.pattern() + "'\n\nActual part: '" + debugActual + "'\nRegex part:  '" + debugPattern + "'");
 65  0
         }
 66  
 
 67  
         private ExpectException(String actual, Pattern pattern, Exception cause) {
 68  0
                 super("Nonmatching regex\n\nActual: '" + actual + "'\nRegex:  '" + pattern.pattern() + "'\n\nNo debug information available", cause);
 69  0
         }
 70  
 
 71  
         /**
 72  
          * Create a new expect exception from a regular expression.
 73  
          * 
 74  
          * @param actual
 75  
          *            Actual value
 76  
          * @param pattern
 77  
          *            Pattern that should match
 78  
          */
 79  
         public static ExpectException fromRegex(final String actual, final Pattern pattern) {
 80  
                 try {
 81  0
                         String p = pattern.pattern();
 82  0
                         Pattern pt = pattern;
 83  0
                         while (!pt.matcher(actual).matches() && p.indexOf(">{0}") != -1) {
 84  0
                                 p = p.substring(0, p.lastIndexOf(">{0}")) + "(.*)";
 85  0
                                 pt = PatternCache.compile(p);
 86  
                         }
 87  0
                         if (!pattern.pattern().equals(p)) {
 88  0
                                 String restpattern = pattern.pattern().substring(p.length() - 4 + 4);
 89  0
                                 Matcher m = pt.matcher(actual);
 90  0
                                 if (!m.matches())
 91  0
                                         throw new RuntimeException();
 92  0
                                 String restactual = m.group(m.groupCount());
 93  0
                                 return new ExpectException(actual, pattern, restactual, restpattern);
 94  
                         }
 95  0
                         return new ExpectException(actual, pattern);
 96  0
                 } catch (PatternSyntaxException ex) {
 97  0
                         return new ExpectException(actual, pattern, ex);
 98  
                 }
 99  
         }
 100  
 }