Skip to content
Permalink
Browse files
fixes [JENKINS-11374] - Add deletion option on "list of Shelved Projects
  • Loading branch information
christiangalsterer committed Mar 25, 2014
1 parent f777d66 commit 27fbda17d389d77ab4698bbbd5c3f98d53eebb17
Showing with 102 additions and 18 deletions.
  1. +102 −18 src/test/java/org/jvnet/hudson/plugins/shelveproject/DeleteProjectExecutableTest.java
@@ -6,45 +6,55 @@
import hudson.model.Hudson;
import hudson.model.Queue.Task;
import hudson.tasks.Shell;
import org.jvnet.hudson.test.HudsonTestCase;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.File;
import java.util.ArrayList;

/**
* @author christian.galsterer
*/
public class DeleteProjectExecutableTest extends HudsonTestCase {
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class DeleteProjectExecutableTest {
private Task parentTask;

@Rule
public JenkinsRule jenkinsRule = new JenkinsRule();

/**
* Tests if a project is deleted.
*
* @throws Exception
*/
@Test
public void testProjectZipIsDeleted() throws Exception {

String projectname = "ProjectWithWorkspace";
String file;
String filename;
ArrayList<String> files = new ArrayList<String>();

FreeStyleProject project = createFreeStyleProject(projectname);
FreeStyleProject project = jenkinsRule.createFreeStyleProject(projectname);
project.getBuildersList().add(new Shell("echo hello"));

FreeStyleBuild b = project.scheduleBuild2(0).get();

assertTrue("Workspace should exist by now",
b.getWorkspace().exists());
assertTrue("Workspace should exist by now", b.getWorkspace().exists());

File shelvedProjectsDir = new File(Hudson.getInstance().getRootDir(), "shelvedProjects");
shelvedProjectsDir.mkdirs();

ShelveProjectExecutable a = new ShelveProjectExecutable(parentTask, project);
a.run();

// Read through target directory and find that the zip has been created.
File[] listOfFiles = shelvedProjectsDir.listFiles();
for (File listOfFile : listOfFiles) {
if (listOfFile.isFile()) {
file = listOfFile.getName();
files.add(file);
if (file.startsWith(projectname) && (file.endsWith(".zip"))) {

// Read through target directory and find that the zip has been created.
for (File file : listOfFiles) {
if (file.isFile()) {
filename = file.getName();
files.add(filename);
if (filename.startsWith(projectname) && (filename.endsWith(".zip"))) {
assertTrue("Found project .zip file in shelvedProjects", true);
} else {
fail("Did not find project .zip file in shelvedProjects");
@@ -56,14 +66,88 @@ public void testProjectZipIsDeleted() throws Exception {
deleteProjectExecutable.run();

// Read through target directory and find that the zip has been deleted.
listOfFiles = shelvedProjectsDir.listFiles();
for (File listOfFile : listOfFiles) {
if (listOfFile.isFile()) {
file = listOfFile.getName();
if (files.contains(file)) {
filename = listOfFile.getName();
if (files.contains(filename)) {
fail("Found project .zip file in shelvedProjects");
}
}
}
}

/**
* Tests if two projects can be unshelved and deleted in parallel
*
* @throws Exception
*/
@Test
public void testParallelProjectUnshelvingAndDeleting() throws Exception {
String filename;

String projectnameToUnshelve = "ProjectToUnshelveWithWorkspace";
String projectnameToDelete = "ProjectToDeleteWithWorkspace";

ArrayList<String> filenamesToUnshelve = new ArrayList<String>();
ArrayList<String> filenamesToDelete = new ArrayList<String>();

// Project to unshelve
FreeStyleProject projectToUnshelve = jenkinsRule.createFreeStyleProject(projectnameToUnshelve);
projectToUnshelve.getBuildersList().add(new Shell("echo hello"));
FreeStyleBuild unshelveBuild = projectToUnshelve.scheduleBuild2(0).get();
assertTrue("Workspace should exist by now", unshelveBuild.getWorkspace().exists());

//Project to delete
FreeStyleProject projectToDelete = jenkinsRule.createFreeStyleProject(projectnameToDelete);
projectToDelete.getBuildersList().add(new Shell("echo hello"));
FreeStyleBuild deleteBuild = projectToDelete.scheduleBuild2(0).get();
assertTrue("Workspace should exist by now", deleteBuild.getWorkspace().exists());

File shelvedProjectsDir = new File(Hudson.getInstance().getRootDir(), "shelvedProjects");
shelvedProjectsDir.mkdirs();

//Shelve project to be deleted later on
ShelveProjectExecutable shelveProjectExecutable = new ShelveProjectExecutable(parentTask, projectToDelete);
shelveProjectExecutable.run();
//Shelve project to be unshelved later on
shelveProjectExecutable = new ShelveProjectExecutable(parentTask, projectToUnshelve);
shelveProjectExecutable.run();

File[] listOfFiles = shelvedProjectsDir.listFiles();
for (File file : listOfFiles) {
if (file.isFile()) {
filename = file.getName();
// Check if project to delete later on is created
if (filename.startsWith(projectnameToDelete) && (filename.endsWith(".zip"))) {
filenamesToDelete.add(filename);
assertTrue("Found projectToDelete .zip file in shelvedProjects", true);
} else if (filename.startsWith(projectnameToUnshelve) && (filename.endsWith(".zip"))) {
filenamesToUnshelve.add(filename);
assertTrue("Found projectToUnshelve .zip file in shelvedProjects", true);
} else {
fail("Unexpected file found in shelvedProjects");
}
}
}

UnshelveProjectExecutable unShelveProjectExecutable = new UnshelveProjectExecutable(parentTask,filenamesToUnshelve.toArray(new String[filenamesToUnshelve.size()]));
DeleteProjectExecutable deleteProjectExecutable = new DeleteProjectExecutable(parentTask, filenamesToDelete.toArray(new String[filenamesToDelete.size()]));

unShelveProjectExecutable.run();
deleteProjectExecutable.run();

listOfFiles = shelvedProjectsDir.listFiles();

// Check if project to delete is deleted and project to unshelve is unshelved
for (File file : listOfFiles) {
if (file.isFile()) {
filename = file.getName();
if (filename.startsWith(projectnameToUnshelve) && (filename.endsWith(".zip"))) {
fail("Found projectToUnshelve .zip file in shelvedProjects");
} else if (filenamesToDelete.contains(filename)) {
fail("Found projectToDelete .zip file in shelvedProjects");
}
}
}
}
}

0 comments on commit 27fbda1

Please sign in to comment.