[ https://issues.apache.org/jira/browse/WW-4923?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16380011#comment-16380011 ]
ASF GitHub Bot commented on WW-4923: ------------------------------------ lukaszlenart closed pull request #212: WW-4923 Null check to avoid NPE when not passing a JDBC connection URL: https://github.com/apache/struts/pull/212 This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/plugins/jasperreports/pom.xml b/plugins/jasperreports/pom.xml index e42ffa707..00b651db1 100644 --- a/plugins/jasperreports/pom.xml +++ b/plugins/jasperreports/pom.xml @@ -49,6 +49,32 @@ </exclusion> </exclusions> </dependency> + <dependency> + <groupId>org.apache.struts</groupId> + <artifactId>struts2-junit-plugin</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>jsp-api</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>javax.servlet-api</artifactId> + <version>3.1.0</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-web</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.easymock</groupId> + <artifactId>easymock</artifactId> + <scope>test</scope> + </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> diff --git a/plugins/jasperreports/src/main/java/org/apache/struts2/views/jasperreports/JasperReportsResult.java b/plugins/jasperreports/src/main/java/org/apache/struts2/views/jasperreports/JasperReportsResult.java index 242469024..c79e1b6c7 100644 --- a/plugins/jasperreports/src/main/java/org/apache/struts2/views/jasperreports/JasperReportsResult.java +++ b/plugins/jasperreports/src/main/java/org/apache/struts2/views/jasperreports/JasperReportsResult.java @@ -384,7 +384,10 @@ protected void doExecute(String finalLocation, ActionInvocation invocation) thro throw new ServletException(e.getMessage(), e); } finally { try { - conn.close(); + if (conn != null) { + // avoid NPE if connection was not used for the report + conn.close(); + } } catch (Exception e) { LOG.warn("Could not close db connection properly", e); } diff --git a/plugins/jasperreports/src/test/java/org/apache/struts2/views/jasperreports/JasperReportsResultTest.java b/plugins/jasperreports/src/test/java/org/apache/struts2/views/jasperreports/JasperReportsResultTest.java new file mode 100644 index 000000000..1bf55acf8 --- /dev/null +++ b/plugins/jasperreports/src/test/java/org/apache/struts2/views/jasperreports/JasperReportsResultTest.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts2.views.jasperreports; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.mock.MockActionInvocation; +import com.opensymphony.xwork2.util.ClassLoaderUtil; +import com.opensymphony.xwork2.util.ValueStack; +import net.sf.jasperreports.engine.JasperCompileManager; +import org.apache.struts2.StrutsStatics; +import org.apache.struts2.StrutsTestCase; +import org.easymock.IAnswer; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.mock.web.MockServletContext; + +import java.net.URL; +import java.sql.Connection; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; + +public class JasperReportsResultTest extends StrutsTestCase { + private MockActionInvocation invocation; + private ValueStack stack; + + public void testConnClose() throws Exception { + JasperReportsResult result = new JasperReportsResult(); + URL url = ClassLoaderUtil.getResource("org/apache/struts2/views/jasperreports/empty.jrxml", this.getClass()); + JasperCompileManager.compileReportToFile(url.getFile(), url.getFile() + ".jasper"); + result.setLocation("org/apache/struts2/views/jasperreports/empty.jrxml.jasper"); + result.setFormat(JasperReportConstants.FORMAT_XML); + + Connection connection = createMock(Connection.class); + final Boolean[] closed = {false}; + connection.close(); + expectLastCall().andAnswer(new IAnswer() { + @Override + public Object answer() throws Throwable { + closed[0] = true; + return null; + } + }); + replay(connection); + + stack.push(connection); + result.setConnection("top"); + + assertFalse(closed[0]); + result.execute(this.invocation); + verify(connection); + assertTrue(closed[0]); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + MockHttpServletResponse response = new MockHttpServletResponse(); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setRequestURI("http://sumeruri"); + ActionContext context = ActionContext.getContext(); + context.put(StrutsStatics.HTTP_RESPONSE, response); + context.put(StrutsStatics.HTTP_REQUEST, request); + this.stack = context.getValueStack(); + MockServletContext servletContext = new MockServletContext(); + context.put(StrutsStatics.SERVLET_CONTEXT, servletContext); + this.invocation = new MockActionInvocation(); + this.invocation.setInvocationContext(context); + this.invocation.setStack(this.stack); + } +} diff --git a/plugins/jasperreports/src/test/resources/org/apache/struts2/views/jasperreports/empty.jrxml b/plugins/jasperreports/src/test/resources/org/apache/struts2/views/jasperreports/empty.jrxml new file mode 100644 index 000000000..816d8609f --- /dev/null +++ b/plugins/jasperreports/src/test/resources/org/apache/struts2/views/jasperreports/empty.jrxml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +--> +<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="test" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="e65f69ca-7c62-4b4b-abc6-2e1a8710f23e"> +</jasperReport> \ No newline at end of file ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org > JasperReportResult: NPE When Not Using SQL Connection > ----------------------------------------------------- > > Key: WW-4923 > URL: https://issues.apache.org/jira/browse/WW-4923 > Project: Struts 2 > Issue Type: Bug > Components: Plugin - JasperReports > Affects Versions: 2.5.14.1 > Reporter: Paul Zepernick > Priority: Minor > Fix For: 2.5.16 > > > The JasperReportResult throws a NPE in the finally when closing the SQL > connection when using a Data Source from the value stack instead of supplying > a SQL connection to the report. -- This message was sent by Atlassian JIRA (v7.6.3#76005)