How to setup custom serenity.properties location? - java

I would like to load custom serenity.properties from specific directory etc. src/test/resources/properties/serenity.properties via gradle
I tried to use -Dproperties parameter with absolute path to file (as it is recommended on their site) but it was not working

You can put your Properties werever you would like but you need to remember the properties is part of the project. Therefore when you pass in your -d properties parameter it will be from the directory you are running the project from. So you would do something very simple like:
-Dproperties=src/test/resources/properties/serenity.properties
Mine is in a properties directory on the root and I modified the name so mine looks like this:
-Dproperties=properties/desktop.properties

You can create a method in your build which loads the data from properties file.
// Read serenity.properties
def getSerenityProperties(){
def props = new Properties();
def propFile = file('/path/to/directory/with/serenity.properties')
props.load(propFile.newDataInputStream())
return props;
}
// assign properties data to a variable
project.ext.serenity = getSerenityProperties()
// use property
serenity.xyz
serenity.pqr

Related

How to get value from gradle.properties in .class file at the time of build

I would like to get a property value from Gradle.properties in Java class.
In Java, the value should be replaced at build time, and in the .jar(.class) file the value will come but not in a .java file. So that we can change the value directly in gradle.properties and no need to change the code.
Is it possible to achieve?
It would be easier to answer if you told your specific use case.
Also it would help to know your application, for example is it a Spring (Boot) app? In that case it would probably make more sense to use Spring Profiles for that.
Anyway, here is a possible solution:
Create a properties file and put it in your resources folder. Define a placeholder, that gradle can replace. For example file "myapp.properties"
greetingText=#greeting#
Add the token (the text between the '#'s) to your gradle.properties:
greeting=Hello world!
Make the build.gradle replace the token with the value from gradle.properties by configuring the processResources task:
processResources {
inputs.file file('gradle.properties')
filter(
ReplaceTokens,
tokens: [
greeting: project.ext.greeting
]
)
}
At runtime load the value from the properties file:
public String getGreeting() throws IOException {
try (
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResource("myapp.properties").openStream();
) {
Properties appProps = new Properties();
appProps.load(inputStream);
return appProps.getProperty("greetingText");
}
}

Extract properties file from JAR dependency and use it in own classpath

I want to use a small program as a dependency in my Maven project. This program is configured by a properties file which can be edited before its execution. So far I have added the dependency as JAR in a local repository.
Now I want to make that dependency's properties file accessible in my own superior classpath, i.e. in myprogram/src/main/resources/config/myprogram.properties and not in myprogram/local-repository/com/example/mydependency/mydependency.jar/mydependency.properties.
I tried to modify the part of the code where the path for the properties file is defined:
public example() {
Properties prop = new Properties();
InputStream input = example.class.getResourceAsStream("/config/myprogram.properties");
prop.load(input);
...
}
I also deleted the original properties file of the dependency before adding it to my local repository.
The whole program is working, but not as expected. Strangely, neither my new properties file in /src/main/resources/config nor the old one in the mydependency.jar is used. It seems that some kind of default properties file is put into my final fat JAR. But I cannot find its source anywhere - even when I try to debug it. That default properties file just seems to appear out of nowhere.
Now, how can I properly move the dependency's properties file to my own classpath?
And where could this default properties file appear from?
Is this an issue with Maven or with the source code itself?
Thanks in advance!
You can do something like this:
public static String getValueFromDependencyProps(ClassLoader cl, String propertiesFile, String key) throws IOException {
Properties prop = new Properties();
prop.load(cl.getResourceAsStream(propertiesFile));
String value = prop.getProperty(key);
if (value == null)
throw new NullPointerException();
else
return value;
}
And then:
String value = getValueFromDependencyProps(YourDependencyClass.class.getClassLoader(), "your.properties", key);
Tested and working. With that I'm able to access any properties file from an external Maven dependency (using one of its classes, YourDependencyClass, to reach it).

How to open a properties file from another package on Netbeans (JAVA)

