0

I ran into a funny situation because java's File.listFiles() method returns a list of Files in a directory without any guarantees on the order. One class reads some information from all files in a directory using this listFiles() method and writes to an OutputStream. A corresponding JUnit test cases tries to compare the data written to the output stream with some expected byteArray. The test passed on my dev machine and the production stack, but failed on a dev server because it uses a different OS and a different version of Java.

In how to File.listFiles in alphabetical order? the solution is to force the sorting of the array returned from File.listFiles(). But I am not comfortable about this solution because it is doing some extra work just so the JUnit test can pass.

So I am just wondering if there is a better way. Any advice? Thanks

Community
  • 1
  • 1
Jim
  • 161
  • 2
  • 10
  • 3
    I think it is worth sorting. Who can guarantee that prod server will not be moved to the cloud after 1 year? – gaborsch Feb 19 '13 at 21:03
  • If you say that the business logic of your application doesn't require 'sorted files' then there still must be some rules how you decide if your OutputStream is a correct file (it is probably used somewhere and it is expected to be in some specific format or smth)? An if these rules exists then you should write your unit tests against those rules – jonasnas Feb 19 '13 at 21:35
  • Thanks for responding. The application aggregates the information found in these files, and each line is an independent unit of information. the receiver of the output acts on each line, so I didn't think sorting was necessary. But judging from all the responses, it does seem sorting the output is the best way to go, and I will put that in place. @GaborSch I am not clear on your comment regarding moving to the cloud -- will it have any impact on sorting? – Jim Feb 20 '13 at 14:23
  • @Jim No, I meant that you can easily change the version of OS, the Java version, whatever - it is not worth to rely on that, unless you want a terrible headache to yourself :) – gaborsch Feb 20 '13 at 14:48

1 Answers1

3

I do not see a problem with always sorting the list. You are not only doing it for the unit test: Reducing randomness in a system is usually a good idea (it makes testing easier, and when problems occur, it makes them easier to reproduce). It's true that you are doing extra work, but unless you are reading thousands and thousands of files, the sorting is unlikely to be a bottleneck (of course, profile regularly).

So in my opinion, sorting is the best option.

If you don't want to / cannot do this, the other option would be to make the unit test smarter; for example, don't compare the whole byteArray in one go, but check total size and that the result for each file is present in the array. Or parse and sort the array in the test (though that may be complicated).

sleske
  • 81,358
  • 34
  • 189
  • 227
  • Great -- it seems unanimous that sorting is the way to go, and I will definitely do that. Thanks everyone! – Jim Feb 20 '13 at 14:25