How do I specify the Dropwizard config file in Intellij for unit tests? - intellij-idea

I'm developing an application in Dropwizard 0.9, using IntelliJ Idea as my IDE. I have a run configuration configured, and it passes the command line arguments "server ./path/to/config.yml" to specify the configuration file to run with.
When I try to run my unit tests, however, I cannot determine how to pass this config file path to the server. The Run/Debug Configuration dialog has a "Program Arguments" field, but it is permanently disabled (and the edit button adjacent to it does nothing).
How can I enable program arguments, or, alternatively, is there another way to specify the config file for tests?

if your config.yml is in the project's root folder, just set server config.yml in the IDE. Take a look at this video.
If you would like to access the config.yml from the project's root folder, just pass the name of the file to the rule.
#ClassRule
public static final DropwizardAppRule<DWGettingStartedConfiguration> RULE
= new DropwizardAppRule<>(DWGettingStartedApplication.class,
"config.yml");
Also, it is possible to store the configuration file for integration tests in the src/test/resources folder. In this case, the file can be accessed using the following code:
/**
* A path to test configuration file.
*/
private static final String CONFIG_PATH
= ResourceHelpers.resourceFilePath("test-config.yml");
/**
* Start the application before all test methods.
*/
#ClassRule
public static final DropwizardAppRule<DropBookmarksConfiguration> RULE
= new DropwizardAppRule<>(
DropBookmarksApplication.class,
CONFIG_PATH);
In addition, please take a look at my example projects here, here and here.

Related

Use Koin fileProperties with an absolute path

I am trying to deploy a koin application. So far I am using fileProperties pointing to a location in src/main/resources called local.properties.
Now I am deploying a jar and would like to tell Koin to look in a particular folder for the server properties, eg /usr/share/my-app/config/
Unfortunately every attempt to run results in this exception
org.koin.core.error.NoPropertyFileFoundException: No properties found for file
I have tried including the file specifically on the class path of the jar command but without any luck.
Is this by design? That the file properties should be within the jar? Or am I doing something totally wrong...
snippets below
install(Koin) {
fileProperties(propertiesFileName)
modules(properties, monitoring, etc)
}
with various run configs
val propertiesFileName = "/usr/share/my-app/config/app.properties"
val propertiesFileName = "file:///usr/share/my-app/config/app.properties"
val propertiesFileName = "/app.properties" //with -cp including the file specifically

hostfile.json webroot property not serving static files

Going nuts :-)
I've installed the latest ASP.NET Core (RC2).
I'd like to be able to create a *.sln, with multiple *.csproj: one for server side development, and one for just client side development.
The reason we are keeping them separate is so that we can have the option of giving the the clientside *.csproj to external developers with better UI skills to work on without needing to know much about the server side code. They could work on the client side html/js using Visual Studio Code or other light weight IDE, and not requiring Visual Studio to get involved.
In the client *.csproj, I'd like to serve static files (html/js/css) for an angular project from the root directory, not from the wwwroot directory, so that gulpfile.js relative paths, etc are identical to how one would set up an angular project without Visual Studio.
As I understand it, the rules are now:
* use the webroot setting in hosting.json if hosting.json file exists.
* otherwise, use wwwroot.
* if that's missing, use root.
* See: https://github.com/aspnet/Hosting/issues/450
First, checked that I had set up static page routing. Created a wwwroot/index.html page. Tada! Works.
Now, renamed the directory to app/ and updated hosting.json to point to it. After a reload of the project, the app/ folder changed icon..good...run...no joy. Fight with it for a while. No success...
Then delete the app/ folder and hosting.json file altogether. End up definitely wanting to throw something...
The only way I'm getting static files is if the folder is called wwwroot. Whether I have a hosting.json file or not.
That's contrary to the documentation at: https://github.com/aspnet/Hosting/issues/450
Has anybody else succeeded in getting rid of the wwwroot folder? If so...how?!?!?
Thank you!
Although you can open up the root of your ASP.NET Core app for serving static files, it's not a good idea because once you do, there's nothing preventing someone from navigating to project.json or any other file in the root.
That being said, here's how you would go about serving up static files outside of wwwroot.
First, create a static class that returns IApplicationBuilder. In here you will define what physical path to make accessible along with an optional URL re-write of that path (see comments in the code):
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.Extensions.FileProviders;
public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UseRootResources(this IApplicationBuilder app, HostingEnvironment env)
{
//var path = Path.Combine(env.ContentRootPath); // WARNING - this opens up the root of the app
var path = Path.Combine(env.ContentRootPath, "static"); // this would allow for serving up contents in a physical path folder named 'static'
var provider = new PhysicalFileProvider(path);
var options = new StaticFileOptions();
// the below line re-writes the path name of the physical path, it can have the string value of anything you want to call it
options.RequestPath = ""; // in this example, an empty string will give the *appearance* of it being served up from the root
//options.RequestPath = "/static"; // this will use the URL path named static, but could be any made-up name you want
options.FileProvider = provider;
app.UseStaticFiles(options);
return app;
}
}
Next, in the Startup.cs, call that function from the Configure method:
app.UseRootResources((HostingEnvironment)env);
Now you can serve up static files outside of wwwroot! Referencing the static file in HTML will use the path you defined in the Options.RequestPath as set in the ApplicationBuilderExtensions class. Assuming you left the RequestPath to an empty string to simulate the root, you then call the resource like it lived there (even though it really lives in the 'static' folder) thanks to the magic of URL re-writing:
<img src="bus.jpg"/>

