Show SynchronousPersisterTest.java syntax highlighted
package test.planner;
import java.io.File;
import java.sql.Timestamp;
import java.util.List;
import junit.framework.JUnit4TestAdapter;
import junit.framework.Test;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import persister.Backlog;
import persister.ConnectionFailedException;
import persister.CouldNotLoadProjectException;
import persister.ForbiddenOperationException;
import persister.IndexCardNotFoundException;
import persister.Iteration;
import persister.Project;
import persister.StoryCard;
import persister.impl.data.IterationDataObject;
import persister.impl.data.StoryCardDataObject;
import persister.local.PersisterToXML;
public class SynchronousPersisterTest extends PersisterTest {
private static PersisterToXML ps;
public static Test suite() {
return new JUnit4TestAdapter(SynchronousPersisterTest.class);
}
@Before
public void setUp() {
try {
resetProjectFiles();
try {
ps = new PersisterToXML(System.getProperty("user.dir")
+ File.separator + "TestDirectory", "ProjectFile");
} catch (CouldNotLoadProjectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ConnectionFailedException e) {
Assert.fail("Could not create an instance of PersisterToXML");
}
}
@After
public void tearDown() {
}
/***************************************************************************
* INITIALIZATION *
**************************************************************************/
@org.junit.Test
public void testInitialize() {
File projectDirectory = ps.getProjectDirectory();
String filename = projectDirectory.getAbsolutePath() + File.separator
+ "ProjectFile.xml";
File projectFile = ps.getFile();
Assert.assertTrue("Project directory is not a directory",
projectDirectory.isDirectory());
Assert.assertTrue("Project directory is not the user.dir",
projectDirectory.getAbsolutePath().equals(
System.getProperty("user.dir") + File.separator
+ "TestDirectory"));
Assert.assertTrue("Project file not available", projectFile
.getAbsolutePath().equals(filename));
}
@org.junit.Test
public void testGetProjectNames() {
List<String> names = ps.getProjectNames();
Assert
.assertTrue(
"The project directory contains an incorrect number of projects",
names.size() == 3);
}
@org.junit.Test
public void testLoad() {
Project p = null;
try {
p = ps.load("ProjectWithBacklogIterationAndStoryCards");
} catch (CouldNotLoadProjectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<StoryCard> backlogStories = p.getBacklog().getStoryCardChildren();
List<Iteration> iterations = p.getIterationChildren();
Assert.assertTrue("Incorrect number os stories in backlog",
backlogStories.size() == 1);
Assert.assertTrue("Incorrect number os stories in backlog", iterations
.size() == 1);
List<StoryCard> iterationStories = p.getIterationChildren().iterator()
.next().getStoryCardChildren();
Assert.assertTrue("Incorrect number os stories in backlog",
iterationStories.size() == 1);
}
@org.junit.Test
public void testLoadAndCreateCard() {
Project p = null;
try {
p = ps.load("ProjectWithBacklogIterationAndStoryCards");
} catch (CouldNotLoadProjectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Iteration iter = ps.createIteration("", "", 22, 33, 44, 55, 2, new Timestamp(0), new Timestamp(1000));
long id = iter.getId();
Assert.assertTrue(id == 7);
Assert.assertTrue(iter.getParent() == 1);
}
/***************************************************************************
* StoryCard Methods *
**************************************************************************/
@org.junit.Test
public void testCreatAndDeleteIteration() {
try {
ps.load("ProjectWithBacklogIterationAndStoryCards");
} catch (CouldNotLoadProjectException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
// long parentid = ps.getProject().getId();
try {
// try to delete an iteration that does not exist
Iteration iteration = new IterationDataObject();
iteration.setId(-1);
try {
iteration = (Iteration)ps.deleteCard(iteration.getId());
} catch (ForbiddenOperationException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
Assert.fail("Expected IndexCardNotFoundException was NOT thrown");
} catch (IndexCardNotFoundException e) {
Assert.assertTrue("Iteration was correctly not found", true);
}
Iteration iteration = new IterationDataObject();
iteration.setId(4);
try {
try {
iteration =(Iteration) ps.deleteCard(iteration.getId());
} catch (ForbiddenOperationException e1) {
e1.printStackTrace();
Assert.fail(e1.getMessage());
}
try {// check if iteration really was deleted
ps.findCard(iteration.getId());
Assert
.fail("Iteration was not deleted from SynchronousPersister");
} catch (IndexCardNotFoundException e) {
// this should happen - everything is ok
}
try {// check if the story card inside the iteration is deleted,
// too
ps.findCard(5);
Assert
.fail("Story was not deleted from SynchronousPersister when iteration was deleted");
} catch (IndexCardNotFoundException e) {
// this should happen - everything is ok
}
} catch (IndexCardNotFoundException e) {
Assert.fail("Iteration was not deleted correctly");
}
}
@org.junit.Test
public void testUndeleteIteration() {
long parentid = ps.getProject().getId();
try {
Iteration iteration = new IterationDataObject();
iteration.setId(999);
iteration.setParent(parentid);
iteration.setName("undeletedIteration");
StoryCard sc = new StoryCardDataObject();
sc.setId(1000);
iteration.addStoryCard(sc);
iteration = (Iteration)ps.undeleteCard(iteration);
iteration = (Iteration)ps.findCard(999);
//sc = ps.findStoryCardById(1000);
sc = (StoryCard)ps.findCard(1000);
Assert.assertEquals(iteration.getId(), (long) 999);
Assert.assertEquals(iteration.getName(), "undeletedIteration");
} catch (ForbiddenOperationException e) {
Assert.fail("Could not undelete an iteration");
} catch (IndexCardNotFoundException e) {
Assert.fail("Could not undelete StoryCard included in iteration");
}
}
@org.junit.Test
public void testCreateAndDeleteBacklog() {
PersisterToXML ps1 = null;
try {
try {
ps1 = new PersisterToXML(System.getProperty("user.dir")
+ File.separator + "TestDirectory", "EmptyProject");
} catch (ConnectionFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (CouldNotLoadProjectException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Backlog b = ps1.createBacklog(22, 33, 44, 55);
b.getId(); //b.setParent(ps1.getProject().getId());
try {
ps1.deleteCard(b.getId());
} catch (IndexCardNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Assert.fail("Should never reach here!");
}
} catch (ForbiddenOperationException e) {
//e.printStackTrace();
Assert.assertTrue("You cannot delete the Backlog without deleting the Project!", true);
}
}
@org.junit.Test
public void testDeleteStoryCard() {
try {
ps.load("ProjectWithBacklogIterationAndStoryCards");
} catch (CouldNotLoadProjectException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
// long parentid = ps.getProject().getId();
try {
// try to delete StoryCard that does not exist
StoryCard sc = new StoryCardDataObject();
sc.setId(-1);
try {
sc = (StoryCard)ps.deleteCard(sc.getId());
} catch (ForbiddenOperationException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
Assert.fail("Expected IndexCardNotFoundException was NOT thrown");
} catch (IndexCardNotFoundException e) {
Assert.assertTrue("StoryCard was correctly not found", true);
}
StoryCard sc = new StoryCardDataObject();
sc.setId(4);
try {
try {
sc = (StoryCard)ps.deleteCard(5);
} catch (ForbiddenOperationException e1) {
e1.printStackTrace();
Assert.fail(e1.getMessage());
}
try {// check if StoryCard really was deleted
ps.findCard(5);
Assert.fail("StoryCard was not deleted from SynchronousPersister");
} catch (IndexCardNotFoundException e) {
// this should happen - everything is ok
}
} catch (IndexCardNotFoundException e) {
Assert.fail("StoryCard was not deleted correctly");
}
}
@org.junit.Test
public void testUndeleteStoryCard() {
try {
Iteration i = ps.createIteration("undeletedStoryCard",
"description empty", 200, 100, 399, 500, (float) 55.0,
new Timestamp(0), new Timestamp(500000));
StoryCard sc = new StoryCardDataObject();
sc.setId(1000);
sc.setParent(i.getId());
sc.setName("undeletedStoryCard");
long id = sc.getParent();
sc = (StoryCard)ps.undeleteCard(sc);
//sc = ps.findStoryCardById(1000);
sc = (StoryCard)ps.findCard(1000);
Assert.assertEquals(sc.getId(), (long) 1000);
Assert.assertEquals(i.getId(), id);
Assert.assertEquals(sc.getName(), "undeletedStoryCard");
} catch (ForbiddenOperationException e) {
Assert.fail("Could not undelete StoryCard");
} catch (IndexCardNotFoundException e) {
Assert.fail("Could not undelete StoryCard");
}
}
@org.junit.Test
public void testCreatAndUpdateStoryCard() {
try {
ps.createIteration("undeletedStoryCard",
"description empty", 200, 100, 399, 500, (float) 55.0,
new Timestamp(0), new Timestamp(500000));
StoryCard sc = ps.createStoryCard("", "", 22, 33, 44, 55, 2, 2, 3, 4, 5, "P");
StoryCard sc1 ;
sc1 = (StoryCard) ps.updateCard(sc);
try {// check if StoryCard really was updated
ps.findCard(sc1.getId());
ps.findCard(sc1.getParent());
} catch (IndexCardNotFoundException e) {
Assert.fail("StoryCard was not updated from SynchronousPersister");
}
} catch (IndexCardNotFoundException e) {
Assert.fail("StoryCard was not updated correctly");
}
}
@org.junit.Test
public void testUpdateIteration() {
try {
Iteration i = ps.createIteration("updateIteration",
"description empty", 200, 100, 401, 500, (float) 55.0,
new Timestamp(0), new Timestamp(500000));
Iteration i1;
i1 = (Iteration)ps.updateCard(i);
try {// check if Iteration really was updated
ps.getProject().getId();
ps.findCard(i1.getId());
} catch (IndexCardNotFoundException e) {
Assert.fail("Iteration was not updated from SynchronousPersister");
}
} catch (IndexCardNotFoundException e) {
Assert.fail("Iteration was not updated correctly");
}
}
@org.junit.Test
public void testUpdateBacklog() {
try {
Backlog b = (Backlog)ps.updateCard(ps.getProject().getBacklog());
Assert.assertTrue("Backlog updated successfully", b.getParent() == 1);
} catch (IndexCardNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@org.junit.Test
public void testMoveStoryCardToNewParent() {
PersisterToXML ps1 = null;
try {
try {
ps1 = new PersisterToXML(System.getProperty("user.dir")
+ File.separator + "TestDirectory", "ProjectWithBacklogIterationAndStoryCards");
} catch (ConnectionFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (CouldNotLoadProjectException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
ps1.moveStoryCardToNewParent(6, 2, 4, 50, 50);
Assert.assertTrue("Card moved sucessfully",(((StoryCard)(ps1.findCard(6))).getParent() == 4));
} catch (IndexCardNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}// // end class
See more files for this project here