freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/ChangeEmailTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 15 Apr 2011 21:18:29 +0200
changeset 282 40fc213a7f43
parent 280 800b821c282e
permissions -rw-r--r--
Using Grizzly
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@282
    20
import org.glassfish.grizzly.http.server.HttpServer;
jaroslav@233
    21
import com.sun.jersey.api.client.Client;
jaroslav@233
    22
import com.sun.jersey.api.client.ClientResponse;
jaroslav@233
    23
import com.sun.jersey.api.client.WebResource;
jaroslav@233
    24
import cz.xelfi.quoridor.webidor.resources.Quoridor;
jaroslav@233
    25
import java.io.File;
jaroslav@233
    26
import java.io.FileOutputStream;
jaroslav@233
    27
import java.io.IOException;
jaroslav@233
    28
import java.net.URI;
jaroslav@233
    29
import java.util.Locale;
jaroslav@233
    30
import java.util.concurrent.Callable;
jaroslav@233
    31
import java.util.regex.Pattern;
jaroslav@233
    32
import javax.ws.rs.core.Cookie;
jaroslav@233
    33
import javax.ws.rs.core.MediaType;
jaroslav@233
    34
import org.junit.AfterClass;
jaroslav@233
    35
import org.junit.Before;
jaroslav@233
    36
import org.junit.BeforeClass;
jaroslav@233
    37
import org.junit.Test;
jaroslav@233
    38
import org.netbeans.junit.MockServices;
jaroslav@233
    39
import static org.junit.Assert.*;
jaroslav@233
    40
jaroslav@233
    41
/**
jaroslav@233
    42
 *
jaroslav@233
    43
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@233
    44
 */
jaroslav@233
    45
