|
package de.andreasschmitt.export.exporter |
|
|
|
import grails.test.mixin.TestFor |
|
import org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletResponse |
|
import org.junit.Test |
|
|
|
import static junit.framework.Assert.assertEquals |
|
|
|
class ExportBuilderTests { |
|
|
|
def builder = new ExportBuilder() |
|
def mockResponse = new GrailsMockHttpServletResponse() |
|
def mockObjects = ['test', 'data'] |
|
def mockOutputStream = new OutputStream() { |
|
@Override |
|
void write(int i) { |
|
} |
|
} |
|
|
|
@Test(expected = ExportingException) |
|
void testNoInput() { |
|
builder.build {} |
|
} |
|
|
|
@Test(expected = ExportingException) |
|
void testNoOut() { |
|
builder.build { |
|
format 'xls' |
|
objects mockObjects |
|
} |
|
} |
|
|
|
@Test(expected = ExportingException) |
|
void testNoObjects() { |
|
builder.build { |
|
format 'xls' |
|
out mockOutputStream |
|
} |
|
} |
|
|
|
@Test(expected = ExportingException) |
|
void testResponseWithoutFileInfo() { |
|
builder.build { |
|
format 'xls' |
|
objects mockObjects |
|
out mockResponse |
|
} |
|
} |
|
|
|
@Test(expected = ExportingException) |
|
void testResponseWithoutFilename() { |
|
builder.build { |
|
format 'xls' |
|
objects mockObjects |
|
out mockResponse |
|
extension 'xls' |
|
} |
|
} |
|
|
|
@Test(expected = ExportingException) |
|
void testResponseWithoutExtension() { |
|
builder.build { |
|
format 'xls' |
|
objects mockObjects |
|
out mockResponse |
|
filename 'test' |
|
} |
|
} |
|
|
|
@Test |
|
void testResponse() { |
|
builder.build { |
|
format 'xls' |
|
objects mockObjects |
|
out mockResponse |
|
filename 'test' |
|
extension 'xls' |
|
} |
|
|
|
assertEquals("Format should be xls", 'xls', builder.format) |
|
assertEquals("out should be mockResponse", mockResponse, builder.out) |
|
assertEquals("filename should be test", 'test', builder.filename) |
|
assertEquals("extension should be xls", 'xls', builder.extension) |
|
assertEquals("objects should be mockObjects", mockObjects, builder.objects) |
|
} |
|
|
|
@Test |
|
void testOutputStream() { |
|
builder.build { |
|
format 'xls' |
|
objects mockObjects |
|
out mockOutputStream |
|
} |
|
|
|
assertEquals("Format should be xls", 'xls', builder.format) |
|
assertEquals("out should be mockOutputStream", mockOutputStream, builder.out) |
|
assertEquals("objects should be mockObjects", mockObjects, builder.objects) |
|
} |
|
|
|
@Test |
|
void testParameters() { |
|
def testMap = [ |
|
test: 'parameters', |
|
doesIt: 'work?' |
|
] |
|
builder.build { |
|
format 'xls' |
|
objects mockObjects |
|
out mockOutputStream |
|
parameters testMap |
|
} |
|
|
|
assertEquals("parameters should be testMap", testMap, builder.parameters) |
|
} |
|
|
|
@Test |
|
void testFields() { |
|
def nameLabel = 'Name', |
|
nameFormatter = {obj, val -> obj?.name}, |
|
abbrLabel = 'Abbr', |
|
abbrFormatter = {obj, val -> obj?.abbreviation}, |
|
otherNamesLabel = 'Other Names', |
|
otherNamesFormatter = {obj, val -> obj?.otherNames} |
|
|
|
builder.build { |
|
format 'xls' |
|
objects mockObjects |
|
out mockOutputStream |
|
fields { |
|
name(nameLabel, nameFormatter) |
|
abbreviation(abbrLabel, abbrFormatter) |
|
otherNames(otherNamesLabel, otherNamesFormatter) |
|
} |
|
} |
|
|
|
assertEquals("fields should be correct", builder.fields, [ |
|
'name', |
|
'abbreviation', |
|
'otherNames' |
|
]) |
|
|
|
assertEquals("labels should be correct", builder.labels, [ |
|
name: nameLabel, |
|
abbreviation: abbrLabel, |
|
otherNames: otherNamesLabel |
|
]) |
|
|
|
assertEquals("formatters should be correct", builder.formatters, [ |
|
name: nameFormatter, |
|
abbreviation: abbrFormatter, |
|
otherNames: otherNamesFormatter |
|
]) |
|
} |
|
|
|
@Test(expected=ExportingException) |
|
void testFormattersWithNotEnoughArgs() { |
|
def nameLabel = 'Name', |
|
nameFormatter = {obj -> obj?.name} |
|
|
|
builder.build { |
|
format 'xls' |
|
objects mockObjects |
|
out mockOutputStream |
|
fields { |
|
name(nameLabel, nameFormatter) |
|
} |
|
} |
|
} |
|
|
|
} |