IntelliJ & Global Config Files

I've been searching for a solution but I can't find one.
I have a global configuration directory in my IntelliJ workspace. I also have several dozen modules. I would like each module to automatically include the global config directory in its path when I run or test a class.
Is there anyway to do this within IntelliJ? I don't think I should need to edit the configuration for each "Run/Debug" config to include the directory.
You'll want to set it in the Defaults for the type of Run or Debug Configuration that you are using.
For example, if I always want a Java Application to have the VM Option -XPutYourThingyHere, then I could go to Edit Configurations, Defaults, Application, and put -XPutYourThingyHere in the VM Options box. Then all new Applications that I run will have that option.

External properties file with Weblogic

I'm looking for the best way to use an external properties file with an application that is going to be deployed on Weblogic 10.3 server. I read a number of articles on the site but I don't want to hard-code the path to the properties file or put the file in the domains/mydomain folder.
Is there a dynamic way of doing this so when the application is deployed the properties file is also installed for example under the deployments folder and read from there?
Many thanks
Another alternative that does not require putting the file in a place other applications will read it is to use the Generic File Loading Overrides:
http://download.oracle.com/docs/cd/E21764_01/web.1111/e13702/config.htm#i1066493
This involves creating a directory that will be the root directory of your deployment, let's call it FooApplication that has FooApplication.ear and FooWeb.war. This is called the Application Installation Directory. Your application goes in the FooApplication/app sub-directory whether it is an archive (like .ear, .war, jar) or whether it is an exploded version of one of those archives. Your optional deployment plan (you must have one to use this feature, it could be a plan that does not do much beyond specifying a config-root element and values as described in the documentation) goes in the FooApplication/plan. You can put your properties that you want to override ones in the application in FooApplication/plan/AppFileOverrides directory structure.
http://download.oracle.com/docs/cd/E21764_01/web.1111/e13702/deployunits.htm#sthref9
Once that style of deployment is done, you write code like this from your application and the contents of myApp.properties get read from the FooApplication/plan/AppFileOverrides/FooWeb.war/myApp.properties will be the actual file that is read in.
Properties myAppProps = new Properties();
InputStream iostream =
Thread.currentThread().getContextClassLoader().getResourceAsStream("myCfg/myApp.properties");
myAppProps.load(iostream);
This is accomplished by adding a classloader to your application as explained in the docs. It might seem tedious to initially configure, but it is a feature that directly satisfies the original question and only for that particular application.
You can set a directory on the classpath and Place your custom properties file in that folder/directory. So that, the entire directory along with property file will be on classpath.
To set the directory on the classpath in weblogic 10.3.x
Create a folder in %DOMAIN_HOME%\config\ folder. example appConfig.
Place your custom property file (Let's say config.properties) in appConfig directory/folder.
Modify the setDomainEnv.cmd (Windows) to include appConfig in the classpath by setting %DOMAIN_HOME%\config\appConfig as value to EXT_POST_CLASSPATH(this variable is already defined in the setDomainEnv.cmd file) variable as below:
set EXT_POST_CLASSPATH=%EXT_POST_CLASSPATH%;%DOMAIN_HOME%\config\appConfig
You can access that file in you java code as below:
InputStream inputStream = Thread.currentThread ().getContextClassLoader().getResourceAsStream ("config.properties");
Properties prop = new Properties();
prop.load(inputStream);
String value = prop.getProperty("key");
Hope this helps.
Approach #2
Use Weblogic shared library
http://download.oracle.com/docs/cd/E12840_01/wls/docs103/programming/libraries.html
Follow below steps
Package all your configuration as separate JAR during the build process
Deploy configuration JAR as shared library
Reference above shared library from your EAR/WAR
Deploy EAR/WAR (Configurations will be available in classpath)
When you say " I read a number of articles on the site but I don't want to hard-code the path to the properties file" I assume you are saying you don't want to hard code it in your Java code. If that is so, then please see below
Answered here:
There are ways to read properties file in Java from weblogic classpath
One (Properties file located in the weblogic domain): Drop the properties file inside the Domain directory. This way the properties file is added to the weblogic classpath automatically and we can read from Java using resourceAsStream.
Two (Properties file from a User defined location):The advantage with this approach is that the property file can reside outside the JAR or EAR file and can be modified conveniently.
package com.test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertyFileExample {
private static Properties prop;
public static void myMethod() {
InputStream is = null;
try {
prop = new Properties();
String propFilePath = System.getProperty(“propFileLocation“);
InputStream iStream = PropertyFileExample.class.getClassLoader().getResourceAsStream(propFilePath);
prop.load(iStream);
prop.getProperty(“dbuser”);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the weblogic setDomainEnv(under bin) we need to pass the location of the property file as a -D argument to JAVA_OPTIONS
set JAVA_OPTIONS=%JAVA_OPTIONS% -DpropFileLocation =/dev/file/properties/some.properties
Hope it helps!
Approach #1
Update your server startup script to pass below system variable to JVM (below is example on Windows OS)
call "%DOMAIN_HOME%\bin\startWebLogic.cmd" "-Dcom.mycompany.myapp.EXTERNAL_CONFIG_PATH=/mycompany/myapp/config" %*
Using this variable which points to your configuration directories, read configurations from there. You will need to make this setting on each server where you want to deploy your application.

Maven test/resources directory and integration test

Hopefully should be a simple question...
I have an integration test module which contains the default directory structure:
src
|-main
|-test
|-java
|-resources
Then within my resources directory I have an xxxx.xml and xxxx.xsd file, and I need to load these files in as part of my test:
#Test
public void should_do_some_stuff_with_xml_and_xsd() // not actual test name
{
File xmlFile = new File("xxxx.xml");
File xsdFile = new File("xxxx.xsd");
...
}
It keeps failing trying to load the file, now I presumed it was down to me needing to give it a relative path from the project root or something. I need this test to run externally to my IDE so I can run the tests on the build server when it gets there...
So my question is, how do I target these files?
The classpath mechanism does not work for files. Relative file paths are resolved from the current directory, not from the classpath elements.
So you could just do
File xmlFile = new File("target/test-classes/xxxx.xml");
File xsdFile = new File("target/test-classes/xxxx.xsd");
However, a much cleaner solution would be to work with InputStreams instead of files. Almost every library that supports File parameters also supports InputStream parameters. And that way you can use ClassLoader magic without manually specifying any paths:
ClassLoader cldr = Thread.currentThread().getContextClassLoader();
InputStream xmlStream = cldr.getResourceAsStream("xxxx.xml");
InputStream xsdStream = cldr.getResourceAsStream("xxxx.xsd");
Reference:
Byte Streams (Sun Java Tutorial)
ClassLoader (javadoc)
InputStream (javadoc)