cancel
Showing results for 
Search instead for 
Did you mean: 

how to use java-diff-utils

SOLVED
Highlighted
Frequent Contributor

how to use java-diff-utils

Hello,

for a test, I need to compare files in a groovy script and I found out that a library could help me perform this in a simple way. However, though there are quite a lot of examples that I can rely on in order to build my own methods, I am unable to use this lib. I cannot find anywhere how to import / use this lib in my groovy script.

I set the java-diff-utils-2.2.0.jar and jsr305-3.0.0.jar in bin/ext and I use the following example in a groovy step, just for trying, but it fails (unable to resolve class Patch @ line 70, column 19)

 

I guess that import is wrong, but I'm not fluent in groovy/java and not familiar at all with dependency and lib handling

Could anyone help me figure out what's wrong ?

 

import difflib.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.AbstractList;
import java.util.LinkedList;
import java.util.List;

public class TestDiffUtils {

    public TestDiffUtils() {

    }

    // Helper method to read the files to compare into memory, convert them to a list of Strings which can be used by the DiffUtils library for comparison
    private static List fileToLines(String filename) {
        List lines = new LinkedList();
        String line;
        try {
            URL path = TestDiffUtils.class.getResource(filename);
            File f = new File(path.getFile());
            BufferedReader input = new BufferedReader(new FileReader(f));
            while ((line = input.readLine()) != null) {
                lines.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return lines;
    }

    // Helper method to convert a String to List<Character> : to be used with DiffUtils.diff() method when finding character diffs within the line diffs
    public static List<Character> asList(final String string) {
        return new AbstractList<Character>() {
            public int size() { return string.length(); }
            public Character get(int index) { return string.charAt(index); }
        };
    }

    private static void performDiff(String testName) {
        String origFileName = null;
        String revisedFileName = null;

        Boolean continueTest = true;    // continue unless we can't

        if( testName.compareTo("large-file-test") == 0 )
        {
            origFileName = "test_large_file.xml";
            revisedFileName = "test_large_file_revised.xml";
        }else if( testName.compareTo("small-file-test") == 0 ){
            origFileName = "originalFile.txt";
            revisedFileName = "revisedFile.txt";
        }else if( testName.compareTo("large-file-test-single-line") == 0 ){
            origFileName = "test_large_file_SingleLine.xml";
            revisedFileName = "test_large_file_revised_SingleLine.xml";
        }else{
            continueTest = false;
        }

        if( continueTest && origFileName != null && revisedFileName != null ) {
            // Convert the orig and revised files to List<String> format that DiffUtils.diff() uses
            List<String> originalLines = fileToLines(origFileName);
            List<String> revisedLines = fileToLines(revisedFileName);

            // Get the line-by-line diffs
            Patch patch = DiffUtils.diff(originalLines, revisedLines);
            List<Delta> deltas = patch.getDeltas();
            for (Delta delta : deltas) {
                // The line in the orig file that this current diff occurs at
                int lineOfDiffInOrig = delta.getOriginal().getPosition() + 1;

                // Only continue with the diff if the revised lines is not empty (we will get IndexOu0tOfBoundsExceptions if we don't do this check)
                if (delta.getOriginal().getLines().size() > 0 && delta.getRevised().getLines().size() > 0) {
                    // Get orig and revised lines in List<Character> format
                    List<Character> origChars = asList(((String) delta.getOriginal().getLines().get(0)));
                    List<Character> revisedChars = asList(((String) delta.getRevised().getLines().get(0)));

                    // Get the character-by-character diffs
                    Patch deltaPatch = DiffUtils.diff(origChars, revisedChars);
                    List<Delta> strDeltas = deltaPatch.getDeltas();
                    for (Delta strDelta : strDeltas) {
                        int charPosOfDiffInOrig = strDelta.getOriginal().getPosition() + 1;
                        int lengthOfDiffInOrig = charPosOfDiffInOrig + strDelta.getOriginal().size();

                        System.out.println("Line" + lineOfDiffInOrig + " : [" + charPosOfDiffInOrig + "," + lengthOfDiffInOrig + "]");
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        //performDiff("large-file-test");
        //performDiff("small-file-test");
        performDiff("large-file-test-single-line");
    }
}

thanks for any help

 

Alex

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Manager

Re: how to use java-diff-utils

Hi @krogold, thank’s for creating this topic.  

This article contains an example of using third-party libraries in ReadyAPI, perhaps, you will find this helpful.

 


Sonya Mihaljova
Community and Education Specialist

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today

View solution in original post

3 REPLIES 3
Highlighted
Community Manager

Re: how to use java-diff-utils

Hi @krogold, thank’s for creating this topic.  

This article contains an example of using third-party libraries in ReadyAPI, perhaps, you will find this helpful.

 


Sonya Mihaljova
Community and Education Specialist

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today

View solution in original post

Highlighted
Frequent Contributor

Re: how to use java-diff-utils

Well, thanks for replying but I already tried for another lib and it worked, my problem is really to know how to import this specific library.

I followed an example but it ended up wrong 😞

I'll go through the process again, following the examples, I may find how to solve my problem

Highlighted
Community Manager

Re: how to use java-diff-utils

I see. Good luck with your investigation, do let us know how this goes! 


Sonya Mihaljova
Community and Education Specialist

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Compete with community members in the TechCorner Challenge and get into the Leaderboard!

Rank Participant Points Earned
1 msiadak 12
2 nmrao 8
3 HimanshuTayal 3
Top Kudoed Authors