浏览代码

Implementation of the first tests

amizukisato 9 年之前
父节点
当前提交
116877c9df

+ 17 - 0
.classpath

@@ -0,0 +1,17 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<classpath>
3
+	<classpathentry kind="src" path="src"/>
4
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5
+	<classpathentry kind="lib" path="lib/commons-codec-1.9.jar"/>
6
+	<classpathentry kind="lib" path="lib/commons-logging-1.2.jar"/>
7
+	<classpathentry kind="lib" path="lib/fluent-hc-4.5.jar"/>
8
+	<classpathentry kind="lib" path="lib/httpclient-4.5.jar"/>
9
+	<classpathentry kind="lib" path="lib/httpclient-cache-4.5.jar"/>
10
+	<classpathentry kind="lib" path="lib/httpclient-win-4.5.jar"/>
11
+	<classpathentry kind="lib" path="lib/httpcore-4.4.1.jar"/>
12
+	<classpathentry kind="lib" path="lib/httpmime-4.5.jar"/>
13
+	<classpathentry kind="lib" path="lib/jna-4.1.0.jar"/>
14
+	<classpathentry kind="lib" path="lib/jna-platform-4.1.0.jar"/>
15
+	<classpathentry kind="lib" path="lib/json-20090211.jar"/>
16
+	<classpathentry kind="output" path="bin"/>
17
+</classpath>

+ 13 - 12
.gitignore

@@ -1,12 +1,13 @@
1
-*.class
2
-
3
-# Mobile Tools for Java (J2ME)
4
-.mtj.tmp/
5
-
6
-# Package Files #
7
-*.jar
8
-*.war
9
-*.ear
10
-
11
-# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
12
-hs_err_pid*
1
+*.class
2
+
3
+# Mobile Tools for Java (J2ME)
4
+.mtj.tmp/
5
+
6
+# Package Files #
7
+*.jar
8
+*.war
9
+*.ear
10
+
11
+# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
12
+hs_err_pid*
13
+/bin/

+ 17 - 0
.project

@@ -0,0 +1,17 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<projectDescription>
3
+	<name>Servelt_Test</name>
4
+	<comment></comment>
5
+	<projects>
6
+	</projects>
7
+	<buildSpec>
8
+		<buildCommand>
9
+			<name>org.eclipse.jdt.core.javabuilder</name>
10
+			<arguments>
11
+			</arguments>
12
+		</buildCommand>
13
+	</buildSpec>
14
+	<natures>
15
+		<nature>org.eclipse.jdt.core.javanature</nature>
16
+	</natures>
17
+</projectDescription>

+ 0 - 15
src/Main.java

@@ -1,15 +0,0 @@
1
-
2
-public class Main {
3
-
4
-	public static void main (String [] args){
5
-		TestServletGet c = new TestServletGet();
6
-		String test = c.getHTML("http://localhost:8080/SnailServlet/data?method=get_categories&user_id=2");
7
-		System.out.println(test);
8
-		
9
-		
10
-		String url = "http://localhost:8080/SnailServlet/user";
11
-		TestServletPost d = new TestServletPost(url);
12
-		
13
-	}
14
-
15
-}

+ 0 - 33
src/TestServletGet.java

@@ -1,33 +0,0 @@
1
-import java.io.BufferedReader;
2
-import java.io.*;
3
-import java.net.*;
4
-import java.io.IOException;
5
-
6
-public class TestServletGet {
7
-
8
-	public String getHTML(String urlToRead){
9
-		URL url;
10
-		HttpURLConnection conn;
11
-		BufferedReader rd;
12
-		String line;
13
-		StringBuilder result = new StringBuilder();
14
-		try{
15
-			url = new URL(urlToRead);
16
-			conn = (HttpURLConnection) 
17
-			url.openConnection();
18
-			conn.setRequestMethod("GET");
19
-			rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
20
-			while ((line = rd.readLine()) != null){
21
-				result.append(line);
22
-			}
23
-			rd.close();
24
-		} catch (IOException e){
25
-			e.printStackTrace();
26
-		} //catch (IOException e){
27
-			//e.printStackTrace();
28
-		//}	
29
-			return result.toString();
30
-	}
31
-}
32
-
33
-

+ 0 - 64
src/TestServletPost.java

