Skip to content

Commit

Permalink
Added test to verify that the application can use Jfr4Jdbc
Browse files Browse the repository at this point in the history
  • Loading branch information
chiroito committed May 1, 2023
1 parent d0550c2 commit b2ab47c
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 4 deletions.
15 changes: 13 additions & 2 deletions .github/workflows/release-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,25 @@ jobs:
- name: Test
run: mvn -B package --file pom.xml

- name: install Snapshot for Artifact Test
run: |
export VERSION=`echo ${{ github.event.release.tag_name }} | awk '{print substr($0, 2)}'`
echo "VERSION=${VERSION}" >> $GITHUB_ENV
sed -e s/999-SNAPSHOT/${VERSION}/g jfr4jdbc-driver/pom.xml > jfr4jdbc-driver/versionedPom.xml
mvn -B package --no-transfer-progress -DskipTests --file jfr4jdbc-driver/versionedPom.xml
mvn -B --no-transfer-progress install:install-file -Dfile=target/jfr4jdbc-driver-${VERSION}.jar -DgroupId=dev.jfr4jdbc -DartifactId=jfr4jdbc-driver -Dversion=${VERSION} -Dpackaging=jar --file jfr4jdbc-driver/versionedPom.xml
- name: Artifact Test
run: |
sed -e s/999-SNAPSHOT/${VERSION}/g artifact-test/pom.xml > artifact-test/versionedPom.xml
mvn -B test --file artifact-test/versionedPom.xml
- name: Deploy Production
env:
OSSRH_USERNAME: ${{ secrets.MAVEN_USERNAME }}
OSSRH_PASSWORD: ${{ secrets.MAVEN_USER_PASSWORD }}
SIGN_KEY_PASS: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
run: |
export VERSION=`echo ${{ github.event.release.tag_name }} | awk '{print substr($0, 2)}'`
sed -e s/999-SNAPSHOT/${VERSION}/g jfr4jdbc-driver/pom.xml > jfr4jdbc-driver/versionedPom.xml
mvn -B deploy --no-transfer-progress -DskipTests --file jfr4jdbc-driver/versionedPom.xml
mkdir release && cp jfr4jdbc-driver/target/*.jar release
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/release-snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ jobs:
- name: Test
run: mvn -B package --file pom.xml

- name: install Snapshot for Artifact Test
run: |
mvn -B package --no-transfer-progress -DskipTests --file jfr4jdbc-driver/pom.xml
mvn -B --no-transfer-progress install:install-file -Dfile=target/jfr4jdbc-driver-999-SNAPSHOT.jar -DgroupId=dev.jfr4jdbc -DartifactId=jfr4jdbc-driver -Dversion=999-SNAPSHOT -Dpackaging=jar --file jfr4jdbc-driver/pom.xml
- name: Artifact Test
run: mvn -B test --file artifact-test/pom.xml

- name: Deploy Snapshot
env:
OSSRH_USERNAME: ${{ secrets.MAVEN_USERNAME }}
Expand Down
49 changes: 49 additions & 0 deletions artifact-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>dev.jfr4jdbc</groupId>
<artifactId>artifact-test</artifactId>
<version>999-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.install.skip>true</maven.install.skip>
</properties>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>

<dependencies>
<dependency>
<groupId>dev.jfr4jdbc</groupId>
<artifactId>jfr4jdbc-driver</artifactId>
<version>999-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package dev.jfr4jdbc.artifact;

import org.junit.jupiter.api.*;

import static org.junit.jupiter.api.Assertions.*;

import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

public class MultiReleaseJarTest {

private JarFile open() throws IOException {
File artifactDir = new File("../jfr4jdbc-driver/target");
File[] files = artifactDir.listFiles((dir, name) -> name.matches("jfr4jdbc-driver-.+\\.jar") && !(name.endsWith("-javadoc.jar") || name.endsWith("-sources.jar")));
assertEquals(1, files.length);
return new JarFile(files[0]);
}

@DisplayName("Multi Release Jar")
@Test
void isMultiRelease() throws Exception {

JarFile jarFile = open();
assertTrue(jarFile.isMultiRelease());
}

@DisplayName("Service Loader File exists")
@Test
void hasServiceLoaderFile() throws IOException {
JarFile jarFile = open();
assertNotNull(jarFile.getEntry("META-INF/services/java.sql.Driver"));
}

@DisplayName("Java 11 Directory exists")
@Test
void hasJava11Directory() throws IOException {
JarFile jarFile = open();
assertNotNull(jarFile.getEntry("META-INF/versions/11"));
}

private final int JAVA_8_VERSION = 52;
private final int JAVA_11_VERSION = 55;

@DisplayName("Classes for JDBC 4.2 exists")
@Test
void hasJdbc42() throws IOException {
JarFile jarFile = open();

assertEquals(JAVA_8_VERSION, readMajorVersion(jarFile, "dev/jfr4jdbc/Jfr4JdbcDataSource.class"));
assertEquals(JAVA_8_VERSION, readMajorVersion(jarFile, "dev/jfr4jdbc/Jfr4JdbcDataSource42.class"));
}

@DisplayName("Classes for JDBC 4.3 exists")
@Test
void hasJdbc43() throws IOException {
JarFile jarFile = open();

assertEquals(JAVA_11_VERSION, readMajorVersion(jarFile, "META-INF/versions/11/dev/jfr4jdbc/Jfr4JdbcDataSource.class"));
assertEquals(JAVA_11_VERSION, readMajorVersion(jarFile, "META-INF/versions/11/dev/jfr4jdbc/Jfr4JdbcDataSource43.class"));
}


private int readMajorVersion(JarFile jarFile, String path) throws IOException {

ZipEntry classFileEntry = jarFile.getEntry(path);
InputStream inputStream = jarFile.getInputStream(classFileEntry);

DataInputStream dataInputStream = new DataInputStream(inputStream);
String cafe = Integer.toHexString(dataInputStream.readUnsignedShort());
assertEquals("cafe", cafe);
String babe = Integer.toHexString(dataInputStream.readUnsignedShort());
assertEquals("babe", babe);

int minorVersion = dataInputStream.readUnsignedShort();
int majorVersion = dataInputStream.readUnsignedShort();

inputStream.close();

return majorVersion;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.jfr4jdbc.artifact;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;


import java.sql.Driver;
import java.util.ServiceLoader;

public class ServiceLoaderTest {
@DisplayName("ServiceLoader loads Jfr4Jdbc")
@Test
void loadedTest() throws Exception {
ServiceLoader<Driver> loader = ServiceLoader.load(Driver.class);
long jfrDriverCount = loader.stream().filter(d -> d.get().getClass().getCanonicalName().equals("dev.jfr4jdbc.Jfr4JdbcDriver")).count();

assertEquals(1, jfrDriverCount);
}
}
3 changes: 1 addition & 2 deletions jfr4jdbc-driver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.jfr4jdbc</groupId>
<artifactId>jfr4jdbc-driver</artifactId>
<version>${jfr4jdbc.version}</version>
<version>999-SNAPSHOT</version>
<name>jfr4jdbc</name>
<description>A JDBC wrapper driver recording JDK Flight Recorder events</description>
<url>https://github.com/chiroito/Jfr4Jdbc</url>
Expand All @@ -15,7 +15,6 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jfr4jdbc.version>999-SNAPSHOT</jfr4jdbc.version>
</properties>

<licenses>
Expand Down

0 comments on commit b2ab47c

Please sign in to comment.