Added throws declarations for IOExceptions on Issue setters.

The FileIssue implementation now checks to ensure the success of the setters.
This commit is contained in:
Jonathan Bernard 2010-05-19 09:54:18 -05:00
parent c39061f771
commit b7670e69f3
11 changed files with 160 additions and 27 deletions

View File

@ -82,7 +82,7 @@
classpathref="groovyc.classpath"/>
</target>
<target name="run-tests" depends="compile-tests">
<target name="test" depends="compile-tests">
<junit fork="yes" haltonfailure="yes">
<classpath refid="test.classpath"/>
<formatter type="brief" usefile="false" />
@ -94,7 +94,7 @@
</junit>
</target>
<target name="build" depends="compile,run-tests">
<target name="build" depends="compile,test">
<mkdir dir="${build.dir}/jar"/>
<jar
destfile="${build.dir}/jar/pit-${application.version}.${build.number.final}.jar"

View File

@ -1,10 +1,11 @@
#Wed, 19 May 2010 08:06:01 -0500
#Sat Apr 24 17:08:00 CDT 2010
build.dir=build
src.dir=src
lib.shared.dir=../shared-libs
test.dir=test
build.number=8
expected.application.version=2.3.0
build.number=1
expected.application.version=2.3.1
lib.dir=lib
release.dir=release
release.jar=pit-${application.version}.jar

Binary file not shown.

Binary file not shown.

View File

