Skip to content

Commit

Permalink
[RESTEASY-1670] Datasource provider cleans files test
Browse files Browse the repository at this point in the history
  • Loading branch information
Katka Novotna authored and asoldano committed Aug 8, 2017
1 parent 2a77468 commit 062c106
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 43 deletions.
Expand Up @@ -24,6 +24,12 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FilePermission;
import java.io.FilenameFilter;
Expand All @@ -41,28 +47,29 @@ public class CleanFilesDataSourceProviderTest {

protected static final Logger logger = Logger.getLogger(CleanFilesDataSourceProviderTest.class.getName());
static ResteasyClient client;
static String serverTmpDir;

@Deployment()
public static Archive<?> deploy() {
WebArchive war = TestUtil.prepareArchive(CleanFilesDataSourceProviderTest.class.getSimpleName());
// DataSource provider creates tmp file in the filesystem
war.addAsManifestResource(PermissionUtil.createPermissionsXmlAsset(new FilePermission("/tmp/-", "read"),
new PropertyPermission("java.io.tmpdir", "read"),
new FilePermission("/tmp", "read")), "permissions.xml");
return TestUtil.finishContainerPrepare(war, null, CleanFilesDataSourceProviderResource.class);
}

@Before
public void init() {
client = new ResteasyClientBuilder().build();
serverTmpDir = getTmpDirectory();
}

@After
public void after() throws Exception {
client.close();
}

private String generateURL(String path) {
private static String generateURL(String path) {
return PortProviderUtil.generateURL(path, CleanFilesDataSourceProviderTest.class.getSimpleName());
}

Expand All @@ -75,19 +82,9 @@ private String generateURL(String path) {
*/
@Test
public void testDataSourceProviderInputStreamOnce() throws Exception {
// get directory for temporary files
File tmpdir = new File(System.getProperty("java.io.tmpdir"));
Assert.assertTrue("java.io.tmpdir does not exists", tmpdir.isDirectory());
logger.info("Tmp directory = " + tmpdir);

// Get count of rest easy temporary files
String[] tmpfiles = tmpdir.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("resteasy-provider-datasource");
}
});
int countBefore = tmpfiles.length;
logger.info("Count of rest easy temporary files in " + tmpdir + " before request: " + countBefore);
// count temporary files before
int countBefore = countFiles(serverTmpDir);
logger.info("Count of Resteasy temporary files in " + serverTmpDir + " before request: " + countBefore);

// http request
HttpClient httpClient = HttpClients.custom().build();
Expand All @@ -101,14 +98,9 @@ public boolean accept(File dir, String name) {
Assert.assertEquals("Status of client request is not correct.", HttpStatus.SC_OK, postStatus);
Assert.assertEquals("Client get wrong response.", CleanFilesDataSourceProviderResource.clientResponse, postResponse);

// Get count of rest easy temporary files again
tmpfiles = tmpdir.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("resteasy-provider-datasource");
}
});
int countAfter = tmpfiles.length;
logger.info("Count of rest easy temporary files in " + tmpdir + " after request: " + countAfter);
// count temporary files after
int countAfter = countFiles(serverTmpDir);
logger.info("Count of Resteasy temporary files in " + serverTmpDir + " after request: " + countAfter);

// Compare
Assert.assertEquals("Client request remove or add some temporary files.", countBefore, countAfter);
Expand All @@ -123,19 +115,9 @@ public boolean accept(File dir, String name) {
*/
@Test
public void testDataSourceProviderInputStreamTwice() throws Exception {
// get directory for temporary files
File tmpdir = new File(System.getProperty("java.io.tmpdir"));
Assert.assertTrue("java.io.tmpdir does not exists", tmpdir.isDirectory());
logger.info("Tmp directory = " + tmpdir);

// Get count of rest easy temporary files
String[] tmpfiles = tmpdir.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("resteasy-provider-datasource");
}
});
int countBefore = tmpfiles.length;
logger.info("Count of rest easy temporary files in " + tmpdir + " before request: " + countBefore);
// count temporary files before
int countBefore = countFiles(serverTmpDir);
logger.info("Count of Resteasy temporary files in " + serverTmpDir + " before request: " + countBefore);

// http request
HttpClient httpClient = HttpClients.custom().build();
Expand All @@ -149,16 +131,56 @@ public boolean accept(File dir, String name) {
Assert.assertEquals(TestUtil.getErrorMessageForKnownIssue("JBEAP-1904", "Status of client request is not correct."), HttpStatus.SC_OK, postStatus);
Assert.assertEquals("Client get wrong response.", CleanFilesDataSourceProviderResource.clientResponse, postResponse);

// Get count of rest easy temporary files again
tmpfiles = tmpdir.list(new FilenameFilter() {
// count temporary files after
int countAfter = countFiles(serverTmpDir);
logger.info("Count of Resteasy temporary files in " + serverTmpDir + " after request: " + countAfter);

// Compare
Assert.assertEquals("Client request remove or add some temporary files.", countBefore, countAfter);
}

/**
* @tpTestDetails Tests that DataSourceProvider removes temporary file it creates in the case when input stream is not read.
* @tpInfo RESTEASY-1670
* @tpSince RESTEasy 3.0.24
*/
@Test
public void testDataSourceProviderInputStreamNotRead() throws Exception {
// count temporary files before
int countBefore = countFiles(serverTmpDir);
logger.info("Count of Resteasy temporary files in " + serverTmpDir + " before request: " + countBefore);

ByteArrayOutputStream baos = new ByteArrayOutputStream(5000);
for (int i = 0; i < 5000; i++) {
baos.write(i);
}
Response response = client.target(generateURL("/never")).request().post(Entity.entity(baos.toByteArray(), MediaType.APPLICATION_OCTET_STREAM));
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());

// count temporary files after
int countAfter = countFiles(serverTmpDir);
logger.info("Count of Resteasy temporary files in " + serverTmpDir + " after request: " + countAfter);

// Compare
Assert.assertEquals("Client request removed or added some temporary files.", countBefore, countAfter);
}

private static String getTmpDirectory() {
Response response = client.target(generateURL("/tmpdirpath")).request().get();
return response.readEntity(String.class);
}

private int countFiles(String dir) {
File tmpdir = new File(dir);
Assert.assertTrue(dir + " does not exists", tmpdir.isDirectory());
logger.info("Tmp directory = " + tmpdir);

// Get count of Resteasy temporary files
String[] tmpfiles = tmpdir.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("resteasy-provider-datasource");
}
});
int countAfter = tmpfiles.length;
logger.info("Count of rest easy temporary files in " + tmpdir + " after request: " + countAfter);

// Compare
Assert.assertEquals("Client request remove or add some temporary files.", countBefore, countAfter);
return tmpfiles.length;
}
}
@@ -1,6 +1,7 @@
package org.jboss.resteasy.test.providers.datasource.resource;

import javax.activation.DataSource;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
Expand All @@ -11,6 +12,13 @@ public class CleanFilesDataSourceProviderResource {

public static String clientResponse = "response_from_client";

@GET
@Path("/tmpdirpath")
@Produces("text/plain")
public String get() {
return System.getProperty("java.io.tmpdir");
}

@POST
@Path("once")
@Produces("text/plain")
Expand All @@ -34,4 +42,12 @@ public String getInputSteamTwice(DataSource dataSource) throws Exception {
is2.close();
return clientResponse;
}

@POST
@Path("never")
@Produces("text/plain")
public String postDataSource(DataSource datasource) {
return datasource.getContentType();
}

}

0 comments on commit 062c106

Please sign in to comment.