Skip to content
Snippets Groups Projects
Commit 65aeba94 authored by Mai Pham's avatar Mai Pham
Browse files

Add test unit and pattern for generating logs

parent 64272b3b
No related branches found
No related tags found
No related merge requests found
Showing
with 168 additions and 49 deletions
localhost 5001 /home/maipham/uiuc/cs425/mp1/grep/files/test.rtf localhost 5001 /home/maipham/uiuc/cs425/mp1/grep/files/test.log
\ No newline at end of file \ No newline at end of file
...@@ -5,9 +5,10 @@ ...@@ -5,9 +5,10 @@
\margl1440\margr1440\vieww11520\viewh8400\viewkind0 \margl1440\margr1440\vieww11520\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0 \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
Error: This is error 1 in log file Error: This is error 1 in log file //normal line
Error: !@2612!!! errejkomjo;m //rare pattern
Error: This is error 2 in log file
Error: This is error 3 in log file
} }
\ No newline at end of file
No preview for this file type
...@@ -18,6 +18,28 @@ ...@@ -18,6 +18,28 @@
<artifactId>argparse4j</artifactId> <artifactId>argparse4j</artifactId>
<version>0.2.0</version> <version>0.2.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.18.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.18.0</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
Manifest-Version: 1.0 Manifest-Version: 1.0
Main-Class: cs425.mp1.Main Main-Class: cs425.mp1.Client
...@@ -33,49 +33,9 @@ public class Client { ...@@ -33,49 +33,9 @@ public class Client {
} }
/* Read config file */ /* Read config file */
FileReader file = new FileReader(ns.getString("configFile")); String filePath = ns.getString("configFile");
BufferedReader reader = new BufferedReader(file);
/* Read server hosts, ports, and log files */
ArrayList<ServerConf> servers = new ArrayList<>();
String line;
while (true) {
line = reader.readLine();
if (line != null) {
String[] s = line.split(" ");
String server = s[0].trim();
int port = Integer.parseInt(s[1].trim());
String logFile = s[2].trim();
servers.add(new ServerConf(server, port, logFile));
} else {
break;
}
}
/* Read grep command */
String pattern = String.join(" ", ns.getList("command")); String pattern = String.join(" ", ns.getList("command"));
pattern = "-" + pattern; ClientHandler.init(filePath, pattern);
/*
Creating threads based on number of servers
*/
int n = servers.size();
ArrayList<GrepThread> threads = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
GrepThread t = new GrepThread(servers.get(i), pattern);
threads.add(t);
t.start();
}
/* End threads */
for (GrepThread t: threads) {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }
} }
package cs425.mp1;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class ClientHandler {
public static void init(String filePath, String pattern) throws IOException {
/* Read server hosts, ports, and log files */
FileReader file = new FileReader(filePath);
ArrayList<ServerConf> servers = new ArrayList<>();
String line;
BufferedReader reader = new BufferedReader(file);
while (true) {
line = reader.readLine();
if (line != null) {
String[] s = line.split(" ");
String server = s[0].trim();
int port = Integer.parseInt(s[1].trim());
String logFile = s[2].trim();
servers.add(new ServerConf(server, port, logFile));
} else {
break;
}
}
/*
Creating threads based on number of servers
*/
int n = servers.size();
ArrayList<GrepThread> threads = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
GrepThread t = new GrepThread(servers.get(i), pattern);
threads.add(t);
t.start();
}
/* End threads */
for (GrepThread t: threads) {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
...@@ -7,8 +7,8 @@ import java.util.ArrayList; ...@@ -7,8 +7,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Grep { public class Grep {
public static List<String> run_cmd(String pattern) throws IOException { public static List<String> run_cmd(String command) throws IOException {
String[] cmd = {"/bin/sh", "-c", "grep --color=always " + pattern}; String[] cmd = {"/bin/sh", "-c", command};
// System.out.println(cmd); // System.out.println(cmd);
ProcessBuilder prc_builder = new ProcessBuilder(cmd); ProcessBuilder prc_builder = new ProcessBuilder(cmd);
Process process = prc_builder.start(); Process process = prc_builder.start();
......
...@@ -47,6 +47,8 @@ public class GrepThread extends Thread{ ...@@ -47,6 +47,8 @@ public class GrepThread extends Thread{
} else } else
System.err.println("Error happens when connecting to server."); System.err.println("Error happens when connecting to server.");
u.printStackTrace(); u.printStackTrace();
} catch (Exception u) {
System.err.println("Client fails to get response from server.");
} }
} }
} }
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import java.util.ArrayList;
import java.util.logging.Logger;
class GrepTest {
private static final Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
private ArrayList<String> generate_logs() {
/**
* known log lines
*/
/**
unknown log lines
*/
return null;
}
@BeforeEach
void setUp() {
// calculator = new Calculator();
/**
* Generate log files every machines
*/
logger.info("Generating log....");
}
@Test
@DisplayName("Test frequent normal case")
void test_normal_case() {
// assertEquals(20, calculator.multiply(4, 5),
// "Regular multiplication should work");
}
@RepeatedTest(5)
@DisplayName("Test rare query pattern")
void test_rare_case() {
// assertEquals(0, calculator.multiply(0, 5), "Multiple with zero should be zero");
// assertEquals(0, calculator.multiply(5, 0), "Multiple with zero should be zero");
}
@Test
@DisplayName("Test partly frequent case")
void test_partly_frequent_case() {
// assertEquals(20, calculator.multiply(4, 5),
// "Regular multiplication should work");
}
@Test
@DisplayName("Test pattern occurs in one log")
void test_pattern_occured_once() {
// assertEquals(20, calculator.multiply(4, 5),
// "Regular multiplication should work");
}
@Test
@DisplayName("Test pattern occurs in several logs")
void test_pattern_occured_several_case() {
// assertEquals(20, calculator.multiply(4, 5),
// "Regular multiplication should work");
}
@Test
@DisplayName("Test pattern occurs in all logs")
void test_pattern_occured_all_case() {
// assertEquals(20, calculator.multiply(4, 5),
// "Regular multiplication should work");
}
@Test
@DisplayName("Test non occurred pattern in any logs")
void test_non_occured_pattern_case() {
// assertEquals(20, calculator.multiply(4, 5),
// "Regular multiplication should work");
}
}
\ No newline at end of file
No preview for this file type
File added
No preview for this file type
No preview for this file type
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment