1 Reply Latest reply on Apr 30, 2008 10:57 AM by Adrien FERIAL

    Read properties from seam.properties for unit test with TestNG

    Adrien FERIAL Newbie

      Hi!


      I've defined some properties in my seam.properties file. In my build.xml file, I've added the line to put the seam.properties file into the ${war.dir}/WEB-INF/classes folder in order to have access to the properties in my webapp.


      However, I can't find a way to access to the properties while executing my TestNG unit tests.


      Do you know a way to define properties in Seam? In components.xml?components.properties?seam.properties?


      I really have trouble to find out how to define properties in seam, in the dev, test and prod environments.


      Thanks in advance for your help.


      Adrien

        • 1. Re: Read properties from seam.properties for unit test with TestNG
          Adrien FERIAL Newbie

          Just to let you know.


          I finally give up with the idea to put my properties into the seam.properties file.


          I created 3 files that I put into the resources/config folder:
          - ${projectname}-test.properties
          - ${projectname}-dev.properties
          - ${projectname}-prod.properties


          I created a new Java class:




          import java.io.IOException;
          import java.io.InputStream;
          import java.util.Properties;
          
          import org.jboss.seam.annotations.Logger;
          import org.jboss.seam.log.Log;
          import org.jboss.seam.log.Logging;
          
          
          /**
           * Allow access to configuration properties
           *
           *
           */
          public class ProjectConfig {
          
              /** configuration file */
              private static final String configurationFilePath = ProjectConstants.PROJECT_NAME + ".properties";
          
              private static Properties  props  = null;
          
              /**
               * return the value of the given property key
               *
               * @param key
               *            key
               *
               * @return
               */
              public static String getProperty(final String key) {
                  if (props == null){
                      props = loadFromResource(configurationFilePath);
                  }
                  return props.getProperty(key);
              }
          
          
              /**
               * code taken from seam src
               *
               * @param resource  resource
               *
               * @return
               */
              private static Properties loadFromResource(final String resource) {
                  props  = new Properties();
                  final InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
                 
                  if (stream != null) {
                      try {
                          log.debug("loadFromResource : reading properties from: " + resource);
          
                          try {
                              props.load(stream);
                          }
                          catch (final IOException ioe) {
                              log.error("loadFromResource : could not read " + resource, ioe);
                              props = null;
                          }
                      }
                      finally {
                          try {
                              stream.close();
                          }
                          catch (final IOException ex) {
                              props = null;
                          }    // swallow
                      }
                  }
                  else {
                      log.debug("loadFromResource : not found: " + resource);
                      props = null;
                  }
          
                  return props;
              }
          }
          



          I finally changed the build.xml file:


          [..]
          <target name="war" depends="compile" description="Build the distribution .war file">
          [...]
              <copy tofile="${war.dir}/WEB-INF/classes/${project.name}.properties" file="${src.resources.dir}/config/${project.name}-${profile}.properties" overwrite="true" />
          </target>
          
          [..]
          <target name="buildtest" depends="compiletest,copytestclasses" description="Build the tests">
                    <copy todir="${test.dir}">
                         <fileset dir="${src.resources.dir}">
                              <exclude name="META-INF/persistence*.xml" />
                              <exclude name="import*.sql" />
                              <exclude name="config/" />
                              <exclude name="${project.name}-*-ds.xml" />
                         </fileset>
                    </copy>
                    <copy tofile="${test.dir}/META-INF/persistence.xml" file="${src.resources.dir}/META-INF/persistence-test.xml" overwrite="true" />
                    <copy tofile="${test.dir}/import.sql" file="${src.resources.dir}/import-test.sql" overwrite="true" />
                    <copy tofile="${test.dir}/${project.name}.properties" file="${src.resources.dir}/config/${project.name}-test.properties" overwrite="true" />
                    <copy todir="${test.dir}" flatten="true">
                         <fileset dir="${src.test.dir}">
                              <include name="**/*Test.xml" />
                         </fileset>
                    </copy>
               </target>
          [..]
          



          Then to load a property known as keyProperty, I just need to do :


          String proprtyValue = ProjectConfig.getProperty("keyProperty");
          



          I hope this could help some people.