Skip to content

Commit

Permalink
eclipse-ee4j#24158 Enhance existing test to test setting a custom pro…
Browse files Browse the repository at this point in the history
…perty

Signed-off-by:Ondro Mihalyi <mihalyi@omnifish.ee>
  • Loading branch information
OndroMih committed Nov 19, 2022
1 parent 8197344 commit 2c99eb6
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ private String getResourceName() {
return dottedNameForResourceName.toString().replace('.', '/');
}

// Methods and variables used within a call to the execute method
private class ExecutionContext {
String pattern = SetOperation.this.pattern;
String attrName = SetOperation.this.attrName;
Expand Down Expand Up @@ -354,21 +355,21 @@ public boolean execute(AdminCommandContext context) {
try {
// it's possible they are trying to create a property object.. lets check this.
// strip out the property name
String parentPattern = getParentFromDottedPattern(this.target);
String parentPattern = getParentFromDottedPattern(target);
if (parentPattern.endsWith("property")) {
ctx.pattern = parentPattern;
return ctx.setAsPropertyObject(dottedNames, context, targetName);
}

// attempt to create missing nodes and search for a matching node again
ctx.createMissingNodes(dottedNames, this.pattern);
ctx.createMissingNodes(dottedNames, ctx.pattern);
} catch (TransactionFailure ex) {
fail(context, localStrings.getLocalString("admin.set.badelement", "Cannot change the element: {0}",
ex.getMessage()), ex);
return false;
}
dottedNames = ctx.findDottedNames(parentNodes, this.pattern);
matchingNodes = getMatchingNodes(dottedNames, this.pattern);
dottedNames = ctx.findDottedNames(parentNodes, ctx.pattern);
matchingNodes = getMatchingNodes(dottedNames, ctx.pattern);
}

Map<ConfigBean, Map<String, String>> changes = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package com.sun.enterprise.v3.admin;

import com.sun.enterprise.v3.common.PlainTextActionReporter;
Expand All @@ -39,9 +38,13 @@
import org.jvnet.hk2.config.Transactions;
import org.jvnet.hk2.config.UnprocessedChangeEvents;
import jakarta.inject.Inject;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayWithSize;
import org.hamcrest.Matchers;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -51,10 +54,11 @@

/**
* test the set command
*
* @author Jerome Dochez
*/
@ExtendWith(KernelJUnitExtension.class)
public class ConfigAttributeSetTest implements ConfigListener {
public class ConfigAttributeSetTest {

@Inject
private ServiceLocator locator;
Expand All @@ -70,15 +74,23 @@ public void addMissingServices() {

@ParameterizedTest(name = "{index}: Test setting {0}")
@CsvSource({
"a direct property,"
"a direct attribute,"
+ "PORT,"
+ "configs.config.server-config.network-config.network-listeners.network-listener.http-listener-1.port,"
+ "8090",
"an aliased property,"
+ "8090"
,
"an aliased attribute,"
+ "PORT,"
+ "server.network-config.network-listeners.network-listener.http-listener-1.port,"
+ "8090"

,
"a property,"
+ "PROPERTY,"
+ "server.network-config.network-listeners.network-listener.http-listener-1.property.a,"
+ "b"

})
public void setListenerPortNumber(String testDescription, String propertyName, String propertyValue) {
public void setListenerAttribute(String testDescription, ListenerAttributeType attributeType, String propertyName, String propertyValue) {
CommandRunnerImpl runner = locator.getService(CommandRunnerImpl.class);
assertNotNull(runner);

Expand All @@ -92,45 +104,93 @@ public void setListenerPortNumber(String testDescription, String propertyName, S
}
}
assertNotNull(listener);
String oldPortValue = listener.getPort();

String oldAttributeValue = attributeType.getAttributeValue(listener);

// Let's register a listener
ObservableBean bean = (ObservableBean) ConfigSupport.getImpl(listener);
bean.addListener(this);

// parameters to the command
ParameterMap parameters = new ParameterMap();
parameters.set("DEFAULT", propertyName + "=" + propertyValue);
// execute the set command.
PlainTextActionReporter reporter = new PlainTextActionReporter();
CommandInvocation invocation = runner.getCommandInvocation("set", reporter, adminSubject).parameters(parameters);
invocation.execute();

assertEquals(ExitCode.SUCCESS, reporter.getActionExitCode());
assertEquals("", reporter.getMessage());

// ensure events are delivered.
locator.<Transactions>getService(Transactions.class).waitForDrain();

// check the result.
String port = listener.getPort();
assertEquals(propertyValue, port);

// check we recevied the event
assertNotNull(event);
assertAll(
() -> assertEquals(oldPortValue, event.getOldValue()),
() -> assertEquals(propertyValue, event.getNewValue()),
() -> assertEquals("port", event.getPropertyName())
);
final PropertyChangeListener beanListener = new PropertyChangeListener(attributeType.getEventPropertyName());
bean.addListener(beanListener);

try {
// parameters to the command
ParameterMap parameters = new ParameterMap();
parameters.set("DEFAULT", propertyName + "=" + propertyValue);
// execute the set command.
PlainTextActionReporter reporter = new PlainTextActionReporter();
CommandInvocation invocation = runner.getCommandInvocation("set", reporter, adminSubject).parameters(parameters);
invocation.execute();

assertEquals(ExitCode.SUCCESS, reporter.getActionExitCode());
assertEquals("", reporter.getMessage());

// ensure events are delivered.
locator.<Transactions>getService(Transactions.class).waitForDrain();

// check the result.
String newAttributeValue = attributeType.getAttributeValue(listener);
assertEquals(propertyValue, newAttributeValue);

// check we recevied the event
assertNotNull(event);
assertAll(
() -> assertEquals(oldAttributeValue, event.getOldValue()),
() -> assertEquals(propertyValue, event.getNewValue()),
() -> assertEquals(attributeType.getEventPropertyName(), event.getPropertyName())
);
} finally {
bean.removeListener(beanListener);
}

}

private enum ListenerAttributeType {
PORT {
@Override
String getAttributeValue(NetworkListener listener) {
return listener.getPort();
}

@Override
String getEventPropertyName() {
return "port";
}

},
PROPERTY {
@Override
String getAttributeValue(NetworkListener listener) {
return listener.getPropertyValue("a");
}

@Override
String getEventPropertyName() {
return "value";
}

};

abstract String getAttributeValue(NetworkListener listener);

abstract String getEventPropertyName();
}

@Override
public UnprocessedChangeEvents changed(PropertyChangeEvent[] propertyChangeEvents) {
assertThat(propertyChangeEvents, arrayWithSize(1));
event = propertyChangeEvents[0];
return null;
private class PropertyChangeListener implements ConfigListener {

private String propertyName;

public PropertyChangeListener(String propertyName) {
this.propertyName = propertyName;
}

@Override
public UnprocessedChangeEvents changed(PropertyChangeEvent[] propertyChangeEvents) {
List<PropertyChangeEvent> events = Arrays.stream(propertyChangeEvents)
.filter(event -> propertyName.equals(event.getPropertyName()))
.collect(Collectors.toList());
assertThat(events, hasSize(1));
event = events.get(0);
return null;
}
}
}

0 comments on commit 2c99eb6

Please sign in to comment.