Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QA-191 Tests for added shutdown grace period system property #3808

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions appserver/tests/pom.xml
Expand Up @@ -54,6 +54,11 @@
<packaging>pom</packaging>
<name>GlassFish Tests related modules</name>

<modules>
<module>test-micro-jar</module>
<!-- module>quicklook</module -->
</modules>

<profiles>
<profile>
<id>ci</id>
Expand Down
27 changes: 27 additions & 0 deletions appserver/tests/test-micro-jar/README.md
@@ -0,0 +1,27 @@
# Payara Micro Jar Based Integration Tests

This module provides plain JUnit tests that use `payara-micro.jar` as only
compile and runtime dependency (+ junit itself).

The setup makes sure the actual delivery is tested and keeps the setup simple
and minimal. Tests do the programmtic equivalent of:

java -jar payara-micro.jar

The `payara-micro.jar` is referenced using a relative path to the module that
generates the jar. Should the jar not exist run:

cd $PAYARA_HOME/appserver/extras/payara-micro/payara-micro-distribution
mvn install

Depending on the status of the build artifacts a full build might be required.

Except from being dependent on a generated jar file the tests in this module
are plain old JUnit tests that can be run using `mvn integration-test`
(or `mvn verify`) or in the IDE as every other JUnit tests.

While this takes full advantage of the simplicity Payara Micro has to offer for
the user it comes with the downside of not having further APIs and internals
available at compile time.
Instead reflection has to be used if internal should be verified.
This module offers the `BeanProxy` utility class to ease reflection usage.
122 changes: 122 additions & 0 deletions appserver/tests/test-micro-jar/pom.xml
@@ -0,0 +1,122 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

Copyright (c) 2019 Payara Foundation and/or its affiliates. All rights reserved.

The contents of this file are subject to the terms of either the GNU
General Public License Version 2 only ("GPL") or the Common Development
and Distribution License("CDDL") (collectively, the "License"). You
may not use this file except in compliance with the License. You can
obtain a copy of the License at
https://github.com/payara/Payara/blob/master/LICENSE.txt
See the License for the specific
language governing permissions and limitations under the License.

When distributing the software, include this License Header Notice in each
file and include the License file at glassfish/legal/LICENSE.txt.

GPL Classpath Exception:
The Payara Foundation designates this particular file as subject to the "Classpath"
exception as provided by the Payara Foundation in the GPL Version 2 section of the License
file that accompanied this code.

Modifications:
If applicable, add the following below the License Header, with the fields
enclosed by brackets [] replaced by your own identifying information:
"Portions Copyright [year] [name of copyright owner]"

Contributor(s):
If you wish your version of this file to be governed by only the CDDL or
only the GPL Version 2, indicate your decision by adding "[Contributor]
elects to include this software in this distribution under the [CDDL or GPL
Version 2] license." If you don't indicate a single choice of license, a
recipient has the option to distribute your version of this file under
either the CDDL, the GPL Version 2 or to extend the choice of license to
its licensees as provided above. However, if you add GPL Version 2 code
and therefore, elected the GPL Version 2 license, then the option applies
only if the new code is made subject to such option by the copyright
holder.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.glassfish.main.tests</groupId>
<artifactId>tests</artifactId>
<version>5.191-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>test-micro-jar</artifactId>

<name>Integration Tests based on payara-micro.jar file</name>
<description>NOTE: this module is dependent on the payara-micro-distribution being run previously
but it cannot have a dependency on this module as this would mess up the classpath for test execution
as some classes get in from the modules that have to be taken only from the generated jar file.

The idea is to test the actual artifact created.
This is not suitable for all kinds of tests as the accessible API is the minumal payara micro API.
</description>

