Hibernate : include configuration files in WEB-INF in tests - java

I have a web app and i am trying to set up the config for the unit tests. I have the following structure
project
-src/main/java
-src/main/resources
-src/test/java
-src/test/resources
-src
-main
-webapp
-WEB-INF
-spring
-test
-spring
All my spring configuration files are stored in the project\src\main\webapp\WEB-INF\spring directory. But the issue is that my test configuration files are stored in the project\src\test\spring directory.
For my tests i want to use some of the configuration files in the project\src\main\webapp\WEB-INF\spring directory but i keep getting a file not found exception when i try access them.
Is there a way to keep my configuration files in the WEB-INF folder but still visible to my test configuration files?

Why do you keep configurations in project\src\main\webapp\WEB-INF\spring ?
You have to use:
src/main/resources
src/test/resources
respectively.
All files located in "src/main/resources" after maven build will be moved to {appRoot}/WEB-INF folder anyway.
Then from your tests you will see all files located in "src/main/resources"

Related

What are the BOOT-INF and META-INF directories in a Spring-Boot JAR?

When I open a jar package, there shows the BOOT-INF and META-INF folders, what's the function of them?
The META-INF folder is the home for the MANIFEST.MF file. This file contains meta data about the contents of the JAR. For example, there is an entry called Main-Class that specifies the name of the Java class with the static main() for executable JAR files.
for more details
BOOT-INF :
Spring Boot applications load from the BOOT-INF folder. Therefore the application classes must be placed in a nested BOOT-INF/classes directory. Dependencies should be placed in a nested BOOT-INF/lib directory.
more about Spring Boot packaging
This is a single jar Spring Boot application.
In addition to the META-INF files for the loader (as in any other jar file) the application itself is placed under BOOT-INF.
Note that such a jar cannot be used as a traditional jar any more.

Dependency jar not loading its own resource file

I have a dependency utilities.jar which has a dev.properties file in the root directory when compiled (in src/main/resources when uncompiled). The jar has a class (PropertiesUtil.java) that loads the properties with:
PropertiesUtil.class.getResourceAsStream("/dev.properties");
This jar is included as a dependency in my webapp. The webapp has its own dev.properties file in its root directory when compiled (in uncompiled form its in the src/main/resources folder).
The utilities.jar does not load its own internal dev.properties file but instead the webapp's dev.properties file.
I tried different methods like
PropertiesUtil.class.getClassLoader.getResourceAsStream("dev.properties");
without success.
I am using Gradle to compile the utilities.jar and the webapp into a war. The properties filename need to be the same because I pass in a JVM property
-Dproperty.filename=[dev|qa|prd].properties
when starting up the webapp. This system property is used to load the correct properties files for the webapp and the utilities.jar.
The utilities.jar is a separate project and packaged with gradle clean build and uploaded to an artifact server. The webapp pulls the utilities.jar from the artifact server when building the war.
I think this problem is occurring because you have two different files with exactly the same path (even though one is inside a jar) inside your classpath. It's very possible one dev.properties is getting overwritten.
I recommend changing the path of at least one of the properties files to be outside of the project root. For example, put the uncompiled dev.properties for Utilities at:
/src/main/resources/utilities/dev.properties
Then access it with:
PropertiesUtil.class.getClassLoader.getResourceAsStream("/utilities/dev.properties");
Then the two properties files won't stomp on each other anymore, and that should fix your problem.

Can't find a file in maven automatic test

I have a directory in my test folder with all my JavaScript file need for my tests.
src/main
src/test
src/test/java
src/test/javascript
src/test/ressources
I have some tests in src/test/java that use some resources in src/test/javascript.
It is well working when i launch Junit test manually. But when i launch the Maven test goal. i have this :
Couldn't read source file "src/test/javascript/envJsOptions.js": src\test\javascript\envJsOptions.js (The system cannot find the path specified)
not sure how you are using js file in junit test
but just try using javascript/envJSOptions.js
src/main/java
src/main/resources
src/test/java
src/test/resources
all these folders can be considered to be in classpath (same level)
so the paths will be started from that point on, if you are using a resource as stream..

Where do I place the applicationContext.xml file within Spring project directory

I am trying to learn about the Spring framework for Java and I am not sure where about's I am supposed to put the applicationContext.xml file for configuring the spring beans. My directory structure is as follows:
Do I put in .settings? Or should it be put at the top level within springapp?
Thanks for the help.
Put into directory WebContent/WEB-INF.
Resources placed into WEB-INF folder are not accessible from web, they are application internal resources. This is good, because your applicationContext.xml shouldn't be accessible from web.
Other good options are WebContent/WEB-INF/classes or just src (both are equal).
Files and folders with . contains Eclipse configuration files, they are internal for Eclipse - do not use them.
I recommend to put it in the src (or src/META-INF) folder and access it via classpath:applicationContext.xml (or classpath:META-INF/applicationContext.xml). (Eclipse will copy this file to WebContent/WEB-INF/classes when it build the war archive.)
Because:
The mayor advantage of src over src/main/webapp/WEB-INF / WebContent-WEB-INF is, that you can access the src files even in the tests (via classpath:applicationContext.xml)
Do NOT put it .settings because the content of this directory gets not deployed in the Web App (it is eclipse configuration folder)
Of course when you use maven, then put the file in src\main\resources (or src\main\resources\META-INF), Maven will copy them to the classpath folder while compiling.
WEB-INF or its subdirectories. This folder's content is packed directly into the root war file, so files that are directly under this folder are accessible as resources with path like '/foo.xml' (or in spring notation classpath:/foo.xml
It needs to be in the classpath. You can put the original editable instance anywhere (e.g. a config directory off the root) but then you will need to have your build management tool (e.g. Ant or Maven) copy it into the classpath for the runtime.

What is the best practice for the location of Java application configuration files?

Is there a best practice for where configuration files should be stored in a Java project. The file type is a Java properties file in this case, but I do use other file types in other projects.
Would the recommendation vary from stand alone application(.jar) to web app(.war)?
You'll find that many open-source projects follow the directory structure used by Maven. In this setup your application source code is kept in src/main/java, application resources, including properties files, in src/main/resources, and other config files in src/main/config. Files related to unit tests use a similar directory structure; src/test/java and src/test/resources.
Personally I tend to use this layout because of its widespread use. I also keep an "etc" directory beneath the project root to house files that aren't directly related to the application. For example, I keep configuration files for PMD and Checkstyle in etc.
In general a common practice is to have a resources directory for configuration files which is copied into the build artifact by the build process. Maven uses this in its default project structure. Within the resources directory, you might also have a META-INF directory and/or a WEB-INF directory in an application packaged as a war.
I use:
META-INF/ for jar files
WEB-INF/ for war files

Categories

Resources