freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/ChangeEmailTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 09 Mar 2010 19:33:46 +0100
changeset 234 0a71b6bd786f
parent 233 ecddc9f373bb
child 264 d60370059c3c
permissions -rw-r--r--
Allowing the server to specify real public URL
jaroslav@233
     1
/*
jaroslav@233
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@233
     3
 *
jaroslav@233
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@233
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@233
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@233
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@233
     8
 * License. You can obtain a copy of the License at
jaroslav@233
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@233
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@233
    11
 * specific language governing permissions and limitations under the
jaroslav@233
    12
 * License.  When distributing the software, include this License Header
jaroslav@233
    13
 * Notice in each file and include the License file at
jaroslav@233
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@233
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@233
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@233
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@233
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@233
    19
 * your own identifying information:
jaroslav@233
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@233
    21
 *
jaroslav@233
    22
 * Contributor(s):
jaroslav@233
    23
 *
jaroslav@233
    24
 * Portions Copyrighted 2010 Jaroslav Tulach
jaroslav@233
    25
 */
jaroslav@233
    26
jaroslav@233
    27
package cz.xelfi.quoridor.freemarkerdor;
jaroslav@233
    28
jaroslav@233
    29
import com.sun.jersey.api.client.Client;
jaroslav@233
    30
import com.sun.jersey.api.client.ClientResponse;
jaroslav@233
    31
import com.sun.jersey.api.client.WebResource;
jaroslav@233
    32
import com.sun.jersey.core.header.MediaTypes;
jaroslav@233
    33
import com.sun.jersey.core.util.MultivaluedMapImpl;
jaroslav@233
    34
import com.sun.net.httpserver.HttpServer;
jaroslav@233
    35
import cz.xelfi.quoridor.webidor.resources.Quoridor;
jaroslav@233
    36
import java.io.File;
jaroslav@233
    37
import java.io.FileOutputStream;
jaroslav@233
    38
import java.io.IOException;
jaroslav@233
    39
import java.io.InputStream;
jaroslav@233
    40
import java.net.URI;
jaroslav@233
    41
import java.util.Locale;
jaroslav@233
    42
import java.util.concurrent.Callable;
jaroslav@233
    43
import java.util.regex.Matcher;
jaroslav@233
    44
import java.util.regex.Pattern;
jaroslav@233
    45
import javax.ws.rs.core.Cookie;
jaroslav@233
    46
import javax.ws.rs.core.MediaType;
jaroslav@233
    47
import javax.ws.rs.core.MultivaluedMap;
jaroslav@233
    48
import javax.ws.rs.core.UriBuilder;
jaroslav@233
    49
import org.junit.AfterClass;
jaroslav@233
    50
import org.junit.Before;
jaroslav@233
    51
import org.junit.BeforeClass;
jaroslav@233
    52
import org.junit.Test;
jaroslav@233
    53
import org.netbeans.junit.MockServices;
jaroslav@233
    54
import static org.junit.Assert.*;
jaroslav@233
    55
jaroslav@233
    56
/**
jaroslav@233
    57
 *
jaroslav@233
    58
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@233
    59
 */
jaroslav@233
    60
