Commit 04d839f2 authored by Pamelpreet Jhinger's avatar Pamelpreet Jhinger
Browse files

mockito

parents 806eecee f21da174
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>demo</groupId>
<artifactId>shared-resources</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>CSS Resources</name>
</project>
\ No newline at end of file
body {
text-align: center;
background-color: rgb(255, 255, 153);
/* background-image: url("https://ae01.alicdn.com/kf/HTB1nAkRi9YH8KJjSspdq6ARgVXa4/white-damask-gray-grey-Floral-pattern-Background-Vinyl-cloth-High-quality-Computer-print-wall-photo-backdrop.jpg"); */
}
header {
position: sticky;
top: 0;
left: 0;
right: 0;
background-color: #ff9966;
}
h1 {
color: teal;
font-family: "Old English Text MT", cursive;
/* background-color: maroon; */
font-size: 100px;
width: 100%;
}
.information {
text-align: left;
width: 50%;
left: 0;
display: inline-block;
font-size: 150%;
}
p {
font-size: 200%;
/* display: inline-block; */
font-family: cursive;
/* background-color: rebeccapurple; */
}
.forms {
/* background-color: rgb(204, 255, 204); */
}
.forms input[type="text"] {
width: 250px;
padding: 10px;
font-weight: bold;
background-color: yellow;
font-family: "Old English Text MT", fantasy;
font-size: 120%;
/* color: blue; */
}
.forms input[type="radio"] {
cursor: pointer;
position: relative;
}
.forms input[type="submit"] {
cursor: pointer;
height: 50px;
width: 100px;
font-weight: bold;
font-size: 120%;
font-family: "Old English Text MT", cursive;
background-color: maroon;
color: yellow;
}
.forms input[type="submit"]:hover {
background-color: blue;
}
.radio-value {
font-family: Stencil, cursive;
font-size: 150%
}
img {
display: inline-block;
border-radius: 50%;
/* align: right; */
width: 250px;
margin-right: 100px;
/* padding-right: 40px; */
}
a:visited {
color: red;
}
a:hover {
color: rgb(51, 204, 51);
}
a:active {
color: blue;
}
.credits {
position: static;
text-align: left;
bottom: 0;
left: 0;
font-size: 20px;
}
......@@ -23,29 +23,16 @@
<version>4.12</version>
</dependency>
<dependency>
<groupId>jmock</groupId>
<artifactId>jmock-cglib</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>jmock</groupId>
<artifactId>jmock</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock</artifactId>
<version>2.9.0</version>
<scope>test</scope>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-junit4</artifactId>
<version>2.9.0</version>
<scope>test</scope>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.23.1</version>
</dependency>
</dependencies>
<build>
<plugins>
......
package ic.doc;
import java.util.List;
public class Query {
private List<String> query;
private String answer;
private String name;
private String description;
private String imgURL;
private String wikiURL;
public Query(List<String> query, String answer, String imgURL, String wikiURL) {
this.query = query;
this.answer = answer;
if(query != null) {
this.answer = this.answer.concat("\n");
public Query(String name, String description, String imgURL, String wikiURL) {
this.name = name;
this.description = description;
if(name != null) {
this.description = this.description.concat("\n");
}
this.imgURL = imgURL;
this.wikiURL = wikiURL;
}
public List<String> getQuery() {
return query;
public String getName() {
return name;
}
public String getAnswer() {
return answer;
public String getDescription() {
return description;
}
public String getImgURL() {
......
package ic.doc;
import ic.doc.web.DBReader;
import java.sql.ResultSet;
import java.util.*;
public class QueryProcessor {
/*Add a map mapping query to answer; change if to switch?;*/
private final Map<List<String>, String> queryMap = new HashMap<>();
private final List<Query> queriesMap = new ArrayList<>();
public QueryProcessor() {
/*this.queryMap*/
this.queriesMap.add(new Query(Arrays.asList("william", "shakespeare"),
"William Shakespeare (26 April 1564 - 23 April 1616) was an\n" +
"English poet, playwright, and actor, widely regarded as the greatest\n" +
"writer in the English language and the world's pre-eminent dramatist. \n",
"https://upload.wikimedia.org/wikipedia/commons/thumb/a/a2/Shakespeare.jpg/702px-Shakespeare.jpg",
"https://en.wikipedia.org/wiki/William_Shakespeare"));
this.queriesMap.add(new Query(Arrays.asList("isaac", "asimov"),
"Isaac Asimov (2 January 1920 - 6 April 1992) was an\n" +
"American writer and professor of Biochemistry, famous for\n" +
"his works of hard science fiction and popular science. \n",
"https://upload.wikimedia.org/wikipedia/commons/thumb/3/34/Isaac.Asimov01.jpg/800px-Isaac.Asimov01.jpg",
"https://en.wikipedia.org/wiki/Isaac_Asimov"));
this.queriesMap.add(new Query(Arrays.asList("alan", "mathison", "turing"),
"Alan Mathison Turing (23 June 1912 - 7 June 1954) was an English\n" +
"mathematician, computer scientist, logician, cryptanalyst, philosopher\n" +
"and theoretical biologist. Turing was highly influential in the\n" +
"development of theoretical computer science, providing a formalisation\n" +
"of the concepts of algorithm and computation with the Turing machine,\n" +
"which can be considered a model of a general-purpose computer. Turing\n" +
"is widely considered to be the father of theoretical computer science\n" +
"and artificial intelligence. Despite these accomplishments, he was\n" +
"never fully recognized in his home country during his lifetime due to\n" +
"his homosexuality, which was then a crime in the UK.",
"https://upload.wikimedia.org/wikipedia/commons/a/a1/Alan_Turing_Aged_16.jpg",
"https://en.wikipedia.org/wiki/Alan_Turing"));
this.queriesMap.add(new Query(Arrays.asList("augusta", "ada", "king", "lovelace"),
"Augusta Ada King, Countess of Lovelace (10 December 1815 - 27 November\n" +
"November 1852) was an English mathematician and writer, chiefly\n" +
"known for her work on Charles Babbage's proposed mechanical\n" +
"general-purpose computer, the Analytical Engine. She was the first to\n" +
"recognise that the machine had applications beyond pure calculation,\n" +
"and published the first algorithm intended to be carried out by such\n" +
"a machine. As a result, she is sometimes regarded as the first to\n" +
"recognise the full potential of a \"computing machine\" and the first\n" +
"computer programmer.",
"https://upload.wikimedia.org/wikipedia/commons/0/0b/Ada_Byron_daguerreotype_by_Antoine_Claudet_1843_or_1850.jpg",
"https://en.wikipedia.org/wiki/Ada_Lovelace"));
this.queriesMap.add(new Query(Arrays.asList("blaise", "pascal"),
"Blaise Pascal (19 June 1623 - 19 August 1662) was a French\n" +
"mathematician, physicist, inventor, writer and Catholic theologian.\n" +
"He was a child prodigy who was educated by his father, a tax collector\n" +
"in Rouen. Pascal's earliest work was in the natural and applied\n" +
"sciences where he made important contributions to the study of fluids,\n" +
"and clarified the concepts of pressure and vacuum by generalising the\n" +
"work of Evangelista Torricelli. Pascal also wrote in defence of the\n" +
"scientific method.",
"https://upload.wikimedia.org/wikipedia/commons/thumb/9/98/Blaise_Pascal_Versailles.JPG/800px-Blaise_Pascal_Versailles.JPG",
"https://en.wikipedia.org/wiki/Blaise_Pascal"));
this.queriesMap.add(new Query(Arrays.asList("william", "wordsworth"),
"William Wordsworth (7 April 1770 – 23 April 1850) was a \n "+
"major English Romantic poet who, with Samuel Taylor Coleridge, \n " +
"helped to launch the Romantic Age in English literature with \n "+
"their joint publication Lyrical Ballads (1798). \n" +
"Wordsworth's magnum opus is generally considered to be \n " +
"The Prelude, a semi-autobiographical poem of his early years \n"+
" that he revised and expanded a number of times. \n" +
" It was posthumously titled and published, before which \n"+
"it was generally known as \"the poem to Coleridge\". \n"+
"Wordsworth was Britain's poet laureate from 1843 \n"+
"until his death from pleurisy on 23 April 1850.",
"https://upload.wikimedia.org/wikipedia/commons/thumb/8/8c/Wordsworth_on_Helvellyn_by_Benjamin_Robert_Haydon.jpg/800px-Wordsworth_on_Helvellyn_by_Benjamin_Robert_Haydon.jpg",
"https://en.wikipedia.org/wiki/William_Wordsworth"));
final DBReader dbReader;
public QueryProcessor(DBReader dbReader) {
this.dbReader = dbReader;
}
public List<Query> process(String query) {
List<Query> possibilities = new ArrayList<>();
// this.queriesMap.add(new Query(Arrays.asList(""),
// ",
// "",
// ));
for(Query q : queriesMap) {
List<String> name = q.getQuery();
boolean found = true;
StringTokenizer queryTokens = new StringTokenizer(query);
public ResultSet process(String query) {
ResultSet possibilities = null;
StringTokenizer queryTokens = new StringTokenizer(query);
StringBuilder sqlQueryBuilder = new StringBuilder();
if (queryTokens.hasMoreTokens()) {
String token = queryTokens.nextToken().toLowerCase();
sqlQueryBuilder.append("SELECT * FROM PEOPLE WHERE identifier LIKE '%" + token + "%'");
while (queryTokens.hasMoreTokens()) {
found &= name.contains(queryTokens.nextToken().toLowerCase());
}
if (found) {
possibilities.add(q);
token = queryTokens.nextToken().toLowerCase();
sqlQueryBuilder.append(" AND identifier LIKE '%" + token+"%'");
}
sqlQueryBuilder.append(";");
String sqlQuery = sqlQueryBuilder.toString();
possibilities = dbReader.executeSQLQuery(sqlQuery);
}
if (possibilities.isEmpty()) {
List<Query> noQueryFound = new ArrayList<>();
noQueryFound.add(new Query(null, "", "", ""));
return noQueryFound;
}
return possibilities;
}
}
......@@ -9,6 +9,8 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class WebServer {
......@@ -24,25 +26,63 @@ public class WebServer {
}
static class Website extends HttpServlet {
final DBReader dbReader;
final QueryProcessor queryProcessor;
public Website(){
dbReader = new DBReader();
queryProcessor = new QueryProcessor(dbReader);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String query = req.getParameter("q");
String type = req.getParameter("type");
if (query == null || type == null) {
new IndexPage().writeTo(resp);
String type = req.getParameter("type"); // || type == null
if (query == null) {
if(type != null) {
String[] queryAndType = type.split(",");
String[] q = queryAndType[0].split("\\+");
renderResultsPage(String.join(" ", q), queryAndType[1], resp);
} else {
new IndexPage().writeTo(resp);
}
} else {
QueryProcessor queryProcessor = new QueryProcessor();
List<Query> possibilities = queryProcessor.process(query);
if (possibilities.size() == 1) {
Query answer = possibilities.get(0);
renderResultsPage(query, "html", resp);
}
}
private void renderResultsPage(String query, String type, HttpServletResponse resp) throws IOException {
ResultSet queryRes = queryProcessor.process(query);
List<Query> possibilities = new ArrayList<>();
if (queryRes != null) {
try {
while (queryRes.next()) {
String name = queryRes.getString("name");
String description = queryRes.getString("description");
String imgUrl = queryRes.getString("imgUrl");
String wikiUrl = queryRes.getString("wikiUrl");
possibilities.add(new Query(name, description, imgUrl, wikiUrl));
}
} catch (Exception e) {
System.out.println("Unknown error occurred");
}
}
if (possibilities.size() == 1) {
Query answer = possibilities.get(0);
if(type != null) {
if (type.equals("html")) {
new HTMLResultPage(query, answer).writeTo(resp);
} else {
new DownloadPage(query, answer, type).writeTo(resp);
}
} else {
new ChoicePage(query, possibilities).writeTo(resp);
}
} else if (possibilities.size() > 1 ){
new ChoicePage(query, possibilities).writeTo(resp);
}
else {
new HTMLResultPage("", null).writeTo(resp);
}
}
}
......
......@@ -23,7 +23,11 @@ public class ChoicePage implements Page {
// Header
writer.println("<html>");
writer.println("<head><title>" + ambiguousQuery + "</title></head>");
writer.println("<head>");
writer.println("<style>");
writer.println(htmlStyle);
writer.println("</style>");
writer.println("<title>" + ambiguousQuery + "</title></head>");
writer.println("<body>");
// Content - list of hyperlinked choices
......@@ -31,9 +35,9 @@ public class ChoicePage implements Page {
"<p>" + ambiguousQuery + " gave many results. Select which result you would like to view " +
"<br><br>");
while (!possibilities.isEmpty()) {
List<String> nameStrings = possibilities.get(0).getQuery();
String name = String.join(" ", nameStrings);
String ref = String.join("+", nameStrings);
String nameString = possibilities.get(0).getName();
String name = String.join(" ", nameString);
String ref = String.join("+", nameString);
writer.print("<a href=\"/?q=" + ref + "&type=html\">" + name + "</a>" +
"<br><br>");
possibilities.remove(0);
......
package ic.doc.web;
import java.sql.*;
public class DBReader {
Connection wiki;
Statement state;
public DBReader(){
try {
Class.forName("org.sqlite.JDBC");
wiki = DriverManager.getConnection("jdbc:sqlite:wiki.db");
state = wiki.createStatement();
} catch (Exception e) {
System.out.println("Failed to open database/statement");
System.exit(0);
}
}
public ResultSet executeSQLQuery(String query) {
ResultSet resultSet = null;
try {
resultSet = state.executeQuery(query);
} catch (SQLException e) {
System.out.println("Failed to execute query");
System.exit(0);
}
return resultSet;
}
}
......@@ -26,7 +26,7 @@ public class DownloadPage implements Page{
resp.setContentType("text/markdown");
}
if (answer == null || answer.getAnswer() == null || answer.getAnswer().isEmpty()) {
if (answer == null || answer.getDescription() == null || answer.getDescription().isEmpty()) {
resp.setHeader("Content-Disposition", "attachment;filename=\"sorry." + format + "\"");
PrintWriter writer = resp.getWriter();
writer.println("#Sorry");
......@@ -50,7 +50,7 @@ public class DownloadPage implements Page{
tmp.deleteOnExit();
FileWriter fw = new FileWriter(tmp);
fw.write("#" + query + "\n");
fw.write(answer.getAnswer());
fw.write(answer.getDescription());
fw.write("![alt text]("+answer.getImgURL()+" \"Pic\")");
fw.write("[Learn More!]("+answer.getWikiURL()+")");
fw.close();
......
......@@ -22,21 +22,39 @@ public class HTMLResultPage implements Page {
// Header
writer.println("<html>");
writer.println("<head><title>" + query + "</title></head>");
writer.println("<head>"+
"<style>"+htmlStyle+"</style>"+
"<title>" + query + "</title></head>");
writer.println("<body>");
// Content
if (answer == null || answer.getAnswer() == null || answer.getAnswer().isEmpty()) {
if (answer == null || answer.getDescription() == null || answer.getDescription().isEmpty()) {
writer.println("<h1>Sorry</h1>");
writer.print("<p>Sorry, we didn't understand <em>" + query + "</em></p>");
writer.println("<p><a href=\"/\">Back to Search Page</a></p>");
} else {
writer.println("<h1>" + query + "</h1>");
writer.println("<p>" + answer.getAnswer().replace("\n", "<br>") + "</p>");
writer.println("<p> <img src=\"" + answer.getImgURL() + "\"> </p>");
String name = String.join("+", answer.getName());
writer.println("<header>");
writer.println("<h1>" + answer.getName() + "</h1>");
writer.println("</header>");
writer.println("<p class=\"information\">" + answer.getDescription().replace("\n", "<br>") + "</p>");
writer.println("<img src=\"" + answer.getImgURL() + "\">");
writer.println("<form>");
writer.println("<button type=\"submit\" name=\"type\" value=\""+name+",markdown\"> Markdown </button>");
writer.println("<button type=\"submit\" name=\"type\" value=\""+name+",pdf\"> PDF </button>");
writer.println("</form>");
writer.println("<p><a href=\""+answer.getWikiURL()+"\" target=\"_blank" +
"\">Learn More!</a></p>");
writer.println("<p><a href=\"/\">Back to Search Page</a></p>");
writer.println("<div class=\"credits\">");
writer.println("<a href=\"" + answer.getImgURL() + "\"> Image credits: from wikipedia");
writer.println("</div>");
}
......
......@@ -12,10 +12,11 @@ public class IndexPage implements Page {
// Header
writer.println("<html>");
writer.println("<head>" +
"<title>Welcome</title>" +
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/\">" +
"</head>");
writer.println("<head>");
writer.println("<style>");
writer.println(htmlStyle);
writer.println("</style>");
writer.println("<title>Welcome</title></head>");
writer.println("<body>");
// Content
......@@ -24,12 +25,6 @@ public class IndexPage implements Page {
"<form>" +
"<input type=\"text\" name=\"q\" />" +
"<br><br>" +
"<input type=\"radio\" name=\"type\" value=\"html\"> HTML" +
"<br>" +
"<input type=\"radio\" name=\"type\" value=\"markdown\"> Download Markdown File" +
"<br>" +
"<input type=\"radio\" name=\"type\" value=\"pdf\"> PDF" +
"<br><br>" +
"<input type=\"submit\">" +
"</form>" +
"</p>");
......
......@@ -4,6 +4,112 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public interface Page {
String htmlStyle = "body {\n" +
" text-align: center;\n" +
" background-color: rgb(255, 255, 153);\n" +
" /* background-image: url(\"https://ae01.alicdn.com/kf/HTB1nAkRi9YH8KJjSspdq6ARgVXa4/white-damask-gray-grey-Floral-pattern-Background-Vinyl-cloth-High-quality-Computer-print-wall-photo-backdrop.jpg\"); */\n" +
"}\n" +
"\n" +
"header {\n" +
" position: sticky;\n" +
" top: 0;\n" +
" left: 0;\n" +
" right: 0;\n" +
" background-color: #ff9966;\n" +
"}\n" +
"\n" +
"h1 {\n" +
" color: teal;\n" +
" font-family: \"Old English Text MT\", cursive;\n" +
" /* background-color: maroon; */\n" +
" font-size: 100px;\n" +
" width: 100%;\n" +
"\n" +
"}\n" +
"\n" +
".information {\n" +
" text-align: left;\n" +
" width: 50%;\n" +
" left: 0;\n" +
" display: inline-block;\n" +
" font-size: 150%;\n" +
"}\n" +
"\n" +
"p {\n" +
" font-size: 200%;\n" +
" /* display: inline-block; */\n" +
"\n" +
" font-family: cursive;\n" +
" /* background-color: rebeccapurple; */\n" +
"}\n" +
"\n" +
<