Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.test.ws

File SunshowerTestServer.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
19% of files have more coverage

Code metrics

8
44
10
1
109
95
15
0.34
4.4
10
1.5

Classes

Class Line # Actions
SunshowerTestServer 14 44 15
0.741935574.2%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package io.sunshower.test.ws;
2   
3    import io.undertow.Undertow;
4    import io.undertow.servlet.api.DeploymentInfo;
5    import java.net.InetSocketAddress;
6    import java.net.Socket;
7    import java.util.logging.Level;
8    import java.util.logging.Logger;
9    import javax.annotation.PostConstruct;
10    import javax.annotation.PreDestroy;
11    import org.jboss.resteasy.plugins.server.undertow.UndertowJaxrsServer;
12    import org.jboss.resteasy.spi.ResteasyDeployment;
13   
 
14    public class SunshowerTestServer {
15   
16    static final Logger log = Logger.getLogger(SunshowerTestServer.class.getName());
17   
18    final int port;
19    final UndertowJaxrsServer server;
20    final String location;
21    private final ResteasyDeployment deployment;
22    private final EnableJAXRS configuration;
23   
 
24  1 toggle public SunshowerTestServer(EnableJAXRS configuration, ResteasyDeployment deployment) {
25  1 checkDeployment(deployment);
26  1 checkConfiguration(configuration);
27  1 this.deployment = deployment;
28  1 this.configuration = configuration;
29  1 this.location = resolveLocation();
30  1 this.port = resolvePort();
31  1 this.server = createServer();
32    }
33   
 
34  1 toggle public String getLocation() {
35  1 return String.format("http://%s:%s/", location, port);
36    }
37   
 
38  1 toggle @PostConstruct
39    public void start() {
40  1 log.log(Level.INFO, "Starting sunshower test server...");
41  1 DeploymentInfo deployment = server.undertowDeployment(this.deployment);
42  1 deployment.setContextPath("/");
43  1 deployment.setClassLoader(getClass().getClassLoader());
44  1 deployment.setDeploymentName("sunshower-test" + hashCode());
45  1 server.deploy(deployment);
46  1 log.log(Level.INFO, "Sunshower test server started");
47    }
48   
 
49  0 toggle @PreDestroy
50    public void stop() {
51  0 log.log(Level.INFO, "Stopping sunshower test server...");
52  0 server.stop();
53  0 log.log(Level.INFO, "Sunshower test server stopped.");
54    }
55   
 
56  1 toggle private String resolveLocation() {
57  1 return configuration.location();
58    }
59   
 
60  1 toggle private int resolvePort() {
61  1 int port = configuration.port();
62  1 if (port == -1) {
63  1 return randomPort();
64    }
65  0 log.log(Level.INFO, "Attempting to start server on requested port {0}", port);
66  0 return port;
67    }
68   
 
69  1 toggle private UndertowJaxrsServer createServer() {
70  1 Undertow.Builder builder =
71    Undertow.builder().addHttpListener(port, location).setWorkerThreads(2).setIoThreads(2);
72  1 return new UndertowJaxrsServer().start(builder);
73    }
74   
 
75  1 toggle private int randomPort() {
76  1 log.log(Level.INFO, "Starting on a random port...");
77  1 int timeout = 200;
78  1 for (int i = 65535; i > 1000; i--) {
79  1 try {
80  1 Socket socket = new Socket();
81  1 socket.connect(new InetSocketAddress(location, i), timeout);
82  0 socket.close();
83    } catch (Exception ex) {
84  1 log.log(Level.INFO, "Found local port: {0}", i);
85  1 return i;
86    }
87    }
88  0 throw new IllegalStateException("Couldn't find any open ports. Can't continue");
89    }
90   
 
91  1 toggle private void checkConfiguration(EnableJAXRS configuration) {
92  1 if (configuration == null) {
93  0 log.log(
94    Level.SEVERE,
95    "Your test class was not annotated with @EnableJAXRS. "
96    + "I don't know how you got here, but this isn't the right place for you.");
97  0 throw new IllegalStateException("Error: @EnableJAXRS was not present. Can't continue");
98    }
99  1 log.log(Level.INFO, "Found @EnableJAXRS with value {0}", configuration);
100    }
101   
 
102  1 toggle private void checkDeployment(ResteasyDeployment deployment) {
103  1 if (deployment == null) {
104  0 log.log(Level.SEVERE, "No deployment found. Can't continue (this is probably a bug)");
105  0 throw new IllegalStateException("Error: @EnableJAXRS was not present. Can't continue");
106    }
107  1 log.log(Level.INFO, "Found @EnableJAXRS with value {0}", deployment);
108    }
109    }