diff --git a/.bazelrc b/.bazelrc index beb0ba4a..28847f43 100644 --- a/.bazelrc +++ b/.bazelrc @@ -87,3 +87,12 @@ build:magnesite_bes --bes_results_url=https://magnesite.cluster.engflow.com/invo # You can obtain an mTLS certificate by visiting the cluster's "Getting Started" # page, after logging in. try-import .bazelrc.user + +# EngFlow Remote caching +build:engflow-rc --remote_cache=grpc://127.0.0.1:8080 +# EngFlow BES forwarding +build:engflow-bes --bes_backend=grpc://127.0.0.1:8080 +build:engflow-bes --bes_results_url=http://127.0.0.1:8080/invocation/ +# EngFlow Remote caching and BES forwarding +build:engflow --config=engflow-rc +build:engflow --config=engflow-bes diff --git a/java/com/engflow/varyinginputs/README.md b/java/com/engflow/varyinginputs/README.md new file mode 100644 index 00000000..89059325 --- /dev/null +++ b/java/com/engflow/varyinginputs/README.md @@ -0,0 +1,23 @@ +# Varying Inputs Example + +## Usage + +To run the project and specify the number of files to be created, use the following command: + +```sh +bazel run //java/com/engflow/varyinginputs/main -- +``` + +Replace `` with the desired number of files to be created. + +## Example + +To create and read 10 files, you would run: + +```sh +bazel run //java/com/engflow/varyinginputs/main -- 10 +``` + +This command will: +1. Use the `Writer` class to create 10 files, each containing 100 random characters, in the `input` directory. +2. Use the `Reader` class to read and print the contents of these files. diff --git a/java/com/engflow/varyinginputs/input/BUILD b/java/com/engflow/varyinginputs/input/BUILD new file mode 100644 index 00000000..cfe5525a --- /dev/null +++ b/java/com/engflow/varyinginputs/input/BUILD @@ -0,0 +1,5 @@ +filegroup( + name = "input", + srcs = glob(["*.txt"]), + visibility = ["//visibility:public"], +) diff --git a/java/com/engflow/varyinginputs/main/BUILD b/java/com/engflow/varyinginputs/main/BUILD new file mode 100644 index 00000000..1fabb81a --- /dev/null +++ b/java/com/engflow/varyinginputs/main/BUILD @@ -0,0 +1,14 @@ +load("@rules_java//java:defs.bzl", "java_binary") + +java_binary( + name = "main", + srcs = ["Main.java"], + deps = [ + "//java/com/engflow/varyinginputs/reader:reader", + "//java/com/engflow/varyinginputs/writer:writer", + ], + data = [ + "//java/com/engflow/varyinginputs/input:input", + ], + main_class = "com.engflow.varyinginputs.main.Main", +) diff --git a/java/com/engflow/varyinginputs/main/Main.java b/java/com/engflow/varyinginputs/main/Main.java new file mode 100644 index 00000000..e7073c8a --- /dev/null +++ b/java/com/engflow/varyinginputs/main/Main.java @@ -0,0 +1,30 @@ +package com.engflow.varyinginputs.main; + +import com.engflow.varyinginputs.reader.Reader; +import com.engflow.varyinginputs.writer.Writer; + +public class Main { + + public static void main(String[] args) { + if (args.length < 1) { + System.out.println("Please provide the number of files as an argument."); + return; + } + + int numberOfFiles; + try { + numberOfFiles = Integer.parseInt(args[0]); + } catch (NumberFormatException e) { + System.out.println("Invalid number format: " + args[0]); + return; + } + + Writer writer = new Writer(); + Reader reader = new Reader(); + + String filePath = "com/engflow/varyinginputs/input"; + + writer.writeFiles(numberOfFiles, filePath); + reader.readFiles(filePath); + } +} diff --git a/java/com/engflow/varyinginputs/reader/BUILD b/java/com/engflow/varyinginputs/reader/BUILD new file mode 100644 index 00000000..4d4843c4 --- /dev/null +++ b/java/com/engflow/varyinginputs/reader/BUILD @@ -0,0 +1,14 @@ +java_library( + name = "reader", + srcs = ["Reader.java"], + visibility = ["//visibility:public"], +) + +java_test( + name = "reader_test", + srcs = ["ReaderTest.java"], + test_class = "com.engflow.varyinginputs.reader.ReaderTest", + deps = [ + ":reader", + ], +) diff --git a/java/com/engflow/varyinginputs/reader/Reader.java b/java/com/engflow/varyinginputs/reader/Reader.java new file mode 100644 index 00000000..9c889250 --- /dev/null +++ b/java/com/engflow/varyinginputs/reader/Reader.java @@ -0,0 +1,45 @@ +package com.engflow.varyinginputs.reader; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.BufferedReader; + +public class Reader { + + /** + * Reads the content of all files in the specified directory and prints it to the console. + * @param inputPath + */ + public void readFiles(String inputPath) { + File directory = new File(inputPath); + + // Check if the directory exists and is a directory + if (!directory.exists() || !directory.isDirectory()) { + System.out.println("Invalid directory path: " + inputPath); + return; + } + + // List all files in the directory and check if there are any + File[] files = directory.listFiles(); + if (files == null || files.length == 0) { + System.out.println("No files found in the directory: " + inputPath); + return; + } + + // Read the content of each file and print it to the console + for (File file : files) { + if (file.isFile()) { + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String line; + System.out.println("Reading file: " + file.getName()); + while ((line = reader.readLine()) != null) { + System.out.println(line); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } +} diff --git a/java/com/engflow/varyinginputs/reader/ReaderTest.java b/java/com/engflow/varyinginputs/reader/ReaderTest.java new file mode 100644 index 00000000..e28c0305 --- /dev/null +++ b/java/com/engflow/varyinginputs/reader/ReaderTest.java @@ -0,0 +1,65 @@ +package com.engflow.varyinginputs.reader; + +import org.junit.Test; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import static org.junit.Assert.assertTrue; + +public class ReaderTest { + + @Test + public void testReadFilesPrintsFileContents() throws IOException { + Reader reader = new Reader(); + String inputPath = "test_input"; + String fileName = "testFile.txt"; + String fileContent = "Hello, World!"; + + // Set up test files + File directory = new File(inputPath); + directory.mkdirs(); + File testFile = new File(inputPath + "/" + fileName); + try (FileWriter writer = new FileWriter(testFile)) { + writer.write(fileContent); + } + + // Capture the output + java.io.ByteArrayOutputStream outContent = new java.io.ByteArrayOutputStream(); + System.setOut(new java.io.PrintStream(outContent)); + + // Run the method + reader.readFiles(inputPath); + + // Verify the output + String expectedOutput = "Reading file: " + fileName + "\n" + fileContent + "\n"; + assertTrue(outContent.toString().contains(expectedOutput)); + + // Clean up after test + testFile.delete(); + directory.delete(); + } + + @Test + public void testReadFilesHandlesEmptyDirectory() { + Reader reader = new Reader(); + String inputPath = "empty_test_input"; + + // Set up empty directory + File directory = new File(inputPath); + directory.mkdirs(); + + // Capture the output + java.io.ByteArrayOutputStream outContent = new java.io.ByteArrayOutputStream(); + System.setOut(new java.io.PrintStream(outContent)); + + // Run the method + reader.readFiles(inputPath); + + // Verify the output + String expectedOutput = "No files found in the directory: " + inputPath + "\n"; + assertTrue(outContent.toString().contains(expectedOutput)); + + // Clean up after test + directory.delete(); + } +} diff --git a/java/com/engflow/varyinginputs/writer/BUILD b/java/com/engflow/varyinginputs/writer/BUILD new file mode 100644 index 00000000..1ad9695a --- /dev/null +++ b/java/com/engflow/varyinginputs/writer/BUILD @@ -0,0 +1,14 @@ +java_library( + name = "writer", + srcs = ["Writer.java"], + visibility = ["//visibility:public"], +) + +java_test( + name = "writer_test", + srcs = ["WriterTest.java"], + test_class = "com.engflow.varyinginputs.writer.WriterTest", + deps = [ + ":writer", + ], +) diff --git a/java/com/engflow/varyinginputs/writer/Writer.java b/java/com/engflow/varyinginputs/writer/Writer.java new file mode 100644 index 00000000..82608598 --- /dev/null +++ b/java/com/engflow/varyinginputs/writer/Writer.java @@ -0,0 +1,61 @@ +package com.engflow.varyinginputs.writer; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Random; +import java.util.UUID; + +public class Writer { + + /** + * Write the specified number of files to the output path. + * @param numberOfFiles + * @param outputPath + */ + public void writeFiles(int numberOfFiles, String outputPath) { + File directory = new File(outputPath); + + if (!directory.exists()) { + directory.mkdirs(); + } + + // list all files in the directory and count them + File[] existingFiles = directory.listFiles(); + int existingFilesCount = existingFiles != null ? existingFiles.length : 0; + + Random random = new Random(); + + // create new files if the number of existing files is less than the required number + if (existingFilesCount < numberOfFiles) { + for (int i = existingFilesCount; i < numberOfFiles; i++) { + // create a new file with a unique name to avoid conflicts + String fileName = outputPath + "/file" + UUID.randomUUID() + ".txt"; + File file = new File(fileName); + + try { + // write 100 random characters to the file + file.createNewFile(); + FileWriter writer = new FileWriter(file); + for (int j = 0; j < 100; j++) { + char randomChar = (char) (random.nextInt(26) + 'a'); + writer.write(randomChar); + } + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + // delete files if the number of existing files is greater than the required number + else if (existingFilesCount > numberOfFiles) { + while (existingFilesCount > numberOfFiles) { + int fileIndex = random.nextInt(existingFilesCount); + File fileToDelete = existingFiles[fileIndex]; + if (fileToDelete.delete()) { + existingFilesCount--; + } + } + } + } +} diff --git a/java/com/engflow/varyinginputs/writer/WriterTest.java b/java/com/engflow/varyinginputs/writer/WriterTest.java new file mode 100644 index 00000000..5cf96b0c --- /dev/null +++ b/java/com/engflow/varyinginputs/writer/WriterTest.java @@ -0,0 +1,66 @@ +package com.engflow.varyinginputs.writer; + +import org.junit.Test; +import java.io.File; +import java.io.IOException; +import static org.junit.Assert.assertEquals; + +public class WriterTest { + + @Test + public void testWriteFilesCreatesCorrectNumberOfFiles() throws IOException { + Writer writer = new Writer(); + String outputPath = "test_output"; + int numberOfFiles = 5; + + // Clean up before test + File directory = new File(outputPath); + if (directory.exists()) { + for (File file : directory.listFiles()) { + file.delete(); + } + directory.delete(); + } + + // Run the method + writer.writeFiles(numberOfFiles, outputPath); + + // Verify the number of files created + File[] files = new File(outputPath).listFiles(); + assertEquals(numberOfFiles, files.length); + + // Clean up after test + for (File file : files) { + file.delete(); + } + new File(outputPath).delete(); + } + + @Test + public void testWriteFilesDeletesExcessFiles() throws IOException { + Writer writer = new Writer(); + String outputPath = "test_output"; + int initialNumberOfFiles = 10; + int finalNumberOfFiles = 5; + + // Create initial files + File directory = new File(outputPath); + directory.mkdirs(); + for (int i = 0; i < initialNumberOfFiles; i++) { + new File(outputPath + "/file" + i + ".txt").createNewFile(); + } + + // Run the method + writer.writeFiles(finalNumberOfFiles, outputPath); + + // Verify the number of files after deletion + File[] files = new File(outputPath).listFiles(); + assertEquals(finalNumberOfFiles, files.length); + + // Clean up after test + for (File file : files) { + file.delete(); + } + new File(outputPath).delete(); + } +}