Skip to content

Commit

Permalink
Added CsvTestControllerSpec and also moved resources file to src/main…
Browse files Browse the repository at this point in the history
… resources folder and fixed test files path.
  • Loading branch information
sachin-verma committed Jul 9, 2015
1 parent c240dbc commit fa6608d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ package org.grails.plugins.csv

class CsvTestController {

static scope = "request"

def index() {
println "---------------------"
println grailsApplication.mainContext['org.grails.plugins.csv.CsvTestController']
render ("Welcome to Grails CSV"+ grailsApplication.config.spring.groovy.template)
}

def writeCsv = {
def rows = []
rows << [1,2]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CSVMapReaderIntTest extends GroovyTestCase {

@Test
void testFileToCsvMapReader() {
def recs = new File("resources/mapTest.csv").toCsvMapReader().toList()
def recs = new File(this.class.classLoader.getResource('mapTest.csv').path).toCsvMapReader().toList()
assert recs.size() == 2
assertEquals([col1: 'val1', col2: 'val2', col3: 'val3'], recs[0])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ class CSVReaderUtilsIntegrationTest extends GroovyTestCase {

@Test
void testFileEachCsvLine() {
//
//Just to clarify it is sufficient to
// write
// this.class.getClassLoader().getResourceAsStream("my_xsl.xsl")
// if my_xsl.xsl is located at src/main/resource/my_xsl.xsl – ubiquibacon Jun 1 at 18:56
def file = new File("resources/fileTest.csv")
def file = new File(this.class.classLoader.getResource('fileTest.csv').path)
assertNotNull(file)
assertTrue(file.exists())
file.eachCsvLine { tokens ->
Expand All @@ -48,7 +43,7 @@ class CSVReaderUtilsIntegrationTest extends GroovyTestCase {

@Test
void testFileEachCsvLineWithSettings() {
def file = new File("resources/fileTest.csv")
def file = new File(this.class.classLoader.getResource('fileTest.csv').path)
assertNotNull(file)
assertTrue(file.exists())

Expand All @@ -59,7 +54,7 @@ class CSVReaderUtilsIntegrationTest extends GroovyTestCase {

@Test
void testInputStreamEachCsvLine() {
def is = new File("resources/fileTest.csv").newInputStream()
def is = this.class.classLoader.getResourceAsStream('fileTest.csv')
assertNotNull(is)
is.eachCsvLine { tokens ->
assertEquals(4, tokens.length)
Expand All @@ -68,7 +63,7 @@ class CSVReaderUtilsIntegrationTest extends GroovyTestCase {

@Test
void testInputStreamEachCsvLineWithSettings() {
def is = new File("resources/fileTest.csv").newInputStream()
def is = this.class.classLoader.getResourceAsStream('fileTest.csv')
assertNotNull(is)
is.toCsvReader(['charset': 'UTF-8']).eachLine { tokens ->
assertEquals(4, tokens.length)
Expand All @@ -77,15 +72,15 @@ class CSVReaderUtilsIntegrationTest extends GroovyTestCase {

@Test
void testReaderEachCsvLine() {
def reader = new FileReader(new File("resources/fileTest.csv"))
def reader = new FileReader(new File(this.class.classLoader.getResource('fileTest.csv').path))
reader.eachCsvLine { tokens ->
assertEquals(4, tokens.length)
}
}

@Test
void testReaderEachCsvLineWithSettings() {
def reader = new FileReader(new File("resources/fileTest.csv"))
def reader = new FileReader(new File(this.class.classLoader.getResource('fileTest.csv').path))
reader.toCsvReader(['charset': 'UTF-8']).eachLine { tokens ->
assertEquals(4, tokens.length)
}
Expand All @@ -100,6 +95,7 @@ class CSVReaderUtilsIntegrationTest extends GroovyTestCase {
}
}

@Test
void testStringEachCsvLineWithSettings() {
String csv = 'testx using "x" as ax separator char'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.grails.plugins.csv

import grails.test.mixin.TestFor
import spock.lang.Specification


@TestFor(CsvTestController)
class CsvTestControllerSpec extends Specification {

def populateValidParams(params) {
assert params != null
}

void "test render as csv"() {

when: "The writeCsv action is executed without ant params"
controller.writeCsv()

then: "The response is correct"
response.contentType == 'text/csv'
response.contentAsString == '"x","y"\n"1","2"\n"3","4"\n"5","6"'
!response.getHeader('Content-Disposition')
}


void "test render as csv as attachments"() {

when: "The writeCsv action is executed with params containing filename"
params.filename = 'my.csv'
controller.writeCsv()

then: "The response is correct"
response.getHeader('Content-Disposition') == 'attachment; filename="my.csv";'
}

void "test render as csv as inline"() {

when: "The writeCsv action is executed with params containing filename and attachment as false"
params.filename = 'my.csv'
params.attachment = false
controller.writeCsv()

then: "The response is correct"
response.getHeader('Content-Disposition') == 'inline; filename="my.csv";'
}

}
File renamed without changes.
File renamed without changes.

0 comments on commit fa6608d

Please sign in to comment.