I have a bean that needs to take some parameters from a properties file but I can not find it (java.lang.NullPointerException) in order to open it. My bean is in the extra.beans package while the properties file is in the extra.dao package. I am trying to do
file = new FileInputStream("database.properties");
prop.load(file);
and I have tried any possible combination for the path but I can not find it. I am using Netbeans 7.4. How can I open it?
You can Use Resource Bundle for that.
ResourceBundle resBundle = ResourceBundle.getBundle("PropertyFileName"); // without extention
String name= resBundle.getString("Required Attribute"); // example username
Specify the full path. It should work.
If you're loading the properties file into a Properties object, try the something like:
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("../dao/database.properties"));
I don't know your full package structure but using this approach and putting the full path to the properties file would also work, ie: /extra/dao/database.properties
file = getClass().getClassLoader().getResourceAsStream("extra/dao/database.properties") ;
prop.load(file);

Is there any way to change a properties file that is in my classpath?

I have a properties file that is in my classpath. My requirement is to change some properties in this file based on some inputs I get from arguments. The nature of arguments decide whether I need to change the properties and if yes which properties to change. The problem is that all classpath entries are loaded at the application startup time, so changing from within my application would not have any effect. How do I overcome this problem?
One possible solution I can think of is to not add this properties file in classpath but add after modifications are done. Is it viable? What can be a good solution?
It doesn't matter whether this file is on your classpath or not. It is a file: if you overwrite its contents, it will have changed. There isn't some in-memory copy that magically gets made at startup. This is very different from classes that are loaded in and which might need change at runtime.
Properties files that adhere to the right format can be read into a java.util.Properties object. You could do that, use the object to alter the properties as needed, then write it back out to the file. Check the store and load methods in that class. Mind that if you use the versions that take an Output/InputStream, the encoding is hard-coded. If the file's encoding is anything else than ISO-8859-1, use a method with an appropriate Writer/Reader.
Depends on how your application is deployed. If your properties files is inside a jar, you won't be able to directly change that properties file since its packaged and zipped up in an archive. You can instead as someone else mentioned load those properties into an object, and then store/write out to an external location, probably a URL based location. URL is convenient because it gets you access to virtually any location and it has that nifty openStream() method for loading properties. Your application could then look for the new file on load, and default to the application startup version if it fails to read/load from the new location.
Here is a sample code:
Properties p = new Properties();
File f = new File("file");
InputStream in = new FileInputStream(f);
p.load(in);
p.put("key", "blah");
OutputStream out = new FileOutputStream(f);
// If no comments p.store(writer);
p.store(out, "properties");
You need to first remove that property from the property file and then re-define it. Their is no way to directly modify the properties file.
Below is an example:
Properties pproperties = new Properties();
if (properties.containsKey("key1")) {
properties.remove("key1");
properties.setProperty("key1", "value1");
properties.store(new FileOutputStream("file.properties"), null);
}

Storing changes in .properties file that has been read via getClass().getResourceAsStream

I am writing a java web application that reads properties from a .properties file. Since I do not know the absolute path of the .properties file, because it depends on the environment the application will run on in the future, I have to load it with "getClass().getResourceAsStream":
Properties props = new Properties();
props.load(getClass().getResourceAsStream("test.properties"));
message = props.getProperty("testdata");
This works as expected. Now I want to change the value for testdata in the file. But I cannot open an Outputstream to write to, because I still don't know the path of the .properties file.
props.setProperty("testdata", "foooo");
props.store(new FileOutputStream("?????"), null);
Is there a way to get the path of the file or can I use the established Properties-object somehow? Any ideas are welcome that allow me to change the .properties file.
You can get an URL by using getResource() rather than using getResourceAsStream()
You can then use that URL to read from and write to your properties file.
File myProps = new File(myUrl.toURI());
FileInputStream in = new FileInputStream(myProps);
Etc.
The Properties class includes a store method that can be used to save the properties back to the stream that was read in getClass().getResourceAsStream("test.properties").

Categories

Resources