|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
40 | 40 | // Checkstyle: allow Class.getSimpleName
|
41 | 41 |
|
42 | 42 | /**
|
43 |
| - * Tests a workaround for {@linkplain ServiceLoader services} without a {@linkplain ServiceLoader |
44 |
| - * provider constructor} (nullary constructor) [GR-19958]. The workaround completely ignores |
45 |
| - * services without a provider constructor, instead of throwing an {@link ServiceConfigurationError} |
46 |
| - * when iterating the services. See the Github issue |
47 |
| - * <a href="https://github.com/oracle/graal/issues/2652">"Spring Service Registry native-image |
48 |
| - * failure due to service loader handling in jersey #2652"</a> for more details. |
| 43 | + * Test both JCA-compliant services and non-JCA-compliant services (without a nullary constructor), |
| 44 | + * and compare the behavior between Native Image and Hotspot. |
49 | 45 | */
|
50 | 46 | public class NoProviderConstructorServiceLoaderTest {
|
51 | 47 |
|
@@ -73,81 +69,77 @@ public abstract static class NoProviderConstructorService implements ServiceInte
|
73 | 69 |
|
74 | 70 | private static final Set<String> EXPECTED = Set.of(ProperService.class.getSimpleName());
|
75 | 71 |
|
76 |
| - /** |
77 |
| - * This should actually throw an {@link ServiceConfigurationError}. |
78 |
| - * |
79 |
| - * @see #testLazyStreamHotspot() |
80 |
| - */ |
81 |
| - @Test |
| 72 | + @Test(expected = ServiceConfigurationError.class) |
82 | 73 | public void testLazyStreamNativeImage() {
|
83 |
| - Assume.assumeTrue("native image specific behavior", ImageInfo.inImageRuntimeCode()); |
84 |
| - Set<String> simpleNames = ServiceLoader.load(ServiceInterface.class).stream() |
85 |
| - .map(provider -> provider.type().getSimpleName()) |
86 |
| - .collect(Collectors.toSet()); |
| 74 | + assumeEnvironment(true); |
| 75 | + Set<String> simpleNames = loadLazyStreamNames(); |
87 | 76 | Assert.assertEquals(EXPECTED, simpleNames);
|
88 | 77 | }
|
89 | 78 |
|
90 |
| - /** |
91 |
| - * This should actually throw an {@link ServiceConfigurationError}. |
92 |
| - * |
93 |
| - * @see #testEagerStreamHotspot() |
94 |
| - */ |
95 |
| - @Test |
| 79 | + @Test(expected = ServiceConfigurationError.class) |
96 | 80 | public void testEagerStreamNativeImage() {
|
97 |
| - Assume.assumeTrue("native image specific behavior", ImageInfo.inImageRuntimeCode()); |
98 |
| - Set<String> simpleNames = ServiceLoader.load(ServiceInterface.class).stream() |
99 |
| - .map(provider -> provider.get().getClass().getSimpleName()) |
100 |
| - .collect(Collectors.toSet()); |
| 81 | + assumeEnvironment(true); |
| 82 | + Set<String> simpleNames = loadEagerStreamNames(); |
101 | 83 | Assert.assertEquals(EXPECTED, simpleNames);
|
102 | 84 | }
|
103 | 85 |
|
104 |
| - /** |
105 |
| - * This should actually throw an {@link ServiceConfigurationError}. |
106 |
| - * |
107 |
| - * @see #testEagerIteratorHotspot() |
108 |
| - */ |
109 |
| - @Test |
| 86 | + @Test(expected = ServiceConfigurationError.class) |
110 | 87 | public void testEagerIteratorNativeImage() {
|
111 |
| - Assume.assumeTrue("native image specific behavior", ImageInfo.inImageRuntimeCode()); |
112 |
| - Set<String> simpleNames = new HashSet<>(); |
113 |
| - ServiceLoader.load(ServiceInterface.class).iterator() |
114 |
| - .forEachRemaining(s -> simpleNames.add(s.getClass().getSimpleName())); |
| 88 | + assumeEnvironment(true); |
| 89 | + Set<String> simpleNames = loadEagerIteratorNames(); |
115 | 90 | Assert.assertEquals(EXPECTED, simpleNames);
|
116 | 91 | }
|
117 | 92 |
|
118 |
| - /** |
119 |
| - * @see #testLazyStreamNativeImage() |
120 |
| - */ |
121 | 93 | @Test(expected = ServiceConfigurationError.class)
|
122 | 94 | public void testLazyStreamHotspot() {
|
123 |
| - Assume.assumeFalse("hotspot specific behavior", ImageInfo.inImageRuntimeCode()); |
124 |
| - Set<String> simpleNames = ServiceLoader.load(ServiceInterface.class).stream() |
| 95 | + assumeEnvironment(false); |
| 96 | + Set<String> simpleNames = loadLazyStreamNames(); |
| 97 | + Assert.assertNull("should not reach", simpleNames); |
| 98 | + } |
| 99 | + |
| 100 | + @Test(expected = ServiceConfigurationError.class) |
| 101 | + public void testEagerIteratorHotspot() { |
| 102 | + assumeEnvironment(false); |
| 103 | + Set<String> simpleNames = loadEagerIteratorNames(); |
| 104 | + Assert.assertNull("should not reach", simpleNames); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Helper method to assume the environment (hotspot/native image). |
| 109 | + */ |
| 110 | + private static void assumeEnvironment(boolean isNativeImage) { |
| 111 | + if (isNativeImage) { |
| 112 | + Assume.assumeTrue("native image specific behavior", ImageInfo.inImageRuntimeCode()); |
| 113 | + } else { |
| 114 | + Assume.assumeFalse("hotspot specific behavior", ImageInfo.inImageRuntimeCode()); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Helper method for lazy stream tests. |
| 120 | + */ |
| 121 | + private static Set<String> loadLazyStreamNames() { |
| 122 | + return ServiceLoader.load(ServiceInterface.class).stream() |
125 | 123 | .map(provider -> provider.type().getSimpleName())
|
126 | 124 | .collect(Collectors.toSet());
|
127 |
| - Assert.assertNull("should not reach", simpleNames); |
128 | 125 | }
|
129 | 126 |
|
130 | 127 | /**
|
131 |
| - * @see #testEagerStreamNativeImage() |
| 128 | + * Helper method for eager stream tests. |
132 | 129 | */
|
133 |
| - @Test(expected = ServiceConfigurationError.class) |
134 |
| - public void testEagerStreamHotspot() { |
135 |
| - Assume.assumeFalse("hotspot specific behavior", ImageInfo.inImageRuntimeCode()); |
136 |
| - Set<String> simpleNames = ServiceLoader.load(ServiceInterface.class).stream() |
| 130 | + private static Set<String> loadEagerStreamNames() { |
| 131 | + return ServiceLoader.load(ServiceInterface.class).stream() |
137 | 132 | .map(provider -> provider.get().getClass().getSimpleName())
|
138 | 133 | .collect(Collectors.toSet());
|
139 |
| - Assert.assertNull("should not reach", simpleNames); |
140 | 134 | }
|
141 | 135 |
|
142 | 136 | /**
|
143 |
| - * @see #testEagerIteratorNativeImage() |
| 137 | + * Helper method for eager iterator tests. |
144 | 138 | */
|
145 |
| - @Test(expected = ServiceConfigurationError.class) |
146 |
| - public void testEagerIteratorHotspot() { |
147 |
| - Assume.assumeFalse("hotspot specific behavior", ImageInfo.inImageRuntimeCode()); |
| 139 | + private static Set<String> loadEagerIteratorNames() { |
148 | 140 | Set<String> simpleNames = new HashSet<>();
|
149 | 141 | ServiceLoader.load(ServiceInterface.class).iterator()
|
150 | 142 | .forEachRemaining(s -> simpleNames.add(s.getClass().getSimpleName()));
|
151 |
| - Assert.assertNull("should not reach", simpleNames); |
| 143 | + return simpleNames; |
152 | 144 | }
|
153 | 145 | }
|
0 commit comments