1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
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 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
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 | |
|
60 | |
|
61 | 69 | private final transient MkGrizzlyAdapter adapter = |
62 | |
new MkGrizzlyAdapter(); |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
private transient GrizzlyWebServer gws; |
68 | |
|
69 | |
|
70 | |
|
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 | |
|
158 | |
|
159 | |
|
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 | |
} |