public class ChangeEmailTest extends Object {
jaroslav@233
    46
    private static File dir;
jaroslav@233
    47
    private static HttpServer stopAPI;
jaroslav@233
    48
    private static HttpServer stopStatistics;
jaroslav@233
    49
    private static Callable<Void> stop;
jaroslav@233
    50
    private WebResource webResource;
jaroslav@233
    51
    private WebResource apiResource;
jaroslav@233
    52
    private WebResource statResource;
jaroslav@233
    53
    private Client client;
jaroslav@233
    54
jaroslav@233
    55
    public ChangeEmailTest() throws Exception {
jaroslav@233
    56
    }
jaroslav@233
    57
jaroslav@233
    58
    @BeforeClass
jaroslav@233
    59
    public static void localeEnglish() throws Exception {
jaroslav@233
    60
        Locale.setDefault(Locale.ENGLISH);
jaroslav@233
    61
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@233
    62
        dir.delete();
jaroslav@233
    63
        dir.mkdirs();
jaroslav@233
    64
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@233
    65
        stopAPI = Quoridor.start(7990);
jaroslav@233
    66
        stopStatistics = Quoridor.start(7992);
jaroslav@234
    67
        stop = UI.startServers(7991, "http://localhost:7990", "http://localhost:7992", null);
jaroslav@233
    68
jaroslav@233
    69
        File passwd = new File(dir, "passwd");
jaroslav@233
    70
        FileOutputStream os = new FileOutputStream(passwd);
jaroslav@233
    71
        os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
jaroslav@233
    72
        os.close();
jaroslav@233
    73
jaroslav@233
    74
        MockServices.setServices(MockEmailer.class);
jaroslav@233
    75
    }
jaroslav@233
    76
jaroslav@233
    77
    @Before
jaroslav@233
    78
    public void setUp() throws Exception {
jaroslav@233
    79
        MockEmailer.clear();
jaroslav@233
    80
jaroslav@233
    81
        client = new Client();
jaroslav@233
    82
        webResource = client.resource(new URI("http://localhost:7991/"));
jaroslav@233
    83
        apiResource = client.resource(new URI("http://localhost:7990/"));
jaroslav@233
    84
        statResource = client.resource(new URI("http://localhost:7992/"));
jaroslav@233
    85
    }
jaroslav@233
    86
jaroslav@233
    87
    @AfterClass
jaroslav@233
    88
    public static void cleanUpAll() throws Exception {
jaroslav@233
    89
        deleteRec(dir);
jaroslav@233
    90
        if (stop != null) {
jaroslav@233
    91
            stop.call();
jaroslav@233
    92
        }
jaroslav@233
    93
        if (stopAPI != null) {
jaroslav@282
    94
            stopAPI.stop();
jaroslav@233
    95
        }
jaroslav@233
    96
        MockServices.setServices();
jaroslav@233
    97
    }
jaroslav@233
    98
jaroslav@233
    99
    static void deleteRec(File dir) throws IOException {
jaroslav@233
   100
        if (dir == null) {
jaroslav@233
   101
            return;
jaroslav@233
   102
        }
jaroslav@233
   103
        File[] arr = dir.listFiles();
jaroslav@233
   104
        if (arr != null) {
jaroslav@233
   105
            for (File f : arr) {
jaroslav@233
   106
                deleteRec(f);
jaroslav@233
   107
            }
jaroslav@233
   108
        }
jaroslav@233
   109
        dir.delete();
jaroslav@233
   110
    }
jaroslav@233
   111
jaroslav@233
   112
    @Test public void testChangeEmail() throws Exception {
jaroslav@233
   113
        String logTest = apiResource.path("login").
jaroslav@233
   114
            queryParam("name", "test").
jaroslav@233
   115
            queryParam("password", "pes").
jaroslav@233
   116
            accept(MediaType.TEXT_PLAIN).
jaroslav@233
   117
            put(String.class);
jaroslav@233
   118
        ClientResponse res = webResource.path("options").
jaroslav@233
   119
            queryParam("email", "xxx@xxx.cz").
jaroslav@233
   120
            cookie(Cookie.valueOf("login=" + logTest)).
jaroslav@233
   121
            accept("text/html").
jaroslav@233
   122
            get(ClientResponse.class);
jaroslav@233
   123
        final String txt = res.getEntity(String.class);
jaroslav@233
   124
jaroslav@233
   125
        assertEquals("Emails sent to", "xxx@xxx.cz", MockEmailer.address);
jaroslav@233
   126
jaroslav@233
   127
        int urlIndex = MockEmailer.text.indexOf(webResource.getURI().toString());
jaroslav@233
   128
        assertTrue("Found in " + MockEmailer.text, urlIndex >= 0);
jaroslav@233
   129
jaroslav@233
   130
        int endIndex = urlIndex;
jaroslav@233
   131
        while (!Character.isWhitespace(MockEmailer.text.charAt(endIndex))) {
jaroslav@233
   132
            endIndex++;
jaroslav@233
   133
        }
jaroslav@233
   134
        final String url = MockEmailer.text.substring(urlIndex, endIndex);
jaroslav@233
   135
        WebResource requestURL = client.resource(url);
jaroslav@233
   136
jaroslav@233
   137
        String response = requestURL.accept("text/html").get(String.class);
jaroslav@233
   138
        Pattern p = Pattern.compile("<input *type=\"text\" *name=\"email\" *value='xxx@xxx.cz'");
jaroslav@233
   139
        assertTrue("New email found" + response, p.matcher(response).find());
jaroslav@233
   140
    }
jaroslav@233
   141
jaroslav@233
   142
    public static final class MockEmailer extends EmailService {
jaroslav@233
   143
        static String address;
jaroslav@233
   144
        static String subject;
jaroslav@233
   145
        static String text;
jaroslav@233
   146
jaroslav@233
   147
        @Override
jaroslav@233
   148
        public void sendEmail(String address, String subject, String text) throws IOException {
jaroslav@233
   149
            assertNull("No email sent yet", MockEmailer.address);
jaroslav@233
   150
            MockEmailer.address = address;
jaroslav@233
   151
            MockEmailer.subject = subject;
jaroslav@233
   152
            MockEmailer.text = text;
jaroslav@233
   153
        }
jaroslav@233
   154
jaroslav@233
   155
        public static void clear() {
jaroslav@233
   156
            MockEmailer.address = null;
jaroslav@233
   157
            MockEmailer.subject = null;
jaroslav@233
   158
            MockEmailer.text = null;
jaroslav@233
   159
        }
jaroslav@233
   160
    }
jaroslav@233
   161
}