@ -23,31 +23,64 @@ public class FileIssue extends Issue {
this.source = file
text = file.text
super.@text = file.text
}
public void setCategory(Category c) {
super.setCategory(c)
source.renameTo(new File(source.canonicalFile.parentFile, getFilename()))
public void setCategory(Category c) throws IOException {
boolean renamed
renamed = source.renameTo(new File(source.canonicalFile.parentFile,
makeFilename(id, c, status, priority)))
if (!renamed)
throw new IOException("I was unable to set the category. "
+ "I need to rename the file for this issue, but something is "
+ "preventing me from doing so (maybe the path to the file is "
+ "no longer valid, or maybe the file is currently open in "
+ "some other program).")
else super.setCategory(c)
}
public void setStatus(Status s) {
super.setStatus(s)
source.renameTo(new File(source.canonicalFile.parentFile, getFilename()))
public void setStatus(Status s) throws IOException {
boolean renamed
renamed = source.renameTo(new File(source.canonicalFile.parentFile,
makeFilename(id, category, s, priority)))
if (!renamed)
throw new IOException("I was unable to set the status. "
+ "I need to rename the file for this issue, but something is "
+ "preventing me from doing so (maybe the path to the file is "
+ "no longer valid, or maybe the file is currently open in "
+ "some other program).")
else super.setStatus(s)
}
public void setPriority(int p) {
super.setPriority(p)
source.renameTo(new File(source.canonicalFile.parentFile, getFilename()))
public void setPriority(int p) throws IOException {
boolean renamed
renamed = source.renameTo(new File(source.canonicalFile.parentFile,
makeFilename(id, category, status, p)))
if (!renamed)
throw new IOException("I was unable to set the priority. "
+ "I need to rename the file for this issue, but something is "
+ "preventing me from doing so (maybe the path to the file is "
+ "no longer valid, or maybe the file is currently open in "
+ "some other program).")
else super.setPriority(p)
}
public String getFilename() {
return makeFilename(id, category, status, priority)
}
public void setText(String text) {
public void setText(String text) throws IOException {
try { source.write(text) }
catch (IOException ioe) {
throw new IOException("I could not save the new text for this "
+ "issue. I can not write to the file for this issue. I do not"
+ " know why, I am sorry (maybe the file can not be reached).")
}
super.setText(text)
source.write(text)
}
public boolean delete() { return source.delete() }

View File

@ -26,7 +26,7 @@ public abstract class Issue {
public Category getCategory() { return category }
public void setCategory(Category c) {
public void setCategory(Category c) throws IOException {
if (c == null)
throw new IAE("Category cannot be null.")
@ -35,7 +35,7 @@ public abstract class Issue {
public Status getStatus() { return status }
public void setStatus(Status s) {
public void setStatus(Status s) throws IOException {
if (s == null)
throw new IAE("Status cannot be null.")
@ -44,13 +44,15 @@ public abstract class Issue {
public int getPriority() { return priority }
public void setPriority(int p) { priority = Math.min(9, Math.max(0, p)) }
public void setPriority(int p) throws IOException {
priority = Math.min(9, Math.max(0, p))
}
public String getTitle() { return text.readLines()[0] }
public String getText() { return text }
public void setText(String t) { text = t }
public void setText(String t) throws IOException { text = t }
public boolean hasDelivery() { return deliveryDate == null }

View File

@ -42,8 +42,13 @@ class FileIssueTest {
assertEquals issues[0].category, Category.FEATURE
assertEquals issues[1].category, Category.TASK
issues[0].category = Category.TASK
issues[1].category = Category.BUG
try {
issues[0].category = Category.TASK
issues[1].category = Category.BUG
} catch (Exception e) {
Assert.fail("An unexpected Exception occurred: "
+ e.getLocalizedMessage())
}
assertEquals issues[0].category, Category.TASK
assertEquals issues[1].category, Category.BUG
@ -52,6 +57,27 @@ class FileIssueTest {
assertTrue new File(testDir, '0002bs5.rst').exists()
assertFalse new File(testDir, '0001fn1.rst').exists()
assertFalse new File(testDir, '0002ts5.rst').exists()
}
@Test void testSetCategoryFails() {
FileInputStream fin
try {
// get a lock to the file to prevent the rename
def issueFile = new File('0001fn1.rst')
fin = new FileInputStream(issueFile)
// try to set the category
issues[0].category = Category.TASK
// should throw IOE before here
Assert.fail()
} catch (IOException ioe) {
} catch (Exception e) {
Assert.fail("Unexpected exception: " + e.getLocalizedMessage())
} finally {
if (fin != null) fin.close()
}
}
@Test void testSetStatus() {
@ -59,8 +85,13 @@ class FileIssueTest {
assertEquals issues[0].status, Status.NEW
assertEquals issues[1].status, Status.RESOLVED
issues[0].status = Status.RESOLVED
issues[1].status = Status.REJECTED
try {
issues[0].status = Status.RESOLVED
issues[1].status = Status.REJECTED
} catch (Exception e) {
Assert.fail("An unexpected Exception occurred: "
+ e.getLocalizedMessage())
}
assertTrue new File(testDir, '0001fs1.rst').exists()
assertTrue new File(testDir, '0002tj5.rst').exists()
@ -68,13 +99,38 @@ class FileIssueTest {
assertFalse new File(testDir, '0002ts5.rst').exists()
}
@Test void testSetStatusFails() {
FileInputStream fin
try {
// get a lock to the file to prevent the rename
def issueFile = new File('0001fn1.rst')
fin = new FileInputStream(issueFile)
// try to set the status
issues[0].status = Status.REJECTED
// should throw IOE before here
Assert.fail()
} catch (IOException ioe) {
} catch (Exception e) {
Assert.fail("Unexpected exception: " + e.getLocalizedMessage())
} finally {
if (fin != null) fin.close()
}
}
@Test void testSetPriority() {
assertEquals issues[0].priority, 1
assertEquals issues[1].priority, 5
issues[0].priority = 2
issues[1].priority = 9
try {
issues[0].priority = 2
issues[1].priority = 9
} catch (Exception e) {
Assert.fail("An unexpected Exception occurred: "
+ e.getLocalizedMessage())
}
assertEquals issues[0].priority, 2
assertEquals issues[1].priority, 9
@ -85,6 +141,26 @@ class FileIssueTest {
assertFalse new File(testDir, '0002ts5.rst').exists()
}
@Test void testSetPriorityFails() {
FileInputStream fin
try {
// get a lock to the file to prevent the rename
def issueFile = new File('0001fn1.rst')
fin = new FileInputStream(issueFile)
// try to set the priority
issues[0].priority = 9
// should throw IOE before here
Assert.fail()
} catch (IOException ioe) {
} catch (Exception e) {
Assert.fail("Unexpected exception: " + e.getLocalizedMessage())
} finally {
if (fin != null) fin.close()
}
}
@Test void testConstruction() {
File issueFile = new File(testDir, '0001fn1.rst')
Issue issue = new FileIssue(issueFile)
@ -100,6 +176,27 @@ class FileIssueTest {
assertEquals issue.source , issueFile
}
@Test void testSetTextFails() {
try {
// make the issue file un-writable
def issueFile = new File('0001fn1.rst')
if (issueFile.setReadOnly()) {
// try to write something
issues[0].text = "This should fail to be written."
// should throw IOE before here
Assert.fail()
} else {
println "Could not run testSetTextFails, unable to change " +
"the test isseu file's permissions."
}
} catch (IOException ioe) {
} catch (Exception e) {
Assert.fail("Unexpected exception: " + e.getLocalizedMessage())
}
}
@Test void testMakeFilename() {
assertEquals FileIssue.makeFilename('0001', Category.BUG,
Status.NEW, 5), '0001bn5.rst'

Binary file not shown.

BIN
release/lib/pit-2.3.1.jar Normal file

Binary file not shown.

Binary file not shown.

View File

@ -1 +1 @@
application.version=2.3.0
application.version=2.3.1