H2 and Postgres array compatibility - java

Can I get h2 to support Postgres array syntax
CREATE TABLE artists
(
release_id integer,
artist_name text,
roles text[]
)
I use h2 to mimic Postgres in my unit tests, but it doesn't like the above DDL because of the definition of roles (if I comment out that column it works). H2 does have an ARRAY datatype is there a way I can write so that my code would work with either h2 or postgres

In fact, you can define integration tests with real postgres DB instead of h2.
It will be more usefull.
The main idea is to run docker instances with dependencies(postgres DB) before integration tests and shut down after.
Here is an example with maven:
First define rules:
<plugin>
<!-- define Integration tests -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<systemPropertiesFile>${ports.env.file}</systemPropertiesFile>
<includes>
<include>**/*IT.*</include>
</includes>
<additionalClasspathElements>
<additionalClasspathElement>resources</additionalClasspathElement>
</additionalClasspathElements>
<systemPropertiesFile>${it.ports.env.file}</systemPropertiesFile>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
Then need to get free ports for your dependencies (for example postgres DB)
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>reserve-network-port</id>
<goals>
<goal>reserve-network-port</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<portNames>
<portName>DB_PORT</portName>
</portNames>
<outputFile>${it.ports.env.file}</outputFile>
</configuration>
</execution>
</executions>
</plugin>
Then you should run and stop docker containers with dependency-services (postgres):
<plugin>
<groupId>com.dkanejs.maven.plugins</groupId>
<artifactId>docker-compose-maven-plugin</artifactId>
<version>4.0.0</version>
<configuration>
<envFile>${it.ports.env.file}</envFile>
<envVars>
<COMPOSE_HTTP_TIMEOUT>120</COMPOSE_HTTP_TIMEOUT>
</envVars>
<services>
<service>db-postgres-test</service>
</services>
<composeFiles>
<composeFile>${session.executionRootDirectory}/docker-compose.db-only.yml
</composeFile>
</composeFiles>
<detachedMode>true</detachedMode>
</configuration>
<executions>
<execution>
<id>up</id>
<phase>pre-integration-test</phase>
<goals>
<goal>up</goal>
</goals>
</execution>
<execution>
<id>down</id>
<phase>post-integration-test</phase>
<goals>
<goal>down</goal>
</goals>
<configuration>
<removeVolumes>true</removeVolumes>
<removeOrphans>true</removeOrphans>
</configuration>
</execution>
</executions>
</plugin>
This solution helps me with the same problem earlier.
I hope, it will help you.

Related

Maven exec won't recognize my app in src/test

I am trying to run a mvn exec for one of my spring boot apps but it is giving me a class loader exception.
I have an app in main named App.java that runs of 28433 port.
I also have an app in src/test named MockServerApp.java.
I am trying to run mvn exec to execute them.
When I do mvn exec on my main app it works and starts. When I do it on my MockServerApp though it causes a class loader exception.
Here is my POM
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>default-cli</id>
<configuration>
<mainClass>com.nulogix.billing.App</mainClass>
</configuration>
</execution>
<execution>
<id>second-cli</id>
<configuration>
<mainClass>com.nulogix.billing.mockserver.MockServerApp</mainClass>
</configuration>
</execution>
</executions>
</plugin>
Here is the error I am getting :
java.lang.ClassNotFoundException: com.nulogix.billing.mockserver.MockServerApp
at java.net.URLClassLoader.findClass (URLClassLoader.java:436)
at java.lang.ClassLoader.loadClass (ClassLoader.java:588)
at java.lang.ClassLoader.loadClass (ClassLoader.java:521)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:270)
at java.lang.Thread.run (Thread.java:835)
How do I fix this?
EDIT: I have tried using the spring-boot-maven-plugin since I will be using the MockAppServer for integration tests and have tried launching both in the pre-integration life cycle.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.nulogix.billing.App</mainClass>
</configuration>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>pre-integration-test2</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
<mainClass>com.nulogix.billing.mockserver.MockServerApp</mainClass>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<mainClass>com.nulogix.billing.App</mainClass>
</configuration>
</execution>
<execution>
<id>post-integration-test2</id>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<mainClass>com.nulogix.billing.mockserver.MockServerApp</mainClass>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
Still gives me the classNotFoundException.
You can mark MockServerApp with following annotations. And instead of trying to run MockServerApp from maven, you can simply run junits from maven and let Spring manage creation of test context.
#RunWith(SpringRunner.class)
#SpringBootTest
You can see further documentation at spring boot testing

Java - Maven JAXB-2 plugin multiple schemes with different configurations doesn't generate classes

We are moving our existing project from Ant + Eclipse to Maven + IntelliJ IDEA.
I am currently using JAXB to generate classes from xsd files. I want to continue the current project structure so i want jaxb2-maven-plugin to generate the classes in a specific location. I have multiple schemes and want to generate the classes in different locations. I'm using multiple plugin execution bindings in order to do that as instructed in the JAXB-2 Maven plugin site.
My problem is that only the first execution is performed. None of the classes in the second execution are generated.
Here is my POM.xml file relevant part:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>schema1</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<schemaDirectory>${basedir}/src/main/resources/schemes</schemaDirectory>
<schemaFiles>myschema1.xsd</schemaFiles>
<packageName>xml</packageName>
<outputDirectory>${basedir}/src/main/java/com/example/dor/a</outputDirectory>
<arguments>-extension -Xcloneable -Xdefault-value -Xsetters -Xannotate</arguments>
<staleFile>${build.directory}/.jaxb-staleFlag-1</staleFile>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
<execution>
<id>schema2</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<schemaDirectory>${basedir}/src/main/resources/schemes</schemaDirectory>
<schemaFiles>myschema2.xsd</schemaFiles>
<packageName>xml</packageName>
<outputDirectory>${basedir}/src/main/java/com/example/dor/b</outputDirectory>
<arguments>-extension -Xcloneable -Xdefault-value -Xsetters -Xannotate</arguments>
<staleFile>${build.directory}/.jaxb-staleFlag-1</staleFile>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
</executions>
</plugin>
An update answer using version 2.5.0 of the plugin. This would be the configuration:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<id>schema1</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>com.your.package</packageName>
<sources>
<source>${project.basedir}/src/main/resources/xsd/sample1/sample1.xsd</source>
</sources>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
<execution>
<id>schema2</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>com.your.package</packageName>
<sources>
<source>${project.basedir}/src/main/resources/xsd/sample2/sample2.xsd</source>
</sources>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
</executions>
</plugin>
Hope it helps for newer configurations.
I would upgrade to 1.6, and you will have to put the 2 schemas in different packages to stop a conflict in the generated ObjectFactory. Below works for me:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>schema1</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<schemaDirectory>${basedir}/src/main/resources/schemes</schemaDirectory>
<schemaFiles>myschema1.xsd</schemaFiles>
<packageName>xml.a</packageName>
<outputDirectory>${basedir}/src/main/generated1</outputDirectory>
<clearOutputDir>true</clearOutputDir>
</configuration>
</execution>
<execution>
<id>schema2</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<schemaDirectory>${basedir}/src/main/resources/schemes</schemaDirectory>
<schemaFiles>myschema2.xsd</schemaFiles>
<packageName>xml.b</packageName>
<outputDirectory>${basedir}/src/main/generated2</outputDirectory>
<clearOutputDir>true</clearOutputDir>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Had the same problem.
Enabled the -X option of maven to see why the second generation is not performed.
The second generation was not run because the staleFile was the same. I had to add to both executions the parameter staleFile having different values:
<staleFile>${project.build.directory}/jaxb2/.xjcStaleFlag1</staleFile>
........
<staleFile>${project.build.directory}/jaxb2/.xjcStaleFlag2</staleFile>

getting authentication errors when running a local version of dynamodb via maven

I'm using Maven to start a local copy of DynamoDb for testing.
For those that are interested, I've copied the instructions here
https://thecarlhall.wordpress.com/2015/11/14/integration-testing-with-dynamodb-locally/ (and I've added them to the end of this question).
The problem is, when I try and create a client that accesses the local version of Dynamo, I get an error:
AmazonServiceException: The request signature we calculated does not match the signature you provided
Everything that I've read says that the secret is not checked when using a local dynamoDb, so my suspicion is that, for whatever reason, i'm accessing my real dynamodb. Can anyone see what i'm doing wrong?
AWSCredentials credentials = new BasicAWSCredentials(myAccessKey, "localTest");
AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials);
client.setEndpoint(dynamo.endpoint);
client.setRegion(Region.getRegion(Regions.EU_WEST_1));
Btw, the same is true for the setRegion. I should be able to set it to "local" but it's failing unless I set a real region.
I'm running my tests in IntelliJ, the maven setup is as follows:
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>install-dynamodb_local</id>
<phase>pre-integration-test</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>http://dynamodb-local.s3-website-${aws.s3.region}.amazonaws.com/dynamodb_local_latest.zip</url>
<unpack>true</unpack>
<outputDirectory>${project.build.directory}/dynamodb</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<goals>
<goal>reserve-network-port</goal>
</goals>
<phase>initialize</phase>
<configuration>
<portNames>
<portName>dynamodblocal.port</portName>
</portNames>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.bazaarvoice.maven.plugins</groupId>
<artifactId>process-exec-maven-plugin</artifactId>
<version>0.7</version>
<executions>
<execution>
<id>dynamodb_local</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<name>dynamodb_local</name>
<waitAfterLaunch>1</waitAfterLaunch>
<arguments>
<argument>java</argument>
<argument>-Djava.library.path=dynamodb/DynamoDBLocal_lib</argument>
<argument>-jar</argument>
<argument>dynamodb/DynamoDBLocal.jar</argument>
<argument>-port</argument>
<argument>${dynamodblocal.port}</argument>
<argument>-sharedDb</argument>
<argument>-inMemory</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins </groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<systemPropertyVariables>
<dynamo.endpoint>http://localhost:${dynamodblocal.port}</dynamo.endpoint>
</systemPropertyVariables>
</configuration>
</plugin>
I finally tracked this down.
My code has the following:
client.setEndpoint(dynamo.endpoint);
client.setRegion(Region.getRegion(Regions.EU_WEST_1));
An undocumented effect of setRegion is that it resets the endPoint back to amazon. By swapping these two statements around (as below), I resolved the problem.
client.setRegion(Region.getRegion(Regions.EU_WEST_1));
client.setEndpoint(dynamo.endpoint);
I hope this helps someone.

Clover does not create database

I'm trying to run Clover to see test coverage, but it will not create any database (and thus not give any report).
Among others, I've tried to run
mvn clean clover2:setup clover2:instrument clover2:clover clover2:check
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-clover2-plugin</artifactId>
<version>4.0.4</version>
<configuration>
<!-- <cloverDatabase>C:\clover\clover.db</cloverDatabase> -->
<!-- <reportsDirectory>${project.build.directory}/testreports</reportsDirectory> -->
<targetPercentage>10%</targetPercentage>
<includes>
<include>**Test.java</include>
<include>**IT.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>instrument</goal>
<goal>check</goal>
<goal>setup</goal>
</goals>
</execution>
</executions>
</plugin>
I've run with and without the <cloverDatabase> and <reportsDirectory> properties. No difference.
No database is ever created anywhere. Why?
I managed to get it working again by removing Clover, and then adding Clover back with only simple settings
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-clover2-plugin</artifactId>
<version>4.0.4</version>
<configuration>
<targetPercentage>70%</targetPercentage>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>instrument</goal>
</goals>
</execution>
</executions>
</plugin>
If I try to add inclusion specifications, then suddenly it won't create any db anymore....

maven: Cannot get pre-integration-phase to work

I'm trying to get pre and post integration phase to work with maven, to no avail.
My goal is to set up and tear down integration tests by running some binaries that are necessary. I'm testing with antrun-plugin and exec-plugin, but none of them prints the messages.
I'm running mvn verify. If I bind the plugins to clean phase and run mvn clean, the echo message and the ls are shown.
What's wrong? I'm using maven3
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12.3</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>ls</executable>
<arguments>
<argument>-la</argument>
</arguments>
</configuration>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>xxx</id>
<configuration>
<target>
<echo>Cleaning deployed website</echo>
</target>
</configuration>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I helped someone else get the failsafe plugin configured correctly earlier. Try explicitly specifying the phases in the failsafe plugin executions. Not sure why that is needed, as the failsafe plugin docs say the goals are supposed to be bound to correct phases by default - but it seems to be.

Categories

Resources