Skip to content

Commit

Permalink
Renamed all methods of Text Reader/Writer classes + refactoring #3306
Browse files Browse the repository at this point in the history

- renamed TextFileReader and TextFileWriter methods
- renamed TestFileReader and TestFileWriter methods
- renamed class TextFileWriter inside docgen to DocGenTextFileWriter
  (to make it clear this is another class).
- renamed class TextFileReader inside docgen to DocGenTextFileReaader
  (to make it clear this is another class).
- DocGenTextFileReader methods renamed
- DocGenTextFileWriter methods renamed
- Introduced constants
- JavaDoc added
- Where possible logic from reader/writer is reused now (except
  in test framework here we have a complete own implementation -
  but this is wanted)
  • Loading branch information
de-jcup authored Aug 7, 2024
1 parent a83ef1c commit 9e69923
Show file tree
Hide file tree
Showing 81 changed files with 336 additions and 340 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public FileBasedAdapterMetaDataCallback(File file) {
public void persist(AdapterMetaData metaData) {
String metaDataJson = JSONConverter.get().toJSON(metaData);
try {
writer.save(file, metaDataJson, true);
writer.writeTextToFile(file, metaDataJson, true);
} catch (IOException e) {
throw new IllegalStateException("Was not able to store meta data!", e);
}
Expand All @@ -45,7 +45,7 @@ public AdapterMetaData getMetaDataOrNull() {
}
try {

String data = reader.loadTextFile(file);
String data = reader.readTextFromFile(file);
if (data == null || data.isEmpty()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void reading_an_empty_file_does_return_null() throws Exception {
void reading_a_clean_json_does_return_empty_metadata_object() throws Exception {
/* prepare */
TestFileWriter writer = new TestFileWriter();
writer.save(testFile, "{}", true);
writer.writeTextToFile(testFile, "{}", true);

/* execute */
AdapterMetaData metaData = callbackToTest.getMetaDataOrNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private void generateAbstractModel(MapGenInfo info) throws Exception {
template.addLine(" }");
template.addLine("}");

context.getTextFileWriter().save(genFile, template.getCode(), true);
context.getTextFileWriter().writeTextToFile(genFile, template.getCode(), true);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private void generatetPublicModel(MapGenInfo info, boolean overwritePublicModelF
template.addLine("");
template.addLine("}");

context.getTextFileWriter().save(genFile, template.getCode(), overwritePublicModelFiles);
context.getTextFileWriter().writeTextToFile(genFile, template.getCode(), overwritePublicModelFiles);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void compress_zip_single_data_txt_file_compressed_output_contains_same_content()
File targetFile = new File(parentFolder, "sourcecode.zip");

TextFileWriter writer = new TextFileWriter();
writer.save(textFile, "This is just a test content", true);
writer.writeTextToFile(textFile, "This is just a test content", true);

/* execute */
supportToTest.compressFolder(ArchiveType.ZIP, dataFolder, targetFile);
Expand All @@ -307,7 +307,7 @@ private void assertFileContains(File file, String expectedContent) throws IOExce
fail("File:" + file + " does not exist!");
}
TextFileReader reader = new TextFileReader();
String content = reader.loadTextFile(file);
String content = reader.readTextFromFile(file);
assertEquals(expectedContent, content);
}

Expand All @@ -324,8 +324,8 @@ void compress_zip_two_txt_files_compressed_output_contains_same_content() throws
File targetFile = new File(parentFolder, "sourcecode.zip");

TextFileWriter writer = new TextFileWriter();
writer.save(textFile1, "text1", true);
writer.save(textFile2, "text2", true);
writer.writeTextToFile(textFile1, "text1", true);
writer.writeTextToFile(textFile2, "text2", true);

/* execute */
supportToTest.compressFolder(ArchiveType.ZIP, dataFolder, targetFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,66 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A class to read text from files. Will always use UTF-8.
*
* @author Albert Tregnaghi
*
*/
public class TextFileReader {

private static final String DEFAULT_LINE_BREAK = "\n";
private static final Charset CHARSET_UTF8 = Charset.forName("UTF-8");

private static final Logger LOG = LoggerFactory.getLogger(TextFileReader.class);

/**
* Load complete text file with default line break (new line)
* Read complete text from file with default line break
*
* @param file resource to load
* @return string, never {@link NullPointerException}
* @throws IOException
* @return string, never <code>null</code>
* @throws IOException when IO problems occur
*/
public String loadTextFile(File file) throws IOException {
return loadTextFile(file, "\n");
public String readTextFromFile(File file) throws IOException {
return readTextFromFile(file, DEFAULT_LINE_BREAK);
}

/**
* Load complete text file
* Read complete text from file
*
* @param file resource to load
* @param lineBreak the line break to use
* @return
* @throws IOException
* @return string, never <code>null</code>
* @throws IOException when IO problems occur
*/
public String loadTextFile(File file, String lineBreak) throws IOException {
return loadTextFile(file, lineBreak, null);
public String readTextFromFile(File file, String lineBreak) throws IOException {
return readTextFromFile(file, lineBreak, null);
}

/**
* Loads text file - if max amount of lines is defined, only this amount of
* Read text from file - if max amount of lines is defined, only this amount of
* lines will be read.
*
* @param file resource to load
* @param lineBreak the line break to use
* @param maxAmountOfLines maximum amount lines to read - minimum is 1. One line
* will always be returned, even when the value is lower
* than 1!
* @return
* @throws IOException
* @return string, never <code>null</code>
* @throws IOException when IO problems occur
*/
public String loadTextFile(File file, String lineBreak, Integer maxAmountOfLines) throws IOException {
public String readTextFromFile(File file, String lineBreak, Integer maxAmountOfLines) throws IOException {
LOG.trace("Read text from file: {}", file);

StringBuilder sb = new StringBuilder();

int linesRead = 0;
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"))) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), CHARSET_UTF8))) {
String line = null;

while ((line = br.readLine()) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A class to write text to files. Will always use UTF-8.
*
* @author Albert Tregnaghi
*
*/
public class TextFileWriter {

private static final Charset CHARSET_UTF_8 = Charset.forName("UTF-8");
private static final Logger LOG = LoggerFactory.getLogger(TextFileWriter.class);

/**
Expand All @@ -26,11 +33,7 @@ public class TextFileWriter {
* before write
* @throws IOException
*/
public void save(File targetFile, String text, boolean overwrite) throws IOException {
internalSave(targetFile, text, overwrite, Charset.forName("UTF-8"));
}

private void internalSave(File targetFile, String text, boolean overwrite, Charset charset) throws IOException {
public void writeTextToFile(File targetFile, String text, boolean overwrite) throws IOException {
if (targetFile == null) {
throw new IllegalArgumentException("null not allowed as file!");
}
Expand Down Expand Up @@ -59,7 +62,7 @@ private void internalSave(File targetFile, String text, boolean overwrite, Chars
throw new IllegalStateException("was not able to create new file:" + targetFile);
}
}
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), charset))) {
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), CHARSET_UTF_8))) {
bw.write(text);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RemoteCredentialContainerTest {
@Test
void resolve_remote_credentials_by_location_and_accept_all_types() {
/* prepare */
String json = TestFileReader.loadTextFile(new File("./src/test/resources/sechub_remote_credentials_config.json"));
String json = TestFileReader.readTextFromFile(new File("./src/test/resources/sechub_remote_credentials_config.json"));
RemoteCredentialConfiguration configuration = RemoteCredentialConfiguration.fromJSONString(json);
containerToTest = new RemoteCredentialContainerFactory().create(configuration);
String location1 = "https://github.com/username/project";
Expand Down Expand Up @@ -58,7 +58,7 @@ void resolve_remote_credentials_by_location_and_accept_all_types() {
@Test
void resolve_remote_credentials_by_location_and_accept_specific_types() {
/* prepare */
String json = TestFileReader.loadTextFile(new File("./src/test/resources/sechub_remote_credentials_config.json"));
String json = TestFileReader.readTextFromFile(new File("./src/test/resources/sechub_remote_credentials_config.json"));
RemoteCredentialConfiguration configuration = RemoteCredentialConfiguration.fromJSONString(json);
containerToTest = new RemoteCredentialContainerFactory().create(configuration);
String type1 = "docker";
Expand Down Expand Up @@ -158,7 +158,7 @@ void resolve_credentials_from_configuration_where_no_types_are_set_but_all_types
@NullSource
void resolve_remote_credential_pattern_by_location_with_empty_or_null_location(String location) {
/* prepare */
String json = TestFileReader.loadTextFile(new File("./src/test/resources/sechub_remote_credentials_config.json"));
String json = TestFileReader.readTextFromFile(new File("./src/test/resources/sechub_remote_credentials_config.json"));
RemoteCredentialConfiguration configuration = RemoteCredentialConfiguration.fromJSONString(json);
containerToTest = new RemoteCredentialContainerFactory().create(configuration);

Expand All @@ -174,7 +174,7 @@ void resolve_remote_credential_pattern_by_location_with_empty_or_null_location(S
@NullSource
void resolve_remote_credential_by_unknown_location_with_empty_or_null_type(String type) {
/* prepare */
String json = TestFileReader.loadTextFile(new File("./src/test/resources/sechub_remote_credentials_config.json"));
String json = TestFileReader.readTextFromFile(new File("./src/test/resources/sechub_remote_credentials_config.json"));
RemoteCredentialConfiguration configuration = RemoteCredentialConfiguration.fromJSONString(json);
containerToTest = new RemoteCredentialContainerFactory().create(configuration);
String location = "unknown-location";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ void sechub_secret_scan_config_source_example__source_required_only_by_secret_sc
}

static SecHubConfigurationModel loadModel(String testFileName) {
String json = TestFileReader.loadTextFile(new File("./src/test/resources/" + testFileName));
String json = TestFileReader.readTextFromFile(new File("./src/test/resources/" + testFileName));
return converter.fromJSON(SecHubConfigurationModel.class, json);
}
}
Loading

0 comments on commit 9e69923

Please sign in to comment.