Skip to content

Commit

Permalink
Replaced CommonsMultiPartFile reference to generic reference, Test ca…
Browse files Browse the repository at this point in the history
…se modifications. #6
  • Loading branch information
ankit-agrawal11 committed Feb 12, 2017
1 parent e36693c commit 0ccaa98
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import grails.util.Holders
import groovy.io.FileType
import org.springframework.context.MessageSource
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.web.multipart.commons.CommonsMultipartFile
import org.springframework.web.multipart.MultipartFile
import java.nio.channels.FileChannel
import org.apache.commons.validator.UrlValidator
import com.causecode.fileuploader.cdn.amazon.AmazonCDNFileUploaderImpl
Expand Down Expand Up @@ -107,7 +107,7 @@ class FileUploaderService {
*/
tempFile = file
} else {
if (file instanceof CommonsMultipartFile) {
if (file instanceof MultipartFile) {
tempFile = new File(newTemporaryDirectoryPath +
"${fileData.fileName}.${fileData.fileExtension}")

Expand Down Expand Up @@ -182,7 +182,7 @@ class FileUploaderService {
if (file instanceof File) {
file.renameTo(new File(path))
} else {
if (file instanceof CommonsMultipartFile) {
if (file instanceof MultipartFile) {
file.transferTo(new File(path))
}
}
Expand Down
10 changes: 0 additions & 10 deletions src/main/groovy/com/causecode/fileuploader/BaseTestSetup.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
*/
package com.causecode.fileuploader

import org.apache.commons.fileupload.disk.DiskFileItem

/**
* This class contains common setup that can be used in unit, functional and integration test cases.
*
Expand All @@ -24,12 +22,4 @@ trait BaseTestSetup {
file.createNewFile()
file << 'This is a test document.'
}

DiskFileItem getDiskFileItemInstance(File fileInstance) {
DiskFileItem fileItem = new DiskFileItem('file', 'text/plain', false, fileInstance.name,
(int) fileInstance.length() , fileInstance.parentFile)
fileItem.outputStream

return fileItem
}
}
4 changes: 2 additions & 2 deletions src/main/groovy/com/causecode/fileuploader/FileGroup.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package com.causecode.fileuploader
import grails.util.Holders
import groovy.util.logging.Slf4j
import org.springframework.context.MessageSource
import org.springframework.web.multipart.commons.CommonsMultipartFile
import org.springframework.web.multipart.MultipartFile

/**
* A utility class which performs operations on properties of a file.
Expand Down Expand Up @@ -49,7 +49,7 @@ class FileGroup {
receivedFileName = file.name
fileSize = file.size()
} else {
if (file instanceof CommonsMultipartFile) { // Means instance is of Spring's CommonsMultipartFile.
if (file instanceof MultipartFile) { // Means instance is of Spring's MultipartFile.
def uploaderFile = file
contentType = uploaderFile?.contentType
empty = uploaderFile?.isEmpty()
Expand Down
15 changes: 8 additions & 7 deletions src/test/groovy/com/causecode/fileuploader/FileGroupSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ package com.causecode.fileuploader
import grails.buildtestdata.mixin.Build
import grails.test.mixin.TestMixin
import grails.test.mixin.support.GrailsUnitTestMixin
import org.apache.commons.fileupload.disk.DiskFileItem
import org.springframework.context.MessageSource
import org.springframework.context.i18n.LocaleContextHolder
import org.springframework.web.multipart.commons.CommonsMultipartFile
import org.springframework.web.multipart.MultipartFile
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile
import spock.lang.Specification

import javax.servlet.http.Part

/**
* This is Unit test file for FileGroup class.
*/
Expand Down Expand Up @@ -127,16 +129,15 @@ class FileGroupSpec extends Specification implements BaseTestSetup {
e.message == 'file too big'
}

void "test getFileNameAndExtensions method when file belongs to CommonsMultipartFile class"() {
given: 'Instances of CommonsMultipartFile and FileGroup class'
void "test getFileNameAndExtensions method when file belongs to StandardMultipartFile class"() {
given: 'Instances of StandardMultipartFile and FileGroup class'
File fileInstance = getFileInstance('./temp/test.txt')
DiskFileItem fileItem = getDiskFileItemInstance(fileInstance)
CommonsMultipartFile commonsMultipartFileInstance = new CommonsMultipartFile(fileItem)
MultipartFile standardMultipartFile = new StandardMultipartFile(Mock(Part), 'test.txt')

FileGroup fileGroupInstance = new FileGroup('testLocal')

when: 'getFileNameAndExtensions method is called'
Map result = fileGroupInstance.getFileNameAndExtensions(commonsMultipartFileInstance, 'testLocal.txt')
Map result = fileGroupInstance.getFileNameAndExtensions(standardMultipartFile, 'testLocal.txt')

then: 'Method returns a valid map'
result.fileName == 'testLocal.txt'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@
*/
package com.causecode.fileuploader

import com.causecode.fileuploader.cdn.amazon.AmazonCDNFileUploaderImpl
import grails.buildtestdata.mixin.Build
import grails.test.mixin.TestFor
import grails.test.runtime.DirtiesRuntime
import grails.util.Holders
import groovy.json.JsonBuilder
import org.apache.commons.fileupload.disk.DiskFileItem
import org.apache.commons.validator.UrlValidator
import org.grails.plugins.codecs.HTMLCodec
import org.springframework.context.MessageSource
import org.springframework.context.i18n.LocaleContextHolder
import org.springframework.web.multipart.commons.CommonsMultipartFile
import org.springframework.web.multipart.MultipartFile
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile
import spock.lang.Unroll
import spock.util.mop.ConfineMetaClassChanges

import javax.servlet.http.Part

/**
* This file contains unit test cases for FileUploaderService class.
*/
Expand Down Expand Up @@ -572,9 +573,7 @@ class FileUploaderServiceSpec extends BaseFileUploaderServiceSpecSetup {
void "test saveFile method for various cases"() {
given: 'An instance of File'
File fileInstance = getFileInstance('./temp/test.txt')

DiskFileItem fileItem = getDiskFileItemInstance(fileInstance)
CommonsMultipartFile commonsMultipartFileInstance = new CommonsMultipartFile(fileItem)
MultipartFile standardMultipartFile = new StandardMultipartFile(Mock(Part), 'test.txt')

and: 'Mocked methods'
mockFileGroupConstructor('CDN')
Expand All @@ -591,15 +590,15 @@ class FileUploaderServiceSpec extends BaseFileUploaderServiceSpecSetup {

when: 'saveFile is called and provider is not specified'
mockGetFileNameAndExtensions()
service.saveFile('testGoogle', commonsMultipartFileInstance, 'test')
service.saveFile('testGoogle', standardMultipartFile, 'test')

then: 'Method should throw StorageConfigurationException'
StorageConfigurationException e = thrown()
e.message == 'Provider not defined in the Config. Please define one.'

when: 'saveFile method is hit'
mockUploadFileMethod(true)
def result = service.saveFile('testGoogle', commonsMultipartFileInstance, 'test')
def result = service.saveFile('testGoogle', standardMultipartFile, 'test')

then: 'Method should return instance of UFile'
result.fileGroup == 'testGoogle'
Expand All @@ -612,9 +611,7 @@ class FileUploaderServiceSpec extends BaseFileUploaderServiceSpecSetup {
void "test saveFile when provider is LOCAL"() {
given: 'File instance'
File fileInstance = getFileInstance('./temp/test.txt')

DiskFileItem fileItem = getDiskFileItemInstance(fileInstance)
CommonsMultipartFile commonsMultipartFileInstance = new CommonsMultipartFile(fileItem)
MultipartFile standardMultipartFile = new StandardMultipartFile(Mock(Part), 'test.txt')

and: 'Mocked methods'
mockFileGroupConstructor('LOCAL')
Expand All @@ -635,7 +632,7 @@ class FileUploaderServiceSpec extends BaseFileUploaderServiceSpecSetup {
result.id != null

when: 'saveFile method is called and error occurs while saving file'
result = service.saveFile('testLocal', commonsMultipartFileInstance, 'test')
result = service.saveFile('testLocal', standardMultipartFile, 'test')

then: 'File would not be saved'
result.id == null
Expand Down

0 comments on commit 0ccaa98

Please sign in to comment.