001 package com.mockrunner.jdbc; 002 003 import java.io.File; 004 import java.util.List; 005 006 import com.mockrunner.mock.jdbc.MockResultSet; 007 import com.mockrunner.util.common.FileUtil; 008 import com.mockrunner.util.common.StringUtil; 009 010 /** 011 * Can be used to create a <code>ResultSet</code> based on 012 * a table specified in a CSV file. You can specify the delimiter 013 * of the columns (default is <i>";"</i>). Furthermore you can specify if the first line 014 * contains the column names (default is <code>false</code>) and if 015 * the column entries should be trimmed (default is <code>true</code>). 016 * The file can be specified directly or by its name. The class 017 * tries to find the file in the absolut or relative path and 018 * (if not found) by calling <code>getResource</code>. Note that the 019 * file must exist in the local file system and cannot be loaded from 020 * inside a jar archive. 021 */ 022 public class FileResultSetFactory implements ResultSetFactory 023 { 024 private File file = null; 025 private String fileName = null; 026 private String delimiter = ";"; 027 private boolean firstLineContainsColumnNames = false; 028 private boolean trim = true; 029 030 public FileResultSetFactory(String fileName) 031 { 032 this.file = new File(fileName); 033 this.fileName = fileName; 034 } 035 036 public FileResultSetFactory(File file) 037 { 038 this.file = file; 039 this.fileName = file.getAbsolutePath(); 040 } 041 042 /** 043 * Get the <code>File</code> being used to read in the 044 * <code>ResultSet</code>. Returns <code>null</code> if 045 * the file does not exist. 046 * @return the file 047 */ 048 public File getFile() 049 { 050 if(file.exists() && file.isFile()) 051 { 052 return file; 053 } 054 else 055 { 056 return FileUtil.findFile(fileName); 057 } 058 } 059 060 /** 061 * Set the delimiter. Default is <i>";"</i>. 062 * @param delimiter the delimiter 063 */ 064 public void setDelimiter(String delimiter) 065 { 066 this.delimiter = delimiter; 067 } 068 069 /** 070 * Set if the first line contains the column names. 071 * Default is <code>false</code>. 072 */ 073 public void setFirstLineContainsColumnNames(boolean firstLineContainsColumnNames) 074 { 075 this.firstLineContainsColumnNames = firstLineContainsColumnNames; 076 } 077 078 /** 079 * Set if the column entries should be trimmed. 080 * Default is <code>true</code>. 081 */ 082 public void setTrim(boolean trim) 083 { 084 this.trim = trim; 085 } 086 087 public MockResultSet create(String id) 088 { 089 MockResultSet resultSet = new MockResultSet(id); 090 List lines = null; 091 File fileToRead = getFile(); 092 if(null == fileToRead) 093 { 094 throw new RuntimeException("File " + fileName + " not found."); 095 } 096 lines = FileUtil.getLinesFromFile(fileToRead); 097 int firstLineNumber = 0; 098 if(firstLineContainsColumnNames) 099 { 100 String firstLine = (String)lines.get(firstLineNumber); 101 firstLineNumber++; 102 String[] names = StringUtil.split(firstLine, delimiter, trim); 103 for(int ii = 0; ii < names.length; ii++) 104 { 105 resultSet.addColumn(names[ii]); 106 } 107 } 108 for(int ii = firstLineNumber; ii < lines.size(); ii++) 109 { 110 String line = (String)lines.get(ii); 111 String[] values = StringUtil.split(line, delimiter, trim); 112 resultSet.addRow(values); 113 } 114 return resultSet; 115 } 116 }