<dependencies>
<!-- NOTE that the dependency is of type "pom". We don't want its jars in classpath -
only that the module is build before this one so that the payara-micro.jar exists -->
<dependency>
<groupId>fish.payara.extras</groupId>
<artifactId>payara-micro</artifactId>
<version>${project.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<!-- NOTE: This is a purely fictional module. Maven simply insists that anything is a module
so we pretend it is -->
<groupId>fish.payara.extras</groupId>
<artifactId>payara-micro-jar</artifactId>
<version>as-is-on-disk</version>
<scope>system</scope>
<systemPath>${project.basedir}/../../extras/payara-micro/payara-micro-distribution/target/payara-micro.jar</systemPath>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
<excludedGroups>fish.payara.test.util.IntegrationTest</excludedGroups>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<groups>fish.payara.test.util.IntegrationTest</groups>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,201 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2019 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.admin.cli;

import static fish.payara.micro.ClusterCommandResult.ExitStatus.FAILURE;
import static fish.payara.micro.ClusterCommandResult.ExitStatus.SUCCESS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.Before;
import org.junit.experimental.categories.Category;

import fish.payara.micro.ClusterCommandResult;
import fish.payara.micro.ClusterCommandResult.ExitStatus;
import fish.payara.test.util.IntegrationTest;
import fish.payara.test.util.PayaraMicroServer;

@Category(IntegrationTest.class)
public abstract class AsAdminIntegrationTest {

private static final String UNSATISFIED_DEPENDENCY_EXCEPTION_CLASS_NAME =
"org.jvnet.hk2.config.UnsatisfiedDependencyException";
private static final String UNACCEPTABLE_VALUE_EXCEPTION_CLASS_NAME =
"org.glassfish.common.util.admin.UnacceptableValueException";

protected final PayaraMicroServer server = PayaraMicroServer.DEFAULT;

@Before
public void serverSetUp() throws Exception {
server.start();
}

@After
public void serverTearDown() {
server.stop();
}

protected static void assertUnchanged(boolean expected, boolean actual) {
assertEquals(Boolean.valueOf(expected), Boolean.valueOf(actual));
}

protected static void assertContains(String expected, String actual) {
assertThat(actual, CoreMatchers.containsString(expected));
}

protected static void assertSuccess(ClusterCommandResult result) {
ExitStatus actual = result.getExitStatus();
if (SUCCESS != actual) {
String msg = result.getOutput();
if (actual == FAILURE && result.getFailureCause() != null) {
StringWriter writer = new StringWriter();
result.getFailureCause().printStackTrace(new PrintWriter(writer));
msg = writer.toString();
}
if (msg != null) {
assertEquals(msg, SUCCESS, actual);
} else {
assertEquals(SUCCESS, actual);
}
}
}

protected static void assertFailure(ClusterCommandResult result) {
assertEquals(FAILURE, result.getExitStatus());
}

private static void assertStatesUsage(ClusterCommandResult result) {
assertTrue("No usage was given.", result.getOutput().contains("Usage: "));
}

protected final void assertMissingParameter(String name, ClusterCommandResult result) {
assertFailure(result);
Class<?> expectedExceptionType = server.getClass(UNSATISFIED_DEPENDENCY_EXCEPTION_CLASS_NAME);
Throwable cause = result.getFailureCause();
assertNotNull(cause);
assertTrue("Error is not caused by a missing parameter but " + cause.getClass().getName(),
expectedExceptionType.isAssignableFrom(cause.getClass()));
String text = result.getOutput();
int afterName = text.indexOf(" with class ");
int beforeName = text.substring(0, afterName).lastIndexOf('.');
String actualName = text.substring(beforeName + 1, afterName);
assertEquals("Error was about another parameter", name, actualName);
assertStatesUsage(result);
}

protected final void assertUnacceptableParameter(String name, ClusterCommandResult result) {
assertFailure(result);
Class<?> expectedExceptionType = server.getClass(UNACCEPTABLE_VALUE_EXCEPTION_CLASS_NAME);
Throwable cause = result.getFailureCause();
assertNotNull(cause);
assertTrue("Error is not caused by a unacceptable parameter but " + cause.getClass().getName(),
expectedExceptionType.isAssignableFrom(cause.getClass()));
String text = result.getOutput();
try {
assertContains("Invalid parameter: " + name, text);
} catch (AssertionError e) {
assertContains("on parameter [ "+name+" ]", text);
}
assertStatesUsage(result);
}

/**
* Runs an as-admin command on the {@link #server}
*/
protected final ClusterCommandResult asadmin(String command, String...args) {
return new PlainClusterCommandResult(server.getInstance().executeLocalAsAdmin(command, args));
}

protected final Class<?> getClass(String name) {
return server.getClass(name);
}

protected final <T> T getExtensionByType(String target, Class<T> type) {
return server.getExtensionByType(target, type);
}

private static final class PlainClusterCommandResult implements ClusterCommandResult {

final ClusterCommandResult wrapped;

public PlainClusterCommandResult(ClusterCommandResult wrapped) {
this.wrapped = wrapped;
}

@Override
public ExitStatus getExitStatus() {
return wrapped.getExitStatus();
}

@Override
public Throwable getFailureCause() {
return wrapped.getFailureCause();
}

@Override
public String getOutput() {
String output = wrapped.getOutput();
if (!output.startsWith("PlainTextActionReporter")) {
return output;
}
int index = output.indexOf("SUCCESS");
if (index < 0) {
index = output.indexOf("FAILURE");
}
if (index > 0) {
return output.substring(index + 7);
}
return output;
}

@Override
public String toString() {
return getExitStatus() + ": " + getOutput();
}
}

}