CPD Results

The following document contains the results of PMD's CPD 4.3.

Duplications

FileLine
org/tap4j/parser/Tap13Parser.java180
org/tap4j/parser/Tap13YamlParser.java318
        if (!isFirstLine) {
            throw new ParserException(
                                      "Invalid position of TAP Header. It must be the first element (apart of Comments) in the TAP Stream.");
        }
    }

    /**
     * Checks if there are more than one TAP Plan in the TAP Stream.
     */
    protected void checkTAPPlanDuplicity() {
        if (this.testSet.getPlan() != null) {
            throw new ParserException("Duplicated TAP Plan found.");
        }
    }

    /**
     * This method is called after the TAP Stream has already been parsed. So we
     * just check if the plan was found before test result or bail outs. If so,
     * skip this check. Otherwise, we shall check if the last line is the TAP
     * Plan.
     * 
     * @deprecated
     */
    protected void checkTAPPlanPosition() {
        if (!this.planBeforeTestResult) {
            Matcher matcher = PLAN_PATTERN.matcher(lastLine);

            if (matcher.matches()) {
                return; // OK
            }

            throw new ParserException("Invalid position of TAP Plan.");
        }
    }

    /**
     * Checks if TAP Plan has been set.
     * 
     * @throws ParserException if TAP Plan has not been set.
     */
    protected void checkTAPPlanIsSet() {
        if (this.testSet.getPlan() == null) {
            throw new ParserException("Missing TAP Plan.");
        }
    }

    /**
     * Extracts the Header from a TAP Line.
     * 
     * @param matcher REGEX Matcher.
     */
    protected void extractHeader(Matcher matcher) {
        final Integer version = Integer.parseInt(matcher.group(1));

        final Header header = new Header(version);

        final String commentToken = matcher.group(2);

        if (commentToken != null) {
            String text = matcher.group(3);
            final Comment comment = new Comment(text);
            header.setComment(comment);
        }

        this.testSet.setHeader(header);
    }

    /**
     * @param matcher REGEX Matcher.
     */
    protected void extractPlan(Matcher matcher) {
        Integer initialTest = Integer.parseInt(matcher.group(1));
        Integer lastTest = Integer.parseInt(matcher.group(3));

        Plan plan = null;
        plan = new Plan(initialTest, lastTest);

        String skipToken = matcher.group(4);
        if (skipToken != null) {
            String reason = matcher.group(5);
            final SkipPlan skip = new SkipPlan(reason);
            plan.setSkip(skip);
        }

        String commentToken = matcher.group(6);
        if (commentToken != null) {
            String text = matcher.group(7);
            final Comment comment = new Comment(text);
            plan.setComment(comment);
        }

        this.testSet.setPlan(plan);
    }

    /**
     * @param matcher REGEX Matcher.
     */
    protected void extractTestResult(Matcher matcher) {
        TestResult testResult = null;

        final String okOrNotOk = matcher.group(1);
        StatusValues status = null;
        if (okOrNotOk.trim().equals("ok")) {
            status = StatusValues.OK;
        } else // regex mate...
        {
            status = StatusValues.NOT_OK;
        }

        Integer testNumber = this.getTestNumber(matcher.group(2));

        testResult = new TestResult(status, testNumber);

        testResult.setDescription(matcher.group(3));

        String directiveToken = matcher.group(4);
        if (directiveToken != null) {
            String directiveText = matcher.group(5);
            DirectiveValues directiveValue = null;
            if (directiveText.trim().equalsIgnoreCase("todo")) {
                directiveValue = DirectiveValues.TODO;
            } else {
                directiveValue = DirectiveValues.SKIP;
            }
            String reason = matcher.group(6);
            Directive directive = new Directive(directiveValue, reason);
            testResult.setDirective(directive);
        }

        String commentToken = matcher.group(7);
        if (commentToken != null) {
            String text = matcher.group(8);
            final Comment comment = new Comment(text);
            comment.setInline(Boolean.TRUE);
            testResult.addComment(comment);
        }

        this.testSet.addTestResult(testResult);
FileLine
org/tap4j/parser/Tap13Parser.java368
org/tap4j/parser/Tap13YamlParser.java508
    }

    /**
     * Simply extracts the footer from the TAP line.
     * 
     * @param matcher REGEX Matcher.
     */
    protected void extractFooter(Matcher matcher) {
        String text = matcher.group(1);
        Footer footer = new Footer(text);

        final String commentToken = matcher.group(2);

        if (commentToken != null) {
            String commentText = matcher.group(3);
            final Comment comment = new Comment(commentText);
            footer.setComment(comment);
        }

        this.testSet.setFooter(footer);
    }

    /*
     * (non-Javadoc)
     * @see org.tap4j.TapConsumer#parseTapStream(java.lang.String)
     */
    public TestSet parseTapStream(String tapStream) {

        this.init();

        Scanner scanner = null;

        try {
            scanner = new Scanner(tapStream);
            String line = null;

            while (scanner.hasNextLine()) {
                line = scanner.nextLine();
                if (StringUtils.isNotEmpty(line)) {
                    this.parseLine(line);
                }
            }
            this.postProcess();
        } catch (Exception e) {
            throw new ParserException("Error parsing TAP Stream: " +
                                      e.getMessage(), e);
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }

        return this.getTestSet();

    }

    /*
     * (non-Javadoc)
     * @see org.tap4j.TapConsumer#parseFile(java.io.File)
     */
    public TestSet parseFile(File tapFile) {

        this.init();

        Scanner scanner = null;

        try {
            scanner = new Scanner(tapFile);
            String line = null;

            while (scanner.hasNextLine()) {
                line = scanner.nextLine();
                if (StringUtils.isNotBlank(line)) {
                    this.parseLine(line);
                }
            }
            this.postProcess();
        } catch (Exception e) {
            throw new ParserException("Error parsing TAP Stream: " +
                                      e.getMessage(), e);
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }

        return this.getTestSet();
    }
