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 org.glassfish.grizzly.http.server.HttpServer;
21 import com.sun.jersey.api.client.Client;
22 import com.sun.jersey.api.client.ClientResponse;
23 import com.sun.jersey.api.client.WebResource;
24 import cz.xelfi.quoridor.webidor.resources.Quoridor;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
29 import java.util.Locale;
30 import java.util.concurrent.Callable;
31 import java.util.regex.Pattern;
32 import javax.ws.rs.core.Cookie;
33 import javax.ws.rs.core.MediaType;
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.netbeans.junit.MockServices;
39 import static org.junit.Assert.*;
43 * @author Jaroslav Tulach <jtulach@netbeans.org>
45 public class ChangeEmailTest extends Object {
46 private static File dir;
47 private static HttpServer stopAPI;
48 private static HttpServer stopStatistics;
49 private static Callable<Void> stop;
50 private WebResource webResource;
51 private WebResource apiResource;
52 private WebResource statResource;
53 private Client client;
55 public ChangeEmailTest() throws Exception {
59 public static void localeEnglish() throws Exception {
60 Locale.setDefault(Locale.ENGLISH);
61 dir = File.createTempFile("quoridor", ".dir");
64 System.setProperty("quoridor.dir", dir.getPath());
65 stopAPI = Quoridor.start(7990);
66 stopStatistics = Quoridor.start(7992);
67 stop = UI.startServers(7991, "http://localhost:7990", "http://localhost:7992", null);
69 File passwd = new File(dir, "passwd");
70 FileOutputStream os = new FileOutputStream(passwd);
71 os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
74 MockServices.setServices(MockEmailer.class);
78 public void setUp() throws Exception {
81 client = new Client();
82 webResource = client.resource(new URI("http://localhost:7991/"));
83 apiResource = client.resource(new URI("http://localhost:7990/"));
84 statResource = client.resource(new URI("http://localhost:7992/"));
88 public static void cleanUpAll() throws Exception {
93 if (stopAPI != null) {
96 MockServices.setServices();
99 static void deleteRec(File dir) throws IOException {
103 File[] arr = dir.listFiles();
112 @Test public void testChangeEmail() throws Exception {
113 String logTest = apiResource.path("login").
114 queryParam("name", "test").
115 queryParam("password", "pes").
116 accept(MediaType.TEXT_PLAIN).
118 ClientResponse res = webResource.path("options").
119 queryParam("email", "xxx@xxx.cz").
120 cookie(Cookie.valueOf("login=" + logTest)).
122 get(ClientResponse.class);
123 final String txt = res.getEntity(String.class);
125 assertEquals("Emails sent to", "xxx@xxx.cz", MockEmailer.address);
127 int urlIndex = MockEmailer.text.indexOf(webResource.getURI().toString());
128 assertTrue("Found in " + MockEmailer.text, urlIndex >= 0);
130 int endIndex = urlIndex;
131 while (!Character.isWhitespace(MockEmailer.text.charAt(endIndex))) {
134 final String url = MockEmailer.text.substring(urlIndex, endIndex);
135 WebResource requestURL = client.resource(url);
137 String response = requestURL.accept("text/html").get(String.class);
138 Pattern p = Pattern.compile("<input *type=\"text\" *name=\"email\" *value='xxx@xxx.cz'");
139 assertTrue("New email found" + response, p.matcher(response).find());
142 public static final class MockEmailer extends EmailService {
143 static String address;
144 static String subject;
148 public void sendEmail(String address, String subject, String text) throws IOException {
149 assertNull("No email sent yet", MockEmailer.address);
150 MockEmailer.address = address;
151 MockEmailer.subject = subject;
152 MockEmailer.text = text;
155 public static void clear() {
156 MockEmailer.address = null;
157 MockEmailer.subject = null;
158 MockEmailer.text = null;