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