source: trunk/autoquest-plugin-php/src/main/java/de/ugoe/cs/autoquest/plugin/php/eventcore/WebRequest.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 5.3 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       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
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.plugin.php.eventcore;
16
17import java.util.ArrayList;
18import java.util.List;
19
20import de.ugoe.cs.autoquest.IReplayDecorator;
21import de.ugoe.cs.autoquest.eventcore.IReplayable;
22
23/**
24 * <p>
25 * Contains all information related to a web request, i.e., the path, the POST variables and the GET
26 * variables. The generated replay are for the command line tool {@code curl}. The requests do not
27 * contain correct values for the POST and GET request. Instead, only the parameters that are part
28 * of the requests are added and the values of the parameters are DATA_$PARAMNAME$_DATA, where
29 * $PARAMNAME$ is the upper case string of the parameter name. This allows test data generators to
30 * insert concrete values, as EventBench does not include a test data generator for web software.
31 * </p>
32 *
33 * @author Steffen Herbold
34 * @version 1.0
35 */
36public class WebRequest implements IReplayable {
37
38    /**
39     * <p>
40     * Id for object serialization.
41     * </p>
42     */
43    private static final long serialVersionUID = 1L;
44
45    /**
46     * <p>
47     * POST variables of the web request.
48     * </p>
49     */
50    List<String> postVars;
51
52    /**
53     * <p>
54     * GET variables of the web request.
55     * </p>
56     */
57    List<String> getVars;
58
59    /**
60     * <p>
61     * URI of the web request.
62     * </p>
63     */
64    String targetUri;
65
66    /**
67     * <p>
68     * URL of the server.
69     * </p>
70     */
71    String serverUrl;
72
73    /**
74     * <p>
75     * Constructor. Creates a new WebRequest.
76     * </p>
77     *
78     * @param uri
79     *            URI of the request
80     * @param postVars
81     *            POST variables of the request
82     * @param getVars
83     *            GET variables of the request
84     */
85    public WebRequest(String url, String uri, List<String> postVars, List<String> getVars) {
86        serverUrl = url;
87        targetUri = uri;
88        this.postVars = new ArrayList<String>(postVars); // defensive copy
89        this.getVars = new ArrayList<String>(getVars);
90    }
91
92    /*
93     * (non-Javadoc)
94     *
95     * @see de.ugoe.cs.autoquest.eventcore.IReplayable#getReplay()
96     */
97    @Override
98    public String getReplay() {
99        StringBuilder builder = new StringBuilder();
100        builder.append("curl");
101        if (!postVars.isEmpty()) {
102            boolean isFirstPost = true;
103            for (String postVar : postVars) {
104                if (isFirstPost) {
105                    builder.append(" --data \"");
106                    isFirstPost = false;
107                }
108                else {
109                    builder.append('&');
110                }
111                builder.append(postVar + "=DATA_" + postVar.toUpperCase() + "_DATA");
112            }
113            builder.append('\"');
114        }
115        builder.append(' ');
116        if (serverUrl != null) {
117            builder.append(serverUrl);
118        }
119        builder.append(targetUri);
120        if (!getVars.isEmpty()) {
121            boolean isFirstGet = true;
122            for (String getVar : getVars) {
123                if (isFirstGet) {
124                    builder.append('?');
125                    isFirstGet = false;
126                }
127                else {
128                    builder.append('&');
129                }
130                builder.append(getVar + "=DATA_" + getVar.toUpperCase() + "_DATA");
131            }
132        }
133        return builder.toString();
134    }
135
136    /**
137     * <p>
138     * Two {@link WebRequest}s are equal, if their {@link #targetUri}, {@link #postVars}, and
139     * {@link #getVars} are equal.
140     * </p>
141     *
142     * @see java.lang.Object#equals(java.lang.Object)
143     */
144    @Override
145    public boolean equals(Object other) {
146        if (this == other) {
147            return true;
148        }
149        if (other instanceof WebRequest) {
150            return targetUri.equals(((WebRequest) other).targetUri) &&
151                postVars.equals(((WebRequest) other).postVars) &&
152                getVars.equals(((WebRequest) other).getVars);
153        }
154        return false;
155    }
156
157    /*
158     * (non-Javadoc)
159     *
160     * @see java.lang.Object#hashCode()
161     */
162    @Override
163    public int hashCode() {
164        int multiplier = 17;
165        int hash = 42;
166
167        hash = multiplier * hash + targetUri.hashCode();
168        hash = multiplier * hash + postVars.hashCode();
169        hash = multiplier * hash + getVars.hashCode();
170
171        return hash;
172    }
173
174    /*
175     * (non-Javadoc)
176     *
177     * @see de.ugoe.cs.autoquest.eventcore.IReplayable#getDecorator()
178     */
179    @Override
180    public IReplayDecorator getDecorator() {
181        return null;
182    }
183
184}
Note: See TracBrowser for help on using the repository browser.