-1

I try to compare this .txt

USER bonb Bird
USER colc Colin Cook
EVENT alea Class ULE 11/11/2011 9:00 11/11/2011 13:00
EVENT alea Meeting MIC 11/11/2011 13:00 11/11/2011 14:00
EVENT alea Lunch Cafeteria 11/11/2011 14:00 11/11/2011 15:00
EVENT bonb Class ULE 11/11/2011 15:00 11/11/2011 19:00
EVENT bonb Meeting Library 11/11/2011 13:00 11/11/2011 14:00

In my class File.java I use this method to convert to String the content of the txt.

public String readElements() throws FileException {
        FileReader fr = null;
        BufferedReader br = null;
        StringBuilder line = new StringBuilder();
        String lineAux = "";

        logger.info("Reads an element of the file: " + this.fileName);

        try {
            fr = new FileReader(this.fileName);
            br = new BufferedReader(fr);

            lineAux = br.readLine();
            while (lineAux != null) {
                line.append(lineAux + System.getProperty("line.separator"));
                lineAux = br.readLine();
            }
        } catch (IOException e) {
            logger.warn("File Error: Can´t open the file: " + this.fileName);
            throw new FileException("File Error: Can´t open the file " + this.fileName);
        } finally {
            try {
                if (null != fr) {
                    fr.close();
                }
            } catch (IOException e) {
                logger.warn("File error: Can´t open the file: " + this.fileName);
                throw new FileException("File error: Can´t open the file: " + this.fileName);
            }
        }
        return line.toString();
    }

I use this tests to try the File.Java The TestReadElements compares that String with the content of the file. And when I use Junit in eclipse or the build.xml in a Linux terminal the test fails.

@Before
    public void setUp() throws Exception {
        this.fileName = "./etc/test/rightCalendar.txt";
        this.file = new File(this.fileName);
    }

@Test
    public void testReadElements() throws FileException {
        assertEquals("USER alea Alex Archer\n" + "USER bonb Bonnie Bird\n" + "USER colc Colin Cook\n"
                + "EVENT alea Class ULE 11/11/2011 9:00 13:00\n" + "EVENT alea Meeting MIC 11/11/2011 13:00 14:00\n"
                + "EVENT alea Lunch Cafeteria 11/11/2011 14:00 15:00\n"
                + "EVENT bonb Class ULE 11/11/2011 15:00 19:00\n"
                + "EVENT bonb Meeting Library 11/11/2011 13:00 14:00\n", this.file.readElements());
    }

I don´t know where the problem is(if the problem is within the build.xml or some other file) I have been changing little things for hours and I´m tired... These are the path files and the comparison that tells me it´s incorrect

DxDouglax
  • 1
  • 1

1 Answers1

1

You are explicitly concatenating the lines being read from the reader with System.getProperty("line.separator"); your screenshot suggests that you're running this on Windows (the path starts with C:\Users), which uses \r\n as the line separator.

Your expected string's lines use \n as the line separator.

Hence, the strings aren't equal because they use different line separators.

It would be better to keep things as a list of strings, and then just compare the lists (List.equals does pair-wise comparison of elements, so it's effectively the same - just without any confusion over line separators).

Andy Turner
  • 137,514
  • 11
  • 162
  • 243