1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/ChangeEmailTest.java Thu Mar 04 19:37:58 2010 +0100
1.3 @@ -0,0 +1,176 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * The contents of this file are subject to the terms of either the GNU
1.8 + * General Public License Version 2 only ("GPL") or the Common
1.9 + * Development and Distribution License("CDDL") (collectively, the
1.10 + * "License"). You may not use this file except in compliance with the
1.11 + * License. You can obtain a copy of the License at
1.12 + * http://www.netbeans.org/cddl-gplv2.html
1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.14 + * specific language governing permissions and limitations under the
1.15 + * License. When distributing the software, include this License Header
1.16 + * Notice in each file and include the License file at
1.17 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.18 + * particular file as subject to the "Classpath" exception as provided
1.19 + * by Sun in the GPL Version 2 section of the License file that
1.20 + * accompanied this code. If applicable, add the following below the
1.21 + * License Header, with the fields enclosed by brackets [] replaced by
1.22 + * your own identifying information:
1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
1.24 + *
1.25 + * Contributor(s):
1.26 + *
1.27 + * Portions Copyrighted 2010 Jaroslav Tulach
1.28 + */
1.29 +
1.30 +package cz.xelfi.quoridor.freemarkerdor;
1.31 +
1.32 +import com.sun.jersey.api.client.Client;
1.33 +import com.sun.jersey.api.client.ClientResponse;
1.34 +import com.sun.jersey.api.client.WebResource;
1.35 +import com.sun.jersey.core.header.MediaTypes;
1.36 +import com.sun.jersey.core.util.MultivaluedMapImpl;
1.37 +import com.sun.net.httpserver.HttpServer;
1.38 +import cz.xelfi.quoridor.webidor.resources.Quoridor;
1.39 +import java.io.File;
1.40 +import java.io.FileOutputStream;
1.41 +import java.io.IOException;
1.42 +import java.io.InputStream;
1.43 +import java.net.URI;
1.44 +import java.util.Locale;
1.45 +import java.util.concurrent.Callable;
1.46 +import java.util.regex.Matcher;
1.47 +import java.util.regex.Pattern;
1.48 +import javax.ws.rs.core.Cookie;
1.49 +import javax.ws.rs.core.MediaType;
1.50 +import javax.ws.rs.core.MultivaluedMap;
1.51 +import javax.ws.rs.core.UriBuilder;
1.52 +import org.junit.AfterClass;
1.53 +import org.junit.Before;
1.54 +import org.junit.BeforeClass;
1.55 +import org.junit.Test;
1.56 +import org.netbeans.junit.MockServices;
1.57 +import static org.junit.Assert.*;
1.58 +
1.59 +/**
1.60 + *
1.61 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.62 + */
1.63 +public class ChangeEmailTest extends Object {
1.64 + private static File dir;
1.65 + private static HttpServer stopAPI;
1.66 + private static HttpServer stopStatistics;
1.67 + private static Callable<Void> stop;
1.68 + private WebResource webResource;
1.69 + private WebResource apiResource;
1.70 + private WebResource statResource;
1.71 + private Client client;
1.72 +
1.73 + public ChangeEmailTest() throws Exception {
1.74 + }
1.75 +
1.76 + @BeforeClass
1.77 + public static void localeEnglish() throws Exception {
1.78 + Locale.setDefault(Locale.ENGLISH);
1.79 + dir = File.createTempFile("quoridor", ".dir");
1.80 + dir.delete();
1.81 + dir.mkdirs();
1.82 + System.setProperty("quoridor.dir", dir.getPath());
1.83 + stopAPI = Quoridor.start(7990);
1.84 + stopStatistics = Quoridor.start(7992);
1.85 + stop = UI.startServers(7991, "http://localhost:7990", "http://localhost:7992");
1.86 +
1.87 + File passwd = new File(dir, "passwd");
1.88 + FileOutputStream os = new FileOutputStream(passwd);
1.89 + os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
1.90 + os.close();
1.91 +
1.92 + MockServices.setServices(MockEmailer.class);
1.93 + }
1.94 +
1.95 + @Before
1.96 + public void setUp() throws Exception {
1.97 + MockEmailer.clear();
1.98 +
1.99 + client = new Client();
1.100 + webResource = client.resource(new URI("http://localhost:7991/"));
1.101 + apiResource = client.resource(new URI("http://localhost:7990/"));
1.102 + statResource = client.resource(new URI("http://localhost:7992/"));
1.103 + }
1.104 +
1.105 + @AfterClass
1.106 + public static void cleanUpAll() throws Exception {
1.107 + deleteRec(dir);
1.108 + if (stop != null) {
1.109 + stop.call();
1.110 + }
1.111 + if (stopAPI != null) {
1.112 + stopAPI.stop(0);
1.113 + }
1.114 + MockServices.setServices();
1.115 + }
1.116 +
1.117 + static void deleteRec(File dir) throws IOException {
1.118 + if (dir == null) {
1.119 + return;
1.120 + }
1.121 + File[] arr = dir.listFiles();
1.122 + if (arr != null) {
1.123 + for (File f : arr) {
1.124 + deleteRec(f);
1.125 + }
1.126 + }
1.127 + dir.delete();
1.128 + }
1.129 +
1.130 + @Test public void testChangeEmail() throws Exception {
1.131 + String logTest = apiResource.path("login").
1.132 + queryParam("name", "test").
1.133 + queryParam("password", "pes").
1.134 + accept(MediaType.TEXT_PLAIN).
1.135 + put(String.class);
1.136 + ClientResponse res = webResource.path("options").
1.137 + queryParam("email", "xxx@xxx.cz").
1.138 + cookie(Cookie.valueOf("login=" + logTest)).
1.139 + accept("text/html").
1.140 + get(ClientResponse.class);
1.141 + final String txt = res.getEntity(String.class);
1.142 +
1.143 + assertEquals("Emails sent to", "xxx@xxx.cz", MockEmailer.address);
1.144 +
1.145 + int urlIndex = MockEmailer.text.indexOf(webResource.getURI().toString());
1.146 + assertTrue("Found in " + MockEmailer.text, urlIndex >= 0);
1.147 +
1.148 + int endIndex = urlIndex;
1.149 + while (!Character.isWhitespace(MockEmailer.text.charAt(endIndex))) {
1.150 + endIndex++;
1.151 + }
1.152 + final String url = MockEmailer.text.substring(urlIndex, endIndex);
1.153 + WebResource requestURL = client.resource(url);
1.154 +
1.155 + String response = requestURL.accept("text/html").get(String.class);
1.156 + Pattern p = Pattern.compile("<input *type=\"text\" *name=\"email\" *value='xxx@xxx.cz'");
1.157 + assertTrue("New email found" + response, p.matcher(response).find());
1.158 + }
1.159 +
1.160 + public static final class MockEmailer extends EmailService {
1.161 + static String address;
1.162 + static String subject;
1.163 + static String text;
1.164 +
1.165 + @Override
1.166 + public void sendEmail(String address, String subject, String text) throws IOException {
1.167 + assertNull("No email sent yet", MockEmailer.address);
1.168 + MockEmailer.address = address;
1.169 + MockEmailer.subject = subject;
1.170 + MockEmailer.text = text;
1.171 + }
1.172 +
1.173 + public static void clear() {
1.174 + MockEmailer.address = null;
1.175 + MockEmailer.subject = null;
1.176 + MockEmailer.text = null;
1.177 + }
1.178 + }
1.179 +}