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

发布于 2024-12-21 19:07:32 字数 98 浏览 2 评论 0原文

我需要比较 2 个 csv 文件并使用 java 将结果输出到 Fitnesse 中的新文件。由于我是 Fitnesse 的新手,请对此进行一些说明。我对要使用什么固定装置感到困惑。

I need to compare 2 csv files and output the results to a new file in Fitnesse using java. Please throw some light on this as I am newbie to Fitnesse. I am confused about what fixtures are to be used.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恰似旧人归 2024-12-28 19:07:32

嗯,有两种方法可以解决这个问题。其中一个非常重视 FitNesse,而另一个则只是完成工作。

选项 1:充分利用 FitNesse

假设两个 CSV 文件之一是固定的并且始终相同,您可以创建一个可以读取该文件的装置,然后将结果与 FitNesse 测试中的表进行比较。这可能看起来像这样:

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

当测试运行时,它将读入文件并将其拆分为行和列,使用标题行将值与列连接起来。然后它返回标准 Slim (我假设目前正在使用 Slim)行列表,它们本身是名称、值对作为字符串的列表。如果匹配,您将看到全绿色。如果它们不匹配,那么您将收到指示不匹配的失败消息。这样的固定装置还可以识别丢失的行或额外的行,您将在页面中看到它们。

选项 2:直接完成

第二个选项不会像 FitNesse 那么友好,但可以工作。该版本会将所有验证推入夹具中。在这种情况下,您将编写一个可以将两个文件名作为输入的固定装置,然后在固定装置代码中进行所有比较。然后它会返回一些失败的指示。它可能看起来像这样(但还有其他设计也可以工作:

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

这可以与如下所示的某种代码一起工作(您仍然需要完成比较 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";
        }
        
    }
    
    
}

如果出现故障,我 的行的字符串。

将返回一个描述失败 href="https://stackoverflow.com/questions/200609/can-you-recommend-a-java-library-for-reading-and-possibility-writing-csv-files" title="Java 问题的 CSV 解析器" >有关 Java 的 CSV 解析器的问题,了解有关解析 CSV 文件的更多信息

最终评论

就我个人而言,我更喜欢选项 1。它利用 FitNesse 进行最终验证,并在页面中向您显示具体的失败情况。但这确实需要一个文件达到一致的预期结果,

有一些关于如何转义内容以嵌入逗号的有趣规则。

无论哪种情况,我都会尝试找到一个使 CSV 文件处理更容易的库,因为您也可能 能够使用 TableTable 样式表构建一些东西来做到这一点,但我不推荐它。

如果您使用 FitLibrary,您应该能够执行与任一示例相同的操作,但编码上会有一些差异。

Well, there are two ways you can approach this. One leverages FitNesse very heavily and another just gets the job done.

Option 1: Leverage FitNesse Heavily

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. 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. 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

The second option, is not going to be as FitNesse friendly, but could work. 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):

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.

Final Comments

Personally, I prefer option 1. It leverages FitNesse for the final validation and shows you the specific failures in page. 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.

You might also be able to build something with the TableTable style table to do this, but I don't recommend it.

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.

云裳 2024-12-28 19:07:32

FitLibrary 的 CompareFiles 装置怎么样?

What about FitLibrary's CompareFiles fixture?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文