org.etlunit.feature.file.FileRuntimeSupportImpl Maven / Gradle / Ivy
package org.etlunit.feature.file;
import org.etlunit.RuntimeSupport;
import org.etlunit.io.FileBuilder;
import org.etlunit.io.file.DataFileManager;
import org.etlunit.io.file.DataFileSchema;
import org.etlunit.json.validator.ResourceNotFoundException;
import org.etlunit.parser.ETLTestValueObject;
import org.etlunit.util.IOUtils;
import javax.inject.Inject;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FileRuntimeSupportImpl implements FileRuntimeSupport
{
public static final String DEFAULT = "default";
private RuntimeSupport runtimeSupport;
private Map fileProducerMap = new HashMap();
private DataFileManager dataFileManager;
@Inject
public void receiveRuntimeSupport(RuntimeSupport runtime)
{
runtimeSupport = runtime;
}
@Inject
public void receiveDataFileManager(DataFileManager dfm)
{
dataFileManager = dfm;
}
public File getDataFile(String package_, String name)
{
return new File(getDataFileDir(package_), name);
}
public File getDataFile(String name)
{
return getDataFile(null, name);
}
public File getAssertionFileDir(String package_)
{
return new FileBuilder(runtimeSupport.getSourceDirectory(package_)).subdir("data").mkdirs().file();
}
public File getAssertionFileDir()
{
return getDataFileDir(null);
}
public File getAssertionFile(String package_, String name)
{
return new FileBuilder(getAssertionFileDir(package_)).mkdirs().name(name + ".delimited").file();
}
public File getAssertionFile(String name)
{
return getAssertionFile(null, name);
}
public File getGeneratedDataFile(String package_, String name)
{
FileBuilder fb = new FileBuilder(runtimeSupport.getGeneratedSourceDirectory("file")).subdir("generated-data");
if (package_ != null)
{
fb = fb.subdir(package_);
}
fb.name(name);
return fb.mkdirsToFile().file();
}
public File getGeneratedDataFile(String name)
{
return getGeneratedDataFile(null, name);
}
public File getReferenceFile(String path, String name)
{
return runtimeSupport.getReferenceFile(path, name);
}
public File getReferenceFileSchema(String package_, String name)
{
FileBuilder fb = new FileBuilder(runtimeSupport.getReferenceDirectory("file/fml"));
if (package_ != null)
{
fb = fb.subdir(package_).mkdirs();
}
File sch = fb.name(name + ".fml").file();
return sch;
}
public File getReferenceFileSchema(String name)
{
return getReferenceFileSchema(null, name);
}
public DataFileSchema locateReferenceFileSchema(String package_, String name, String generifiedName, ETLTestValueObject parameters)
{
if (parameters != null)
{
ETLTestValueObject query = parameters.query(AssertOperation.REFERENCEFILETYPE_JSON_NAME);
if (query != null)
{
String valueAsString = query.getValueAsString();
File referenceFileSchema = getReferenceFileSchema(package_, valueAsString);
if (referenceFileSchema.exists())
{
return dataFileManager.loadDataFileSchema(referenceFileSchema);
}
else
{
// this is an error if the explicitly-defined type is not found
return dataFileManager.loadDataFileSchemaFromResource(valueAsString + ".fml");
}
}
}
if (name != null)
{
File referenceFileSchema = getReferenceFileSchema(package_, name);
if (referenceFileSchema.exists())
{
return dataFileManager.loadDataFileSchema(referenceFileSchema);
}
else
{
try
{
return dataFileManager.loadDataFileSchemaFromResource(name + ".fml");
}
catch(ResourceNotFoundException rnfe)
{
// just continue on - this is normal
}
}
}
File referenceFileSchema = getReferenceFileSchema(package_, generifiedName);
if (referenceFileSchema.exists())
{
return dataFileManager.loadDataFileSchema(referenceFileSchema);
}
else
{
return dataFileManager.loadDataFileSchemaFromResource(generifiedName + ".fml");
}
}
public DataFileSchema locateReferenceFileSchema(String name, String generifiedName, ETLTestValueObject parameters)
{
return locateReferenceFileSchema(null, name, generifiedName, parameters);
}
public void persistDataFileSchema(DataFileSchema schema) throws IOException
{
persistDataFileSchema(null, schema);
}
public void persistDataFileSchema(String package_, DataFileSchema schema) throws IOException
{
File file = getReferenceFileSchema(package_, schema.getId());
IOUtils.writeBufferToFile(file, new StringBuffer(schema.toJsonString()));
}
public File getDataFileDir(String package_)
{
return new FileBuilder(runtimeSupport.getSourceDirectory(package_)).subdir("files").mkdirs().file();
}
public File getDataFileDir()
{
return getDataFileDir(null);
}
public void registerFileProducer(FileProducer producer)
{
if (fileProducerMap.containsKey(producer.getName()))
{
throw new IllegalArgumentException("File producer [" + producer.getName() + "] already registered as class [" + fileProducerMap.get(producer.getName()).getClass() + "]");
}
if (fileProducerMap.size() == 0)
{
fileProducerMap.put(DEFAULT, producer);
}
fileProducerMap.put(producer.getName(), producer);
}
public FileProducer getRegisteredProducer(String name)
{
if (name == null)
{
name = DEFAULT;
}
if (!fileProducerMap.containsKey(name))
{
throw new IllegalArgumentException("File producer [" + name + "] not registered.");
}
return fileProducerMap.get(name);
}
public List getReferenceFileSchemaPackages()
{
return runtimeSupport.getReferencePackages("file/fml");
}
public List getReferenceFileSchemasForPackage(String package_)
{
File fmlRef = runtimeSupport.getReferenceDirectory(package_ == null ? "file/fml" : ("file/fml/" + package_));
final List schemas = new ArrayList();
fmlRef.listFiles(new FileFilter()
{
public boolean accept(File pathname)
{
if (pathname.isFile() && IOUtils.fileHasExtension(pathname, "fml"))
{
schemas.add(IOUtils.removeExtension(pathname));
}
return false;
}
});
return schemas;
}
}