001: package blackbox;
002:
003: import java.io.PrintWriter;
004: import java.util.Arrays;
005:
006: import org.junit.*;
007: import static org.junit.Assert.*;
008:
009: import com.meterware.httpunit.*;
010:
011: import velosurf.util.Logger;
012:
013: public class BlackboxTests {
014:
015: /* TODO parametrize the port number with ant property ${test.webcontainer.port}
016: (requires a filtered copy of the source files) */
017: public static final int SERVER_PORT = 8081;
018:
019: private void checkText(WebResponse resp, String id, String text)
020: throws Exception {
021: HTMLElement element = resp.getElementWithID(id);
022: assertNotNull(element);
023: assertEquals(text, element.getText());
024: }
025:
026: public @BeforeClass
027: static void initLogger() throws Exception {
028: Logger.setWriter(new PrintWriter("log/blackbox.log"));
029: }
030:
031: public @Test
032: void basicTests() throws Exception {
033: WebConversation wc = new WebConversation();
034: WebRequest req = new GetMethodWebRequest("http://localhost:"
035: + SERVER_PORT + "/basic.html");
036: WebResponse resp = wc.getResponse(req);
037: checkText(resp, "test1", "Addison Wesley Professional");
038: checkText(resp, "test2.1", "Effective Java");
039: checkText(resp, "test2.2", "TCP/IP Illustrated, Volume 1");
040: assertNull(resp.getElementWithID("test2.3"));
041: checkText(resp, "test3", "Effective Java");
042: checkText(resp, "test4", "Joshua Bloch");
043: }
044:
045: public @Test
046: void paramfetching() throws Exception {
047: WebConversation wc = new WebConversation();
048: WebRequest req = new GetMethodWebRequest("http://localhost:"
049: + SERVER_PORT + "/obfuscate.html?id=1");
050: WebResponse resp = wc.getResponse(req);
051: String obfId = resp.getElementWithID("obfuscated").getText();
052: req = new GetMethodWebRequest("http://localhost:" + SERVER_PORT
053: + "/fetch.html?publisher_id=" + obfId + "&book_id="
054: + obfId);
055: resp = wc.getResponse(req);
056: checkText(resp, "publisher", "Addison Wesley Professional");
057: checkText(resp, "book", "Effective Java");
058: }
059:
060: public @Test
061: void authentication() throws Exception {
062: /* try to reach a protected resource */
063: WebConversation wc = new WebConversation();
064: WebRequest req = new GetMethodWebRequest("http://localhost:"
065: + SERVER_PORT + "/auth/protected.html");
066: WebResponse resp = wc.getResponse(req);
067: assertEquals("Login", resp.getTitle());
068: /* log in */
069: WebForm form = resp.getFormWithName("login");
070: form.setParameter("login", "foo");
071: form.setParameter("password", "bar");
072: resp = form.submit();
073: /* check we reach the initially requested resource */
074: assertEquals("protected", resp.getTitle());
075: /* log out */
076: WebLink logout = resp.getLinkWith("logout");
077: assertNotNull(logout);
078: resp = logout.click();
079: assertEquals("Login", resp.getTitle());
080: /* bad login */
081: form = resp.getFormWithName("login");
082: form.setParameter("login", "foo");
083: form.setParameter("password", "badpass");
084: resp = form.submit();
085: assertEquals("Login", resp.getTitle());
086: String msg = resp.getElementWithID("message").getText();
087: /* in english or french, may depend of the system */
088: assertTrue(Arrays
089: .asList(
090: new String[] { "Bad login or password.",
091: "Mauvais login ou mot de passe...",
092: "badLogin" }).contains(msg));
093: /* log in again, check we reach the auth index */
094: form = resp.getFormWithName("login");
095: form.setParameter("login", "foo");
096: form.setParameter("password", "bar");
097: resp = form.submit();
098: assertEquals("Protected Index", resp.getTitle());
099: }
100:
101: public @Test
102: void validation() throws Exception {
103: WebConversation wc = new WebConversation();
104: WebRequest req = new GetMethodWebRequest("http://localhost:"
105: + SERVER_PORT + "/input.html");
106: WebResponse resp = wc.getResponse(req);
107: assertEquals("Input form", resp.getTitle());
108: /* first try with bad values */
109: WebForm form = resp.getFormWithName("input");
110: form.setParameter("string", "aa");
111: form.setParameter("string2", "123-1234");
112: form.setParameter("number", "0");
113: form.setParameter("oneof", "test0");
114: form.setParameter("mydate", "2-7-2006");
115: form.setParameter("email", "toto@tata@titi");
116: //form.setParameter("email2",(String)null);
117: form.setParameter("book_id", "0");
118: resp = form.submit();
119: assertEquals("Input form", resp.getTitle());
120: /* check error messages */
121: checkText(resp, "1",
122: "field string: value 'aa' is not of the proper length");
123: checkText(resp, "2",
124: "field string2: value '123-1234' is not valid");
125: checkText(resp, "3",
126: "field number: '0' is not in the valid range");
127: checkText(resp, "4",
128: "field oneof: value 'test0' must be one of: test1, test2, test3");
129: checkText(resp, "5",
130: "field mydate: '2-7-2006' is not a valid date or is outside range");
131: checkText(resp, "6",
132: "field email: 'toto@tata@titi' is not a valid email");
133: checkText(resp, "7", "field email2 cannot be empty");
134: checkText(resp, "8",
135: "field book_id: value '0' not found in book.book_id");
136: assertNull(resp.getElementWithID("9"));
137: /* check that the form retained the values */
138: form = resp.getFormWithName("input");
139: assertEquals("aa", form.getParameterValue("string"));
140: assertEquals("123-1234", form.getParameterValue("string2"));
141: assertEquals("0", form.getParameterValue("number"));
142: assertEquals("test0", form.getParameterValue("oneof"));
143: assertEquals("2-7-2006", form.getParameterValue("mydate"));
144: assertEquals("toto@tata@titi", form.getParameterValue("email"));
145: assertEquals("", form.getParameterValue("email2"));
146: assertEquals("0", form.getParameterValue("book_id"));
147: /* resubmit with good values excecpt for email2*/
148: form.setParameter("string", "aaaaaa");
149: form.setParameter("string2", "123-123");
150: form.setParameter("number", "1");
151: form.setParameter("oneof", "test1");
152: form.setParameter("mydate", "2-8-2006");
153: form.setParameter("email", "claude@renegat.net");
154: form.setParameter("book_id", "1");
155: /* test DNS email checking */
156: form.setParameter("email2", "toto@azerty.blabla");
157: resp = form.submit();
158: assertEquals("Input form", resp.getTitle());
159: checkText(resp, "1",
160: "field email2: 'toto@azerty.blabla' is not a valid email");
161: assertNull(resp.getElementWithID("2"));
162: /* test SMTP email checking */
163: form = resp.getFormWithName("input");
164: form.setParameter("email2", "inexistant_email@gmail.com");
165: resp = form.submit();
166: assertEquals("Input form", resp.getTitle());
167: checkText(resp, "1",
168: "field email2: 'inexistant_email@gmail.com' is not a valid email");
169: assertNull(resp.getElementWithID("2"));
170: /* now with a valid email... */
171: form = resp.getFormWithName("input");
172: form.setParameter("email2", "claude.brisson@gmail.com");
173: resp = form.submit();
174: assertEquals("Good values!", resp.getTitle());
175: }
176:
177: public @Test
178: void xinclude() throws Exception {
179: WebConversation wc = new WebConversation();
180: WebRequest req = new GetMethodWebRequest("http://localhost:"
181: + SERVER_PORT + "/xinclude.html");
182: WebResponse resp = wc.getResponse(req);
183: assertEquals("XInclude", resp.getTitle());
184: checkText(resp, "result", "1");
185: }
186:
187: public @Test
188: void testinsert() throws Exception {
189: WebConversation wc = new WebConversation();
190: WebRequest req = new GetMethodWebRequest("http://localhost:"
191: + SERVER_PORT + "/insert.html");
192: WebResponse resp = wc.getResponse(req);
193: checkText(resp, "result", "3");
194: }
195:
196: }
|