public class ChangeEmailTest extends Object {
jaroslav@233
    61
    private static File dir;
jaroslav@233
    62
    private static HttpServer stopAPI;
jaroslav@233
    63
    private static HttpServer stopStatistics;
jaroslav@233
    64
    private static Callable<Void> stop;
jaroslav@233
    65
    private WebResource webResource;
jaroslav@233
    66
    private WebResource apiResource;
jaroslav@233
    67
    private WebResource statResource;
jaroslav@233
    68
    private Client client;
jaroslav@233
    69
jaroslav@233
    70
    public ChangeEmailTest() throws Exception {
jaroslav@233
    71
    }
jaroslav@233
    72
jaroslav@233
    73
    @BeforeClass
jaroslav@233
    74
    public static void localeEnglish() throws Exception {
jaroslav@233
    75
        Locale.setDefault(Locale.ENGLISH);
jaroslav@233
    76
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@233
    77
        dir.delete();
jaroslav@233
    78
        dir.mkdirs();
jaroslav@233
    79
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@233
    80
        stopAPI = Quoridor.start(7990);
jaroslav@233
    81
        stopStatistics = Quoridor.start(7992);
jaroslav@234
    82
        stop = UI.startServers(7991, "http://localhost:7990", "http://localhost:7992", null);
jaroslav@233
    83
jaroslav@233
    84
        File passwd = new File(dir, "passwd");
jaroslav@233
    85
        FileOutputStream os = new FileOutputStream(passwd);
jaroslav@233
    86
        os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
jaroslav@233
    87
        os.close();
jaroslav@233
    88
jaroslav@233
    89
        MockServices.setServices(MockEmailer.class);
jaroslav@233
    90
    }
jaroslav@233
    91
jaroslav@233
    92
    @Before
jaroslav@233
    93
    public void setUp() throws Exception {
jaroslav@233
    94
        MockEmailer.clear();
jaroslav@233
    95
jaroslav@233
    96
        client = new Client();
jaroslav@233
    97
        webResource = client.resource(new URI("http://localhost:7991/"));
jaroslav@233
    98
        apiResource = client.resource(new URI("http://localhost:7990/"));
jaroslav@233
    99
        statResource = client.resource(new URI("http://localhost:7992/"));
jaroslav@233
   100
    }
jaroslav@233
   101
jaroslav@233
   102
    @AfterClass
jaroslav@233
   103
    public static void cleanUpAll() throws Exception {
jaroslav@233
   104
        deleteRec(dir);
jaroslav@233
   105
        if (stop != null) {
jaroslav@233
   106
            stop.call();
jaroslav@233
   107
        }
jaroslav@233
   108
        if (stopAPI != null) {
jaroslav@233
   109
            stopAPI.stop(0);
jaroslav@233
   110
        }
jaroslav@233
   111
        MockServices.setServices();
jaroslav@233
   112
    }
jaroslav@233
   113
jaroslav@233
   114
    static void deleteRec(File dir) throws IOException {
jaroslav@233
   115
        if (dir == null) {
jaroslav@233
   116
            return;
jaroslav@233
   117
        }
jaroslav@233
   118
        File[] arr = dir.listFiles();
jaroslav@233
   119
        if (arr != null) {
jaroslav@233
   120
            for (File f : arr) {
jaroslav@233
   121
                deleteRec(f);
jaroslav@233
   122
            }
jaroslav@233
   123
        }
jaroslav@233
   124
        dir.delete();
jaroslav@233
   125
    }
jaroslav@233
   126
jaroslav@233
   127
    @Test public void testChangeEmail() throws Exception {
jaroslav@233
   128
        String logTest = apiResource.path("login").
jaroslav@233
   129
            queryParam("name", "test").
jaroslav@233
   130
            queryParam("password", "pes").
jaroslav@233
   131
            accept(MediaType.TEXT_PLAIN).
jaroslav@233
   132
            put(String.class);
jaroslav@233
   133
        ClientResponse res = webResource.path("options").
jaroslav@233
   134
            queryParam("email", "xxx@xxx.cz").
jaroslav@233
   135
            cookie(Cookie.valueOf("login=" + logTest)).
jaroslav@233
   136
            accept("text/html").
jaroslav@233
   137
            get(ClientResponse.class);
jaroslav@233
   138
        final String txt = res.getEntity(String.class);
jaroslav@233
   139
jaroslav@233
   140
        assertEquals("Emails sent to", "xxx@xxx.cz", MockEmailer.address);
jaroslav@233
   141
jaroslav@233
   142
        int urlIndex = MockEmailer.text.indexOf(webResource.getURI().toString());
jaroslav@233
   143
        assertTrue("Found in " + MockEmailer.text, urlIndex >= 0);
jaroslav@233
   144
jaroslav@233
   145
        int endIndex = urlIndex;
jaroslav@233
   146
        while (!Character.isWhitespace(MockEmailer.text.charAt(endIndex))) {
jaroslav@233
   147
            endIndex++;
jaroslav@233
   148
        }
jaroslav@233
   149
        final String url = MockEmailer.text.substring(urlIndex, endIndex);
jaroslav@233
   150
        WebResource requestURL = client.resource(url);
jaroslav@233
   151
jaroslav@233
   152
        String response = requestURL.accept("text/html").get(String.class);
jaroslav@233
   153
        Pattern p = Pattern.compile("<input *type=\"text\" *name=\"email\" *value='xxx@xxx.cz'");
jaroslav@233
   154
        assertTrue("New email found" + response, p.matcher(response).find());
jaroslav@233
   155
    }
jaroslav@233
   156
jaroslav@233
   157
    public static final class MockEmailer extends EmailService {
jaroslav@233
   158
        static String address;
jaroslav@233
   159
        static String subject;
jaroslav@233
   160
        static String text;
jaroslav@233
   161
jaroslav@233
   162
        @Override
jaroslav@233
   163
        public void sendEmail(String address, String subject, String text) throws IOException {
jaroslav@233
   164
            assertNull("No email sent yet", MockEmailer.address);
jaroslav@233
   165
            MockEmailer.address = address;
jaroslav@233
   166
            MockEmailer.subject = subject;
jaroslav@233
   167
            MockEmailer.text = text;
jaroslav@233
   168
        }
jaroslav@233
   169
jaroslav@233
   170
        public static void clear() {
jaroslav@233
   171
            MockEmailer.address = null;
jaroslav@233
   172
            MockEmailer.subject = null;
jaroslav@233
   173
            MockEmailer.text = null;
jaroslav@233
   174
        }
jaroslav@233
   175
    }
jaroslav@233
   176
}