2 * Quoridor server and related libraries
3 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://www.gnu.org/licenses/.
18 package cz.xelfi.quoridor.freemarkerdor;
20 import com.sun.jersey.api.client.Client;
21 import com.sun.jersey.api.client.ClientResponse;
22 import com.sun.jersey.api.client.WebResource;
23 import com.sun.jersey.core.header.MediaTypes;
24 import com.sun.jersey.core.util.MultivaluedMapImpl;
25 import com.sun.net.httpserver.HttpServer;
26 import cz.xelfi.quoridor.webidor.resources.Quoridor;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
32 import java.util.Locale;
33 import java.util.concurrent.Callable;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36 import javax.ws.rs.core.Cookie;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.MultivaluedMap;
39 import javax.ws.rs.core.UriBuilder;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.netbeans.junit.MockServices;
45 import static org.junit.Assert.*;
49 * @author Jaroslav Tulach <jtulach@netbeans.org>
51 public class ChangeEmailTest extends Object {
52 private static File dir;
53 private static HttpServer stopAPI;
54 private static HttpServer stopStatistics;
55 private static Callable<Void> stop;
56 private WebResource webResource;
57 private WebResource apiResource;
58 private WebResource statResource;
59 private Client client;
61 public ChangeEmailTest() throws Exception {
65 public static void localeEnglish() throws Exception {
66 Locale.setDefault(Locale.ENGLISH);
67 dir = File.createTempFile("quoridor", ".dir");
70 System.setProperty("quoridor.dir", dir.getPath());
71 stopAPI = Quoridor.start(7990);
72 stopStatistics = Quoridor.start(7992);
73 stop = UI.startServers(7991, "http://localhost:7990", "http://localhost:7992", null);
75 File passwd = new File(dir, "passwd");
76 FileOutputStream os = new FileOutputStream(passwd);
77 os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
80 MockServices.setServices(MockEmailer.class);
84 public void setUp() throws Exception {
87 client = new Client();
88 webResource = client.resource(new URI("http://localhost:7991/"));
89 apiResource = client.resource(new URI("http://localhost:7990/"));
90 statResource = client.resource(new URI("http://localhost:7992/"));
94 public static void cleanUpAll() throws Exception {
99 if (stopAPI != null) {
102 MockServices.setServices();
105 static void deleteRec(File dir) throws IOException {
109 File[] arr = dir.listFiles();
118 @Test public void testChangeEmail() throws Exception {
119 String logTest = apiResource.path("login").
120 queryParam("name", "test").
121 queryParam("password", "pes").
122 accept(MediaType.TEXT_PLAIN).
124 ClientResponse res = webResource.path("options").
125 queryParam("email", "xxx@xxx.cz").
126 cookie(Cookie.valueOf("login=" + logTest)).
128 get(ClientResponse.class);
129 final String txt = res.getEntity(String.class);
131 assertEquals("Emails sent to", "xxx@xxx.cz", MockEmailer.address);
133 int urlIndex = MockEmailer.text.indexOf(webResource.getURI().toString());
134 assertTrue("Found in " + MockEmailer.text, urlIndex >= 0);
136 int endIndex = urlIndex;
137 while (!Character.isWhitespace(MockEmailer.text.charAt(endIndex))) {
140 final String url = MockEmailer.text.substring(urlIndex, endIndex);
141 WebResource requestURL = client.resource(url);
143 String response = requestURL.accept("text/html").get(String.class);
144 Pattern p = Pattern.compile("<input *type=\"text\" *name=\"email\" *value='xxx@xxx.cz'");
145 assertTrue("New email found" + response, p.matcher(response).find());
148 public static final class MockEmailer extends EmailService {
149 static String address;
150 static String subject;
154 public void sendEmail(String address, String subject, String text) throws IOException {
155 assertNull("No email sent yet", MockEmailer.address);
156 MockEmailer.address = address;
157 MockEmailer.subject = subject;
158 MockEmailer.text = text;
161 public static void clear() {
162 MockEmailer.address = null;
163 MockEmailer.subject = null;
164 MockEmailer.text = null;