@@ -1,64 +0,0 @@
1
-import java.io.BufferedReader;
2
-import java.io.IOException;
3
-import java.io.InputStream;
4
-import java.io.InputStreamReader;
5
-import java.util.ArrayList;
6
-import java.util.List;
7
-
8
-import org.apache.http.HttpEntity;
9
-import org.apache.http.HttpResponse;
10
-import org.apache.http.NameValuePair;
11
-import org.apache.http.client.HttpClient;
12
-import org.apache.http.client.entity.UrlEncodedFormEntity;
13
-import org.apache.http.client.methods.HttpPost;
14
-import org.apache.http.impl.client.HttpClients;
15
-import org.apache.http.message.BasicNameValuePair;
16
-
17
-
18
-public class TestServletPost{
19
-
20
-
21
-	public TestServletPost(String url){
22
-		
23
-		
24
-		InputStream instream = null;
25
-
26
-		try {
27
-			HttpClient httpclient = HttpClients.createDefault();
28
-			HttpPost httppost = new HttpPost(url);
29
-
30
-			// Request parameters and other properties.
31
-			List<NameValuePair> params = new ArrayList<NameValuePair>(2);
32
-			params.add(new BasicNameValuePair("method", "create_user"));
33
-			params.add(new BasicNameValuePair("name", "Gleisson"));
34
-			params.add(new BasicNameValuePair("bornDate", "1987/26/04"));
35
-			params.add(new BasicNameValuePair("sex", "m"));
36
-			params.add(new BasicNameValuePair("password", "SAUDE"));
37
-			params.add(new BasicNameValuePair("email", "sdrhgdsaj@sjhsdehj.com"));
38
-			httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
39
-
40
-			//Execute and get the response.
41
-			HttpResponse response = httpclient.execute(httppost);
42
-			HttpEntity entity = response.getEntity();
43
-
44
-			if (entity != null) {
45
-				 instream = entity.getContent();
46
-				 BufferedReader rd = new BufferedReader(new InputStreamReader(instream));
47
-				 String line;
48
-				while((line = rd.readLine()) != null){
49
-					System.out.println(line);
50
-					
51
-				}
52
-			}}
53
-			catch (Exception e){
54
-			}finally {
55
-				try {
56
-					instream.close();
57
-				} catch (IOException e) {
58
-					// TODO Auto-generated catch block
59
-					e.printStackTrace();
60
-				}
61
-			}
62
-			
63
-		
64
-	}}

+ 30 - 0
src/test/resource/ExecuteGet.java

@@ -0,0 +1,30 @@
1
+package test.resource;
2
+import java.io.BufferedReader;
3
+import java.io.*;
4
+import java.net.*;
5
+import java.io.IOException;
6
+
7
+public class ExecuteGet {
8
+
9
+	public String get(String urlToRead) throws IOException{
10
+		
11
+		URL url;
12
+		HttpURLConnection conn;
13
+		BufferedReader rd;
14
+		String line;
15
+		StringBuilder result = new StringBuilder();
16
+
17
+		url = new URL(urlToRead);
18
+		conn = (HttpURLConnection) 
19
+				url.openConnection();
20
+		conn.setRequestMethod("GET");
21
+		rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
22
+		while ((line = rd.readLine()) != null){
23
+			result.append(line);
24
+		}
25
+		rd.close();
26
+		return result.toString();
27
+	}
28
+}
29
+
30
+

+ 57 - 0
src/test/resource/ExecutePost.java

