• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Searching for files of a specific type in jdk1.4

 
Ronan Dowd
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am using jdk1.6 and want to find an easy way, from within a JUnit test, to locate files in my project of a specific type
I want my test to find all files that end with "hbm.xml" and once i have a list of the files, to then
read their contents.

I can do the below individually for one file but I've to specify it's path:

String fileContents = org.apache.commons.io.FileUtils.readFileToString("src/main/resources/com/boreas/model/Customer.hbm.xml")

I don't want to have to update my test if a user adds a new hbm.xml file. I'm sure theres some way of doing this in java.
Any help would be great.
Cheers. Ro.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out File.listFiles(FileFilter) or File.listFiles(FilenameFilter). The former is probably better because that one allows you to list folders as well, so you can check the folder recursively. In code, with a few gaps:
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taking a step back: why are you trying to do this?
 
Ronan Dowd
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, am using hibernate in my application and want to, as part of a JUnit test, execute all named queries that i've defined. In hibernate, named queries are defined in files
ending in "hbm.xml" so I want to find all files in my application with this file ending then I'm going to use the java Matcher and Pattern classes to
find, in each "hbm.xml" file the name of the query. The i'm going to execute each query. I tried to see if there was a way in hibernate to get all named queries which you've
defined but there doesn't appear to be. Hence me going the java route of finding the file and parsing the contents..
Ro.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if that makes any sense to me: it doesn't test that the named queries you actually *need* are there, that would have to be done on a query-by-query basis, by code that knows (a) what the query is named, and (b) what the query is supposed to return.

I'm just not really sure what you're testing doing what you want to do is all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic