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

[WFCORE-1756]: Adding content to an exploded deployment with overwrite to false should fail if file already exists #1770

Merged
merged 2 commits into from Sep 1, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -595,7 +595,11 @@ public byte[] addContentToExploded(byte[] deploymentHash, List<ExplodedContent>
if(in == null) {
Files.createDirectory(targetFile);
} else {
Files.copy(in, targetFile, StandardCopyOption.REPLACE_EXISTING);
if(overwrite) {
Files.copy(in, targetFile, StandardCopyOption.REPLACE_EXISTING);
} else {
Files.copy(in, targetFile);
}
}
}
}
Expand Down
Expand Up @@ -27,6 +27,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -64,6 +65,7 @@ public class ContentRepositoryTest {
private static final boolean IS_WINDOWS = AccessController.doPrivileged((PrivilegedAction<Boolean>) () ->
System.getProperty("os.name", null).toLowerCase(Locale.ENGLISH).contains("windows"));
private static final FileTime time = FileTime.from(Instant.parse("2007-12-03T10:15:30.00Z"));

private ContentRepository repository;
private final File rootDir = new File("target", "repository");
private final File tmpRootDir = new File("target", "tmp");
Expand Down Expand Up @@ -239,7 +241,7 @@ public void testChangeExplodedContent() throws Exception {
true);
assertThat(hash, is(notNullValue()));
assertThat(HashUtil.bytesToHexString(hash), is(updatedExpectedResult));
try(InputStream addedContent = repository.readContent(hash, "test.jsp")) {
try (InputStream addedContent = repository.readContent(hash, "test.jsp")) {
assertThat(addedContent, is(notNullValue()));
assertThat(readFileContent(addedContent), is("this is a test"));
}
Expand All @@ -248,6 +250,25 @@ public void testChangeExplodedContent() throws Exception {
hash = repository.removeContentFromExploded(hash, Collections.singletonList("test.jsp"));
assertThat(hash, is(notNullValue()));
assertThat(HashUtil.bytesToHexString(hash), is(expResult));
updatedExpectedResult = "a44921155d75009d885db3357005b85b435cf59f";
hash = repository.addContentToExploded(hash,
Collections.singletonList(new ExplodedContent("test.jsp",
new ByteArrayInputStream("this is an overwrite test".getBytes(StandardCharsets.UTF_8)))),
true);
assertThat(hash, is(notNullValue()));
assertThat(HashUtil.bytesToHexString(hash), is(updatedExpectedResult));
try (InputStream addedContent = repository.readContent(hash, "test.jsp")) {
assertThat(addedContent, is(notNullValue()));
assertThat(readFileContent(addedContent), is("this is an overwrite test"));
}
try {
hash = repository.addContentToExploded(hash,
Collections.singletonList(new ExplodedContent("test.jsp",
new ByteArrayInputStream("this is a failure test".getBytes(StandardCharsets.UTF_8)))),
false);
fail("Overwritting shouldn't work");
} catch( ExplodedContentException ex) {
}
}
}

Expand All @@ -259,21 +280,21 @@ public void testListContents() throws Exception {
String expResult = "b1f18e286615dda0643633ec31f1a17d90e48875";
//hash is different from the simple overlay.xhtml as we add the content folder name in the computation
assertThat(hash, is(notNullValue()));
Path content = repository.getContent(hash).getPhysicalFile().toPath();
Path content = repository.getContent(hash).getPhysicalFile().toPath();
String contentHtml = readFileContent(content.resolve("overlay.xhtml"));
String expectedContentHtml = readFileContent(getResourceAsStream("overlay.xhtml"));
assertThat(contentHtml, is(expectedContentHtml));
assertThat(HashUtil.bytesToHexString(hash), is(expResult));
String updatedExpectedResult = "161a2c95b16d5ffede0721c2cec984ca51009082";
hash = repository.addContentToExploded(hash,
Collections.singletonList(new ExplodedContent("test.jsp",new ByteArrayInputStream("this is a test".getBytes(StandardCharsets.UTF_8)))),
Collections.singletonList(new ExplodedContent("test.jsp", new ByteArrayInputStream("this is a test".getBytes(StandardCharsets.UTF_8)))),
true);
assertThat(hash, is(notNullValue()));
assertThat(HashUtil.bytesToHexString(hash), is(updatedExpectedResult));
List<String> contents = repository.listContent(hash, "", ContentFilter.Factory.createContentFilter(-1, false)).stream().map(ContentRepositoryElement::getPath).collect(Collectors.toList());
assertThat(contents.size(), is(2));
assertThat(contents, CoreMatchers.hasItems("test.jsp", "overlay.xhtml"));
hash = repository.addContentToExploded(hash, Collections.singletonList(new ExplodedContent("test/empty-file.txt",emptyStream())), true);
hash = repository.addContentToExploded(hash, Collections.singletonList(new ExplodedContent("test/empty-file.txt", emptyStream())), true);
hash = repository.addContentToExploded(hash, Collections.singletonList(new ExplodedContent("empty-dir", null)), true);
contents = repository.listContent(hash, "", ContentFilter.Factory.createContentFilter(-1, false)).stream().map(ContentRepositoryElement::getPath).collect(Collectors.toList());
assertThat(contents, is(notNullValue()));
Expand Down Expand Up @@ -524,6 +545,7 @@ private InputStream getFileInputStream(final Path path) throws IOException {
}

private static class CarriageReturnRemovalInputStream extends InputStream {

private final InputStream delegate;

private CarriageReturnRemovalInputStream(final InputStream delegate) {
Expand Down
Expand Up @@ -155,43 +155,45 @@ public void testDeploymentFileApi() throws Exception {
final JavaArchive archive = ServiceActivatorDeploymentUtil.createServiceActivatorDeploymentArchive("test-deployment.jar", properties);
final JavaArchive archive2 = ServiceActivatorDeploymentUtil.createServiceActivatorDeploymentArchive("test-deployment.jar", properties2);
archive2.addAsManifestResource(DeploymentTestCase.class.getPackage(), "marker.txt", "marker.txt");
final File dir = new File("target/archives");

final ModelControllerClient client = managementClient.getControllerClient();
final ServerDeploymentManager manager = ServerDeploymentManager.Factory.create(client);
try {
testDeployments(false, new DeploymentExecutor() {

testDeployments(false, new DeploymentExecutor() {

@Override
public void initialDeploy() throws IOException {
Future<?> future = manager.execute(manager.newDeploymentPlan().add("test-deployment.jar", exportArchive(archive))
.deploy("test-deployment.jar").build());
awaitDeploymentExecution(future);
}

@Override
public void fullReplace() throws IOException {
Future<?> future = manager.execute(manager.newDeploymentPlan().replace("test-deployment.jar", exportArchive(archive2)).build());
awaitDeploymentExecution(future);
}
@Override
public void initialDeploy() throws IOException {
Future<?> future = manager.execute(manager.newDeploymentPlan().add("test-deployment.jar", exportArchive(archive))
.deploy("test-deployment.jar").build());
awaitDeploymentExecution(future);
}

@Override
public void undeploy() {
Future<?> future = manager.execute(manager.newDeploymentPlan().undeploy("test-deployment.jar").remove("test-deployment.jar").build());
awaitDeploymentExecution(future);
}
@Override
public void fullReplace() throws IOException {
Future<?> future = manager.execute(manager.newDeploymentPlan().replace("test-deployment.jar", exportArchive(archive2)).build());
awaitDeploymentExecution(future);
}

private File exportArchive(JavaArchive archive) {
final File dir = new File("target/archives");
dir.mkdirs();
final File file = new File(dir, "test-deployment.jar");
if (file.exists()) {
file.delete();
@Override
public void undeploy() {
Future<?> future = manager.execute(manager.newDeploymentPlan().undeploy("test-deployment.jar").remove("test-deployment.jar").build());
awaitDeploymentExecution(future);
}
archive.as(ZipExporter.class).exportTo(file, true);
return file;
}
});

private File exportArchive(JavaArchive archive) {
dir.mkdirs();
final File file = new File(dir, "test-deployment.jar");
if (file.exists()) {
file.delete();
}
archive.as(ZipExporter.class).exportTo(file, true);
return file;
}
});
} finally {
cleanFile(dir);
}
}

@Test
Expand Down Expand Up @@ -371,6 +373,7 @@ public void undeploy() {
});
} finally {
removeDeploymentScanner(client, scannerName);
cleanFile(dir);
}
}

Expand Down Expand Up @@ -501,6 +504,7 @@ public void undeploy() {
});
} finally {
removeDeploymentScanner(client, scannerName);
cleanFile(dir);
}
}

Expand Down Expand Up @@ -541,20 +545,20 @@ public void testAddingDeploymentScannerWillLaunchScan() throws Exception {
Files.delete(target);
Files.delete(deployed);
Files.delete(deployDir);
cleanFile(dir.toFile());
}
}

@Test
public void testExplodedFilesystemDeployment() throws Exception {

final File deployDir = createDeploymentDir("exploded-deployments");

final File dir = new File("target/archives");
ModelControllerClient client = managementClient.getControllerClient();
final String scannerName = "exploded";
addDeploymentScanner(deployDir, client, scannerName, false);
try {
final JavaArchive archive = ServiceActivatorDeploymentUtil.createServiceActivatorDeploymentArchive("test-deployment.jar", properties);
final File dir = new File("target/archives");
dir.mkdirs();
archive.as(ExplodedExporter.class).exportExploded(deployDir);

Expand Down Expand Up @@ -664,6 +668,7 @@ public void undeploy() {
});
} finally {
removeDeploymentScanner(client, scannerName);
cleanFile(dir);
}
}

Expand Down