@@ -0,0 +1,57 @@
1
+package test.resource;
2
+import java.io.BufferedReader;
3
+
4
+import java.io.InputStream;
5
+import java.io.InputStreamReader;
6
+import java.util.ArrayList;
7
+import java.util.List;
8
+import java.util.Map;
9
+
10
+import org.apache.http.HttpEntity;
11
+import org.apache.http.HttpResponse;
12
+import org.apache.http.NameValuePair;
13
+import org.apache.http.client.HttpClient;
14
+import org.apache.http.client.entity.UrlEncodedFormEntity;
15
+import org.apache.http.client.methods.HttpPost;
16
+import org.apache.http.impl.client.HttpClients;
17
+import org.apache.http.message.BasicNameValuePair;
18
+
19
+
20
+public class ExecutePost{
21
+
22
+
23
+
24
+	public String execute(String url, Map<String, String> parameters) throws Exception {
25
+
26
+		InputStream instream = null;
27
+
28
+		HttpClient httpclient = HttpClients.createDefault();
29
+		HttpPost httppost = new HttpPost(url);
30
+		List<NameValuePair> params = new ArrayList<NameValuePair>();
31
+		StringBuilder sb = new StringBuilder();
32
+		
33
+		
34
+		for (Map.Entry<String,String> entry : parameters.entrySet()) {
35
+		    String key = entry.getKey();
36
+		    String value  = entry.getValue();
37
+		    params.add(new BasicNameValuePair(key, value));
38
+		    
39
+		}
40
+		
41
+		httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
42
+		
43
+		HttpResponse response = httpclient.execute(httppost);
44
+		HttpEntity entity = response.getEntity();
45
+
46
+		if (entity != null) {
47
+			instream = entity.getContent();
48
+			BufferedReader rd = new BufferedReader(new InputStreamReader(instream));
49
+			String line;
50
+			while((line = rd.readLine()) != null){
51
+				sb.append(line);
52
+
53
+			}
54
+		}
55
+		
56
+		return sb.toString();
57
+	}}

+ 39 - 0
src/test/resource/Run.java

@@ -0,0 +1,39 @@
1
+package test.resource;
2
+
3
+import java.util.ArrayList;
4
+import java.util.List;
5
+
6
+import test.servlet.event.get.TestGetCategories;
7
+import test.servlet.event.get.TestImportDataFacebook;
8
+import test.servlet.event.get.TestSearch;
9
+import test.servlet.event.post.TestCreateCategory;
10
+import test.servlet.event.post.TestSetCategory;
11
+import test.servlet.user.post.TestCreateUser;
12
+import test.servlet.user.post.TestLogin;
13
+
14
+public class Run {
15
+
16
+	public static void main (String [] args){
17
+		
18
+		List<ServletTest> tests = new ArrayList<ServletTest>();
19
+		
20
+		tests.add(new TestImportDataFacebook() );
21
+		tests.add(new TestGetCategories());
22
+		tests.add(new TestSearch());
23
+		
24
+		
25
+		tests.add(new TestCreateCategory());
26
+		tests.add(new TestSetCategory());
27
+		
28
+		
29
+		tests.add(new TestCreateUser());
30
+		tests.add(new TestLogin());
31
+		
32
+		for(ServletTest test : tests)
33
+		{
34
+			test.runTest();
35
+		}
36
+		
37
+	}
38
+
39
+}

+ 39 - 0
src/test/resource/ServletTest.java

@@ -0,0 +1,39 @@
1
+package test.resource;
2
+
3
+import org.json.JSONArray;
4
+import org.json.JSONObject;
5
+
6
+public abstract class ServletTest {
7
+	
8
+	
9
+	public void runTest()
10
+	{
11
+		try{
12
+			
13
+			if(this.test())
14
+				System.out.println("Test "+ this.getClass().getSimpleName() + " ......pass");
15
+			else
16
+				System.err.println("Test "+ this.getClass().getSimpleName() + " ......fail");
17
+				
18
+		}catch(Exception e)
19
+		{
20
+			e.printStackTrace();
21
+			System.err.println("Test "+ this.getClass().getSimpleName() + " ......fail");
22
+		}
23
+		
24
+		
25
+	}
26
+	
27
+	protected abstract boolean test() throws Exception;
28
+	
29
+	public String domain = "http://localhost:8080/SnailServlet/";
30
+	public abstract String buildUrl();
31
+	
32
+	protected boolean analyse(String result) throws Exception{
33
+		
34
+		
35
+		JSONArray json = new JSONArray(result);
36
+		return json.getJSONObject(1).getBoolean("request");
37
+	}
38
+	
39
+}

+ 18 - 0
src/test/resource/ServletTestGet.java

@@ -0,0 +1,18 @@
1
+package test.resource;
2
+
3
+public abstract class ServletTestGet extends ServletTest{
4
+
5
+	@Override
6
+	public boolean test() throws Exception {
7
+		
8
+		ExecuteGet executor = new ExecuteGet();
9
+		String url = domain +this.buildUrl();
10
+		String result = executor.get(url);
11
+		return this.analyse(result);
12
+		
13
+	}
14
+	
15
+	
16
+	
17
+
18
+}

