From 3bcee514477314db0e631211e26e0847dd4a8fc7 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 3 Jul 2023 00:33:51 -0400 Subject: [PATCH] only:dependency injection towards TDD --- .../test/SdCardFieldsGeneratorTest.java | 2 +- .../main/java/com/rusefi/ReaderProvider.java | 11 ++++++++ .../main/java/com/rusefi/ReaderStateImpl.java | 12 ++++++++- .../java/com/rusefi/TriggerMetaGenerator.java | 2 +- .../com/rusefi/ldmp/LiveDataProcessor.java | 27 ++++++++++++------- 5 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 java_tools/configuration_definition_base/src/main/java/com/rusefi/ReaderProvider.java diff --git a/java_tools/configuration_definition/src/test/java/com/rusefi/test/SdCardFieldsGeneratorTest.java b/java_tools/configuration_definition/src/test/java/com/rusefi/test/SdCardFieldsGeneratorTest.java index 1a774c5ae4..db82caf874 100644 --- a/java_tools/configuration_definition/src/test/java/com/rusefi/test/SdCardFieldsGeneratorTest.java +++ b/java_tools/configuration_definition/src/test/java/com/rusefi/test/SdCardFieldsGeneratorTest.java @@ -79,7 +79,7 @@ public class SdCardFieldsGeneratorTest { } private static void processAndAssert(String input, String expectedOutput, Actor actor) { - ReaderStateImpl state = new ReaderStateImpl(); + ReaderStateImpl state = new ReaderStateImpl(null); actor.act(state); SdCardFieldsConsumer consumer = new SdCardFieldsConsumer(LazyFile.TEST); diff --git a/java_tools/configuration_definition_base/src/main/java/com/rusefi/ReaderProvider.java b/java_tools/configuration_definition_base/src/main/java/com/rusefi/ReaderProvider.java new file mode 100644 index 0000000000..b0879cd9a7 --- /dev/null +++ b/java_tools/configuration_definition_base/src/main/java/com/rusefi/ReaderProvider.java @@ -0,0 +1,11 @@ +package com.rusefi; + +import com.rusefi.util.IoUtils; + +import java.io.*; + +public interface ReaderProvider { + ReaderProvider REAL = fileName -> new InputStreamReader(new FileInputStream(fileName), IoUtils.CHARSET.name()); + + Reader read(String fileName) throws FileNotFoundException, UnsupportedEncodingException; +} diff --git a/java_tools/configuration_definition_base/src/main/java/com/rusefi/ReaderStateImpl.java b/java_tools/configuration_definition_base/src/main/java/com/rusefi/ReaderStateImpl.java index 3e54a833cd..7711c7f6d5 100644 --- a/java_tools/configuration_definition_base/src/main/java/com/rusefi/ReaderStateImpl.java +++ b/java_tools/configuration_definition_base/src/main/java/com/rusefi/ReaderStateImpl.java @@ -37,6 +37,7 @@ public class ReaderStateImpl implements ReaderState { private final Map tsCustomSize = new HashMap<>(); private final Map tsCustomLine = new HashMap<>(); private final Map structures = new HashMap<>(); + private final ReaderProvider readerProvider; private String headerMessage; // well, technically those should be a builder for state, not this state class itself private String tsFileOutputName = "rusefi.ini"; @@ -49,6 +50,14 @@ public class ReaderStateImpl implements ReaderState { private final EnumsReader enumsReader = new EnumsReader(); private final VariableRegistry variableRegistry = new VariableRegistry(); + public ReaderStateImpl() { + this(ReaderProvider.REAL); + } + + public ReaderStateImpl(ReaderProvider readerProvider) { + this.readerProvider = readerProvider; + } + @Override public void setWithC_Defines(boolean withC_Defines) { this.withC_Defines = withC_Defines; @@ -91,6 +100,7 @@ public class ReaderStateImpl implements ReaderState { structure.addBitField(bitField); } + @Override public void doJob() throws IOException { @@ -102,7 +112,7 @@ public class ReaderStateImpl implements ReaderState { * the destinations/writers */ SystemOut.println("Reading definition from " + Objects.requireNonNull(definitionInputFile)); - BufferedReader definitionReader = new BufferedReader(new InputStreamReader(new FileInputStream(RootHolder.ROOT + definitionInputFile), IoUtils.CHARSET.name())); + BufferedReader definitionReader = new BufferedReader(readerProvider.read(RootHolder.ROOT + definitionInputFile)); readBufferedReader(definitionReader, destinations); if (destCDefinesFileName != null) { diff --git a/java_tools/configuration_definition_base/src/main/java/com/rusefi/TriggerMetaGenerator.java b/java_tools/configuration_definition_base/src/main/java/com/rusefi/TriggerMetaGenerator.java index 22fa6e7577..2716e048a2 100644 --- a/java_tools/configuration_definition_base/src/main/java/com/rusefi/TriggerMetaGenerator.java +++ b/java_tools/configuration_definition_base/src/main/java/com/rusefi/TriggerMetaGenerator.java @@ -4,7 +4,7 @@ import java.io.IOException; public class TriggerMetaGenerator { public static void main(String[] a) throws IOException { - ReaderStateImpl reader = new ReaderStateImpl(); + ReaderStateImpl reader = new ReaderStateImpl(null); reader.setDefinitionInputFile("integration/rusefi_config_trigger.txt"); reader.addCHeaderDestination("../unit_tests/TriggerMeta.h"); reader.addJavaDestination("../java_tools/trigger-image/src/main/java/com/rusefi/config/generated/TriggerMeta.java"); diff --git a/java_tools/configuration_definition_base/src/main/java/com/rusefi/ldmp/LiveDataProcessor.java b/java_tools/configuration_definition_base/src/main/java/com/rusefi/ldmp/LiveDataProcessor.java index ccac094ee8..c9696bed22 100644 --- a/java_tools/configuration_definition_base/src/main/java/com/rusefi/ldmp/LiveDataProcessor.java +++ b/java_tools/configuration_definition_base/src/main/java/com/rusefi/ldmp/LiveDataProcessor.java @@ -3,13 +3,11 @@ package com.rusefi.ldmp; import com.devexperts.logging.Logging; import com.rusefi.*; import com.rusefi.output.*; +import com.rusefi.util.IoUtils; import com.rusefi.util.LazyFile; import org.yaml.snakeyaml.Yaml; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; +import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; @@ -24,6 +22,8 @@ public class LiveDataProcessor { private final static String tsOutputsDestination = "console/binary/"; + private final ReaderProvider readerProvider; + private final GaugeConsumer gaugeConsumer = new GaugeConsumer(tsOutputsDestination + File.separator + "generated/gauges.ini"); private final StringBuilder enumContent = new StringBuilder(header + @@ -43,6 +43,10 @@ public class LiveDataProcessor { private final String extraPrepend = System.getProperty("LiveDataProcessor.extra_prepend"); + public LiveDataProcessor(ReaderProvider readerProvider) { + this.readerProvider = readerProvider; + } + public static void main(String[] args) throws IOException { if (args.length != 1) { System.err.println("One parameter expected: name of live data yaml input file"); @@ -50,10 +54,10 @@ public class LiveDataProcessor { } TriggerMetaGenerator.main(null); String yamlFileName = args[0]; - Yaml yaml = new Yaml(); - Map data = yaml.load(new FileReader(yamlFileName)); + Map data = getStringObjectMap(new FileReader(yamlFileName)); - LiveDataProcessor liveDataProcessor = new LiveDataProcessor(); + + LiveDataProcessor liveDataProcessor = new LiveDataProcessor(ReaderProvider.REAL); int sensorTsPosition = liveDataProcessor.handleYaml(data); liveDataProcessor.writeFiles(); @@ -78,6 +82,11 @@ public class LiveDataProcessor { liveDataProcessor.end(); } + public static Map getStringObjectMap(Reader reader) { + Yaml yaml = new Yaml(); + return yaml.load(reader); + } + private void end() throws IOException { gaugeConsumer.endFile(); } @@ -86,7 +95,7 @@ public class LiveDataProcessor { void onEntry(String name, String javaName, String folder, String prepend, boolean withCDefines, String[] outputNames, String constexpr, String conditional, String engineModule, Boolean isPtr) throws IOException; } - private int handleYaml(Map data) throws IOException { + public int handleYaml(Map data) throws IOException { JavaSensorsConsumer javaSensorsConsumer = new JavaSensorsConsumer(); OutputsSectionConsumer outputsSections = new OutputsSectionConsumer(tsOutputsDestination + File.separator + "generated/output_channels.ini"); @@ -107,7 +116,7 @@ public class LiveDataProcessor { baseAddressCHeader.append("#define " + name.toUpperCase() + "_BASE_ADDRESS " + startingPosition + "\n"); - ReaderState state = new ReaderStateImpl(); + ReaderState state = new ReaderStateImpl(readerProvider); state.setDefinitionInputFile(folder + File.separator + name + ".txt"); state.setWithC_Defines(withCDefines);