1 /**
2 * Copyright 2007 Art Technology Group, Inc (ATG)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and limitations under the License.
13 */
14
15
16 package atg.taglib.json;
17
18 import atg.taglib.json.util.JSONArray;
19 import atg.taglib.json.util.JSONException;
20 import atg.taglib.json.util.JSONObject;
21
22 import java.io.IOException;
23
24 import org.apache.commons.httpclient.HttpClient;
25 import org.apache.commons.httpclient.HttpException;
26 import org.apache.commons.httpclient.HttpMethod;
27 import org.apache.commons.httpclient.HttpStatus;
28 import org.apache.commons.httpclient.methods.GetMethod;
29
30 /**
31 * Helper class for running taglib tests
32 *
33 * @author James Wiltshire
34 * @version $Id$
35 */
36
37 public class Helper
38 {
39 private static final String SERVER_URL=
40 "http://localhost:"+System.getProperty("containerPort")+"/json-taglib/";
41
42 /**
43 * Get a response from the server for a test
44 *
45 * @param pTestUrl The test url, relative to the tests context root
46 * @return the <code>ResponseData</code> object for this test
47 * @throws IOException
48 * @throws HttpException
49 * @throws JSONException
50 */
51 public static ResponseData getData(String pType, int pTestNum) throws HttpException, IOException, JSONException{
52
53 HttpClient client = new HttpClient();
54 HttpMethod method = new GetMethod(getTestUrl(pType,pTestNum));
55
56
57 int statusCode=client.executeMethod(method);
58 String responseBody=method.getResponseBodyAsString();
59 method.releaseConnection();
60
61
62 ResponseData data=new ResponseData();
63 data.body=responseBody.trim();
64 data.statusCode=statusCode;
65 if (statusCode==HttpStatus.SC_OK){
66
67 data.json=parseJsonTextToObject(responseBody);
68 }
69
70
71 method = new GetMethod(getStatus200ResultUrl(pType,pTestNum));
72 data.expectedStatusCode=HttpStatus.SC_OK;
73
74
75 statusCode=client.executeMethod(method);
76
77 if (statusCode==HttpStatus.SC_NOT_FOUND){
78
79 method.releaseConnection();
80 method = new GetMethod(getStatus500ResultUrl(pType,pTestNum));
81 statusCode=client.executeMethod(method);
82 data.expectedStatusCode=HttpStatus.SC_INTERNAL_SERVER_ERROR;
83 }
84
85 responseBody=method.getResponseBodyAsString().trim();
86 method.releaseConnection();
87
88
89 if (data.expectedStatusCode==HttpStatus.SC_OK){
90
91 data.expectedJson=parseJsonTextToObject(responseBody);
92 }
93 else{
94
95 data.expectedMsgKey=responseBody.trim();
96 }
97
98 return data;
99 }
100
101 /**
102 * Get the URL to invoke for a particular test
103 *
104 * @param pType
105 * @param pTestNum
106 * @return
107 */
108 private static String getTestUrl(String pType, int pTestNum)
109 {
110 return SERVER_URL+"tests/"+pType+"/"+pTestNum+".jsp";
111 }
112
113 /**
114 * Get the URL to receive the response for a test that has a status
115 * of 200 OK
116 *
117 * @param pType
118 * @param pTestNum
119 * @return
120 */
121 private static String getStatus200ResultUrl(String pType, int pTestNum)
122 {
123 return SERVER_URL+"results/200_OK/"+pType+"/"+pTestNum+".jsp";
124 }
125
126 /**
127 * Get the URL to receive the response for a test that has a status
128 * of 500 SERVER_ERROR
129 *
130 * @param pType
131 * @param pTestNum
132 * @return
133 */
134 private static String getStatus500ResultUrl(String pType, int pTestNum)
135 {
136 return SERVER_URL+"results/500_ERROR/"+pType+"/"+pTestNum+".jsp";
137 }
138
139 private static Object parseJsonTextToObject(String pJsonText) throws JSONException
140 {
141 if (pJsonText.trim().startsWith("[")){
142
143 return new JSONArray(pJsonText);
144 }
145
146
147 return new JSONObject(pJsonText);
148 }
149
150 }
151