Spring Boot ã§æ¸ç±ã®è²¸åºç¶æ³ç¢ºèªã»è²¸åºç³è«ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ï¼ )( 貸åºå¸ææ¸ç± CSV ãã¡ã¤ã«ã¢ãããã¼ãç»é¢ã®ä½æï¼ )
æ¦è¦
- ä»åã®æé ã§ç¢ºèªã§ããã®ã¯ä»¥ä¸ã®å
容ã§ãã
- 貸åºå¸ææ¸ç± CSV ãã¡ã¤ã«ã¢ãããã¼ãç»é¢ã®ä½æ
- ãã¹ããä½æãã¾ãã
- 貸åºå¸ææ¸ç± CSV ãã¡ã¤ã«ã¢ãããã¼ãç»é¢ã®ä½æ
åç §ãããµã¤ãã»æ¸ç±
-
How to diff files/folders in Gradle?
http://stackoverflow.com/questions/30401436/how-to-diff-files-folders-in-gradle -
comparedirs.groovy
https://gist.github.com/igormukhin/71d780c4274336eeb297 Javaéçºã®å¼·åãªç¸æ£ã¨ãã¦ä»ãã使ããGroovy - SlideShare
http://www.slideshare.net/nobeans/javagroovyUsing Spring MVC Test to unit test multipart POST request
http://stackoverflow.com/questions/21800726/using-spring-mvc-test-to-unit-test-multipart-post-request- MultipartFile ã¯ã©ã¹ã使ç¨ãããã¹ããããããã®æ¹æ³ãåç §ãã¾ãããMockMultipartFile ã§ã¢ãã¯ãä½æãã¦ãã¹ããã¾ãã
Java Code Examples for org.springframework.validation.BeanPropertyBindingResult
http://www.programcreek.com/java-api-examples/index.php?api=org.springframework.validation.BeanPropertyBindingResult- Errors ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®ä½ãªãã¸ã§ã¯ãã MapBindingResult ã¯ã©ã¹ã§çæããæ¹æ³ãåèã«ãã¾ããã
ç®æ¬¡
- ããã¯ã¢ããããªã¹ãã¢å¯¾è±¡ã®ãã¼ãã«ã追å ãããã¹ããã¼ã¿ãç¨æãã
- ãã¹ãæªä½æã®ã¯ã©ã¹ä¸è¦§ãåºåãã Gradle ã¿ã¹ã¯ãä½æãã
- ãã¹ãä½æ対象ã®ã¯ã©ã¹ã決ãã
- MessagesPropertiesHelper ã¯ã©ã¹ã®ãã¹ãã®ä½æ
- BooklistCsvFileService ã¯ã©ã¹ã®ãã¹ãã®ä½æ
- 次åã¯ããã
æé
ããã¯ã¢ããããªã¹ãã¢å¯¾è±¡ã®ãã¼ãã«ã追å ãããã¹ããã¼ã¿ãç¨æãã
lending_app, lending_book ãã¼ãã«ãããã¯ã¢ããããªã¹ãã¢ã®å¯¾è±¡ã«ãã¾ããsrc/test/java/ksbysample/common/test ã®ä¸ã® TestDataResource.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããã¼ã¿ãç¨æãã¾ããlending_app, lending_book ã®åæãã¼ã¿ã¯ç©ºã«ãã¾ããsrc/test/resources/testdata/base ã®ä¸ã« lending_app.csv, lending_book.csv ãä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ãæªä½æã®ã¯ã©ã¹ä¸è¦§ãåºåãã Gradle ã¿ã¹ã¯ãä½æãã
ãã¹ãæªä½æã®ã¯ã©ã¹ä¸è¦§ãåºåãã¦ãããæ©è½ã欲ãããªã£ãã®ã§ãGradle ã¿ã¹ã¯ã§å®è£ ãããã¨ã«ãã¾ããbuild.gradle ã ãªã³ã¯å ã®ãã®ï¼ããã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
ä½æãã printClassWhatNotMakeTest ã¿ã¹ã¯ã¯ä»¥ä¸ã®ä»æ§ã§ãã
- src/main/java 㨠src/test/java ã®ä¸ã® .java ãã¡ã¤ã«ãæ¯è¼ãããã¹ãã¯ã©ã¹ ( ï½Test.java ) ãä½æããã¦ããªã .java ãã¡ã¤ã«ä¸è¦§ãã³ã³ã½ã¼ã«ã«åºåãã¾ãã
- ã¿ã¹ã¯å ã®å¤æ° excludePaths ã«åºå対象å¤ã«ãã Path ãè¨å®ã§ãã¾ãã
- ã¿ã¹ã¯å ã®å¤æ° excludeFileNamePatterns ã«åºå対象å¤ã«ãããã¡ã¤ã«åãè¨å®ã§ãã¾ãã
Gradle projects View ã®å·¦ä¸ã«ãããRefresh all Gradle projectsããã¿ã³ãã¯ãªãã¯ãã¦æ´æ°ãã¾ãã
ãã¹ãä½æ対象ã®ã¯ã©ã¹ã決ãã
Gradle projects View ãã printClassWhatNotMakeTest ã¿ã¹ã¯ãå®è¡ãã¾ãã
åºåãããã¯ã©ã¹ã®å ã以ä¸ã®ã¯ã©ã¹ã®ãã¹ããä½æãããã¨ã«ãã¾ãã
MessagesPropertiesHelper ã¯ã©ã¹ã®ãã¹ãã®ä½æ
src/main/java/ksbysample/webapp/lending/helper/message ã®ä¸ã® MessagesPropertiesHelper.java ã§ãCreate Testããã¤ã¢ãã°ã表示ãããã¹ãã¯ã©ã¹ãä½æãã¾ãã
src/test/java/ksbysample/webapp/lending/helper/message ã®ä¸ã« MessagesPropertiesHelperTest.java ãä½æããã¾ãã
src/test/java/ksbysample/webapp/lending/helper/message ã®ä¸ã® MessagesPropertiesHelperTest.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è¡ãã¾ããMessagesPropertiesHelperTest ã¯ã©ã¹ã®ã¯ã©ã¹åã«ã«ã¼ã½ã«ã移åããã³ã³ããã¹ãã¡ãã¥ã¼ã表示å¾ãRun 'MessagesPropertiesHelperTest' with Coverageããé¸æãã¾ãã
ãã¹ããæåãããã¨ã確èªã§ãã¾ãã
BooklistCsvFileService ã¯ã©ã¹ã®ãã¹ãã®ä½æ
src/main/java/ksbysample/webapp/lending/service/file ã®ä¸ã® BooklistCsvFileService.java ã§ãCreate Testããã¤ã¢ãã°ã表示ãããã¹ãã¯ã©ã¹ãä½æãã¾ãã
src/test/java/ksbysample/webapp/lending/service/file ã®ä¸ã« BooklistCsvFileServiceTest.java ãä½æããã¾ãã
ãã¹ãã®ããã«ã³ã³ã¹ãã©ã¯ã¿ãå¿ è¦ãªã®ã§è¿½å ãã¾ããsrc/main/java/ksbysample/webapp/lending/service/file ã®ä¸ã® BooklistCSVRecord.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/test/java/ksbysample/webapp/lending/service/file ã®ä¸ã® BooklistCsvFileServiceTest.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è¡ãã¾ããBooklistCsvFileServiceTest ã¯ã©ã¹ã®ã¯ã©ã¹åã«ã«ã¼ã½ã«ã移åããã³ã³ããã¹ãã¡ãã¥ã¼ã表示å¾ãRun 'BooklistCsvFileServiceTest' with Coverageããé¸æãã¾ãã
ãã¹ããæåãããã¨ã確èªã§ãã¾ãã
次åã¯ããã
ãã¹ãã®ä½æãç¶ãã¾ãã
ã½ã¼ã¹ã³ã¼ã
TestDataResource.java
Component public class TestDataResource extends ExternalResource { private final String TESTDATA_DIR = "src/test/resources/testdata/base"; private final String BACKUP_FILE_NAME = "ksbylending_backup"; private final List<String> BACKUP_TABLES = Arrays.asList( "user_info" , "user_role" , "library_forsearch" , "lending_app" , "lending_book" );
- BACKUP_TABLES ã®é åã« "lending_app", "lending_book" ã追å ãã¾ãã
lending_app.csv, lending_book.csv
â lending_app.csv
lending_app_id,status,lending_user_id,approval_user_id
â lending_book.csv
lending_book_id,lending_app_id,isbn,book_name,lending_state,lending_app_flg,lending_app_reason,approval_result,approval_reason
build.gradle
â ãã®ï¼
task downloadCssFontsJs << { ..... } task printClassWhatTestNotMake << { def srcDir = new File("src/main/java"); def excludePaths = [ "src/main/java/ksbysample/webapp/lending/Application.java" , "src/main/java/ksbysample/webapp/lending/config" , "src/main/java/ksbysample/webapp/lending/dao" , "src/main/java/ksbysample/webapp/lending/entity" , "src/main/java/ksbysample/webapp/lending/exception" , "src/main/java/ksbysample/webapp/lending/helper/page/PagenationHelper.java" , "src/main/java/ksbysample/webapp/lending/security/LendingUser.java" , "src/main/java/ksbysample/webapp/lending/security/RoleAwareAuthenticationSuccessHandler.java" , "src/main/java/ksbysample/webapp/lending/service/calilapi/Librar" , "src/main/java/ksbysample/webapp/lending/service/file/BooklistCSVRecord.java" , "src/main/java/ksbysample/webapp/lending/service/openweathermapapi" , "src/main/java/ksbysample/webapp/lending/service/queue/InquiringStatusOfBookQueueMessage.java" , "src/main/java/ksbysample/webapp/lending/util/doma" , "src/main/java/ksbysample/webapp/lending/util/velocity/VelocityUtils.java" , "src/main/java/ksbysample/webapp/lending/webapi/common/CommonWebApiResponse.java" , "src/main/java/ksbysample/webapp/lending/webapi/weather" ]; def excludeFileNamePatterns = [ ".*EventListener.java" , ".*Form.java" , ".*Values.java" ]; compareSrcAndTestDir(srcDir, excludePaths, excludeFileNamePatterns); }
- downloadCssFontsJs ã¿ã¹ã¯ã®ä¸ã« printClassWhatTestNotMake ã¿ã¹ã¯ã追å ãã¾ãã
â ãã®ï¼
void downloadBootstrapFileInputMinJs(String workDirPath, String staticDirPath) { ..... } def compareSrcAndTestDir(srcDir, excludePaths, excludeFileNamePatterns) { def existFlg; for (srcFile in srcDir.listFiles()) { String srcFilePath = (srcFile.toPath() as String).replaceAll("\\\\", "/"); existFlg = false; for (exclude in excludePaths) { if (srcFilePath =~ /^${exclude as String}/) { existFlg = true; break; } } if (existFlg == true) continue; for (exclude in excludeFileNamePatterns) { if (srcFilePath =~ /${exclude as String}/) { existFlg = true break; } } if (existFlg == true) continue; if (srcFile.isDirectory()) { compareSrcAndTestDir(srcFile, excludePaths, excludeFileNamePatterns); } else { String testFilePath = srcFilePath.replaceFirst(/^src\/main\/java/, "src/test/java").replaceFirst(/\.java$/, "Test.java"); def testFile = new File(testFilePath); if (!testFile.exists()) { println(srcFilePath); } } } }
- downloadBootstrapFileInputMinJs é¢æ°ã®ä¸ã« compareSrcAndTestDiré¢æ°ã追å ãã¾ãã
MessagesPropertiesHelperTest.java
package ksbysample.webapp.lending.helper.message; import ksbysample.webapp.lending.Application; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public class MessagesPropertiesHelperTest { @Autowired private MessagesPropertiesHelper messagesPropertiesHelper; @Test public void testGetMessage_NoArgs() throws Exception { String message = messagesPropertiesHelper.getMessage("AbstractUserDetailsAuthenticationProvider.locked" , null); assertThat(message).isEqualTo("å ¥åããã ID ã¯ããã¯ããã¦ãã¾ã"); } @Test public void testGetMessage_Args() throws Exception { int line = 1; int length = 3; String message = messagesPropertiesHelper.getMessage("UploadBooklistForm.fileupload.lengtherr" , new Object[]{line, length}); assertThat(message).isEqualTo("1è¡ç®ã®ã¬ã³ã¼ãã®é ç®æ°ã 2åã§ã¯ããã¾ãã ( 3å )ã"); } }
BooklistCSVRecord.java
package ksbysample.webapp.lending.service.file; import com.univocity.parsers.annotations.Parsed; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class BooklistCSVRecord { @Parsed private String isbn; @Parsed(field = "æ¸å") private String bookName; }
- lombok ã® @NoArgsConstructor, @AllArgsConstructor ã¢ããã¼ã·ã§ã³ãä»å ãã¾ãã
BooklistCsvFileServiceTest.java
package ksbysample.webapp.lending.service.file; import com.univocity.parsers.csv.CsvWriter; import com.univocity.parsers.csv.CsvWriterSettings; import ksbysample.webapp.lending.Application; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.validation.Errors; import org.springframework.validation.MapBindingResult; import org.springframework.validation.ObjectError; import java.io.BufferedWriter; import java.io.InputStream; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.util.HashMap; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public class BooklistCsvFileServiceTest { @Autowired private BooklistCsvFileService booklistCsvFileService; @Test public void testValidateUploadFile_NoErrorCsvFile() throws Exception { MockMultipartFile multipartFile = createNoErrorCsvFile(); Errors errors = new MapBindingResult(new HashMap<String, String>(), ""); booklistCsvFileService.validateUploadFile(multipartFile, errors); assertThat(errors.hasErrors()).isFalse(); } @Test public void testValidateUploadFile_ErrorCsvFile() throws Exception { MockMultipartFile multipartFile = createErrorCsvFile(); Errors errors = new MapBindingResult(new HashMap<String, String>(), ""); booklistCsvFileService.validateUploadFile(multipartFile, errors); assertThat(errors.hasErrors()).isTrue(); assertThat(errors.getErrorCount()).isEqualTo(6); assertThat(errors.getAllErrors()) .contains(new ObjectError("", new String[]{"UploadBooklistForm.fileupload.lengtherr"}, new Object[]{2, 3}, null)) .contains(new ObjectError("", new String[]{"UploadBooklistForm.fileupload.isbn.patternerr"}, new Object[]{3, "978-4-7741-5x77-3"}, null)) .contains(new ObjectError("", new String[]{"UploadBooklistForm.fileupload.isbn.lengtherr"}, new Object[]{4, "978-4-79173-8014-9"}, null)) .contains(new ObjectError("", new String[]{"UploadBooklistForm.fileupload.isbn.numlengtherr"}, new Object[]{4, "97847917380149"}, null)) .contains(new ObjectError("", new String[]{"UploadBooklistForm.fileupload.isbn.numlengtherr"}, new Object[]{5, "97847197347784"}, null)) .contains(new ObjectError("", new String[]{"UploadBooklistForm.fileupload.bookname.lengtherr"}, new Object[]{6, "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"}, null)); } @Test public void testConvertFileToList() throws Exception { MockMultipartFile multipartFile = createNoErrorCsvFile(); List<BooklistCSVRecord> booklistCSVRecordList = booklistCsvFileService.convertFileToList(multipartFile); assertThat(booklistCSVRecordList).hasSize(5); assertThat(booklistCSVRecordList).contains(new BooklistCSVRecord("978-4-7741-5377-3", "JUnitå®è·µå ¥é")); } private MockMultipartFile createNoErrorCsvFile() throws Exception { Path path = Files.createTempFile("ãã¹ã", "csv"); try (BufferedWriter bw = Files.newBufferedWriter(path, Charset.forName("Windows-31J"))) { CsvWriterSettings settings = new CsvWriterSettings(); settings.setQuoteAllFields(true); CsvWriter writer = new CsvWriter(bw, settings); writer.writeHeaders("ISBN", "æ¸å"); writer.writeRow("978-4-7741-6366-6", "GitHubå®è·µå ¥é"); writer.writeRow("978-4-7741-5377-3", "JUnitå®è·µå ¥é"); writer.writeRow("978-4-7973-8014-9", "Javaæå¼·ãªãã¡ã¬ã³ã¹"); writer.writeRow("978-4-7973-4778-4", "ã¢ã¸ã£ã¤ã«ã½ããã¦ã§ã¢éçºã®å¥¥ç¾©"); writer.writeRow("978-4-87311-704-1", "Javaã«ããé¢æ°åããã°ã©ãã³ã°"); writer.close(); } MockMultipartFile multipartFile; try (InputStream is = Files.newInputStream(path)) { multipartFile = new MockMultipartFile("ãã¹ã.csv", is); } return multipartFile; } private MockMultipartFile createErrorCsvFile() throws Exception { Path path = Files.createTempFile("ãã¹ãï¼", "csv"); try (BufferedWriter bw = Files.newBufferedWriter(path, Charset.forName("Windows-31J"))) { CsvWriterSettings settings = new CsvWriterSettings(); settings.setQuoteAllFields(true); CsvWriter writer = new CsvWriter(bw, settings); writer.writeHeaders("ISBN", "æ¸å"); writer.writeRow("978-4-7741-6366-6", "GitHubå®è·µå ¥é", "é ç®ãï¼ã¤"); writer.writeRow("978-4-7741-5x77-3", "JUnitå®è·µå ¥é"); writer.writeRow("978-4-79173-8014-9", "Javaæå¼·ãªãã¡ã¬ã³ã¹"); writer.writeRow("978-4-719734778-4", "ã¢ã¸ã£ã¤ã«ã½ããã¦ã§ã¢éçºã®å¥¥ç¾©"); writer.writeRow("978-4-87311-704-1", "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"); writer.close(); } MockMultipartFile multipartFile; try (InputStream is = Files.newInputStream(path)) { multipartFile = new MockMultipartFile("ãã¹ãï¼.csv", is); } return multipartFile; } }
- ãã¹ãã®ãã¤ã³ãã以ä¸ã«è¨è¼ãã¾ãã
- ãã¹ãç¨ã® MultipartFile ã®ã¤ã³ã¹ã¿ã³ã¹ã¯ org.springframework.mock.web.MockMultipartFile ãå©ç¨ãã¦çæãã¾ãã
- CSV ãã¡ã¤ã«ã¯ Files.createTempFile 㧠Path ãªãã¸ã§ã¯ããåå¾ããå¾ãFiles.newBufferedWriter 㨠uniVocity-parsers ãå©ç¨ãã¦ä½æãã¾ããä½æå¾ãFiles.newInputStream 㧠MultipartFile ã®ã¤ã³ã¹ã¿ã³ã¹ã«èªã¿è¾¼ã¿ã¾ãã
- Errors ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã¯ MapBindingResult ã¯ã©ã¹ã§çæãã¾ãã第ï¼å¼æ°ã¯ç©ºæååã§æ§ãã¾ãããæååãæå®ããå ´åã«ã¯ããã®å¾ã® new ObjectError ã®ç¬¬ï¼å¼æ°ã«åãæååãæå®ãã¾ãã
å±¥æ´
2015/10/08
åççºè¡ã