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-2017, 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.aspects.RetryOnFailure;
 34  
 import com.jcabi.log.Logger;
 35  
 import com.sun.grizzly.http.embed.GrizzlyWebServer;
 36  
 import java.io.IOException;
 37  
 import java.net.ServerSocket;
 38  
 import java.net.URI;
 39  
 import java.util.Collection;
 40  
 import lombok.EqualsAndHashCode;
 41  
 import org.hamcrest.Matcher;
 42  
 import org.hamcrest.core.IsAnything;
 43  
 
 44  
 /**
 45  
  * Implementation of {@link MkContainer} based on Grizzly Server.
 46  
  *
 47  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 48  
  * @version $Id: 8d7dd7b6f69ccc7309054b094d710d6af59c44db $
 49  
  * @since 0.10
 50  
  * @see MkContainer
 51  
  * @checkstyle TooManyMethods (200 lines)
 52  
  */
 53  
 @SuppressWarnings("PMD.TooManyMethods")
 54  0
 @EqualsAndHashCode(of = { "adapter", "gws", "port" })
 55  
 @Loggable(Loggable.DEBUG)
 56  69
 public final class MkGrizzlyContainer implements MkContainer {
 57  
 
 58  
     /**
 59  
      * Grizzly adapter.
 60  
      */
 61  69
     private final transient MkGrizzlyAdapter adapter =
 62  
         new MkGrizzlyAdapter();
 63  
 
 64  
     /**
 65  
      * Grizzly container.
 66  
      */
 67  
     private transient GrizzlyWebServer gws;
 68  
 
 69  
     /**
 70  
      * Port where it works.
 71  
      */
 72  
     private transient int port;
 73  
 
 74  
     @Override
 75  
     public MkContainer next(final MkAnswer answer) {
 76  83
         return this.next(answer, new IsAnything<MkQuery>());
 77  
     }
 78  
 
 79  
     @Override
 80  
     public MkContainer next(final MkAnswer answer,
 81  
         final Matcher<MkQuery> condition) {
 82  94
         return this.next(answer, condition, 1);
 83  
     }
 84  
 
 85  
     @Override
 86  
     public MkContainer next(final MkAnswer answer,
 87  
         final Matcher<MkQuery> condition, final int count) {
 88  99
         this.adapter.next(answer, condition, count);
 89  99
         return this;
 90  
     }
 91  
 
 92  
     @Override
 93  
     public MkQuery take() {
 94  30
         return this.adapter.take();
 95  
     }
 96  
 
 97  
     @Override
 98  
     public MkQuery take(final Matcher<MkAnswer> matcher) {
 99  1
         return this.adapter.take(matcher);
 100  
     }
 101  
 
 102  
     @Override
 103  
     public Collection<MkQuery> takeAll(final Matcher<MkAnswer> matcher) {
 104  3
         return this.adapter.takeAll(matcher);
 105  
     }
 106  
 
 107  
     @Override
 108  
     public int queries() {
 109  10
         return this.adapter.queries();
 110  
     }
 111  
 
 112  
     @Override
 113  
     public MkContainer start() throws IOException {
 114  69
         return this.start(MkGrizzlyContainer.reserve());
 115  
     }
 116  
 
 117  
     @Override
 118  
     public MkContainer start(final int prt) throws IOException {
 119  67
         if (this.port != 0) {
 120  0
             throw new IllegalStateException(
 121  
                 String.format(
 122  
                     "already listening on port %d, use #stop() first",
 123  
                     this.port
 124  
                 )
 125  
             );
 126  
         }
 127  67
         this.port = prt;
 128  67
         this.gws = new GrizzlyWebServer(this.port);
 129  69
         this.gws.addGrizzlyAdapter(this.adapter, new String[] {"/"});
 130  69
         this.gws.start();
 131  68
         Logger.info(this, "started on port #%s", prt);
 132  69
         return this;
 133  
     }
 134  
 
 135  
     @Override
 136  
     public void stop() {
 137  65
         if (this.gws != null) {
 138  65
             this.gws.stop();
 139  
         }
 140  65
         Logger.info(this, "stopped on port #%s", this.port);
 141  65
         this.port = 0;
 142  65
     }
 143  
 
 144  
     @Override
 145  
     public URI home() {
 146  71
         return URI.create(
 147  
             String.format("http://localhost:%d/", this.port)
 148  
         );
 149  
     }
 150  
 
 151  
     @Override
 152  
     public void close() {
 153  9
         this.stop();
 154  9
     }
 155  
 
 156  
     /**
 157  
      * Reserve port.
 158  
      * @return Reserved TCP port
 159  
      * @throws IOException If fails
 160  
      */
 161  
     @RetryOnFailure
 162  
     private static int reserve() throws IOException {
 163  
         final int reserved;
 164  69
         try (final ServerSocket socket = new ServerSocket(0)) {
 165  68
             reserved = socket.getLocalPort();
 166  67
         }
 167  66
         return reserved;
 168  
     }
 169  
 }