Skip to content

Commit

Permalink
Merge branch 'unsupported-file-encoding'
Browse files Browse the repository at this point in the history
  • Loading branch information
knewjade committed Dec 11, 2022
2 parents 29fca12 + 130f243 commit f0d585c
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 24 deletions.
19 changes: 17 additions & 2 deletions src/main/java/entry/common/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import exceptions.FinderParseException;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.nio.charset.MalformedInputException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -19,11 +22,13 @@
import java.util.stream.Stream;

public class Loader {
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

// フィールドの情報を読み込む
public static Optional<FieldData> loadFieldData(
CommandLineWrapper wrapper, FumenLoader fumenLoader,
String pageOptName, String fumenOptName, String fieldPathOptName,
String defaultFieldText, Charset charset,
String defaultFieldText,
FunctionParseException<FieldData, Optional<FieldData>> callbackWithFumen,
FunctionParseException<LinkedList<String>, Optional<FieldData>> callbackWithText
) throws FinderParseException {
Expand All @@ -46,7 +51,7 @@ public static Optional<FieldData> loadFieldData(
Path path = Paths.get(fieldPath);

LinkedList<String> fieldLines;
try (Stream<String> lines = Files.lines(path, charset)) {
try (Stream<String> lines = Files.lines(path, DEFAULT_CHARSET)) {
fieldLines = lines
.map(str -> {
if (str.contains("#"))
Expand All @@ -56,6 +61,11 @@ public static Optional<FieldData> loadFieldData(
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toCollection(LinkedList::new));
} catch (UncheckedIOException e) {
if (e.getCause() instanceof MalformedInputException) {
throw new FinderParseException("File encoding is probably unexpected. solution-finder supports UTF-8.");
}
throw e;
} catch (IOException e) {
throw new FinderParseException("Cannot open field file");
}
Expand Down Expand Up @@ -93,6 +103,11 @@ public static List<String> loadPatterns(

try (Stream<String> lines = Files.lines(path, charset)) {
return lines.collect(Collectors.toList());
} catch (UncheckedIOException e) {
if (e.getCause() instanceof MalformedInputException) {
throw new FinderParseException("File encoding is probably unexpected. solution-finder supports UTF-8.");
}
throw e;
} catch (IOException e) {
throw new FinderParseException("Cannot open patterns file", e);
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/entry/path/PathSettingParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ protected Optional<PathSettings> parse(CommandLineWrapper wrapper) throws Finder
PathOptions.Fumen.optName(),
PathOptions.FieldPath.optName(),
DEFAULT_FIELD_TXT,
Charset.forName(CHARSET_NAME),
Optional::of,
fieldLines -> {
try {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/entry/percent/PercentSettingParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ protected Optional<PercentSettings> parse(CommandLineWrapper wrapper) throws Fin
PercentOptions.Fumen.optName(),
PercentOptions.FieldPath.optName(),
DEFAULT_FIELD_TXT,
Charset.forName(CHARSET_NAME),
Optional::of,
fieldLines -> {
try {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/entry/ren/RenSettingParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ protected Optional<RenSettings> parse(CommandLineWrapper wrapper) throws FinderP
RenOptions.Fumen.optName(),
RenOptions.FieldPath.optName(),
DEFAULT_FIELD_TXT,
Charset.forName(CHARSET_NAME),
Optional::of,
fieldLines -> {
// フィールドの設定
Expand Down
1 change: 0 additions & 1 deletion src/main/java/entry/setup/SetupSettingParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ protected Optional<SetupSettings> parse(CommandLineWrapper wrapper) throws Finde
SetupOptions.Fumen.optName(),
SetupOptions.FieldPath.optName(),
DEFAULT_FIELD_TXT,
Charset.forName(CHARSET_NAME),
fieldData -> {
ColoredField coloredField = fieldData.toColoredField();

Expand Down
1 change: 0 additions & 1 deletion src/main/java/entry/spin/SpinSettingParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ protected Optional<SpinSettings> parse(CommandLineWrapper wrapper) throws Finder
SpinOptions.Fumen.optName(),
SpinOptions.FieldPath.optName(),
DEFAULT_FIELD_TXT,
Charset.forName(CHARSET_NAME),
Optional::of,
fieldLines -> {
// フィールドの設定
Expand Down
56 changes: 42 additions & 14 deletions src/test/java/_usecase/ConfigFileHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,91 +5,119 @@
import com.google.common.io.FileWriteMode;
import com.google.common.io.Files;
import common.SyntaxException;
import common.datastore.blocks.Pieces;
import common.pattern.LoadedPatternGenerator;
import core.field.Field;
import core.field.FieldView;
import core.mino.Piece;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.nio.charset.Charset;
import java.util.stream.Collectors;

public class ConfigFileHelper {
private static final String FIELD_PATH = concatPath("input", "field.txt");
private static final String PATTERN_PATH = concatPath("input", "patterns.txt");

private static final String LINE_SEPARATOR = System.lineSeparator();
public static final Charset DEFAULT_CHARSET = Charsets.UTF_8;

public static void createFieldFile(String text) throws IOException {
createFieldFile(text, "field", concatPath("input"));
}

public static void createFieldFile(String text, String fileName, String directoryPath) throws IOException {
createNewTextFile(text, fileName, directoryPath);
createNewTextFile(text, fileName, directoryPath, DEFAULT_CHARSET);
}

private static String concatPath(String... names) {
return FileHelper.concatPath(names);
}

public static void createFieldFile(Field field, int height) throws IOException {
createFieldFile(field, height, "field");
createFieldFile(field, height, DEFAULT_CHARSET);
}

public static void createFieldFile(Field field, int height, Charset charset) throws IOException {
createFieldFile(field, height, "field", charset);
}

public static void createFieldFile(Field field, int height, String fileName) throws IOException {
createFieldFile(field, height, fileName, DEFAULT_CHARSET);
}

private static void createFieldFile(Field field, int height, String fileName, Charset charset) throws IOException {
String path = concatPath("input");
createFieldFile(field, height, fileName, path);
createFieldFile(field, height, fileName, path, charset);
}

public static void createFieldFile(Field field, int height, String fileName, String directoryPath) throws IOException {
createFieldFile(field, height, fileName, directoryPath, DEFAULT_CHARSET);
}

private static void createFieldFile(Field field, int height, String fileName, String directoryPath, Charset charset) throws IOException {
String text = height + LINE_SEPARATOR + FieldView.toString(field, height);
createNewTextFile(text, fileName, directoryPath);
createNewTextFile(text, fileName, directoryPath, charset);
}

public static void deleteFieldFile() throws IOException {
File file = new File(FIELD_PATH);
FileHelper.deleteFileAndClose(file);
}

private static void createNewTextFile(String text, String fileName, String parentDirectoryPath) throws IOException {
private static void createNewTextFile(String text, String fileName, String parentDirectoryPath, Charset charset) throws IOException {
File file = new File(concatPath(parentDirectoryPath, fileName + ".txt"));
FileHelper.deleteFileAndClose(file);

// noinspection ResultOfMethodCallIgnored
file.createNewFile();
CharSink charSink = Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);
CharSink charSink = Files.asCharSink(file, charset, FileWriteMode.APPEND);
charSink.write(text);
}

public static void createPatternFile(String pattern) throws IOException {
createPatternFile(pattern, "patterns");
createPatternFile(pattern, DEFAULT_CHARSET);
}

public static void createPatternFile(String pattern, Charset charset) throws IOException {
createPatternFile(pattern, "patterns", charset);
}

public static void createPatternFile(String pattern, String fileName) throws IOException {
createPatternFile(pattern, concatPath("input"), fileName);
createPatternFile(pattern, fileName, DEFAULT_CHARSET);
}

private static void createPatternFile(String pattern, String fileName, Charset charset) throws IOException {
createPatternFile(pattern, concatPath("input"), fileName, charset);
}

public static void createPatternFile(String pattern, String directoryPath, String fileName) throws IOException {
createNewTextFile(pattern, fileName, directoryPath);
createPatternFile(pattern, directoryPath, fileName, DEFAULT_CHARSET);
}

private static void createPatternFile(String pattern, String directoryPath, String fileName, Charset charset) throws IOException {
createNewTextFile(pattern, fileName, directoryPath, charset);
}

public static void createPatternFileFromCommand(String patternCommand) throws IOException, SyntaxException {
createPatternFileFromCommand(patternCommand, "patterns");
}

public static void createPatternFileFromCommand(String patternCommand, String fileName) throws IOException, SyntaxException {
createNewTextFileAndExpand(patternCommand, fileName, concatPath("input"));
createPatternFileFromCommand(patternCommand, fileName, DEFAULT_CHARSET);
}

private static void createPatternFileFromCommand(String patternCommand, String fileName, Charset charset) throws IOException, SyntaxException {
createNewTextFileAndExpand(patternCommand, fileName, concatPath("input"), charset);
}

private static void createNewTextFileAndExpand(String pattern, String fileName, String parentDirectoryPath) throws IOException, SyntaxException {
private static void createNewTextFileAndExpand(String pattern, String fileName, String parentDirectoryPath, Charset charset) throws IOException, SyntaxException {
File file = new File(concatPath(parentDirectoryPath, fileName + ".txt"));
FileHelper.deleteFileAndClose(file);

// noinspection ResultOfMethodCallIgnored
file.createNewFile();
CharSink charSink = Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);
CharSink charSink = Files.asCharSink(file, charset, FileWriteMode.APPEND);
String text = new LoadedPatternGenerator(pattern).blocksStream()
.map(pieces -> pieces.blockStream().map(Piece::getName).collect(Collectors.joining()))
.collect(Collectors.joining(System.lineSeparator()));
Expand Down
17 changes: 14 additions & 3 deletions src/test/java/_usecase/path/PathFileCaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import _usecase.RunnerHelper;
import _usecase.path.files.OutputFileHelper;
import _usecase.path.files.PathHTML;
import com.google.common.base.Charsets;
import core.field.Field;
import core.field.FieldFactory;
import entry.EntryPointMain;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -40,8 +42,13 @@ void useFieldFileAndPatternsFile1() throws Exception {
);

int height = 4;
ConfigFileHelper.createFieldFile(field, height);
ConfigFileHelper.createPatternFile("*p4");
Charset charset = Charsets.UTF_8;

ConfigFileHelper.deleteFieldFile();
ConfigFileHelper.createFieldFile(field, height, charset);

ConfigFileHelper.deletePatternFile();
ConfigFileHelper.createPatternFile("*p4", charset);

String command = "path";
Log log = RunnerHelper.runnerCatchingLog(() -> EntryPointMain.main(command.split(" ")));
Expand Down Expand Up @@ -269,7 +276,11 @@ void getLog() throws Exception {

assertThat(log.getReturnCode()).isEqualTo(0);

String logFile = Files.lines(Paths.get("test_output_log/test_last_output.txt")).collect(Collectors.joining(LINE_SEPARATOR)) + LINE_SEPARATOR;
String logFile;
try (Stream<String> lines = Files.lines(Paths.get("test_output_log/test_last_output.txt"))) {
logFile = lines.collect(Collectors.joining(LINE_SEPARATOR)) + LINE_SEPARATOR;
}

assertThat(log.getOutput())
.isEqualTo(logFile);

Expand Down
38 changes: 38 additions & 0 deletions src/test/java/_usecase/path/PathIrregularCaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import _usecase.Log;
import _usecase.RunnerHelper;
import _usecase.path.files.OutputFileHelper;
import com.google.common.base.Charsets;
import core.field.Field;
import core.field.FieldFactory;
import core.field.FieldView;
Expand All @@ -13,6 +14,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -675,4 +677,40 @@ void use180RotationWithDefault() throws Exception {
assertThat(log.getReturnCode()).isNotEqualTo(0);
assertThat(log.getError()).contains("kicks do not support 180");
}

@Test
void unsupportedFieldFileEncoding() throws Exception {
int height = 4;
Charset unexpectedCharset = Charsets.UTF_16;

ConfigFileHelper.deleteFieldFile();
ConfigFileHelper.createFieldFile(FieldFactory.createField(height), height, unexpectedCharset);

ConfigFileHelper.deletePatternFile();
ConfigFileHelper.createPatternFile("*p4");

String command = "path";
Log log = RunnerHelper.runnerCatchingLog(() -> EntryPointMain.main(command.split(" ")));

assertThat(log.getReturnCode()).isNotEqualTo(0);
assertThat(log.getError()).contains("File encoding is probably unexpected. solution-finder supports UTF-8.");
}

@Test
void unsupportedPatternFileEncoding() throws Exception {
int height = 4;
Charset unexpectedCharset = Charsets.UTF_16;

ConfigFileHelper.deleteFieldFile();
ConfigFileHelper.createFieldFile(FieldFactory.createField(height), height);

ConfigFileHelper.deletePatternFile();
ConfigFileHelper.createPatternFile("*p4", unexpectedCharset);

String command = "path";
Log log = RunnerHelper.runnerCatchingLog(() -> EntryPointMain.main(command.split(" ")));

assertThat(log.getReturnCode()).isNotEqualTo(0);
assertThat(log.getError()).contains("File encoding is probably unexpected. solution-finder supports UTF-8.");
}
}

0 comments on commit f0d585c

Please sign in to comment.