-
Notifications
You must be signed in to change notification settings - Fork 138
/
ServerUtils.java
130 lines (117 loc) · 4.3 KB
/
ServerUtils.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (c) 2021, 2023 Eclipse Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.tests.utils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import org.glassfish.embeddable.CommandResult;
import org.glassfish.embeddable.GlassFish;
import org.glassfish.embeddable.GlassFishException;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import static org.junit.jupiter.api.Assertions.fail;
/**
* @author David Matejcek
*/
public class ServerUtils {
/**
* Tries to allocate a free local port.
*
* @return a free local port number.
* @throws IllegalStateException if it fails for 20 times
*/
public static int getFreePort() throws IllegalStateException {
int counter = 0;
while (true) {
counter++;
try {
final ServerSocket socket = new ServerSocket(0);
final int port = socket.getLocalPort();
socket.setSoTimeout(1);
socket.setReuseAddress(true);
socket.close();
return port;
} catch (IOException e) {
if (counter >= 20) {
throw new IllegalStateException("Cannot open random port, tried 20 times.", e);
}
}
}
}
/**
* @return the IP address of the localhost.
*/
public static String getLocalIP4Address() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
throw new IllegalStateException("Cannot determine the local address.", e);
}
}
/**
* Creates a simple war file made of provided classes.
*
* @param warFile the WAR file
* @param classes a classes to be included to the WAR file
* @return file usable for deployment.
*/
public static File createWar(final File warFile, final Class<?>... classes) {
try {
final WebArchive war = ShrinkWrap.create(WebArchive.class).addClasses(classes);
war.as(ZipExporter.class).exportTo(warFile, true);
return warFile;
} catch (Exception e) {
return fail(e);
}
}
/**
* Downloads content from the url.
* Expects there is a service listening and returning textual response.
* Therefore this is usable just for simple servlets.
*
* @param url target URL
* @return content from the url.
* @throws IOException if an error occurred
*/
public static String download(final URL url) throws IOException {
final Object object = url.getContent();
if (object instanceof InputStream) {
try (InputStream input = (InputStream) object; Scanner scanner = new Scanner(input, StandardCharsets.UTF_8)) {
return scanner.nextLine();
}
}
return fail("Expected input stream, but received this: " + object);
}
/**
* Executes a command on embedded GlassFish instance.
*
* @param glassfish an embedded GlassFish instance
* @param cmd a command to run
* @param params command parameters
* @return result of command execution
* @throws GlassFishException if an error occurred
*/
public static CommandResult runCommand(GlassFish glassfish, String cmd, String params) throws GlassFishException {
return glassfish.getCommandRunner().run(cmd, params);
}
}