Browse files

100% coverage

  • Loading branch information...
1 parent 69397e7 commit 24930d8d865910933ab9bc33d4431baf74fbee88 Mike Hugo committed Mar 30, 2012
View
13 BuildingQuality/sample-code/metrics/grails-app/conf/BuildConfig.groovy
@@ -49,3 +49,16 @@ grails.project.dependency.resolution = {
build ":tomcat:$grailsVersion"
}
}
+codenarc.reports = {
+ MyXmlReport('xml') { // The report name "MyXmlReport" is user-defined; Report type is 'xml'
+ outputFile = 'target/codenarc/CodeNarc-Report.xml' // Set the 'outputFile' property of the (XML) Report
+ title = 'CodeNarc XML Report' // Set the 'title' property of the (XML) Report
+ }
+ MyHtmlReport('html') { // Report type is 'html'
+ outputFile = 'target/codenarc/CodeNarc-Report.html'
+ title = 'CodeNarc HTML Report'
+ }
+}
+codenarc.propertiesFile = 'grails-app/conf/codenarc.properties'
+codenarc.ruleSetFiles = 'rulesets/basic.xml,rulesets/exceptions.xml, rulesets/imports.xml,rulesets/grails.xml, rulesets/unused.xml, rulesets/logging.xml, rulesets/junit.xml'
+codenarc.maxPriority1Violations=0
View
3 BuildingQuality/sample-code/metrics/grails-app/conf/codenarc.properties
@@ -0,0 +1,3 @@
+Println.priority=1
+SystemErrPrint.priority=1
+SystemOutPrint.priority=1
View
1 BuildingQuality/sample-code/metrics/grails-app/controllers/metrics/BookController.groovy
@@ -60,6 +60,7 @@ class BookController {
return
}
+ println params.version
if (params.version) {
def version = params.version.toLong()
if (bookInstance.version > version) {
View
2 BuildingQuality/sample-code/metrics/grails-app/domain/metrics/Book.groovy
@@ -2,6 +2,8 @@ package metrics
class Book {
+ String title
+
static constraints = {
}
}
View
63 BuildingQuality/sample-code/metrics/test/unit/metrics/BookControllerTests.groovy
@@ -1,9 +1,8 @@
package metrics
-
-
-import org.junit.*
-import grails.test.mixin.*
+import grails.test.mixin.Mock
+import grails.test.mixin.TestFor
+import org.springframework.dao.DataIntegrityViolationException
@TestFor(BookController)
@Mock(Book)
@@ -12,8 +11,7 @@ class BookControllerTests {
def populateValidParams(params) {
assert params != null
- // TODO: Populate valid properties like...
- //params["name"] = 'someValidName'
+ params["title"] = 'someValidTitle'
}
void testIndex() {
@@ -29,6 +27,19 @@ class BookControllerTests {
assert model.bookInstanceTotal == 0
}
+ void testList_maxParams() {
+ params.max = 10
+
+ (1..20).each {
+ new Book(title:it).save(failOnError: true)
+ }
+
+ def model = controller.list()
+
+ assert model.bookInstanceList.size() == params.max
+ assert model.bookInstanceTotal == Book.count()
+ }
+
void testCreate() {
def model = controller.create()
@@ -97,15 +108,16 @@ class BookControllerTests {
response.reset()
-
populateValidParams(params)
def book = new Book(params)
-
assert book.save() != null
+ book.version = 1
+ assert book.save() != null
+
// test invalid parameters in update
params.id = book.id
- //TODO: add invalid values to params object
+ params.title = null
controller.update()
@@ -120,6 +132,19 @@ class BookControllerTests {
assert response.redirectedUrl == "/book/show/$book.id"
assert flash.message != null
+ // test with valid version number
+ assert book.version == 1
+ response.reset()
+ book.clearErrors()
+
+ populateValidParams(params)
+ params.version = book.version
+ controller.update()
+
+ assert response.redirectedUrl == "/book/show/$book.id"
+ assert flash.message != null
+
+
//test outdated version number
response.reset()
book.clearErrors()
@@ -156,4 +181,24 @@ class BookControllerTests {
assert Book.get(book.id) == null
assert response.redirectedUrl == '/book/list'
}
+
+ void testDelete_mockDataIntegrityConstriant() {
+ Book.metaClass.'static'.delete = {Map args->
+ throw new DataIntegrityViolationException('BOOM!')
+ }
+
+ populateValidParams(params)
+ def book = new Book(params)
+
+ assert book.save() != null
+ assert Book.count() == 1
+
+ params.id = book.id
+ controller.delete()
+ assert response.redirectedUrl == "/book/show/$book.id"
+ assert flash.message != null
+
+ }
+
+
}
View
4 BuildingQuality/sample-code/metrics/test/unit/metrics/BookTests.groovy
@@ -12,6 +12,8 @@ import org.junit.*
class BookTests {
void testSomething() {
- fail "Implement me"
+ Book b = new Book(title: 'title')
+ b.save()
+ assert !b.hasErrors()
}
}

0 comments on commit 24930d8

Please sign in to comment.