+ 44 - 0
src/test/resource/ServletTestPost.java

@@ -0,0 +1,44 @@
1
+package test.resource;
2
+
3
+import java.util.Map;
4
+
5
+import org.json.JSONArray;
6
+
7
+import javafx.util.Pair;
8
+
9
+public abstract class ServletTestPost extends ServletTest {
10
+
11
+	@Override
12
+	public boolean test() throws Exception {
13
+		ExecutePost post = new ExecutePost();
14
+
15
+
16
+		String url = domain +this.buildUrl();
17
+		Map<String , String > parameters = this.buildParameters();
18
+		String result = post.execute(url, parameters);
19
+		return this.analyse(result);
20
+	}
21
+
22
+	public abstract Map<String , String> buildParameters();
23
+
24
+	public abstract Pair<String, String> buildCheckSum();
25
+
26
+	@Override
27
+	protected boolean analyse(String result ) throws Exception
28
+	{
29
+	System.out.println(result);
30
+		JSONArray json = new JSONArray(result);
31
+		boolean firstResult = super.analyse(result);
32
+		Pair<String, String > check = this.buildCheckSum();
33
+		
34
+		String value = json.getJSONObject(0).getString(check.getKey());
35
+	
36
+		
37
+		boolean secondResult = value.equals(check.getValue());
38
+		
39
+		return firstResult&&secondResult;
40
+		
41
+
42
+	}
43
+
44
+}

+ 13 - 0
src/test/servlet/event/get/TestGetCategories.java

@@ -0,0 +1,13 @@
1
+package test.servlet.event.get;
2
+
3
+import test.resource.ServletTestGet;
4
+
5
+public class TestGetCategories  extends ServletTestGet{
6
+
7
+	@Override
8
+	public String buildUrl() {
9
+		// TODO Auto-generated method stub
10
+		return "data?method=get_categories&user_id=2";
11
+	}
12
+
13
+}

+ 21 - 0
src/test/servlet/event/get/TestImportDataFacebook.java

@@ -0,0 +1,21 @@
1
+package test.servlet.event.get;
2
+
3
+import test.resource.ServletTestGet;
4
+
5
+public class TestImportDataFacebook extends ServletTestGet{
6
+
7
+
8
+
9
+	@Override
10
+	public String buildUrl() {
11
+	
12
+		
13
+		return "data?method=import_data&"
14
+				+ "token=CAABvo49PZBskBAFbAkwfl0SV9gK0yZARhciNoCrZBxgE6KZB8MNLmAGt6rKXxuyomndCmuJGQhSp5KXwgfZBtLKmu6VvzZBcgeVrNr3eAI2h78UDOBOMFayFt9RdIdj13I670xeiZAZAAbTJ6tvt8sQEPZCtknilnTgqlyOxZB8PSDIQtyrNVVKJKm3RyKudF995UA5og6hHmsaAZDZD&socialnetwork=facebook&quantity=4&user_id=1";
15
+	}
16
+	
17
+	
18
+
19
+	
20
+
21
+}

+ 15 - 0
src/test/servlet/event/get/TestSearch.java

@@ -0,0 +1,15 @@
1
+package test.servlet.event.get;
2
+
3
+import test.resource.ServletTestGet;
4
+
5
+public class TestSearch extends ServletTestGet{
6
+
7
+	@Override
8
+	public String buildUrl() {
9
+		// TODO Auto-generated method stub
10
+		return "data?method=search&user_id=2&start_date=2005-12-26&end_date=2012-02-14&categories_id=2,4";
11
+	}
12
+
13
+	
14
+	
15
+}

+ 35 - 0
src/test/servlet/event/post/TestCreateCategory.java

@@ -0,0 +1,35 @@
1
+package test.servlet.event.post;
2
+
3
+import java.util.HashMap;
4
+import java.util.Map;
5
+
6
+import javafx.util.Pair;
7
+import test.resource.ServletTestPost;
8
+
9
+public class TestCreateCategory extends ServletTestPost {
10
+
11
+	@Override
12
+	public Map<String, String> buildParameters() {
13
+		// TODO Auto-generated method stub
14
+		Map <String , String> parameters = new HashMap<String , String>();
15
+		parameters.put("name", "Radialize");
16
+		parameters.put("color", "green");
17
+		parameters.put("icon", "logo");
18
+		return parameters;
19
+		
20
+	}
21
+
22
+	@Override
23
+	public String buildUrl() {
24
+		// TODO Auto-generated method stub
25
+		return "data?method=create_category";
26
+	}
27
+
28
+	@Override
29
+	public Pair<String, String> buildCheckSum() {
30
+		// TODO Auto-generated method stub
31
+		return new Pair<String, String>("name", "Radialize");
32
+	}
33
+
34
+	
35
+}

