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