Skip to content

Commit

Permalink
Issue eclipse-ee4j#23507 server-mgmgt tests transformed from testNG t…
Browse files Browse the repository at this point in the history
…o junit5
  • Loading branch information
dmatej committed Jul 5, 2021
1 parent b1087de commit 6bd40a8
Show file tree
Hide file tree
Showing 19 changed files with 1,005 additions and 1,191 deletions.
10 changes: 5 additions & 5 deletions nucleus/admin/server-mgmt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@
<artifactId>admin-cli</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.annotations</groupId>
<artifactId>logging-annotation-processor</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2018-2021 Oracle 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
Expand All @@ -18,56 +18,62 @@

import java.util.Properties;

import org.testng.annotations.Test;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

import com.sun.enterprise.admin.servermgmt.DomainConfig;
import com.sun.enterprise.admin.servermgmt.DomainException;

public class TestDomainPortValidator {
public class DomainPortValidatorTest {

private DomainPortValidator _portValidator = null;
private DomainPortValidator portValidator;

@Test (expectedExceptions = DomainException.class)
@Test
public void testForNullPorts() throws Exception {
DomainConfig domainConfig = new DomainConfig("test", null);
domainConfig.add(DomainConfig.K_VALIDATE_PORTS, Boolean.TRUE);
_portValidator = new DomainPortValidator(domainConfig, new Properties());
_portValidator.validateAndSetPorts();
portValidator = new DomainPortValidator(domainConfig, new Properties());
assertThrows(DomainException.class, () -> portValidator.validateAndSetPorts());
}

@Test (expectedExceptions = DomainException.class)

@Test
public void testForNonNumericPort() throws Exception {
DomainConfig domainConfig = new DomainConfig("test", null);
domainConfig.add(DomainConfig.K_VALIDATE_PORTS, Boolean.TRUE);
domainConfig.add(DomainConfig.K_ADMIN_PORT, "admin2");
_portValidator = new DomainPortValidator(domainConfig, new Properties());
_portValidator.validateAndSetPorts();
portValidator = new DomainPortValidator(domainConfig, new Properties());
assertThrows(DomainException.class, () -> portValidator.validateAndSetPorts());
}

@Test (expectedExceptions = DomainException.class)

@Test
public void testForNegativePort() throws Exception {
DomainConfig domainConfig = new DomainConfig("test", null);
domainConfig.add(DomainConfig.K_VALIDATE_PORTS, Boolean.TRUE);
domainConfig.add(DomainConfig.K_ADMIN_PORT, "-2");
_portValidator = new DomainPortValidator(domainConfig, new Properties());
_portValidator.validateAndSetPorts();
portValidator = new DomainPortValidator(domainConfig, new Properties());
assertThrows(DomainException.class, () -> portValidator.validateAndSetPorts());
}

@Test (expectedExceptions = DomainException.class)

@Test
public void testForPortValueZero() throws Exception {
DomainConfig domainConfig = new DomainConfig("test", null);
domainConfig.add(DomainConfig.K_VALIDATE_PORTS, Boolean.TRUE);
domainConfig.add(DomainConfig.K_ADMIN_PORT, "0");
_portValidator = new DomainPortValidator(domainConfig, new Properties());
_portValidator.validateAndSetPorts();
portValidator = new DomainPortValidator(domainConfig, new Properties());
assertThrows(DomainException.class, () -> portValidator.validateAndSetPorts());
}

@Test (expectedExceptions = DomainException.class)

@Test
public void testForMaxPort() throws Exception {
DomainConfig domainConfig = new DomainConfig("test", null);
domainConfig.add(DomainConfig.K_VALIDATE_PORTS, Boolean.TRUE);
domainConfig.add(DomainConfig.K_ADMIN_PORT, String.valueOf((DomainPortValidator.PORT_MAX_VAL + 1)));
_portValidator = new DomainPortValidator(domainConfig, new Properties());
_portValidator.validateAndSetPorts();
portValidator = new DomainPortValidator(domainConfig, new Properties());
assertThrows(DomainException.class, () -> portValidator.validateAndSetPorts());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright (c) 2013, 2018-2021 Oracle 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 com.sun.enterprise.admin.servermgmt.stringsubs;

import com.sun.enterprise.admin.servermgmt.stringsubs.impl.AttributePreprocessorImpl;
import com.sun.enterprise.admin.servermgmt.stringsubs.impl.StringSubstitutionEngineTest;
import com.sun.enterprise.admin.servermgmt.stringsubs.impl.SubstituableFactoryImpl;
import com.sun.enterprise.admin.servermgmt.test.ServerMgmgtTestFiles;
import com.sun.enterprise.admin.servermgmt.xml.stringsubs.Group;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Unit Test class for {@link StringSubstitutionFactory}.
*/
public class StringSubstitutionFactoryTest {

private static final ServerMgmgtTestFiles TEST_FILES = new ServerMgmgtTestFiles(StringSubstitutionEngineTest.class);
private static final String TEST_FILE_NAME = "testStringSubs.txt";
private static final String TEST_ARCHIVE_NAME = "testStringSubsArchive.jar";
private static final String VALID_GROUP_ID = "valid_group";
private static final Map<String, String> SUBSTITUTION_MAP = Map.of(
"JAVA", "REPLACED_JAVA",
"JAVA_HOME", "REPLACED_JAVA_HOME",
"MW_HOME", "REPLACED_MW_HOME",
"TEST_FILE_DIR_PATH", TEST_FILES.getBasePackageAbsolutePath().toString());


/**
* Test String substitution for invalid stream.
*/
@Test
public void testStringSubstitutorInvalidStream() throws Exception {
try (InputStream invalidStream = TEST_FILES.openInputStream(TEST_FILES.getBaseClass().getSimpleName() + ".class")) {
StringSubstitutionFactory.createStringSubstitutor(invalidStream);
fail("No exception thrown for invalid stream.");
} catch (StringSubstitutionException e) {
assertEquals("Failed to parse given stream against the schema xsd/schema/stringsubs.xsd.", e.getMessage());
}
}

/**
* Test String substitution for null stream.
*/
@Test
public void testStringSubstitutorNullStream() {
try {
StringSubstitutionFactory.createStringSubstitutor(null);
fail("No exception thrown for null stream.");
} catch (StringSubstitutionException e) {
assertEquals("InputStream is null", e.getMessage());
}
}

/**
* Test String substitution for valid stream.
* @throws StringSubstitutionException
* @throws IOException
*/
@Test
public void testStringSubstitutorValidStream() throws StringSubstitutionException, IOException {
final StringSubstitutor substitutor;
try (InputStream validStream = TEST_FILES.openInputStream("stringsubs.xml")) {
substitutor = StringSubstitutionFactory.createStringSubstitutor(validStream);
}
substitutor.setAttributePreprocessor(new AttributePreprocessorImpl(SUBSTITUTION_MAP));
backUpTestFile();
try {
substitutor.substituteAll();
for (Group group : substitutor.getStringSubsDefinition().getGroup()) {
if (group.getId().equals(VALID_GROUP_ID)) {
validateSubstitutedArchiveEntries(group);
}
}
} finally {
restoreTestFile();
}
}

/**
* Validate if the substitution occurred properly in the test file.
*/
private void validateTestFile(File testFile) throws IOException {
final List<String> lines = Files.readAllLines(testFile.toPath());
assertEquals(2, lines.size());
assertEquals("Substitute REPLACED_JAVA_HOME REPLACED_JAVA @MW_", lines.get(0));
assertEquals("HOME@", lines.get(1));
}

/**
* Validate all the substitutable archive entries.
*/
private void validateSubstitutedArchiveEntries(Group group) throws IOException {
List<? extends Substitutable> substituables = new SubstituableFactoryImpl().getArchiveEntrySubstitutable(group.getArchive().get(0));
for (Substitutable substituable : substituables) {
validateTestFile(new File(substituable.getName()));
substituable.finish();
}
}

/**
* Restore the archive by performing reverse substitution.
*/
private void backUpTestFile() {
try {
for (File file : TEST_FILES.getBasePackageDirectory().listFiles()) {
if (file.getName().endsWith(TEST_ARCHIVE_NAME) || file.getName().endsWith(TEST_FILE_NAME)) {
Files.copy(file.toPath(), new File(file.getAbsolutePath() + ".bkp").toPath());
}
}
} catch (Exception e) {
throw new IllegalStateException("Error occured while creating a backup archive before subsitution.", e);
}
}

private void restoreTestFile() {
try {
for (File file : TEST_FILES.getBasePackageDirectory().listFiles()) {
if (file.getName().endsWith(TEST_ARCHIVE_NAME) || file.getName().endsWith(TEST_FILE_NAME)) {
file.delete();
}
}
for (File file : TEST_FILES.getBasePackageDirectory().listFiles()) {
if (file.getAbsolutePath().endsWith(".bkp")) {
file.renameTo(new File(file.getAbsolutePath().replace(".bkp", "")));
}
}
} catch (Exception e) {
throw new IllegalStateException("Error occured while restoring the archive after subsitution.", e);
}
}
}
Loading

0 comments on commit 6bd40a8

Please sign in to comment.