Skip to content
Permalink
Browse files
Enforce the topic and event checks required by the specification
  • Loading branch information
timothyjward committed Aug 12, 2021
1 parent 95af677 commit b90f57a0e98a39889f8579f53e7168d5d0312896
Showing 2 changed files with 35 additions and 7 deletions.
@@ -34,6 +34,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -366,17 +367,22 @@ void stop() {

@Override
public void deliver(Object event) {
Objects.requireNonNull(event, "The event object must not be null");
String topicName = event.getClass().getName().replace('.', '/');
deliver(topicName, event);
}

@Override
public void deliver(String topic, Object event) {
checkTopicSyntax(topic);
Objects.requireNonNull(event, "The event object must not be null");
deliver(topic, EventConverter.forTypedEvent(event));
}

@Override
public void deliverUntyped(String topic, Map<String, ?> eventData) {
checkTopicSyntax(topic);
Objects.requireNonNull(eventData, "The event object must not be null");
deliver(topic, EventConverter.forUntypedEvent(eventData));
}

@@ -409,6 +415,28 @@ private void deliver(String topic, EventConverter convertibleEventData) {
queue.addAll(deliveryTasks);
}

private static void checkTopicSyntax(String topic) {

if(topic == null) {
throw new IllegalArgumentException("The topic name is not permitted to be null");
}

boolean slashPermitted = false;
for(int i = 0; i < topic.length(); i++) {
int c = topic.codePointAt(i);
if('/' == c) {
if(slashPermitted && i != (topic.length() - 1)) {
slashPermitted = false;
continue;
}
} else if ('-' == c || Character.isJavaIdentifierPart(c)) {
slashPermitted = true;
continue;
}
throw new IllegalArgumentException("Illegal character " + c + " at index " + i + " of topic string: " + topic);
}
}

private class EventThread extends Thread {

private final AtomicBoolean running = new AtomicBoolean(true);
@@ -261,46 +261,46 @@ public void testUntypedEventSending() throws InterruptedException {

Map<String, Object> serviceProperties = new HashMap<>();

serviceProperties.put(TYPED_EVENT_TOPICS, TestEvent.class.getName());
serviceProperties.put(TYPED_EVENT_TOPICS, TestEvent.class.getName().replace('.', '/'));
serviceProperties.put(TYPED_EVENT_TYPE, TestEvent.class.getName());
serviceProperties.put(SERVICE_ID, 42L);

impl.addTypedEventHandler(handlerA, serviceProperties);

serviceProperties = new HashMap<>();

serviceProperties.put(TYPED_EVENT_TOPICS, TestEvent2.class.getName());
serviceProperties.put(TYPED_EVENT_TOPICS, TestEvent2.class.getName().replace('.', '/'));
serviceProperties.put(TYPED_EVENT_TYPE, TestEvent2.class.getName());
serviceProperties.put(SERVICE_ID, 43L);

impl.addTypedEventHandler(handlerB, serviceProperties);

serviceProperties = new HashMap<>();

serviceProperties.put(TYPED_EVENT_TOPICS, TestEvent.class.getName());
serviceProperties.put(TYPED_EVENT_TOPICS, TestEvent.class.getName().replace('.', '/'));
serviceProperties.put(SERVICE_ID, 44L);

impl.addUntypedEventHandler(untypedHandlerA, serviceProperties);

serviceProperties = new HashMap<>();

serviceProperties.put(TYPED_EVENT_TOPICS, TestEvent2.class.getName());
serviceProperties.put(TYPED_EVENT_TOPICS, TestEvent2.class.getName().replace('.', '/'));
serviceProperties.put(SERVICE_ID, 45L);

impl.addUntypedEventHandler(untypedHandlerB, serviceProperties);

impl.deliver(event.getClass().getName(), standardConverter().convert(event).to(Map.class));
impl.deliver(event.getClass().getName().replace('.', '/'), standardConverter().convert(event).to(Map.class));

assertTrue(semA.tryAcquire(1, TimeUnit.SECONDS));

Mockito.verify(handlerA).notify(Mockito.eq(TestEvent.class.getName()),
Mockito.verify(handlerA).notify(Mockito.eq(TestEvent.class.getName().replace('.', '/')),
Mockito.argThat(isTestEventWithMessage("boo")));

assertFalse(semB.tryAcquire(1, TimeUnit.SECONDS));

assertTrue(untypedSemA.tryAcquire(1, TimeUnit.SECONDS));

Mockito.verify(untypedHandlerA).notifyUntyped(Mockito.eq(TestEvent.class.getName()),
Mockito.verify(untypedHandlerA).notifyUntyped(Mockito.eq(TestEvent.class.getName().replace('.', '/')),
Mockito.argThat(isUntypedTestEventWithMessage("boo")));

assertFalse(untypedSemB.tryAcquire(1, TimeUnit.SECONDS));

0 comments on commit b90f57a

Please sign in to comment.