Search code examples
javaxmlproperties-fileresource-files

Which type of resource file to use to store constants


I am developing an application that tests web Services, and I use JUnit parameterized tests. I want to read parameters from a resources file. I am wondering which is the best way to store these parameters.

  1. In a .properties file ?

    test1.inputPath= C:\\Path
    test1.expectedOutputPath= C:\\Path
    test2.inputPath= C:\\Path2
    test2.expectedOutputPath= C:\\Path2
    
  2. In an xml file?

    <test>
      <inputPath>  C:\Path <\inputPath>
      <expectedOutputPath>  C:\Path <\expectedOutputPath>
    <\test>
    <test>
      <inputPath>  C:\Path2 <\inputPath>
      <expectedOutputPath>  C:\Path2 <\expectedOutputPath>
    <\test>
    
  3. Other way to do this?

Thanks.


Solution

  • The best solution that I found for my problem is to use PropertiesConfiguration of Apache Commons Configuration. It is very simple to use:

    In my .properties file :

    test1= Path1,Path2
    test2= Path3,Path4
    

    Then I read the .properties file automatically and I retrieve paths as a String array for each test.

    @Parameters
    public static Collection<Object[]> readPropertiesFile(){
        ArrayList<Object[]> result= new ArrayList<Object[]>();
        try {
            Configuration config = new PropertiesConfiguration("testPaths.properties");
            Iterator<String> keys=config.getKeys();
            while(keys.hasNext()){
                String[] paths= config.getStringArray(keys.next());
                result.add(paths);          
            }
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
        return result;
    }