-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractMemberProviderTest.java
More file actions
119 lines (96 loc) · 4.37 KB
/
AbstractMemberProviderTest.java
File metadata and controls
119 lines (96 loc) · 4.37 KB
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.cloud;
import org.apache.catalina.tribes.Member;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import static org.junit.jupiter.api.Assertions.*;
class AbstractMemberProviderTest {
private ConcreteProvider provider;
@BeforeEach
void setUp() {
provider = new ConcreteProvider();
}
// -------------------------------------------------------------------------
// constructor
// -------------------------------------------------------------------------
@Test
void constructor_initializesMd5() {
// MD5 is guaranteed by the Java Security specification on all compliant JVMs.
// If the algorithm were unavailable the field would silently remain null, which
// would cause a NullPointerException the first time digest() is called in a subclass.
assertNotNull(provider.md5, "md5 digest should be initialized in constructor");
}
// -------------------------------------------------------------------------
// getEnv()
// -------------------------------------------------------------------------
@Test
void getEnv_returnsNullWhenKeyNotFound() {
assertNull(provider.getEnvForTest("THIS_ENV_KEY_DOES_NOT_EXIST_12345"));
}
@Test
void getEnv_returnsNullWhenAllKeysMissing() {
assertNull(provider.getEnvForTest("FAKE_KEY_AAA_99999", "FAKE_KEY_BBB_99999"));
}
@Test
void getEnv_noKeys_returnsNull() {
// Calling getEnv() with an empty varargs array must return null because the loop
// body never executes.
assertNull(provider.getEnvForTest());
}
@Test
void getEnv_nullKey_throwsNullPointerException() {
// System.getenv(null) throws NullPointerException per the Java specification.
// A null element in the keys array propagates that exception to the caller.
// This documents that null keys are not supported; callers must ensure all keys
// are non-null.
assertThrows(NullPointerException.class,
() -> provider.getEnvForTest((String) null));
}
@Test
void getEnv_returnsFirstNonNullValue() {
String pathValue = System.getenv("PATH");
Assumptions.assumeTrue(pathValue != null, "PATH env var not available, skipping");
String result = provider.getEnvForTest("THIS_KEY_DOES_NOT_EXIST", "PATH");
assertEquals(pathValue, result, "Should skip missing keys and return the first found value");
}
@Test
void getEnv_stopsAtFirstFoundKey() {
String pathValue = System.getenv("PATH");
Assumptions.assumeTrue(pathValue != null, "PATH env var not available, skipping");
String result = provider.getEnvForTest("PATH", "THIS_KEY_DOES_NOT_EXIST");
assertEquals(pathValue, result, "Should return immediately when the first key is found");
}
// -------------------------------------------------------------------------
// Helper: concrete subclass to expose protected members for testing
// -------------------------------------------------------------------------
private static class ConcreteProvider extends AbstractMemberProvider {
String getEnvForTest(String... keys) {
return getEnv(keys);
}
@Override
public void init(Properties properties) throws Exception {}
@Override
public List<? extends Member> getMembers() throws Exception {
return Collections.emptyList();
}
}
}