This content has been marked as final.
Show 1 reply
-
1. Re: Read properties from seam.properties for unit test with TestNG
josief Apr 30, 2008 10:57 AM (in response to josief)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.propertiesI 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.