Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Oct 31, 2024
1 parent 5d53c97 commit 47c688b
Show file tree
Hide file tree
Showing 17 changed files with 335 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

<parent>
<groupId>io.quarkiverse.barcode</groupId>
<artifactId>quarkus-barcode-parent</artifactId>
<artifactId>quarkus-barcode4j-parent</artifactId>
<version>999-SNAPSHOT</version>
</parent>
<artifactId>quarkus-barcode-integration-tests</artifactId>
<name>Quarkus Barcode - Integration Tests</name>
<artifactId>quarkus-barcode4j-integration-tests</artifactId>
<name>Quarkus Barcode4J - Integration Tests</name>

<properties>
<skipITs>true</skipITs>
Expand All @@ -28,21 +28,6 @@
<artifactId>quarkus-barcode4j</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkiverse.barcode</groupId>
<artifactId>quarkus-okapi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkiverse.barcode</groupId>
<artifactId>quarkus-qrcodegen</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkiverse.barcode</groupId>
<artifactId>quarkus-zxing</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
Expand All @@ -31,32 +38,31 @@
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.output.BarcodeCanvasSetupException;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.output.svg.SVGCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;
import org.w3c.dom.DocumentFragment;

@Path("/barcode4j")
@ApplicationScoped
public class Barcode4JResource extends BaseImageResource {

private static final int DPI = 150;

@GET
@Path("code39")
@Path("code39/png")
@APIResponse(responseCode = "200", description = "Document downloaded", content = @Content(mediaType = MediaType.APPLICATION_OCTET_STREAM, schema = @Schema(type = SchemaType.STRING, format = "binary")))
public Response code39() throws IOException {
public Response code39Png() throws IOException {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
//Create the barcode bean
final Code39Bean bean = new Code39Bean();
final int dpi = 150;

//Configure the barcode generator
bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar, width exactly one pixel
bean.setWideFactor(3);
bean.doQuietZone(false);
final Code39Bean bean = createCode39Bean();

BitmapCanvasProvider canvas = new BitmapCanvasProvider(
outputStream, PNG_MIME_TYPE, dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
outputStream, PNG_MIME_TYPE, DPI, BufferedImage.TYPE_BYTE_BINARY, false, 0);

//Generate the barcode
bean.generateBarcode(canvas, "Hello World");
bean.generateBarcode(canvas, "Quarkus Rocks");

//Signal end of generation
canvas.finish();
Expand All @@ -65,4 +71,38 @@ public Response code39() throws IOException {
return buildImageResponse(outputStream.toByteArray(), PNG_MIME_TYPE, "barcode4j-code39.png");
}
}

@GET
@Path("code39/svg")
@APIResponse(responseCode = "200", description = "Document downloaded", content = @Content(mediaType = MediaType.APPLICATION_OCTET_STREAM, schema = @Schema(type = SchemaType.STRING, format = "binary")))
public Response code39Svg() throws IOException, BarcodeCanvasSetupException, TransformerException {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
//Create the barcode bean
final Code39Bean bean = createCode39Bean();

SVGCanvasProvider canvas = new SVGCanvasProvider(false, 0);

//Generate the barcode
bean.generateBarcode(canvas, "Quarkus Rocks");

DocumentFragment frag = canvas.getDOMFragment();
TransformerFactory factory = TransformerFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
Transformer trans = factory.newTransformer();
Source src = new javax.xml.transform.dom.DOMSource(frag);
Result res = new javax.xml.transform.stream.StreamResult(outputStream);
trans.transform(src, res);

// return the image
return buildImageResponse(outputStream.toByteArray(), SVG_MIME_TYPE, "barcode4j-code39.svg");
}
}

private Code39Bean createCode39Bean() {
final Code39Bean bean = new Code39Bean();
bean.setModuleWidth(UnitConv.in2mm(1.0f / DPI)); //makes the narrow bar, width exactly one pixel
bean.setWideFactor(3);
bean.doQuietZone(false);
return bean;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class BaseImageResource {
public static final String CONTENT_DISPOSITION = "attachment;filename=%s";
public static final String PNG_MIME_TYPE = "image/png";
public static final String SVG_MIME_TYPE = "image/svg+xml";

/**
* Builds a Response containing image data with appropriate headers.
Expand All @@ -21,4 +22,4 @@ protected Response buildImageResponse(byte[] imageData, String mimeType, String
response.header(HttpHeaders.CONTENT_TYPE, mimeType);
return response.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.quarkiverse.barcode.it;

import static io.restassured.RestAssured.given;
import static jakarta.ws.rs.core.HttpHeaders.CONTENT_LENGTH;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class Barcode4JResourceTest {

@Test
public void testBarcode4JCode39Png() {
given()
.when().get("/barcode4j/code39/png")
.then()
.statusCode(200)
.contentType(equalTo(BaseImageResource.PNG_MIME_TYPE))
.header(CONTENT_LENGTH, Integer::parseInt, greaterThan(0))
.body(notNullValue());
}

@Test
public void testBarcode4JCode39Svg() {
given()
.when().get("/barcode4j/code39/svg")
.then()
.statusCode(200)
.contentType(equalTo(BaseImageResource.SVG_MIME_TYPE))
.header(CONTENT_LENGTH, Integer::parseInt, greaterThan(0))
.body(is(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><svg height=\"17.8219mm\" viewBox=\"0 0 40.76 17.8219\" width=\"40.76mm\" xmlns=\"http://www.w3.org/2000/svg\"><g fill=\"black\" stroke=\"none\"><rect height=\"15\" width=\"0.1693\" x=\"0\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"0.6773\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"1.016\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"1.6933\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"2.3707\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"2.73\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"3.0687\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"3.4073\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"3.746\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"4.762\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"5.46\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"6.476\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"6.8147\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"7.1533\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"7.492\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"8.19\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"8.8673\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"9.206\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"9.8833\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"10.222\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"10.92\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"11.5973\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"11.936\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"12.2747\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"13.2907\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"13.65\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"14.3273\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"14.666\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"15.0047\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"15.682\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"16.38\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"17.396\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"17.7347\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"18.0733\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"18.412\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"19.11\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"19.4487\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"20.126\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"20.4647\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"21.4807\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"21.84\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"22.5173\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"23.1947\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"23.5333\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"24.2107\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"24.57\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"25.2473\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"25.586\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"25.9247\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"26.9407\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"27.3\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"27.9773\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"28.316\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"28.9933\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"29.6707\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"30.03\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"30.7073\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"31.3847\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"32.062\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"32.4007\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"32.76\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"33.4373\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"33.776\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"34.1147\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"34.792\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"35.49\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"35.8287\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"36.506\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"36.8447\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"37.8607\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"38.22\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"38.8973\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"39.236\" y=\"0\"/><rect height=\"15\" width=\"0.508\" x=\"39.9133\" y=\"0\"/><rect height=\"15\" width=\"0.1693\" x=\"40.5907\" y=\"0\"/><text font-family=\"Helvetica\" font-size=\"2.8219\" text-anchor=\"middle\" x=\"20.38\" y=\"17.6228\">Quarkus Rocks</text></g></svg>"));
}
}
14 changes: 14 additions & 0 deletions ext-barcode4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,18 @@
<module>deployment</module>
<module>runtime</module>
</modules>
<profiles>
<profile>
<id>it</id>
<activation>
<property>
<name>performRelease</name>
<value>!true</value>
</property>
</activation>
<modules>
<module>integration-tests</module>
</modules>
</profile>
</profiles>
</project>
103 changes: 103 additions & 0 deletions ext-okapi/integration-tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.quarkiverse.barcode</groupId>
<artifactId>quarkus-okapi-parent</artifactId>
<version>999-SNAPSHOT</version>
</parent>
<artifactId>quarkus-okapi-integration-tests</artifactId>
<name>Quarkus Okapi - Integration Tests</name>

<properties>
<skipITs>true</skipITs>
</properties>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.barcode</groupId>
<artifactId>quarkus-okapi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>native-image</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${native.surefire.skip}</skipTests>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<skipITs>false</skipITs>
<quarkus.native.enabled>true</quarkus.native.enabled>
</properties>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkiverse.barcode.it;

import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response;

public class BaseImageResource {
public static final String CONTENT_DISPOSITION = "attachment;filename=%s";
public static final String PNG_MIME_TYPE = "image/png";
public static final String SVG_MIME_TYPE = "image/svg+xml";

/**
* Builds a Response containing image data with appropriate headers.
*
* @param imageData The raw bytes of the image
* @param mimeType The MIME type of the image (e.g. "image/png")
* @param fileName The filename to use in the Content-Disposition header
* @return A Response object configured with the image data and headers
*/
protected Response buildImageResponse(byte[] imageData, String mimeType, String fileName) {
final Response.ResponseBuilder response = Response.ok(imageData);
response.header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION.formatted(fileName));
response.header(HttpHeaders.CONTENT_TYPE, mimeType);
return response.build();
}

protected String stripWhitespace(String svgContent) {
// Remove unnecessary whitespace characters (spaces, tabs, newlines) from SVG content
return svgContent
.replaceAll(">\\s+<", "><") // Remove whitespace between tags
.replaceAll("\\s{2,}", " ") // Replace multiple spaces with a single space
.replaceAll("\\s+/>", "/>"); // Trim spaces before self-closing tags
}
}
Loading

0 comments on commit 47c688b

Please sign in to comment.