Skip to content

Commit 6deb0dc

Browse files
committed
[java] start the secure server only when needed in unit tests
1 parent 61a1eb1 commit 6deb0dc

13 files changed

+95
-29
lines changed

java/test/org/openqa/selenium/CookieImplementationTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
import org.openqa.selenium.environment.DomainHelper;
3636
import org.openqa.selenium.testing.Ignore;
3737
import org.openqa.selenium.testing.JupiterTestBase;
38+
import org.openqa.selenium.testing.NeedsSecureServer;
3839
import org.openqa.selenium.testing.NotWorkingInRemoteBazelBuilds;
3940
import org.openqa.selenium.testing.NotYetImplemented;
4041
import org.openqa.selenium.testing.SwitchToTopAfterTest;
4142

43+
@NeedsSecureServer
4244
class CookieImplementationTest extends JupiterTestBase {
4345

4446
private DomainHelper domainHelper;

java/test/org/openqa/selenium/PageLoadingTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@
3636
import org.openqa.selenium.testing.Ignore;
3737
import org.openqa.selenium.testing.JupiterTestBase;
3838
import org.openqa.selenium.testing.NeedsFreshDriver;
39+
import org.openqa.selenium.testing.NeedsSecureServer;
3940
import org.openqa.selenium.testing.NoDriverAfterTest;
4041
import org.openqa.selenium.testing.NotYetImplemented;
4142
import org.openqa.selenium.testing.SwitchToTopAfterTest;
4243

44+
@NeedsSecureServer
4345
class PageLoadingTest extends JupiterTestBase {
4446

4547
@Test

java/test/org/openqa/selenium/environment/InProcessTestEnvironment.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public class InProcessTestEnvironment implements TestEnvironment {
2424

2525
private final AppServer appServer;
2626

27-
public InProcessTestEnvironment() {
28-
appServer = new NettyAppServer();
27+
public InProcessTestEnvironment(boolean secureServer) {
28+
appServer = new NettyAppServer(secureServer);
2929
appServer.start();
3030
}
3131

@@ -40,6 +40,6 @@ public void stop() {
4040
}
4141

4242
public static void main(String[] args) {
43-
new InProcessTestEnvironment();
43+
new InProcessTestEnvironment(true);
4444
}
4545
}

java/test/org/openqa/selenium/environment/webserver/NettyAppServer.java

+14-12
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ public class NettyAppServer implements AppServer {
6868
LOG.log(
6969
Level.WARNING,
7070
String.format("NettyAppServer retry #%s. ", e.getAttemptCount()));
71-
initValues();
71+
initValues(secure != null);
7272
})
7373
.onRetriesExceeded(e -> LOG.log(Level.WARNING, "NettyAppServer start aborted."))
7474
.build();
7575

76-
public NettyAppServer() {
77-
initValues();
76+
public NettyAppServer(boolean secureServer) {
77+
initValues(secureServer);
7878
}
7979

8080
public NettyAppServer(HttpHandler handler) {
@@ -116,7 +116,7 @@ public static void main(String[] args) {
116116
System.out.printf("Server started. Root URL: %s%n", server.whereIs("/"));
117117
}
118118

119-
private void initValues() {
119+
private void initValues(boolean secureServer) {
120120
Config config = createDefaultConfig();
121121
BaseServerOptions options = new BaseServerOptions(config);
122122

@@ -128,16 +128,18 @@ private void initValues() {
128128

129129
server = new NettyServer(options, handler);
130130

131-
Config secureConfig = new CompoundConfig(sslConfig, createDefaultConfig());
132-
BaseServerOptions secureOptions = new BaseServerOptions(secureConfig);
131+
if (secureServer) {
132+
Config secureConfig = new CompoundConfig(sslConfig, createDefaultConfig());
133+
BaseServerOptions secureOptions = new BaseServerOptions(secureConfig);
133134

134-
HttpHandler secureHandler =
135-
new HandlersForTests(
136-
secureOptions.getHostname().orElse("localhost"),
137-
secureOptions.getPort(),
138-
tempDir.toPath());
135+
HttpHandler secureHandler =
136+
new HandlersForTests(
137+
secureOptions.getHostname().orElse("localhost"),
138+
secureOptions.getPort(),
139+
tempDir.toPath());
139140

140-
secure = new NettyServer(secureOptions, secureHandler);
141+
secure = new NettyServer(secureOptions, secureHandler);
142+
}
141143
}
142144

143145
@Override

java/test/org/openqa/selenium/environment/webserver/NettyAppServerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ class NettyAppServerTest extends AppServerTestBase {
2121

2222
@Override
2323
protected AppServer createAppServer() {
24-
return new NettyAppServer();
24+
return new NettyAppServer(false);
2525
}
2626
}

java/test/org/openqa/selenium/federatedcredentialmanagement/FederatedCredentialManagementTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
import org.openqa.selenium.chrome.ChromeOptions;
3737
import org.openqa.selenium.support.ui.WebDriverWait;
3838
import org.openqa.selenium.testing.JupiterTestBase;
39+
import org.openqa.selenium.testing.NeedsSecureServer;
3940
import org.openqa.selenium.testing.NotYetImplemented;
4041

42+
@NeedsSecureServer
4143
class FederatedCredentialManagementTest extends JupiterTestBase {
4244

4345
private JavascriptExecutor jsAwareDriver;

java/test/org/openqa/selenium/grid/router/RemoteWebDriverBiDiTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class RemoteWebDriverBiDiTest {
6161

6262
@BeforeAll
6363
static void serverSetup() {
64-
server = new NettyAppServer();
64+
server = new NettyAppServer(false);
6565
server.start();
6666
}
6767

java/test/org/openqa/selenium/grid/router/RemoteWebDriverDownloadTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void setupServers() {
8888
tearDowns.add(deployment);
8989

9090
server = deployment.getServer();
91-
appServer = new NettyAppServer();
91+
appServer = new NettyAppServer(false);
9292
tearDowns.add(() -> appServer.stop());
9393
appServer.start();
9494
}

java/test/org/openqa/selenium/javascript/JavaScriptTestSuite.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
import java.util.List;
3030
import java.util.function.Function;
3131
import java.util.function.Supplier;
32+
import org.junit.jupiter.api.AfterAll;
3233
import org.junit.jupiter.api.AfterEach;
33-
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.BeforeAll;
3435
import org.junit.jupiter.api.DynamicTest;
3536
import org.junit.jupiter.api.TestFactory;
3637
import org.openqa.selenium.WebDriver;
@@ -48,7 +49,7 @@ class JavaScriptTestSuite {
4849

4950
private final long timeout;
5051

51-
private TestEnvironment testEnvironment;
52+
private static TestEnvironment testEnvironment;
5253

5354
public JavaScriptTestSuite() {
5455
this.timeout = Math.max(0, Long.getLong("js.test.timeout", 0));
@@ -59,22 +60,26 @@ private static boolean isBazel() {
5960
return InProject.findRunfilesRoot() != null;
6061
}
6162

62-
@BeforeEach
63-
public void setup() {
63+
@BeforeAll
64+
public static void setup() {
6465
// this field is actually in use, javascript test do access it
65-
testEnvironment = GlobalTestEnvironment.getOrCreate(InProcessTestEnvironment::new);
66+
testEnvironment = GlobalTestEnvironment.getOrCreate(() -> new InProcessTestEnvironment(true));
6667
}
6768

6869
@AfterEach
69-
public void teardown() throws IOException {
70-
if (testEnvironment != null) {
71-
testEnvironment.stop();
72-
}
70+
public void clear() throws IOException {
7371
if (driverSupplier != null) {
7472
((Closeable) driverSupplier).close();
7573
}
7674
}
7775

76+
@AfterAll
77+
public static void teardown() throws IOException {
78+
if (testEnvironment != null) {
79+
testEnvironment.stop();
80+
}
81+
}
82+
7883
@TestFactory
7984
public Collection<DynamicTest> dynamicTests() throws IOException {
8085
final Path baseDir = InProject.findProjectRoot();

java/test/org/openqa/selenium/safari/CrossDomainTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class CrossDomainTest extends JupiterTestBase {
4141

4242
@BeforeAll
4343
public static void startSecondServer() {
44-
otherServer = new NettyAppServer();
44+
otherServer = new NettyAppServer(false);
4545
otherServer.start();
4646

4747
otherPages = new Pages(otherServer);

java/test/org/openqa/selenium/testing/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ java_library(
2020
"Ignore.java",
2121
"IgnoreList.java",
2222
"NeedsFreshDriver.java",
23+
"NeedsSecureServer.java",
2324
"NoDriverAfterTest.java",
2425
"NoDriverBeforeTest.java",
2526
"NotWorkingInRemoteBazelBuilds.java",

java/test/org/openqa/selenium/testing/JupiterTestBase.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.net.MalformedURLException;
2323
import java.net.URL;
2424
import java.time.Duration;
25+
import java.util.Optional;
26+
import java.util.logging.Logger;
2527
import org.junit.jupiter.api.AfterEach;
2628
import org.junit.jupiter.api.BeforeAll;
2729
import org.junit.jupiter.api.BeforeEach;
@@ -37,6 +39,8 @@
3739

3840
public abstract class JupiterTestBase {
3941

42+
private static final Logger LOG = Logger.getLogger(JupiterTestBase.class.getName());
43+
4044
@RegisterExtension protected static SeleniumExtension seleniumExtension = new SeleniumExtension();
4145

4246
protected TestEnvironment environment;
@@ -54,9 +58,27 @@ public static void shouldTestBeRunAtAll() {
5458

5559
@BeforeEach
5660
public void prepareEnvironment() {
57-
environment = GlobalTestEnvironment.getOrCreate(InProcessTestEnvironment::new);
61+
boolean needsSecureServer =
62+
Optional.ofNullable(this.getClass().getAnnotation(NeedsSecureServer.class))
63+
.map(NeedsSecureServer::value)
64+
.orElse(false);
65+
66+
environment =
67+
GlobalTestEnvironment.getOrCreate(() -> new InProcessTestEnvironment(needsSecureServer));
5868
appServer = environment.getAppServer();
5969

70+
if (needsSecureServer) {
71+
try {
72+
appServer.whereIsSecure("/");
73+
} catch (IllegalStateException ex) {
74+
// this should not happen with bazel, a new JVM is used for each class
75+
// the annotation is on class level, so we should never see this
76+
LOG.info("appServer is restarted with secureServer=true");
77+
environment.stop();
78+
environment = new InProcessTestEnvironment(true);
79+
}
80+
}
81+
6082
pages = new Pages(appServer);
6183

6284
driver = seleniumExtension.getDriver();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.testing;
19+
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
@Retention(RetentionPolicy.RUNTIME)
26+
@Target(ElementType.TYPE)
27+
public @interface NeedsSecureServer {
28+
29+
boolean value() default true;
30+
}

0 commit comments

Comments
 (0)