简体   繁体   English

使用java在fitnesse中进行文件比较

[英]file comparison in fitnesse using java

I need to compare 2 csv files and output the results to a new file in Fitnesse using java. 我需要比较2个csv文件,并使用java将结果输出到Fitnesse中的新文件。 Please throw some light on this as I am newbie to Fitnesse. 请注意这点,因为我是Fitnesse的新手。 I am confused about what fixtures are to be used. 我对使用什么灯具很困惑。

Well, there are two ways you can approach this. 那么,有两种方法可以解决这个问题。 One leverages FitNesse very heavily and another just gets the job done. 一个人非常重视FitNesse而另一个人只是完成了工作。

Option 1: Leverage FitNesse Heavily 选项1:大力利用FitNesse

Assuming that one of the two CSV files is fixed and always the same, you could create a fixture that can read the file in and then compare the result to a table in your FitNesse test. 假设两个CSV文件中的一个是固定的并且始终相同,您可以创建一个可以读取文件的夹具,然后将结果与FitNesse测试中的表进行比较。 This might look something like this: 这可能看起来像这样:

|Query:list csv data|c:\test\file_to_check.csv|
|col1       |col2       |col3     |
|Joe        |12         |red      |
|Steve      |15         |purple   |

When the test runs, it would read in the file and split it into rows and columns, using the header row to connect values with columns. 当测试运行时,它将读入文件并将其拆分为行和列,使用标题行将值与列连接。 It then returns the standard Slim (I'm assuming this is using Slim at the moment) List of rows, which themselves are lists of name, value pairs as strings. 然后它返回标准的Slim(我假设此时正在使用Slim)行列表,它们本身是名称列表,值对作为字符串。 If things match, you will see all green. 如果事情匹配,你会看到所有绿色。 If they don't match, then you will get failures indicating the mismatch. 如果它们不匹配,那么您将收到指示不匹配的故障。 Such a fixture would also identify missing rows or extra rows and you would see them in page. 这样的夹具还可以识别缺失的行或额外的行,您将在页面中看到它们。

Option 2: Just get it done 选项2:完成它

The second option, is not going to be as FitNesse friendly, but could work. 第二个选择,不会像FitNesse友好,但可以工作。 This version would push all of the validation into the fixture. 这个版本会将所有验证推送到灯具中。 In that case you would write a fixture that could take two filenames as inputs and then do a comparison all inside the fixture code. 在这种情况下,您可以编写一个夹具,可以将两个文件名作为输入,然后在夹具代码中进行比较。 Then it would return some indication of the failures. 然后它会返回一些失败的迹象。 It could look like this (but there are other designs that could work too: 它可能看起来像这样(但也有其他设计可以工作:

|compare csv files|
|file1             |file2             |match?|
|c:\test_file_1.csv|c:\test_file_1.csv|true  |

This would work with some sort of code like the following (you still have to do the hard part of comparing the CSV files): 这可以使用某种代码,如下所示(您仍然需要进行比较CSV文件的困难部分):

public class CompareCsvFiles {

    private String filenameForFirstFile = null;
    private String filenameForSecondFile = null;
    private String matched = null;

    public void setFile1(String filename){
        filenameForFirstFile = filename;
    }

    public void setFile2(String filename){
        filenameForSecondFile = filename;
    }

    public String match(){
        return new CsvCompareTool(filenameForFirstFile, filenameForSecondFile).match();
    }

    private class CsvCompareTool{
        private String filename1 = null;
        private String filename2 = null;

        public CsvCompareTool(String file1, String file2){
            filename1 = file1;
            filename2 = file2;
        }

        public String match(){
            // create the necessary code to do the comparison here.  
            // I'll leave that to you.
            return "Not implemented yet";
        }

    }


}

If there is a failure, I would return a string that describes the row that had the failure. 如果出现故障,我会返回一个描述失败行的字符串。

See Question about CSV parser for Java for more about parsing CSV files. 有关解析CSV文件的更多信息,请参阅有关CSV解析器for Java的问题

Final Comments 最后评论

Personally, I prefer option 1. It leverages FitNesse for the final validation and shows you the specific failures in page. 就个人而言,我更喜欢选项1.它利用FitNesse进行最终验证,并向您显示页面中的特定故障。 But that does require one file to be a consistent expected result. 但这确实需要一个文件是一致的预期结果。

In either case, I would try to find a library that makes CSV file processing easier, as there are some fun rules about how to escape things to have embedded commas. 在任何一种情况下,我都会尝试找到一个使CSV文件处理更容易的库,因为有一些有趣的规则可以解决如何逃避嵌入逗号的问题。

You might also be able to build something with the TableTable style table to do this, but I don't recommend it. 您也可以使用TableTable样式表构建一些东西来执行此操作,但我不建议这样做。

If you are using FitLibrary, you should be able to do the same sort of thing as either example, but there would be some differences in the coding. 如果你正在使用FitLibrary,你应该能够做任何一个例子,但编码会有一些差异。

FitLibrary的CompareFiles灯具怎么样?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM