[kwlug-disc] Requirements for test
Joe Wennechuk
youcanreachmehere at hotmail.com
Wed Dec 31 14:35:38 EST 2014
Here are the requirements for my test that I have done..
================================================= Prerequisities------------------------------------------------- - JDK 1.6 installed with JAVA_HOME env variable set to this location
================================================= Instructions------------------------------------------------- - Implement link counter that returns the number of HTML links on each HTTP address provided as a command line argument: Ex.: $ java -jar build/libs/linkcounter.jar http://acme.com/ http://mysitewithlinks/ http://192.168.2.3/ http://acme.com/ 66 http://mysitewithlinks/ 23989 http://192.168.2.3/ 9456 - Implementation constrains: * For simplification assume that the link is defined as '<[whitespace]a[whitespace]' or '<[whitespace]A[whitespace]'. ('<a ', '< a h', '<A >', '<a attr=' are all valid links) * All the processed pages use 8 bit encoding * DO NOT use any 3rd party libraries, just what you get from JRE * The html pages to process can be very, VERY large! * Process the list in parallel. * You may write tests to src/test/java using provided libraries (junit, hamcrest, mockito)
================================================= Building and Testing------------------------------------------------- - To build the project execute "./gradlew build" - To execute tests gradlew build "./gradlew test"
================================================= Running the Application------------------------------------------------- - To run the application do the following from the current (this) directory: java -jar build/libs/linkcounter.jar http://www.acme.com/ http://mysitewithlinks/ http://192.168.2.3/ And this is the code I wrote, please critique as much as you can, as I am not a developer..
package com.acme;/** * Description: This class contains main method that returns the number of HTML links on each address provided * by command line argument: * Ex.: * <pre> * $ java -jar linkcounter.jar http://acme.com/ http://mysitewithlinks/ http://192.168.2.3/ * http://acme.com/ 227 * http://mysitewithlinks/ 23989 * http://192.168.2.3/ 9456 * </pre> * * See README for more information. * * <p>Copyright 2000-2012, NetSuite, Inc.</p> */import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;import java.util.regex.Pattern;import java.util.regex.Matcher;import java.io.IOException;import java.net.MalformedURLException;
public class LinkCounter extends Thread{ private String url; private int linkCount; public LinkCounter(String url){ this.url = url; this.linkCount = 0; } public String getURL(){ return url; } public int getLinkCount(){ return linkCount; } public int getCount(String url){ try{ URL website = new URL(url); URLConnection connection = website.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null){ linkCount += countLinks(inputLine); } } catch (MalformedURLException e){ e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } return linkCount; } public int countLinks(String inputLine){ int links = 0; Pattern pattern = Pattern.compile("<a[^>]*>(.*?)</a>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(inputLine); while (matcher.find()) links ++; return links; } @Override public void run(){ getCount(this.url); System.out.println(getURL() + "/ " + getLinkCount()); } public static void main(String args[]) { for(int i = 0; i < args.length; i++){ new LinkCounter(args[i]).start(); } }}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://kwlug.org/pipermail/kwlug-disc_kwlug.org/attachments/20141231/3fd38208/attachment.htm>
More information about the kwlug-disc
mailing list