Spring DAO test (how to specify location of sqlite db file) - spring

I'm using Spring 4 and I've a JUnit test which uses jdbc template to persist data to an sqlite 3 database (which is essentially a file in my case called test.db on the file system). My test works fine and does persist to the database file successfully.
I'm using Maven to build and when I run my Junit test it creates the test.db file in the root folder of the project (i.e. in the same directory where the pom.xml file is). I would like to ensure this file gets created in the target/ directory instead and referenced in there.
Is there a way in a spring test that i can tell spring where to "create" this test.db file?
Any help would be great -
Thanks Ro

Somewhere in one of your files you should have a path variable to the database. This is probably in our JDBC template file. It should look something like this:
DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
dataSource.setUrl("jdbc:derby:c:\temp\database\test01;create=true");
http://www.vogella.com/tutorials/SpringJDBC/article.html

Related

How using folder repository to migration flyway

I'm using java 11 and flyway.
I have a doubt, I'm trying to access a repository folder to apply the scripts in the database.
Example: When my application is started, it will check if there are .SQL files inside a specific folder, if there is, it will apply the script in the informed bank and then it will delete this file. I need to do this because I'm going to fetch the script files from an external repository. In the test I'm doing I'm getting the files from the repository and putting them in a folder at temp.
Flyway flyway = Flyway
.configure()
.locations("filesystem:" + "C:\Users\user_name\AppData\Local\Temp\versionamento_flyway\2\19")
.dataSource(databaseUrl, databaseUser, databasePassword)
.baselineOnMigrate(true)
.connectRetries(2)
.load();
MigrateResult result = flyway.migrate();

Unable to read CSV file when uploading to Google Cloud Platform

My Spring Boot Application structure is this
Spring boot app structure
Within csvService.java class, I am trying to read the test.csv file. I am able to read it on localhost but when I deploy it to Google Cloud Platform, it fails to read the file giving FileNotFoundException.
This is the line of code responsible for reading the csv file:
CSVReader reader = new CSVReader(new InputStreamReader(newFileInputStream("test.csv"), "UTF8"));
The file structure is not the same where you deployed it and the test.csv file likely did not get packaged in your binary. Thus you're getting a FileNotFoundException - the file simply isn't where it was on your local machine.
Things to specify/clarify for your project:
Where and how do you deploy? Is it GKE, GCE or AppEngine? Look up for each of those how they behave when you deploy a jar file.
Why do you want to deploy a test.csv file in production? Surely you want to fetch that csv file on fly and not statically (unless it has some specific meaning) - if you do, put it in the resources folder and treat it as such. You will need to slightly alter your file reading code.
Actually the issue was in the Docker file. The image was getting created and wsa only using the jar. I needed to add the below line in the Docker file.
COPY --from=builder /app/test.csv /test.csv

Spring boot external properties not working

My project structure looks like as attached file. Even though I have profile specific properties, I would like to run my app with external properties file i.e., outside of jar file.
I tried with following command:
java -jar test_service.jar --spring.config.location=file:///C:/external_props/test.properties
But its taking application-default.properties file.
from log file:
No active profile set, falling back to default profiles: default
Why it is not taking external properties file ?
When you pass --spring.config.location command line argument SpringBoot won't consider application-*.properties files in src/main/resources directory. The filename you mentioned for --spring.config.location is taken as base filename, in your case test. So, it will only load test.properties file from that path you provided as default profile.
If you want to enable certain profile, say prod, you need to create file C:/external_props/application-prod.properties and enable prod profile using --spring.profiles.active=prod.
Spring will automatically look for some property file in a specific location.
From where you execute the jar file, Spring will look in that directory for a property file called application.properties
An other way is to put a config directory in the directory you execute the jar from and put the application properties in there.
There is one more option and that is the -Dspring.profiles.active={profiles} parameter.
Spring will then look in the directory or config directory to application-{profile}.properties
Reference:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
Also i think you use the
--spring.config.location=file:///C:/external_props/test.properties
is not used correctly for a windows based file path.
Windows uses the \ instead of the /.

Loading property file on openshift / linux hosting (Spring)

I am loading my Application Configuration property file like this
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setLocation(new ClassPathResource(propertiesFilename));
This code is working fine on my local environment and I have my property file here
But on production it is not able to pick the property file. Where should I keep it so that it works on both local and production with same code.
Unzip the production WAR and see if the properties file is there. If it's not then it means that the build process as some logic (maven profile, ant If clause) that removes the file depending on the environment.
Try to check the pom.xml or ant.xml files for resource filtering functionality that might have removed the file, and replaced it with for example app-prod.properties.

Netbeans Including XML and Properties when building Spring projects with Maven

I am new to Maven and Spring. I'm using Netbeans 7 as my IDE, and setting up a Spring 3 project using Maven.
Everything seemed to set up smoothly, and I began running through the Spring User Guide. However, I'm getting a file not found exception when trying to load my context.xml file.
I have an App class located at com.myproject and the context.xml file is located at com.myproject.conf
I'm using the following line of code in App.java to try and load the context.xml file:
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
But when I run the application, it results in:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [context.xml]; nested exception is java.io.FileNotFoundException: class path resource [context.xml] cannot be opened because it does not exist
Looking at the NetBeans output, it also looks like it's not picking up the log4j.properties file which is also located in com.myproject.conf
I looked at the jar that the build process created, and the entire com.myproject.conf package is missing, meaning the .xml and .properties are missing as well. I've tried moving these config files to the com.myproject package as well as just putting them at the root of the project which don't yield any different results.
So I'm making the assumption that my maven project isn't set up entirely correctly, or maybe a setting isn't correct within NetBeans.
It seems like you need to learn about resources in Maven projects (such as XML, bitmaps, etc...). These are stored in separate directories. See here.
Put the package in "Other Sources"
I have the same problem with a .property file to manage the Internationalization.
I create it in com.company.app.view.resources at the Source Packages directory.
When I build the project and then look at my war file (target).. I don't found the .property file in /WEB-INF/classes/com/company/app/view/resources
Then I put the package in "Other Sources" directory and it works for me.

Resources