freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/ChangeEmailTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 234 0a71b6bd786f
child 280 800b821c282e
permissions -rw-r--r--
Changing headers to GPLv3
jaroslav@233
     1
/*
jaroslav@264
     2
 * Quoridor server and related libraries
jaroslav@264
     3
 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@233
     4
 *
jaroslav@264
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@264
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@264
     7
 * the Free Software Foundation, either version 3 of the License.
jaroslav@233
     8
 *
jaroslav@264
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@264
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@264
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@264
    12
 * GNU General Public License for more details.
jaroslav@233
    13
 *
jaroslav@264
    14
 * You should have received a copy of the GNU General Public License
jaroslav@264
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@264
    16
 * If not, see http://www.gnu.org/licenses/.
jaroslav@233
    17
 */
jaroslav@233
    18
package cz.xelfi.quoridor.freemarkerdor;
jaroslav@233
    19
jaroslav@233
    20
import com.sun.jersey.api.client.Client;
jaroslav@233
    21
import com.sun.jersey.api.client.ClientResponse;
jaroslav@233
    22
import com.sun.jersey.api.client.WebResource;
jaroslav@233
    23
import com.sun.jersey.core.header.MediaTypes;
jaroslav@233
    24
import com.sun.jersey.core.util.MultivaluedMapImpl;
jaroslav@233
    25
import com.sun.net.httpserver.HttpServer;
jaroslav@233
    26
import cz.xelfi.quoridor.webidor.resources.Quoridor;
jaroslav@233
    27
import java.io.File;
jaroslav@233
    28
import java.io.FileOutputStream;
jaroslav@233
    29
import java.io.IOException;
jaroslav@233
    30
import java.io.InputStream;
jaroslav@233
    31
import java.net.URI;
jaroslav@233
    32
import java.util.Locale;
jaroslav@233
    33
import java.util.concurrent.Callable;
jaroslav@233
    34
import java.util.regex.Matcher;
jaroslav@233
    35
import java.util.regex.Pattern;
jaroslav@233
    36
import javax.ws.rs.core.Cookie;
jaroslav@233
    37
import javax.ws.rs.core.MediaType;
jaroslav@233
    38
import javax.ws.rs.core.MultivaluedMap;
jaroslav@233
    39
import javax.ws.rs.core.UriBuilder;
jaroslav@233
    40
import org.junit.AfterClass;
jaroslav@233
    41
import org.junit.Before;
jaroslav@233
    42
import org.junit.BeforeClass;
jaroslav@233
    43
import org.junit.Test;
jaroslav@233
    44
import org.netbeans.junit.MockServices;
jaroslav@233
    45
import static org.junit.Assert.*;
jaroslav@233
    46
jaroslav@233
    47
/**
jaroslav@233
    48
 *
jaroslav@233
    49
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@233
    50
 */
jaroslav@233
    51
