Skip to content

Commit

Permalink
[lang][core] Add utility functions for the Event element.
Browse files Browse the repository at this point in the history
see #168

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Feb 13, 2015
1 parent adaf0d2 commit 5869f77
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
25 changes: 25 additions & 0 deletions plugins/io.sarl.lang.core/src/io/sarl/lang/core/Event.java
Expand Up @@ -21,6 +21,7 @@
package io.sarl.lang.core;

import java.io.Serializable;
import java.util.UUID;

/**
* Elementary interaction unit inside an {@link EventSpace} An event is the
Expand Down Expand Up @@ -114,4 +115,28 @@ public String toString() {
+ "]"; //$NON-NLS-1$
}

/** Replies if the event was emitted by an entity with the given address.
*
* @param address - the address of the emitter to test.
* @return <code>true</code> if the given event was emitted by
* an entity with the given address; otherwise <code>false</code>.
* @since 0.2
*/
public boolean isFrom(Address address) {
return (address != null) && address.equals(getSource());
}

/** Replies if the event was emitted by an entity with the given identifier.
*
* @param entityId - the identifier of the emitter to test.
* @return <code>true</code> if the given event was emitted by
* an entity with the given identifier; otherwise <code>false</code>.
* @since 0.2
*/
public boolean isFrom(UUID entityId) {
Address source = getSource();
return (entityId != null) && (source != null)
&& entityId.equals(source.getUUID());
}

}
Expand Up @@ -20,8 +20,11 @@
*/
package io.sarl.lang.core.tests.core;

import java.util.UUID;

import io.sarl.lang.core.Address;
import io.sarl.lang.core.Event;
import io.sarl.lang.core.SpaceID;
import io.sarl.tests.api.AbstractSarlTest;
import io.sarl.tests.api.Nullable;

Expand All @@ -38,6 +41,7 @@
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
@SuppressWarnings("all")
public class EventTest extends AbstractSarlTest {

@Nullable
Expand All @@ -50,15 +54,17 @@ private static Event mockEvent() {
};
}

/**
*/
@Before
public void setUp() {
this.event = mockEvent();
}

private static Address mockAddress(UUID contextId, UUID spaceId, UUID agentID) {
SpaceID sid = new SpaceID(contextId, spaceId, null);
Address adr = new Address(Mockito.spy(sid), agentID);
return Mockito.spy(adr);
}

/**
*/
@Test
public void getSource() {
assertNull(this.event.getSource());
Expand All @@ -67,8 +73,6 @@ public void getSource() {
assertSame(adr, this.event.getSource());
}

/**
*/
@Test
public void setSource() {
assertNull(this.event.getSource());
Expand All @@ -77,8 +81,6 @@ public void setSource() {
assertSame(adr, this.event.getSource());
}

/**
*/
@Test
public void equals() {
Event e;
Expand All @@ -101,8 +103,6 @@ public void equals() {
assertFalse(this.event.equals(e));
}

/**
*/
@Test
public void testHashCode() {
Event e;
Expand All @@ -120,5 +120,34 @@ public void testHashCode() {
e.setSource(adr2);
assertNotEquals(this.event.hashCode(), e.hashCode());
}

@Test
public void isFromAddress() {
Address adr = mockAddress(UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID());
this.event.setSource(adr);

Address adr0 = Mockito.mock(Address.class);
Address adr1 = Mockito.mock(Address.class);

assertFalse(this.event.isFrom((Address) null));
assertTrue(this.event.isFrom(adr));
assertFalse(this.event.isFrom(adr0));
assertFalse(this.event.isFrom(adr1));
}

@Test
public void isFromUUID() {
UUID id = UUID.randomUUID();
Address adr = mockAddress(UUID.randomUUID(), UUID.randomUUID(), id);
this.event.setSource(adr);

UUID id0 = UUID.randomUUID();
UUID id1 = UUID.randomUUID();

assertFalse(this.event.isFrom((UUID) null));
assertTrue(this.event.isFrom(id));
assertFalse(this.event.isFrom(id0));
assertFalse(this.event.isFrom(id1));
}

}

0 comments on commit 5869f77

Please sign in to comment.