Skip to content

Commit

Permalink
Merge pull request playframework#1259 from xael-fry/1246_1248_additions
Browse files Browse the repository at this point in the history
[#1849] restore checks in TransactionalJPATest that were accidentally deleted in PR 1246
  • Loading branch information
xael-fry committed Aug 23, 2018
2 parents 8ef5453 + 1a83e19 commit c6630cf
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 97 deletions.
132 changes: 71 additions & 61 deletions framework/src/play/test/FunctionalTest.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
package play.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.nio.channels.Channels;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.regex.Pattern;

import org.apache.commons.lang.ArrayUtils;
import org.junit.Before;

import com.ning.http.client.FluentCaseInsensitiveStringsMap;
import com.ning.http.client.multipart.FilePart;
import com.ning.http.client.multipart.MultipartBody;
import com.ning.http.client.multipart.MultipartUtils;
import com.ning.http.client.multipart.Part;
import com.ning.http.client.multipart.StringPart;

import org.apache.commons.lang.ArrayUtils;
import org.junit.Before;
import play.Invoker;
import play.Invoker.InvocationContext;
import play.classloading.enhancers.ControllersEnhancer.ControllerInstrumentation;
Expand All @@ -39,10 +19,29 @@
import play.mvc.Http;
import play.mvc.Http.Request;
import play.mvc.Http.Response;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import play.mvc.Router.ActionDefinition;
import play.mvc.Scope.RenderArgs;
import play.mvc.results.RenderStatic;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.nio.channels.Channels;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.regex.Pattern;

/**
* Application tests support
Expand Down Expand Up @@ -82,7 +81,7 @@ public static Response GET(Object url, boolean followRedirect) {
Http.Header redirectedTo = response.headers.get("Location");
String location = redirectedTo.value();
if (location.contains("http")) {
java.net.URL redirectedUrl = null;
java.net.URL redirectedUrl;
try {
redirectedUrl = new java.net.URL(redirectedTo.value());
} catch (MalformedURLException e) {
Expand All @@ -106,7 +105,7 @@ public static Response GET(Object url, boolean followRedirect) {
* @return the response
*/
public static Response GET(Request request, Object url) {
String path = "";
String path;
String queryString = "";
String turl = url.toString();
if (turl.contains("?")) {
Expand Down Expand Up @@ -160,7 +159,7 @@ public static Response POST(Object url, String contenttype, InputStream body) {
* @return the response
*/
public static Response POST(Request request, Object url, String contenttype, InputStream body) {
String path = "";
String path;
String queryString = "";
String turl = url.toString();
if (turl.contains("?")) {
Expand Down Expand Up @@ -196,7 +195,7 @@ public static Response POST(Object url, Map<String, String> parameters, Map<Stri
}

public static Response POST(Object url, Map<String, String> parameters) {
return POST(newRequest(), url, parameters, new HashMap<String, File>());
return POST(newRequest(), url, parameters, new HashMap<>());
}

public static Response POST(Request request, Object url, Map<String, String> parameters, Map<String, File> files) {
Expand All @@ -215,11 +214,11 @@ public static Response POST(Request request, Object url, Map<String, String> par
}
}

MultipartBody requestEntity = null;
MultipartBody requestEntity;
/*
* ^1 MultipartBody::read is not working (if parts.isEmpty() == true) byte[] array = null;
**/
_ByteArrayOutputStream baos = null;
_ByteArrayOutputStream baos;
try {
requestEntity = MultipartUtils.newMultipartBody(parts, new FluentCaseInsensitiveStringsMap());
request.headers.putAll(ArrayUtils
Expand All @@ -237,7 +236,7 @@ public static Response POST(Request request, Object url, Map<String, String> par
}
// InputStream body = new ByteArrayInputStream(array != null ? array :
// new byte[0]); // ^1
InputStream body = new ByteArrayInputStream(baos != null ? baos.getByteArray() : new byte[0]);
InputStream body = new ByteArrayInputStream(baos.getByteArray());
return POST(request, url, MULTIPART_FORM_DATA, body);
}

Expand All @@ -259,7 +258,7 @@ public static Response PUT(Object url, String contenttype, String body) {
* @return the response
*/
public static Response PUT(Request request, Object url, String contenttype, String body) {
String path = "";
String path;
String queryString = "";
String turl = url.toString();
if (turl.contains("?")) {
Expand Down Expand Up @@ -293,7 +292,7 @@ public static Response DELETE(String url) {
* @return the response
*/
public static Response DELETE(Request request, Object url) {
String path = "";
String path;
String queryString = "";
String turl = url.toString();
if (turl.contains("?")) {
Expand All @@ -317,7 +316,7 @@ public static void makeRequest(final Request request, final Response response) {
final Future<?> invocationResult = TestEngine.functionalTestsExecutor.submit(new Invoker.Invocation() {

@Override
public void execute() throws Exception {
public void execute() {
renderArgs.clear();
ActionInvoker.invoke(request, response);

Expand Down Expand Up @@ -372,7 +371,14 @@ public InvocationContext getInvocationContext() {
invocationResult.get();
}
catch (ExecutionException e) {
throw unwrapOriginalException(e);
RuntimeException originalException = unwrapOriginalException(e);
if (originalException instanceof RenderStatic) {
response.status = 200;
response.direct = ((RenderStatic) originalException).file;
}
else {
throw originalException;
}
}
catch (Exception e) {
throw new RuntimeException(e);
Expand All @@ -399,15 +405,16 @@ public InvocationContext getInvocationContext() {
}
}

/**
* Check if the original exceptions fits the usual patterns.
* If yes, return the very original runtime exception.
*/
private static RuntimeException unwrapOriginalException(final ExecutionException e) {
// Check if the original exceptions fits the usual patterns. If yes, throw the very
// original runtime exception
final Throwable executionCause = e.getCause();
if (executionCause != null
&& (executionCause instanceof JavaExecutionException || executionCause instanceof UnexpectedException)) {
final Throwable originalCause = executionCause.getCause();
if (originalCause != null && originalCause instanceof RuntimeException) {
throw (RuntimeException) originalCause;
Throwable executionCause = e.getCause();
if (executionCause instanceof JavaExecutionException || executionCause instanceof UnexpectedException) {
Throwable originalCause = executionCause.getCause();
if (originalCause instanceof RuntimeException) {
return (RuntimeException) originalCause;
}
}
// As a last fallback, just wrap everything up
Expand Down Expand Up @@ -441,23 +448,20 @@ public static Response newResponse() {
// Register an onWriteChunk action so that Response.writeChunk() won't throw
// an unhandled exception if the controller action calls it.
response.onWriteChunk(
new Action<Object>() {
@Override
public void invoke(Object chunk) {
// Mimic the behavior of PlayHandler$LazyChunkedInput.writeChunk()
if (chunk != null) {
try {
byte[] bytes;
if (chunk instanceof byte[]) {
bytes = (byte[]) chunk;
} else {
bytes = chunk.toString().getBytes(response.encoding);
}
response.out.write(bytes);
} catch (Exception exception) {
// Something is wrong with the chunk.
throw new RuntimeException(exception);
chunk -> {
// Mimic the behavior of PlayHandler$LazyChunkedInput.writeChunk()
if (chunk != null) {
try {
byte[] bytes;
if (chunk instanceof byte[]) {
bytes = (byte[]) chunk;
} else {
bytes = chunk.toString().getBytes(response.encoding);
}
response.out.write(bytes);
} catch (Exception exception) {
// Something is wrong with the chunk.
throw new RuntimeException(exception);
}
}
});
Expand All @@ -466,8 +470,7 @@ public void invoke(Object chunk) {
}

public static Request newRequest() {
Request request = Request.createRequest(null, "GET", "/", "", null, null, null, null, false, 80, "localhost", false, null, null);
return request;
return Request.createRequest(null, "GET", "/", "", null, null, null, null, false, 80, "localhost", false, null, null);
}

// Assertions
Expand Down Expand Up @@ -503,6 +506,13 @@ public static void assertStatus(int status, Response response) {
assertEquals("Response status ", (Object) status, response.status);
}

public static void assertIsStaticFile(Response response, String filePath) {
assertIsOk(response);

String file = (String) response.direct;
assertEquals(filePath, file);
}

/**
* Exact equality assertion on response body
*
Expand Down
20 changes: 10 additions & 10 deletions samples-and-tests/just-test-cases/app/controllers/Binary.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package controllers;

import play.*;
import models.UserWithAvatar;
import play.data.Upload;
import play.mvc.*;
import play.mvc.Controller;
import play.mvc.Http;
import play.test.Fixtures;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.*;

import models.*;
import play.vfs.VirtualFile;
import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Binary extends Controller {

Expand Down Expand Up @@ -114,12 +114,12 @@ public ErrorInputStream() {

@Override
public int read() throws IOException {
throw new IOException();
throw new IOException("io ups");
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
throw new IOException();
throw new IOException("io failed");
}

@Override
Expand All @@ -128,7 +128,7 @@ public long skip(long n) throws IOException {
}

@Override
public void close() throws IOException {
public void close() {
errorInputStreamClosed = true;
}
}
Expand Down
28 changes: 16 additions & 12 deletions samples-and-tests/just-test-cases/test/BinaryTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import controllers.Binary;
import org.junit.Before;
import org.junit.Test;

import play.Logger;
import play.Play;
import play.data.MemoryUpload;
import play.data.Upload;
import play.mvc.Http;
import play.exceptions.UnexpectedException;
import play.mvc.Http.Response;
import play.mvc.results.Redirect;
import play.test.Fixtures;
import play.test.FunctionalTest;
import controllers.Binary;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class BinaryTest extends FunctionalTest {

Expand Down Expand Up @@ -235,10 +232,17 @@ public void testGetEmptyBinary() {
assertTrue(Binary.emptyInputStreamClosed);
}

@Test(expected = Exception.class)
@Test
public void testGetErrorBinary() {
try {
GET("/binary/getErrorBinary");
fail("expected wrapped IOException");
}
catch (RuntimeException expected) {
assertTrue(expected.getCause() instanceof ExecutionException);
assertTrue(expected.getCause().getCause() instanceof UnexpectedException);
assertTrue(expected.getCause().getCause().getCause() instanceof IOException);
assertEquals("io failed", expected.getCause().getCause().getCause().getMessage());
}
finally {
assertTrue(Binary.errorInputStreamClosed);
Expand Down
Loading

0 comments on commit c6630cf

Please sign in to comment.