We had to make a StringServer that can keep track of a single string that gets added to by incoming requests.
The request looks like this - "/add-message?s=Hello"
The effect of this request is to concatenate a new line (\n) and the string after = to the running string, and then respond with the entire string so far.
The code I wrote for the web Server is-
Server After using /add-message?s=Hi.
When I used /add-message the method handleRequest from my code was called.
The argument for handleRequest is URI url which gets the value of the whole URL of the server (http://localhost:4001/add-message?s=Hi).
At this point newStr is empty but when the method gets called it checks if the path of the url contains /add-message.
If it contains /add-message it then checks if there is any String to be added to newStr
To do this, it splits the query part of the url across the "=". and in this case the value path variable gets is {"s","Hi"}.
And if it has the String is added with a \n.
Now, newStr has a value of Hi\n.
Using /add-message 2nd time.
Server After using /add-message?s=Everyone.
When I used /add-message the method handleRequest from my code was called.
The argument for handleRequest is URI url which gets the value of the whole URL of the server (http://localhost:4001/add-message?s=everone).
At this point newStr has a value of Hi\n.
When the method is called it checks if the path contains /add-message.
If it contains /add-message it then checks if there is any String to be added to newStr
To do this, it splits the query part of the url across the "=". and in this case the value path variable gets is {"s","Everyone"}.
And if it has the String is added with a \n.
Now, newStr has a value of Hi\nEveryone\n.
@Test
public void testFilter(){
List<String> testList= new ArrayList<>();
testList.add("band");
testList.add("bake");
testList.add("hello");
testList.add("bald");
testList.add("madhav");
StringChecker sc=new StringChecker() {
public boolean checkString(String s) {
if (s.contains("ba")){
return true;
}else{
return false;
}
}
};
List<String> answerList= new ArrayList<>();
answerList.add("band");
answerList.add("bake");
answerList.add("bald");
assertEquals(answerList,ListExamples.filter(testList,sc));
This input induces a failure.
@Test
public void testFilter2(){
List<String> testList= new ArrayList<>();
testList.add("band");
testList.add("hello");
testList.add("madhav");
StringChecker sc=new StringChecker() {
public boolean checkString(String s) {
if (s.contains("ba")){
return true;
}else{
return false;
}
}
};
List<String> answerList= new ArrayList<>();
answerList.add("band");
assertEquals(answerList,ListExamples.filter(testList,sc));
}
This input doesn't induce a failure.
Running of the two tests in vscode-
Code before bug fix-
class ListExamples {
// Returns a new list that has all the elements of the input list for which
// the StringChecker returns true, and not the elements that return false, in
// the same order they appeared in the input list;
static List<String> filter(List<String> list, StringChecker sc) {
List<String> result = new ArrayList<>();
for(String s: list) {
if(sc.checkString(s)) {
result.add(0,s);
}
}
return result;
}
Code after bug fix-
class ListExamples {
// Returns a new list that has all the elements of the input list for which
// the StringChecker returns true, and not the elements that return false, in
// the same order they appeared in the input list;
static List<String> filter(List<String> list, StringChecker sc) {
List<String> result = new ArrayList<>();
for(String s: list) {
if(sc.checkString(s)) {
result.add(s);
}
}
return result;
}
This code had a very small bug- result.add(0,s) instead of result.add(s).
This resulted in the result array to show its elements in the order opposite of what was intended as elements were being
prepended in the new array instead of being appended.
I learned how to write and run junit tests in vscode in lab 2 which I didn't know before. This is a very useful tool for
unit testing your code. I also learned how to debug a piece of code. We were given a bunch of methods with bugs and we had to write
Junit tests to figure out the bugs and fix it. These are extremely useful skills and I think will help me tremendously in the future.