public class ChangeEmailTest extends Object {
jaroslav@233
    52
    private static File dir;
jaroslav@233
    53
    private static HttpServer stopAPI;
jaroslav@233
    54
    private static HttpServer stopStatistics;
jaroslav@233
    55
    private static Callable<Void> stop;
jaroslav@233
    56
    private WebResource webResource;
jaroslav@233
    57
    private WebResource apiResource;
jaroslav@233
    58
    private WebResource statResource;
jaroslav@233
    59
    private Client client;
jaroslav@233
    60
jaroslav@233
    61
    public ChangeEmailTest() throws Exception {
jaroslav@233
    62
    }
jaroslav@233
    63
jaroslav@233
    64
    @BeforeClass
jaroslav@233
    65
    public static void localeEnglish() throws Exception {
jaroslav@233
    66
        Locale.setDefault(Locale.ENGLISH);
jaroslav@233
    67
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@233
    68
        dir.delete();
jaroslav@233
    69
        dir.mkdirs();
jaroslav@233
    70
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@233
    71
        stopAPI = Quoridor.start(7990);
jaroslav@233
    72
        stopStatistics = Quoridor.start(7992);
jaroslav@234
    73
        stop = UI.startServers(7991, "http://localhost:7990", "http://localhost:7992", null);
jaroslav@233
    74
jaroslav@233
    75
        File passwd = new File(dir, "passwd");
jaroslav@233
    76
        FileOutputStream os = new FileOutputStream(passwd);
jaroslav@233
    77
        os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
jaroslav@233
    78
        os.close();
jaroslav@233
    79
jaroslav@233
    80
        MockServices.setServices(MockEmailer.class);
jaroslav@233
    81
    }
jaroslav@233
    82
jaroslav@233
    83
    @Before
jaroslav@233
    84
    public void setUp() throws Exception {
jaroslav@233
    85
        MockEmailer.clear();
jaroslav@233
    86
jaroslav@233
    87
        client = new Client();
jaroslav@233
    88
        webResource = client.resource(new URI("http://localhost:7991/"));
jaroslav@233
    89
        apiResource = client.resource(new URI("http://localhost:7990/"));
jaroslav@233
    90
        statResource = client.resource(new URI("http://localhost:7992/"));
jaroslav@233
    91
    }
jaroslav@233
    92
jaroslav@233
    93
    @AfterClass
jaroslav@233
    94
    public static void cleanUpAll() throws Exception {
jaroslav@233
    95
        deleteRec(dir);
jaroslav@233
    96
        if (stop != null) {
jaroslav@233
    97
            stop.call();
jaroslav@233
    98
        }
jaroslav@233
    99
        if (stopAPI != null) {
jaroslav@233
   100
            stopAPI.stop(0);
jaroslav@233
   101
        }
jaroslav@233
   102
        MockServices.setServices();
jaroslav@233
   103
    }
jaroslav@233
   104
jaroslav@233
   105
    static void deleteRec(File dir) throws IOException {
jaroslav@233
   106
        if (dir == null) {
jaroslav@233
   107
            return;
jaroslav@233
   108
        }
jaroslav@233
   109
        File[] arr = dir.listFiles();
jaroslav@233
   110
        if (arr != null) {
jaroslav@233
   111
            for (File f : arr) {
jaroslav@233
   112
                deleteRec(f);
jaroslav@233
   113
            }
jaroslav@233
   114
        }
jaroslav@233
   115
        dir.delete();
jaroslav@233
   116
    }
jaroslav@233
   117
jaroslav@233
   118
    @Test public void testChangeEmail() throws Exception {
jaroslav@233
   119
        String logTest = apiResource.path("login").
jaroslav@233
   120
            queryParam("name", "test").
jaroslav@233
   121
            queryParam("password", "pes").
jaroslav@233
   122
            accept(MediaType.TEXT_PLAIN).
jaroslav@233
   123
            put(String.class);
jaroslav@233
   124
        ClientResponse res = webResource.path("options").
jaroslav@233
   125
            queryParam("email", "xxx@xxx.cz").
jaroslav@233
   126
            cookie(Cookie.valueOf("login=" + logTest)).
jaroslav@233
   127
            accept("text/html").
jaroslav@233
   128
            get(ClientResponse.class);
jaroslav@233
   129
        final String txt = res.getEntity(String.class);
jaroslav@233
   130
jaroslav@233
   131
        assertEquals("Emails sent to", "xxx@xxx.cz", MockEmailer.address);
jaroslav@233
   132
jaroslav@233
   133
        int urlIndex = MockEmailer.text.indexOf(webResource.getURI().toString());
jaroslav@233
   134
        assertTrue("Found in " + MockEmailer.text, urlIndex >= 0);
jaroslav@233
   135
jaroslav@233
   136
        int endIndex = urlIndex;
jaroslav@233
   137
        while (!Character.isWhitespace(MockEmailer.text.charAt(endIndex))) {
jaroslav@233
   138
            endIndex++;
jaroslav@233
   139
        }
jaroslav@233
   140
        final String url = MockEmailer.text.substring(urlIndex, endIndex);
jaroslav@233
   141
        WebResource requestURL = client.resource(url);
jaroslav@233
   142
jaroslav@233
   143
        String response = requestURL.accept("text/html").get(String.class);
jaroslav@233
   144
        Pattern p = Pattern.compile("<input *type=\"text\" *name=\"email\" *value='xxx@xxx.cz'");
jaroslav@233
   145
        assertTrue("New email found" + response, p.matcher(response).find());
jaroslav@233
   146
    }
jaroslav@233
   147
jaroslav@233
   148
    public static final class MockEmailer extends EmailService {
jaroslav@233
   149
        static String address;
jaroslav@233
   150
        static String subject;
jaroslav@233
   151
        static String text;
jaroslav@233
   152
jaroslav@233
   153
        @Override
jaroslav@233
   154
        public void sendEmail(String address, String subject, String text) throws IOException {
jaroslav@233
   155
            assertNull("No email sent yet", MockEmailer.address);
jaroslav@233
   156
            MockEmailer.address = address;
jaroslav@233
   157
            MockEmailer.subject = subject;
jaroslav@233
   158
            MockEmailer.text = text;
jaroslav@233
   159
        }
jaroslav@233
   160
jaroslav@233
   161
        public static void clear() {
jaroslav@233
   162
            MockEmailer.address = null;
jaroslav@233
   163
            MockEmailer.subject = null;
jaroslav@233
   164
            MockEmailer.text = null;
jaroslav@233
   165
        }
jaroslav@233
   166
    }
jaroslav@233
   167
}