+ 39 - 0
src/test/servlet/event/post/TestSetCategory.java

@@ -0,0 +1,39 @@
1
+package test.servlet.event.post;
2
+
3
+import java.util.HashMap;
4
+import java.util.Map;
5
+
6
+import javafx.util.Pair;
7
+import test.resource.ServletTestPost;
8
+
9
+public class TestSetCategory extends ServletTestPost {
10
+
11
+	@Override
12
+	public Map<String, String> buildParameters() {
13
+		// TODO Auto-generated method stub
14
+		Map <String , String> parameters = new HashMap<String , String>();
15
+		parameters.put("category_id", "29");
16
+		parameters.put("item_id", "30");
17
+		
18
+		return parameters;
19
+		
20
+		
21
+	}
22
+
23
+	@Override
24
+	public String buildUrl() {
25
+		// TODO Auto-generated method stub
26
+		return "data?method=set_cotegory";
27
+	}
28
+
29
+	@Override
30
+	public Pair<String, String> buildCheckSum() {
31
+		// TODO Auto-generated method stub
32
+		return new Pair<String, String>("categoryId", "29");
33
+	}
34
+	
35
+	
36
+	
37
+	
38
+
39
+}

+ 40 - 0
src/test/servlet/user/post/TestCreateUser.java

@@ -0,0 +1,40 @@
1
+package test.servlet.user.post;
2
+
3
+import java.util.HashMap;
4
+import java.util.Map;
5
+
6
+import javafx.util.Pair;
7
+import test.resource.ServletTestPost;
8
+
9
+public class TestCreateUser extends ServletTestPost {
10
+
11
+	@Override
12
+	public Map<String, String> buildParameters() {
13
+		Map <String , String> parameters = new HashMap<String , String>();
14
+		parameters.put("name", "Fernanda");
15
+		parameters.put("born_date", "1991-04-22");
16
+		parameters.put("sex", "m");
17
+		parameters.put("password", "99719797");
18
+		parameters.put("email", "ailtonjapaitapeva@idealize.com");
19
+		
20
+		
21
+		
22
+		return parameters;
23
+		
24
+	}
25
+
26
+	@Override
27
+	public String buildUrl() {
28
+		// TODO Auto-generated method stub
29
+		return "user?method=create_user";
30
+	}
31
+
32
+	@Override
33
+	public Pair<String, String> buildCheckSum() {
34
+		// TODO Auto-generated method stub
35
+		return new Pair<String, String>("name", "Fernanda");
36
+	}
37
+	
38
+	
39
+	
40
+}

+ 40 - 0
src/test/servlet/user/post/TestLogin.java

@@ -0,0 +1,40 @@
1
+package test.servlet.user.post;
2
+
3
+import java.util.HashMap;
4
+import java.util.Map;
5
+
6
+import javafx.util.Pair;
7
+import test.resource.ServletTestPost;
8
+
9
+public class TestLogin  extends  ServletTestPost{
10
+
11
+	@Override
12
+	public Map<String, String> buildParameters() {
13
+		// TODO Auto-generated method stub
14
+		Map <String , String> parameters = new HashMap<String , String>();
15
+		
16
+		parameters.put("password", "99719797");
17
+		parameters.put("email", "ailtonjapaitapeva@idealize.com");
18
+		
19
+		
20
+		
21
+		return parameters;
22
+		
23
+	}
24
+
25
+	@Override
26
+	public String buildUrl() {
27
+		// TODO Auto-generated method stub
28
+		return "user?method=login";
29
+	}
30
+
31
+	@Override
32
+	public Pair<String, String> buildCheckSum() {
33
+		// TODO Auto-generated method stub
34
+		return new Pair<String, String>("name","Fernanda" );
35
+	}
36
+	
37
+	
38
+	
39
+
40
+}