Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,21 @@
<version>${json.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -96,7 +95,7 @@ public void init(Properties properties) throws IOException {
streamProvider = new TokenStreamProvider(saToken, caCertFile);
} else {
// TODO: implement CertificateStreamProvider
throw new NotImplementedException();
throw new UnsupportedOperationException("CertificateStreamProvider is not yet implemented");
}

String ver = getEnv(ENV_PREFIX + "API_VERSION");
Expand Down
119 changes: 119 additions & 0 deletions src/test/java/org/apache/tomcat/cloud/AbstractMemberProviderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();
}
}
}
Loading