This is a simple java library to enable loading of properties from files either on the filesystem or from the classpath in fewer lines of code.
To use the library you will need to add a dependency to your project. In gradle you would do this by adding the following to your build.gradle file:
dependencies {
compile "com.github.michaelruocco:file-loader:{latest-version}"
}
Once you have the jar on your classpath you can load your properties from the file system by doing the following:
String path = "folder/file-system.properties";
PropertyLoader propertyLoader = new FileSystemPropertyLoader();
Properties properties = propertyLoader.load(path);
Alternatively you can load a property file from the classpath by doing:
String path = "package/classpath.properties";
PropertyLoader propertyLoader = new ClasspathPropertyLoader();
Properties properties = propertyLoader.load(path);
If the properties are not loaded the code will throw a PropertiesNotFoundException but you are not forced to handle this if you do not want to.
Additionally, if you are using this library from test code, and you are not concerned with mocking the loader there are also static helper method that can be used to load the properties in a single line, so the above would be shortened to:
Properties properties = FileLoader.loadPropertiesFromFileSystem("folder/file-system.properties");
or:
Properties properties = FileLoader.loadPropertiesFromClasspath("package/classpath.properties");
You can also use the library to load file content as well as properties, again this can be done either from the local file system or the classpath in the same way as described for properties above
To load from the file system you would do:
String path = "folder/file-system.txt";
FileContentLoader loader = new FileSystemFileContentLoader();
String content = loader.loadContent(path);
To load from the classpath you would do:
String path = "package/classpath.txt";
FileContentLoader loader = new ClasspathFileContentLoader();
String content = loader.loadContent(path);
If the file content is not loaded the code will throw a FileContentLoadException but you are not forced to handle this if you do not want to.
Additionally, if you are using this library from test code, and you are not concerned with mocking the loader there are also static helper method that can be used to load the properties in a single line, so the above would be shortened to:
String content = FileLoader.loadContentFromFileSystem("folder/file-system.txt");
or:
String content = FileLoader.loadContentFromClasspath("package/classpath.txt");
You can also use the library to load file lines as well as properties, again this can be done either from the local file system or the classpath in the same way as described for properties above
To load from the file system you would do:
String path = "folder/file-system.txt";
FileLineLoader loader = new FileSystemFileLineLoader();
Collection<String> lines = loader.loadLines(path);
To load from the classpath you would do:
String path = "package/classpath.txt";
FileLineLoader loader = new ClasspathFileLineLoader();
Collection<String> lines = loader.loadLines(path);
If the file content is not loaded the code will throw a FileContentLoadException but you are not forced to handle this if you do not want to.
Additionally, if you are using this library from test code, and you are not concerned with mocking the loader there are also static helper method that can be used to load the properties in a single line, so the above would be shortened to:
Collection<String> lines = FileLoader.loadContentLinesFromFileSystem("folder/file-system.txt");
or:
Collection<String> lines = FileLoader.loadContentLinesFromClasspath("package/classpath.txt");
It is also possible to load file content and have it automatically encoded into Base64 format, there are alternative implementations of FileContentLoader that you can use to do this, again it can be done from either the classpath or the file system.
To load from the file system you would do:
String path = "folder/file-system.txt";
FileContentLoader loader = new FileSystemBase64FileContentLoader();
Properties properties = loader.loadContent(path);
To load from the classpath you would do:
String path = "package/classpath.txt";
FileContentLoader loader = new ClasspathBase64FileContentLoader();
Properties properties = loader.loadContent(path);
If the file content is not loaded the code will throw a FileContentLoadException but you are not forced to handle this if you do not want to.
Additionally, if you are using this library from test code, and you are not concerned with mocking the loader there are also static helper method that can be used to load the properties in a single line, so the above would be shortened to:
String base64Content = FileLoader.loadBase64EncodedContentFromFileSystem("folder/file-system.txt");
or:
String base64Content = FileLoader.loadBase64EncodedContentFromClasspath("package/classpath.txt");
It is also possible to load and input stream as well as file content as a string. Again this can be done from either the classpath or the file system.
To load an input stream from the file system you would do:
String path = "folder/file-system.txt";
InputStreamLoader loader = new FileSystemInputStreamLoader();
InputStream inputStream = loader.load(path);
// don't forget to close the stream when you're done with it!
To load an input stream from the classpath you would do:
String path = "package/file-system.txt";
InputStreamLoader loader = new ClasspathInputStreamLoader();
InputStream inputStream = loader.load(path);
// don't forget to close the stream when you're done with it!
If the file content is not loaded the code will throw a FileLoadException but you are not forced to handle this if you do not want to.
There are also fake implementations of both FileContentLoader and InputStreamLoader which you can use when unit testing classes that use this library. They easily allow you to control the content or stream that is returned, as well as allowing you to verify that the correct path has been used when loading.
File Content Example:
FakeFileContentLoader loader = new FakeFileContentLoader();
loader.setContent("my fake content"); // set the fake loader to return specified file content when load method is called
ClassUnderTest classUnderTest = new ClassUnderTest(loader); // pass your fake loader to your class under test
classUnderTest.doAction();
String pathPassedToLoadMethod = loader.getLastLoadedPath();
Input Stream Example:
FakeInputStreamLoader loader = new FakeInputStreamLoader();
InputStream mockedStream = new ByteArrayInputStream("mockedStream".getBytes());
loader.setInputStream(mockedStream); // set the fake loader to return mocked input stream when load method is called
ClassUnderTest classUnderTest = new ClassUnderTest(loader); // pass your fake loader to your class under test
classUnderTest.doAction();
String pathPassedToLoadMethod = loader.getLastLoadedPath();
// cleans build directories
// prints currentVersion
// formats code
// builds code
// runs tests
// checks for gradle issues
// checks dependency versions
./gradlew clean currentVersion dependencyUpdates criticalLintGradle spotlessApply build