Skip to content

Commit

Permalink
Refactor Image tests add Lombok (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored Oct 18, 2024
1 parent b2ed01e commit d1dd03c
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 70 deletions.
14 changes: 14 additions & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<name>Quarkus JasperReports - Integration Tests</name>
<properties>
<skipITs>true</skipITs>
<lombok.version>1.18.34</lombok.version>
</properties>
<dependencies>
<dependency>
Expand All @@ -36,6 +37,12 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
Expand Down Expand Up @@ -81,6 +88,13 @@
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<parameters>true</parameters>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.quarkiverse.jasperreports.it;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import jakarta.inject.Inject;

Expand Down Expand Up @@ -55,7 +54,7 @@ protected ByteArrayOutputStream exportXml(JasperPrint jasperPrint, boolean embed
}

protected ByteArrayOutputStream exportHtml(JasperPrint jasperPrint, ReportContext reportContext)
throws JRException, IOException {
throws JRException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
HtmlExporter exporter = new HtmlExporter(DefaultJasperReportsContext.getInstance());

Expand Down Expand Up @@ -135,4 +134,4 @@ protected ByteArrayOutputStream exportXlsx(JasperPrint jasperPrint) throws JRExc
return outputStream;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.stream.Stream;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;

import org.jboss.logging.Logger;

import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;
import lombok.Getter;
import lombok.extern.jbosslog.JBossLog;
import net.sf.jasperreports.engine.export.FileHtmlResourceHandler;

@ApplicationScoped
@Getter
@JBossLog
public class Application {

private static final Logger LOG = Logger.getLogger(Application.class);

private FileHtmlResourceHandler imageHandler;

private Path tempFolder;
Expand All @@ -27,28 +29,21 @@ void onStart(@Observes StartupEvent evt) throws IOException {
imageHandler = new FileHtmlResourceHandler(tempFolder.toFile(), "/images/{0}");
}

void onStop(@Observes ShutdownEvent evt) throws IOException {
if (null != tempFolder) {
Files.walk(tempFolder).forEach((p) -> {
try {
Files.delete(p);
} catch (IOException ex) {
LOG.warn(ex.getMessage());
}
});

if (Files.exists(tempFolder)) {
Files.delete(tempFolder);
void onStop(@Observes ShutdownEvent evt) {
if (tempFolder != null && Files.exists(tempFolder)) {
try (Stream<Path> paths = Files.walk(tempFolder)) {
paths.sorted(Comparator.reverseOrder()) // Delete files before directories
.forEach(p -> {
try {
Files.delete(p);
} catch (IOException ex) {
log.warn("Failed to delete: " + p, ex);
}
});
} catch (IOException e) {
log.error("Error occurred while deleting temp folder: " + tempFolder, e);
}
}
}

public FileHtmlResourceHandler getImageHandler() {
return imageHandler;
}

public Path getTempFolder() {
return tempFolder;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.core.Response;

import org.jboss.logging.Logger;
import lombok.extern.jbosslog.JBossLog;

@Path("images")
@RequestScoped
@JBossLog
public class ImageResource {

private static final Logger LOG = Logger.getLogger(ImageResource.class);

@Inject
Application app;

Expand All @@ -32,7 +31,7 @@ public Response get(@PathParam("fileName") final String fileName) throws IOExcep
if (Files.exists(path) && Files.isRegularFile(path) && Files.isReadable(path)) {
final String contentType = Files.probeContentType(path);

LOG.debugf("Loading file %s of type %s", path.toString(), contentType);
log.debugf("Loading file %s of type %s", path.toString(), contentType);

try (final InputStream is = Files.newInputStream(path)) {
return Response.ok(is.readAllBytes(), contentType).build();
Expand All @@ -41,4 +40,4 @@ public Response get(@PathParam("fileName") final String fileName) throws IOExcep

return Response.status(NOT_FOUND).build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static jakarta.ws.rs.core.MediaType.TEXT_HTML;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -46,16 +45,10 @@ public class JasperReportsImageResource extends AbstractJasperResource {
@APIResponse(responseCode = "200", description = "Fetch an HTML report with images", content = @Content(mediaType = TEXT_HTML))
@GET
@Path("html")
public Response getHtml() throws JRException, IOException {
final SimpleReportContext reportContext = new SimpleReportContext();
final Map<String, Object> params = new HashMap<>();
params.put(JRParameter.REPORT_LOCALE, Locale.US);
params.put(JRParameter.REPORT_CONTEXT, reportContext);
params.put("QUARKUS_URL", uriInfo.getBaseUri().toString());

public Response getHtml() throws JRException {
final long start = System.currentTimeMillis();
final JasperPrint jasperPrint = JasperFillManager.getInstance(repo.getContext()).fillFromRepo(TEST_REPORT_NAME, params,
new JREmptyDataSource());
final SimpleReportContext reportContext = new SimpleReportContext();
final JasperPrint jasperPrint = print(reportContext);

final Response.ResponseBuilder response = Response.ok();
ByteArrayOutputStream outputStream = exportHtml(jasperPrint, reportContext);
Expand All @@ -70,15 +63,8 @@ public Response getHtml() throws JRException, IOException {
@GET
@Path("pdf")
public Response getPdf() throws JRException {
final SimpleReportContext reportContext = new SimpleReportContext();
final Map<String, Object> params = new HashMap<>();
params.put(JRParameter.REPORT_LOCALE, Locale.US);
params.put(JRParameter.REPORT_CONTEXT, reportContext);
params.put("QUARKUS_URL", uriInfo.getBaseUri().toString());

final long start = System.currentTimeMillis();
final JasperPrint jasperPrint = JasperFillManager.getInstance(repo.getContext()).fillFromRepo(TEST_REPORT_NAME, params,
new JREmptyDataSource());
final JasperPrint jasperPrint = print(new SimpleReportContext());

final Response.ResponseBuilder response = Response.ok();
ByteArrayOutputStream outputStream = exportPdf(jasperPrint);
Expand All @@ -94,15 +80,8 @@ public Response getPdf() throws JRException {
@GET
@Path("xlsx")
public Response getXlsx() throws JRException {
final SimpleReportContext reportContext = new SimpleReportContext();
final Map<String, Object> params = new HashMap<>();
params.put(JRParameter.REPORT_LOCALE, Locale.US);
params.put(JRParameter.REPORT_CONTEXT, reportContext);
params.put("QUARKUS_URL", uriInfo.getBaseUri().toString());

final long start = System.currentTimeMillis();
final JasperPrint jasperPrint = JasperFillManager.getInstance(repo.getContext()).fillFromRepo(TEST_REPORT_NAME, params,
new JREmptyDataSource());
final JasperPrint jasperPrint = print(new SimpleReportContext());

final Response.ResponseBuilder response = Response.ok();
ByteArrayOutputStream outputStream = exportXlsx(jasperPrint);
Expand All @@ -114,4 +93,13 @@ public Response getXlsx() throws JRException {
return response.build();
}

}
private JasperPrint print(SimpleReportContext reportContext) throws JRException {
final Map<String, Object> params = new HashMap<>();
params.put(JRParameter.REPORT_LOCALE, Locale.US);
params.put(JRParameter.REPORT_CONTEXT, reportContext);
params.put("QUARKUS_URL", uriInfo.getBaseUri().toString());

return JasperFillManager.getInstance(repo.getContext()).fillFromRepo(TEST_REPORT_NAME, params,
new JREmptyDataSource());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
quarkus.application.name=JasperReports IT
quarkus.native.resources.includes=data/**
quarkus.native.resources.includes=data/**,images/**

# DATABASE
%prod.quarkus.datasource.jdbc.url=jdbc:h2:mem:default
Expand Down
10 changes: 0 additions & 10 deletions runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@
<artifactId>metadata-extractor</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.mozilla</groupId>
<artifactId>rhino</artifactId>
<version>1.7.15</version>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
Expand All @@ -117,11 +112,6 @@
<artifactId>Saxon-HE</artifactId>
<version>12.5</version>
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>3.26.2</version>
</dependency>
<dependency>
<groupId>net.tascalate.javaflow</groupId>
<artifactId>net.tascalate.javaflow.api</artifactId>
Expand Down

0 comments on commit d1dd03c

Please sign in to comment.