Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Varying inputs example #360

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions java/com/engflow/varyinginputs/README.md
Original file line number Diff line number Diff line change
@@ -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 -- <num_files>
```

Replace `<num_files>` 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.
5 changes: 5 additions & 0 deletions java/com/engflow/varyinginputs/input/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
filegroup(
name = "input",
srcs = glob(["*.txt"]),
visibility = ["//visibility:public"],
)
14 changes: 14 additions & 0 deletions java/com/engflow/varyinginputs/main/BUILD
Original file line number Diff line number Diff line change
@@ -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",
)
30 changes: 30 additions & 0 deletions java/com/engflow/varyinginputs/main/Main.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
14 changes: 14 additions & 0 deletions java/com/engflow/varyinginputs/reader/BUILD
Original file line number Diff line number Diff line change
@@ -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",
],
)
45 changes: 45 additions & 0 deletions java/com/engflow/varyinginputs/reader/Reader.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
}
65 changes: 65 additions & 0 deletions java/com/engflow/varyinginputs/reader/ReaderTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
14 changes: 14 additions & 0 deletions java/com/engflow/varyinginputs/writer/BUILD
Original file line number Diff line number Diff line change
@@ -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",
],
)
61 changes: 61 additions & 0 deletions java/com/engflow/varyinginputs/writer/Writer.java
Original file line number Diff line number Diff line change
@@ -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--;
}
}
}
}
}
66 changes: 66 additions & 0 deletions java/com/engflow/varyinginputs/writer/WriterTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading