Skip to content

Commit

Permalink
Add event filtering test
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Pinčuk <alexander.v.pinchuk@gmail.com>
  • Loading branch information
avpinchuk committed Feb 13, 2024
1 parent 8b009ae commit 2cac164
Showing 1 changed file with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
*
* 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 org.glassfish.tests.kernel.deployment;

import jakarta.inject.Inject;

import java.util.ArrayList;
import java.util.List;

import org.glassfish.api.event.EventListener;
import org.glassfish.api.event.EventListener.Event;
import org.glassfish.api.event.EventTypes;
import org.glassfish.api.event.Events;
import org.glassfish.api.event.RestrictTo;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.main.core.kernel.test.KernelJUnitExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.glassfish.api.event.EventTypes.PREPARE_SHUTDOWN;
import static org.glassfish.api.event.EventTypes.PREPARE_SHUTDOWN_NAME;
import static org.glassfish.api.event.EventTypes.SERVER_READY;
import static org.glassfish.api.event.EventTypes.SERVER_SHUTDOWN;
import static org.glassfish.api.event.EventTypes.SERVER_STARTUP;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertSame;

@ExtendWith(KernelJUnitExtension.class)
public class EventFilteringTest {

@Inject
private ServiceLocator serviceLocator;

@Test
public void restrictedEventListenerTest() {
Events events = serviceLocator.getService(Events.class);

List<EventTypes<?>> eventTypes = new ArrayList<>();

// Do not replace with lambda because annotation will be lost
EventListener listener = new EventListener() {
@Override
public void event(@RestrictTo(PREPARE_SHUTDOWN_NAME) Event<?> event) {
eventTypes.add(event.type());
}
};

events.register(listener);

events.send(new Event<>(SERVER_STARTUP), false);
events.send(new Event<>(SERVER_READY), false);
events.send(new Event<>(PREPARE_SHUTDOWN), false);
events.send(new Event<>(SERVER_SHUTDOWN), false);

assertAll(
() -> assertThat(eventTypes, hasSize(1)),
() -> assertSame(eventTypes.get(0), PREPARE_SHUTDOWN)
);

events.unregister(listener);
}

@Test
public void unrestrictedEventListenerTest() {
Events events = serviceLocator.getService(Events.class);

List<EventTypes<?>> eventTypes = new ArrayList<>();

EventListener listener = event -> eventTypes.add(event.type());

events.register(listener);

events.send(new Event<>(SERVER_STARTUP), false);
events.send(new Event<>(SERVER_READY), false);
events.send(new Event<>(PREPARE_SHUTDOWN), false);
events.send(new Event<>(SERVER_SHUTDOWN), false);

assertAll(
() -> assertThat(eventTypes, hasSize(4)),
() -> assertThat(eventTypes,
contains(SERVER_STARTUP, SERVER_READY, PREPARE_SHUTDOWN, SERVER_SHUTDOWN))
);

events.unregister(listener);
}
}

0 comments on commit 2cac164

Please sign in to comment.