Skip to content

Commit

Permalink
add serialization test for virtualized report
Browse files Browse the repository at this point in the history
  • Loading branch information
dadza committed May 10, 2021
1 parent 7934c2e commit 0071233
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JasperReports is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
*/
package net.sf.jasperreports;

import net.sf.jasperreports.engine.JRVirtualizer;

/**
* @author Lucian Chirita (lucianc@users.sourceforge.net)
*/
public class NoVirtualizerContainer implements VirtualizerContainer
{

@Override
public JRVirtualizer getVirtualizer()
{
return null;
}

@Override
public void setReadOnly()
{
//NOOP
}

@Override
public void dispose()
{
//NOOP
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JasperReports is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
*/
package net.sf.jasperreports;

import net.sf.jasperreports.engine.JRVirtualizer;
import net.sf.jasperreports.engine.fill.JRAbstractLRUVirtualizer;

/**
* @author Lucian Chirita (lucianc@users.sourceforge.net)
*/
public class OwnVirtualizerContainer implements VirtualizerContainer
{

private final JRAbstractLRUVirtualizer virtualizer;

public OwnVirtualizerContainer(JRAbstractLRUVirtualizer virtualizer)
{
this.virtualizer = virtualizer;
}

@Override
public JRVirtualizer getVirtualizer()
{
return virtualizer;
}

@Override
public void setReadOnly()
{
virtualizer.setReadOnly(true);
}

@Override
public void dispose()
{
virtualizer.cleanup();
}

}
97 changes: 97 additions & 0 deletions jasperreports/tests/net/sf/jasperreports/PrintSerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JasperReports is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
*/
package net.sf.jasperreports;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.function.BiConsumer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRVirtualizer;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.engine.util.JRSaver;

/**
* @author Lucian Chirita (lucianc@users.sourceforge.net)
*/
public class PrintSerializer implements BiConsumer<Report, JasperPrint>
{

private static final Log log = LogFactory.getLog(PrintSerializer.class);

private static final PrintSerializer INSTANCE_NO_VIRTUALIZER = new PrintSerializer(new NoVirtualizerContainer());

public static PrintSerializer instance()
{
return INSTANCE_NO_VIRTUALIZER;
}

private VirtualizerContainer virtualizerContainer;

public PrintSerializer(VirtualizerContainer virtualizerContainer)
{
this.virtualizerContainer = virtualizerContainer;
}

@Override
public void accept(Report report, JasperPrint print)
{
try
{
if (log.isDebugEnabled())
{
log.debug("Serializing report " + report.getJRXML());
}

ByteArrayOutputStream printOut = new ByteArrayOutputStream();
JRSaver.saveObject(print, printOut);

try
{
JRVirtualizer virtualizer = virtualizerContainer.getVirtualizer();
if (log.isDebugEnabled())
{
log.debug("Loading serialized report " + report.getJRXML() + " with virtualizer " + virtualizer);
}

JasperPrint savedPrint = JRLoader.loadJasperPrint(new ByteArrayInputStream(printOut.toByteArray()), virtualizer);
virtualizerContainer.setReadOnly();
report.checkDigest(savedPrint);
}
finally
{
virtualizerContainer.dispose();
}
}
catch (JRException e)
{
throw new RuntimeException(e);
}
}

}
33 changes: 28 additions & 5 deletions jasperreports/tests/net/sf/jasperreports/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.function.BiConsumer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -71,6 +72,7 @@ public class Report

private String jrxml;
private String jrpxml;
private BiConsumer<Report, JasperPrint> printConsumer = Report::checkDigest;

public Report(String basename)
{
Expand Down Expand Up @@ -108,6 +110,16 @@ public void init()
}
}

public String getJRXML()
{
return jrxml;
}

public void addPrintConsumer(BiConsumer<Report, JasperPrint> printConsumer)
{
this.printConsumer = this.printConsumer.andThen(printConsumer);
}

public JasperReport compileReport() throws JRException, IOException
{
InputStream jrxmlInput = JRLoader.getResourceInputStream(jrxml);
Expand Down Expand Up @@ -146,7 +158,7 @@ public void runReport(Map<String, Object> params)
JasperPrint print = fillManager.fill(report, reportParams);
reportComplete(reportParams, print);
}
catch (JRException | NoSuchAlgorithmException | IOException e)
catch (JRException e)
{
throw new RuntimeException(e);
}
Expand All @@ -164,7 +176,6 @@ protected Map<String, Object> reportParams(Map<String, Object> params)
}

protected void reportComplete(Map<String, Object> params, JasperPrint print)
throws NoSuchAlgorithmException, FileNotFoundException, JRException, IOException
{
JRVirtualizer virtualizer = (JRVirtualizer) params.get(JRParameter.REPORT_VIRTUALIZER);
if (virtualizer instanceof JRAbstractLRUVirtualizer)
Expand All @@ -174,16 +185,28 @@ protected void reportComplete(Map<String, Object> params, JasperPrint print)

assert !print.getPages().isEmpty();

String digestString = xmlDigest(print);
log.debug("Report " + jrxml + " got " + digestString);
assert digestString.equals(referenceJRPXMLDigest);
printConsumer.accept(this, print);

if (virtualizer != null)
{
virtualizer.cleanup();
}
}

public void checkDigest(JasperPrint print)
{
try
{
String digestString = xmlDigest(print);
log.debug("Report " + jrxml + " got " + digestString);
assert digestString.equals(referenceJRPXMLDigest);
}
catch (NoSuchAlgorithmException | JRException | IOException e)
{
throw new RuntimeException(e);
}
}

protected String xmlDigest(JasperPrint print)
throws NoSuchAlgorithmException, FileNotFoundException, JRException, IOException
{
Expand Down
40 changes: 40 additions & 0 deletions jasperreports/tests/net/sf/jasperreports/VirtualizerContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JasperReports is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
*/
package net.sf.jasperreports;

import net.sf.jasperreports.engine.JRVirtualizer;

/**
* @author Lucian Chirita (lucianc@users.sourceforge.net)
*/
public interface VirtualizerContainer
{

JRVirtualizer getVirtualizer();

void setReadOnly();

void dispose();

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
*/
package net.sf.jasperreports.async;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import net.sf.jasperreports.Report;
Expand Down Expand Up @@ -63,7 +61,7 @@ public void runReport(Map<String, Object> params, FillListener fillListener)
JasperPrint print = accessor.getFinalJasperPrint();
reportComplete(reportParams, print);
}
catch (JRException | NoSuchAlgorithmException | IOException e)
catch (JRException e)
{
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import net.sf.jasperreports.OwnVirtualizerContainer;
import net.sf.jasperreports.PrintSerializer;
import net.sf.jasperreports.Report;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRParameter;
Expand All @@ -48,6 +50,8 @@ public void initReport() throws JRException, IOException
{
report = new Report("net/sf/jasperreports/virtualization/repo/FirstJasper.jrxml",
"net/sf/jasperreports/virtualization/FirstJasper.reference.jrpxml");
report.addPrintConsumer(PrintSerializer.instance());
report.addPrintConsumer(new PrintSerializer(new OwnVirtualizerContainer(new JRGzipVirtualizer(5))));
report.init();
}

Expand Down

0 comments on commit 0071233

Please sign in to comment.