Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3412 - Fix TCK test #3518

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,32 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.core.impl;
package cloud.piranha.core.api;

import cloud.piranha.core.api.WebApplication;
import cloud.piranha.core.api.WebApplicationRequest;
import cloud.piranha.core.api.WebApplicationResponse;
import java.io.PrintWriter;
import java.io.Writer;

/**
* The JUnit tests for the ServletOutputStream API.
*
* The WebApplicationPrintWriter API.
*
* @author Manfred Riem (mriem@manorrock.com)
*/
public class ServletOutputStreamTest extends cloud.piranha.core.tests.ServletOutputStreamTest {

@Override
public WebApplication createWebApplication() {
return new DefaultWebApplication();
}

@Override
public WebApplicationRequest createWebApplicationRequest() {
return new DefaultWebApplicationRequest();
}

@Override
public WebApplicationResponse createWebApplicationResponse() {
return new DefaultWebApplicationResponse();
public class WebApplicationPrintWriter extends PrintWriter {

/**
* Stores the response object.
*/
protected WebApplicationResponse response;

/**
* Constructor.
*
* @param response the response object.
* @param writer the writer.
* @param autoFlush the auto flush flag.
*/
public WebApplicationPrintWriter(WebApplicationResponse response, Writer writer, boolean autoFlush) {
super(writer, autoFlush);
this.response = response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public interface WebApplicationResponse extends HttpServletResponse {
* @return the content language.
*/
String getContentLanguage();

/**
* Get the content length.
*/
long getContentLength();

/**
* {@return the cookies}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.core.impl;

import cloud.piranha.core.api.WebApplicationPrintWriter;
import cloud.piranha.core.api.WebApplicationResponse;
import java.io.IOException;
import java.io.Writer;

/**
* The PrintWriter for a WebApplicationResponse.
*
* @author Manfred Riem (mriem@manorrock.com)
*/
public class DefaultWebApplicationPrintWriter extends WebApplicationPrintWriter {

/**
* Stores the content length.
*/
private long contentLength = -1;

/**
* Stores the content written.
*/
private long contentWritten = 0;

/**
* Constructor.
*
* @param response the response object.
* @param writer the writer.
* @param autoFlush the auto flush flag.
*/
public DefaultWebApplicationPrintWriter(WebApplicationResponse response, Writer writer, boolean autoFlush) {
super(response, writer, autoFlush);
contentLength = response.getContentLength() * 2;
}

@Override
public void write(char[] characters) {
if (characters != null) {
for (int i = 0; i < characters.length; i++) {
write(characters[i]);
}
}
}

@Override
public void write(String s) {
if (s != null) {
s.chars().forEach(c -> write(c));
}
}

@Override
public void write(int c) {
super.write(c);
if (contentLength > 0) {
contentWritten++;
if (contentWritten > contentLength) {
try {
response.flushBuffer();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ public String getCharacterEncoding() {
public String getContentLanguage() {
return contentLanguage;
}

@Override
public long getContentLength() {
return contentLength;
}

@Override
public String getContentType() {
Expand Down Expand Up @@ -409,7 +414,7 @@ public synchronized PrintWriter getWriter() throws IOException {
setCharacterEncoding(ISO_8859_1);
}
gotWriter = true;
writer = new PrintWriter(new OutputStreamWriter(webApplicationOutputStream, characterEncoding), false);
writer = new DefaultWebApplicationPrintWriter(this, new OutputStreamWriter(webApplicationOutputStream, characterEncoding), false);
}
result = writer;
} else {
Expand Down
1 change: 1 addition & 0 deletions core/impl/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@
opens cloud.piranha.core.impl;
requires transitive cloud.piranha.core.api;
requires transitive cloud.piranha.resource.impl;
requires java.base;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,61 +25,37 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.core.tests;
package cloud.piranha.core.impl;

import cloud.piranha.core.api.WebApplication;
import cloud.piranha.core.api.WebApplicationRequest;
import cloud.piranha.core.api.WebApplicationResponse;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.WriteListener;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* The JUnit tests for the ServletOutputStream API.
*
* The JUnit tests for the DefaultWebApplicationResponse class.
*
* @author Manfred Riem (mriem@manorrock.com)
*/
public abstract class ServletOutputStreamTest {

/**
* Create a web application.
*
* @return the web application.
*/
public abstract WebApplication createWebApplication();

/**
* Create a web application request.
*
* @return the web application request.
*/
public abstract WebApplicationRequest createWebApplicationRequest();

/**
* Create a web application response.
*
* @return the web application response.
*/
public abstract WebApplicationResponse createWebApplicationResponse();

class DefaultWebApplicationOutputStreamTest {

/**
* Test isReady method.
*
*
* @throws Exception when a serious error occurs.
*/
void testIsReady() throws Exception {
WebApplicationResponse response = createWebApplicationResponse();
DefaultWebApplicationResponse response = new DefaultWebApplicationResponse();
ServletOutputStream outputStream = response.getOutputStream();
assertFalse(outputStream.isReady());
}

/**
* Test setWriteListener method.
*/
void testWriteListener() throws Exception {
WebApplicationResponse response = createWebApplicationResponse();
DefaultWebApplicationResponse response = new DefaultWebApplicationResponse();
ServletOutputStream outputStream = response.getOutputStream();
outputStream.setWriteListener(new WriteListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,25 @@ void testFlushBuffer() {
fail();
}
}

/**
* Test flushBuffer method.
*
* @throws Exception when a serious error occurs.
*/
@Test
void testFlushBuffer2() throws Exception {
DefaultWebApplicationResponse response = new DefaultWebApplicationResponse();
response.setContentLength(20);
PrintWriter writer = response.getWriter();
writer.print("0123456789");
writer.print("0123456789");
writer.print("0123456789");
writer.print("0123456789");
writer.println("And we flushed!");
response.addIntHeader("header1", 12345);
assertNull(response.getHeader("header1"));
}

/**
* Test getBufferSize method.
Expand Down
Loading