FileLine
org/tap4j/ext/junit/JUnitYAMLishUtils.java131
org/tap4j/ext/testng/TestNGYAMLishUtils.java146
            StackTraceElement[] els = testException.getStackTrace();

            for (int i = 0; i < els.length; i++) {
                StackTraceElement el = els[i];
                line = getLineNumberFromExceptionTraceLine(el.toString(),
                                                           lookFor.toString());
                if (line.equals("") == Boolean.FALSE) {
                    break;
                }
            }
        }
        return line;
    }

    /**
     * Get the error line number from the exception stack trace
     * 
     * @param exceptionTraceLine
     * @param substrToSearch
     * @return error line number
     */
    public static String getLineNumberFromExceptionTraceLine(String exceptionTraceLine,
                                                             String substrToSearch) {
        String lineNumber = "";
        int index = exceptionTraceLine.indexOf(substrToSearch);
        if (index >= 0) {
            int length = substrToSearch.length() + index;
            if (exceptionTraceLine.lastIndexOf(')') > length) {
                lineNumber = exceptionTraceLine
                    .substring(length, exceptionTraceLine.lastIndexOf(')'));
            }
        }
        return lineNumber;
    }

    /**
     * Get tested method name
     * 
     * @param testMethod
     * @return tested method name
     */
    public static String getName(JUnitTestData testMethod) {
FileLine
org/tap4j/parser/Tap13Parser.java318
org/tap4j/parser/Tap13YamlParser.java455
        this.testSet.addTapLine(testResult);
    }

    /**
     * Returns the test number out from an input String. If the string is null
     * or equals "" this method returns the next test result number. Otherwise
     * it will return the input String value parsed to an Integer.
     * 
     * @param testNumber
     * @return
     */
    private Integer getTestNumber(String testNumber) {
        Integer integerTestNumber = null;
        if (StringUtils.isEmpty(testNumber)) {
            integerTestNumber = (this.testSet.getTestResults().size() + 1);
        } else {
            integerTestNumber = Integer.parseInt(testNumber);
        }
        return integerTestNumber;
    }

    /**
     * @param matcher REGEX Matcher.
     */
    protected void extractBailOut(Matcher matcher) {
        String reason = matcher.group(1);

        BailOut bailOut = new BailOut(reason);

        String commentToken = matcher.group(2);

        if (commentToken != null) {
            String text = matcher.group(3);
            Comment comment = new Comment(text);
            bailOut.setComment(comment);
        }

        this.testSet.addBailOut(bailOut);
FileLine
org/tap4j/ext/junit/JUnitYAMLishUtils.java200
org/tap4j/ext/testng/TestNGYAMLishUtils.java397
        Throwable throwable = testMethod.getFailException();

        if (throwable != null) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            throwable.printStackTrace(pw);
            String stackTraceString = sw.toString();
            stackTraceString = stackTraceString.trim().replaceAll("\\r\\n",
                                                                  "\n");

            StringTokenizer st = new StringTokenizer(stackTraceString,
                                                     LINE_SEPARATOR);

            while (st.hasMoreTokens()) {
                String stackTraceLine = st.nextToken();
                stackTrace.append(stackTraceLine);
                stackTrace.append(LINE_SEPARATOR);
            }

        } else {
            stackTrace.append('~');
        }

        return stackTrace.toString();
    }

    /**
     * Extract the class name from a given junit test description
     * 
     * @param description
     * @return a class name
     */
    public static String extractClassName(Description description) {