Coverage Report - com.jcabi.http.mock.MkGrizzlyContainer
 
Classes in this File Line Coverage Branch Coverage Complexity
MkGrizzlyContainer
93%
30/32
13%
4/30
0
 
 1  
 /**
 2  
  * Copyright (c) 2011-2015, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.http.mock;
 31  
 
 32  
 import com.jcabi.aspects.Loggable;
 33  
 import com.jcabi.log.Logger;
 34  
 import com.sun.grizzly.http.embed.GrizzlyWebServer;
 35  
 import java.io.IOException;
 36  
 import java.net.ServerSocket;
 37  
 import java.net.URI;
 38  
 import java.util.Collection;
 39  
 import lombok.EqualsAndHashCode;
 40  
 import org.hamcrest.Matcher;
 41  
 import org.hamcrest.core.IsAnything;
 42  
 
 43  
 /**
 44  
  * Implementation of {@link MkContainer} based on Grizzly Server.
 45  
  *
 46  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 47  
  * @version $Id: c8cd697ced7aea5c4aa14f3411e2737b8bf61dbd $
 48  
  * @since 0.10
 49  
  * @see MkContainer
 50  
  * @checkstyle TooManyMethods (200 lines)
 51  
  */
 52  
 @SuppressWarnings("PMD.TooManyMethods")
 53  0
 @EqualsAndHashCode(of = { "adapter", "gws", "port" })
 54  
 @Loggable(Loggable.DEBUG)
 55  63
 public final class MkGrizzlyContainer implements MkContainer {
 56  
 
 57  
     /**
 58  
      * Grizzly adapter.
 59  
      */
 60  63
     private final transient MkGrizzlyAdapter adapter =
 61  
         new MkGrizzlyAdapter();
 62  
 
 63  
     /**
 64  
      * Grizzly container.
 65  
      */
 66  
     private transient GrizzlyWebServer gws;
 67  
 
 68  
     /**
 69  
      * Port where it works.
 70  
      */
 71  
     private transient int port;
 72  
 
 73  
     @Override
 74  
     public MkContainer next(final MkAnswer answer) {
 75  79
         return this.next(answer, new IsAnything<MkQuery>());
 76  
     }
 77  
 
 78  
     @Override
 79  
     public MkContainer next(final MkAnswer answer,
 80  
         final Matcher<MkQuery> condition) {
 81  84
         return this.next(answer, condition, 1);
 82  
     }
 83  
 
 84  
     @Override
 85  
     public MkContainer next(final MkAnswer answer,
 86  
         final Matcher<MkQuery> condition, final int count) {
 87  89
         this.adapter.next(answer, condition, count);
 88  89
         return this;
 89  
     }
 90  
 
 91  
     @Override
 92  
     public MkQuery take() {
 93  27
         return this.adapter.take();
 94  
     }
 95  
 
 96  
     @Override
 97  
     public MkQuery take(final Matcher<MkAnswer> matcher) {
 98  1
         return this.adapter.take(matcher);
 99  
     }
 100  
 
 101  
     @Override
 102  
     public Collection<MkQuery> takeAll(final Matcher<MkAnswer> matcher) {
 103  3
         return this.adapter.takeAll(matcher);
 104  
     }
 105  
 
 106  
     @Override
 107  
     public int queries() {
 108  10
         return this.adapter.queries();
 109  
     }
 110  
 
 111  
     @Override
 112  
     public MkContainer start() throws IOException {
 113  63
         return this.start(MkGrizzlyContainer.reserve());
 114  
     }
 115  
 
 116  
     @Override
 117  
     public MkContainer start(final int prt) throws IOException {
 118  63
         if (this.port != 0) {
 119  0
             throw new IllegalStateException(
 120  
                 String.format(
 121  
                     "already listening on port %d, use #stop() first",
 122  
                     this.port
 123  
                 )
 124  
             );
 125  
         }
 126  63
         this.port = prt;
 127  63
         this.gws = new GrizzlyWebServer(this.port);
 128  63
         this.gws.addGrizzlyAdapter(this.adapter, new String[] {"/"});
 129  63
         this.gws.start();
 130  63
         Logger.info(this, "started on port #%s", prt);
 131  63
         return this;
 132  
     }
 133  
 
 134  
     @Override
 135  
     public void stop() {
 136  59
         if (this.gws != null) {
 137  59
             this.gws.stop();
 138  
         }
 139  59
         Logger.info(this, "stopped on port #%s", this.port);
 140  59
         this.port = 0;
 141  59
     }
 142  
 
 143  
     @Override
 144  
     public URI home() {
 145  65
         return URI.create(
 146  
             String.format("http://localhost:%d/", this.port)
 147  
         );
 148  
     }
 149  
 
 150  
     @Override
 151  
     public void close() {
 152  8
         this.stop();
 153  8
     }
 154  
 
 155  
     /**
 156  
      * Reserve port.
 157  
      * @return Reserved TCP port
 158  
      * @throws IOException If fails
 159  
      */
 160  
     private static int reserve() throws IOException {
 161  
         final int reserved;
 162  63
         try (final ServerSocket socket = new ServerSocket(0)) {
 163  63
             reserved = socket.getLocalPort();
 164  63
         }
 165  63
         return reserved;
 166  
     }
 167  
 }