2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * The contents of this file are subject to the terms of either the GNU
5 * General Public License Version 2 only ("GPL") or the Common
6 * Development and Distribution License("CDDL") (collectively, the
7 * "License"). You may not use this file except in compliance with the
8 * License. You can obtain a copy of the License at
9 * http://www.netbeans.org/cddl-gplv2.html
10 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
11 * specific language governing permissions and limitations under the
12 * License. When distributing the software, include this License Header
13 * Notice in each file and include the License file at
14 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
15 * particular file as subject to the "Classpath" exception as provided
16 * by Sun in the GPL Version 2 section of the License file that
17 * accompanied this code. If applicable, add the following below the
18 * License Header, with the fields enclosed by brackets [] replaced by
19 * your own identifying information:
20 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Portions Copyrighted 2010 Jaroslav Tulach
27 package cz.xelfi.quoridor.freemarkerdor;
29 import com.sun.jersey.api.client.Client;
30 import com.sun.jersey.api.client.ClientResponse;
31 import com.sun.jersey.api.client.WebResource;
32 import com.sun.jersey.core.header.MediaTypes;
33 import com.sun.jersey.core.util.MultivaluedMapImpl;
34 import com.sun.net.httpserver.HttpServer;
35 import cz.xelfi.quoridor.webidor.resources.Quoridor;
37 import java.io.FileOutputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
41 import java.util.Locale;
42 import java.util.concurrent.Callable;
43 import java.util.regex.Matcher;
44 import java.util.regex.Pattern;
45 import javax.ws.rs.core.Cookie;
46 import javax.ws.rs.core.MediaType;
47 import javax.ws.rs.core.MultivaluedMap;
48 import javax.ws.rs.core.UriBuilder;
49 import org.junit.AfterClass;
50 import org.junit.Before;
51 import org.junit.BeforeClass;
52 import org.junit.Test;
53 import org.netbeans.junit.MockServices;
54 import static org.junit.Assert.*;
58 * @author Jaroslav Tulach <jtulach@netbeans.org>
60 public class ChangeEmailTest extends Object {
61 private static File dir;
62 private static HttpServer stopAPI;
63 private static HttpServer stopStatistics;
64 private static Callable<Void> stop;
65 private WebResource webResource;
66 private WebResource apiResource;
67 private WebResource statResource;
68 private Client client;
70 public ChangeEmailTest() throws Exception {
74 public static void localeEnglish() throws Exception {
75 Locale.setDefault(Locale.ENGLISH);
76 dir = File.createTempFile("quoridor", ".dir");
79 System.setProperty("quoridor.dir", dir.getPath());
80 stopAPI = Quoridor.start(7990);
81 stopStatistics = Quoridor.start(7992);
82 stop = UI.startServers(7991, "http://localhost:7990", "http://localhost:7992", null);
84 File passwd = new File(dir, "passwd");
85 FileOutputStream os = new FileOutputStream(passwd);
86 os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
89 MockServices.setServices(MockEmailer.class);
93 public void setUp() throws Exception {
96 client = new Client();
97 webResource = client.resource(new URI("http://localhost:7991/"));
98 apiResource = client.resource(new URI("http://localhost:7990/"));
99 statResource = client.resource(new URI("http://localhost:7992/"));
103 public static void cleanUpAll() throws Exception {
108 if (stopAPI != null) {
111 MockServices.setServices();
114 static void deleteRec(File dir) throws IOException {
118 File[] arr = dir.listFiles();
127 @Test public void testChangeEmail() throws Exception {
128 String logTest = apiResource.path("login").
129 queryParam("name", "test").
130 queryParam("password", "pes").
131 accept(MediaType.TEXT_PLAIN).
133 ClientResponse res = webResource.path("options").
134 queryParam("email", "xxx@xxx.cz").
135 cookie(Cookie.valueOf("login=" + logTest)).
137 get(ClientResponse.class);
138 final String txt = res.getEntity(String.class);
140 assertEquals("Emails sent to", "xxx@xxx.cz", MockEmailer.address);
142 int urlIndex = MockEmailer.text.indexOf(webResource.getURI().toString());
143 assertTrue("Found in " + MockEmailer.text, urlIndex >= 0);
145 int endIndex = urlIndex;
146 while (!Character.isWhitespace(MockEmailer.text.charAt(endIndex))) {
149 final String url = MockEmailer.text.substring(urlIndex, endIndex);
150 WebResource requestURL = client.resource(url);
152 String response = requestURL.accept("text/html").get(String.class);
153 Pattern p = Pattern.compile("<input *type=\"text\" *name=\"email\" *value='xxx@xxx.cz'");
154 assertTrue("New email found" + response, p.matcher(response).find());
157 public static final class MockEmailer extends EmailService {
158 static String address;
159 static String subject;
163 public void sendEmail(String address, String subject, String text) throws IOException {
164 assertNull("No email sent yet", MockEmailer.address);
165 MockEmailer.address = address;
166 MockEmailer.subject = subject;
167 MockEmailer.text = text;
170 public static void clear() {
171 MockEmailer.address = null;
172 MockEmailer.subject = null;
173 MockEmailer.text = null;