Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
Deprecate getOrCreateSpace.
Browse files Browse the repository at this point in the history
see #92

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Nov 25, 2014
1 parent f69886e commit 2990867
Show file tree
Hide file tree
Showing 4 changed files with 328 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,33 @@ public <S extends io.sarl.lang.core.Space> S createSpace(Class<? extends SpaceSp
return this.spaceRepository.createSpace(new SpaceID(this.id, spaceUUID, spec), spec, creationParams);
}


/** {@inheritDoc}
* @deprecated see {@link #getOrCreateSpaceWithSpec(Class, UUID, Object...)}.
*/
@Override
public <S extends io.sarl.lang.core.Space> S getOrCreateSpace(
@Deprecated
public <S extends Space> S getOrCreateSpace(
Class<? extends SpaceSpecification<S>> spec, UUID spaceUUID,
Object... creationParams) {
return this.spaceRepository.getOrCreateSpace(spec, new SpaceID(this.id, spaceUUID, spec), creationParams);
return getOrCreateSpaceWithSpec(spec, spaceUUID, creationParams);
}

/** {@inheritDoc}
*/
@Override
public <S extends Space> S getOrCreateSpaceWithSpec(
Class<? extends SpaceSpecification<S>> spec, UUID spaceUUID,
Object... creationParams) {
return this.spaceRepository.getOrCreateSpaceWithSpec(new SpaceID(this.id, spaceUUID, spec), spec, creationParams);
}

/** {@inheritDoc}
*/
@Override
public <S extends Space> S getOrCreateSpaceWithID(UUID spaceUUID,
Class<? extends SpaceSpecification<S>> spec,
Object... creationParams) {
return this.spaceRepository.getOrCreateSpaceWithID(new SpaceID(this.id, spaceUUID, spec), spec, creationParams);
}

/** {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private synchronized <S extends Space> S createSpaceInstance(
assert (spaceID.getSpaceSpecification() == null || spaceID.getSpaceSpecification().equals(spec))
: "The specification type is invalid"; //$NON-NLS-1$
// Split the call to create() to let the JVM to create the "empty" array for creation parameters.
if (creationParams != null) {
if (creationParams != null && creationParams.length > 0) {
space = this.injector.getInstance(spec).create(spaceID, creationParams);
} else {
space = this.injector.getInstance(spec).create(spaceID);
Expand All @@ -160,7 +160,7 @@ private synchronized <S extends Space> S createSpaceInstance(
this.spacesBySpec.put(id.getSpaceSpecification(), id);
if (isLocalCreation) {
Object[] sharedParams = NO_PARAMETERS;
if (creationParams != null) {
if (creationParams != null && creationParams.length > 0) {
List<Object> serializableParameters = new ArrayList<>(creationParams.length);
for (Object creationParameter : creationParams) {
if (creationParameter instanceof Serializable) {
Expand Down Expand Up @@ -234,33 +234,31 @@ protected synchronized void removeLocalSpaceDefinitions(boolean isLocalDestructi
* @param spaceID - ID of the space.
* @param spec - specification of the space.
* @param creationParams - creation parameters.
* @return the new space.
* @return the new space, or <code>null</code> if the space already exists.
*/
@SuppressWarnings("unchecked")
public synchronized <S extends io.sarl.lang.core.Space> S createSpace(
SpaceID spaceID,
Class<? extends SpaceSpecification<S>> spec,
Object... creationParams) {
S space = (S) this.spaces.get(spaceID);
if (space == null) {
space = createSpaceInstance(spec, spaceID, true, creationParams);
Object... creationParams) {
if (!this.spaces.containsKey(spaceID)) {
return createSpaceInstance(spec, spaceID, true, creationParams);
}
return space;
return null;
}

/** Retrive the first space of the given specification, or create a space if none.
/** Retrieve the first space of the given specification, or create a space if none.
*
* @param <S> - the type of the space to reply.
* @param spec - specification of the space.
* @param spaceID - ID of the space (used only when creating a space).
* @param spec - specification of the space.
* @param creationParams - creation parameters (used only when creating a space).
* @return the new space.
*/
@SuppressWarnings("unchecked")
public synchronized <S extends io.sarl.lang.core.Space> S getOrCreateSpace(
public synchronized <S extends io.sarl.lang.core.Space> S getOrCreateSpaceWithSpec(
SpaceID spaceID,
Class<? extends SpaceSpecification<S>> spec,
SpaceID spaceID,
Object... creationParams) {
Object... creationParams) {
Collection<SpaceID> ispaces = this.spacesBySpec.get(spec);
S firstSpace;
if (ispaces == null || ispaces.isEmpty()) {
Expand All @@ -272,6 +270,27 @@ public synchronized <S extends io.sarl.lang.core.Space> S getOrCreateSpace(
return firstSpace;
}

/** Retrieve the first space of the given identifier, or create a space if none.
*
* @param <S> - the type of the space to reply.
* @param spaceID - ID of the space.
* @param spec - specification of the space.
* @param creationParams - creation parameters (used only when creating a space).
* @return the new space.
*/
@SuppressWarnings("unchecked")
public synchronized <S extends io.sarl.lang.core.Space> S getOrCreateSpaceWithID(
SpaceID spaceID,
Class<? extends SpaceSpecification<S>> spec,
Object... creationParams) {
Space space = this.spaces.get(spaceID);
if (space == null) {
space = createSpaceInstance(spec, spaceID, true, creationParams);
}
assert (space != null);
return (S) space;
}

/**
* Returns the collection of all spaces stored in this repository.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ public class ContextTest extends Assert {
private UUID contextId;
private UUID spaceId;
private Map<UUID,OpenEventSpace> spaces;

private SpaceRepository spaceRepository;

private Context context;

private SpaceRepositoryListener spaceListener;
private SpaceRepositoryListener privateListener;

@Before
public void setUp() {
this.contextId = UUID.randomUUID();
Expand All @@ -86,36 +86,54 @@ public void setUp() {
this.spaceListener = Mockito.mock(SpaceRepositoryListener.class);
this.spaceRepository = Mockito.mock(SpaceRepository.class);
Mockito.when(this.spaceRepository.createSpace(Matchers.any(SpaceID.class), Matchers.any(Class.class)))
.thenAnswer(new Answer<Space>() {
@Override
public Space answer(InvocationOnMock invocation)
throws Throwable {
OpenEventSpace space = Mockito.mock(OpenEventSpace.class);
Mockito.when(space.getID()).thenReturn((SpaceID)invocation.getArguments()[0]);
ContextTest.this.spaces.put(((SpaceID)invocation.getArguments()[0]).getID(), space);
assert(ContextTest.this.privateListener!=null);
ContextTest.this.privateListener.spaceCreated(space, true);
return space;
.thenAnswer(new Answer<Space>() {
@Override
public Space answer(InvocationOnMock invocation)
throws Throwable {
OpenEventSpace space = Mockito.mock(OpenEventSpace.class);
Mockito.when(space.getID()).thenReturn((SpaceID)invocation.getArguments()[0]);
ContextTest.this.spaces.put(((SpaceID)invocation.getArguments()[0]).getID(), space);
assert(ContextTest.this.privateListener!=null);
ContextTest.this.privateListener.spaceCreated(space, true);
return space;
}
});
Mockito.when(this.spaceRepository.getOrCreateSpaceWithSpec(Matchers.any(SpaceID.class), Matchers.any(Class.class)))
.thenAnswer(new Answer<Space>() {
@Override
public Space answer(InvocationOnMock invocation)
throws Throwable {
for(Space s : ContextTest.this.spaces.values()) {
if (s.getID().equals(invocation.getArguments()[0])) {
return s;
}
}
});
Mockito.when(this.spaceRepository.getOrCreateSpace(Matchers.any(Class.class), Matchers.any(SpaceID.class)))
.thenAnswer(new Answer<Space>() {
@Override
public Space answer(InvocationOnMock invocation)
throws Throwable {
for(Space s : ContextTest.this.spaces.values()) {
if (s.getID().equals(invocation.getArguments()[1])) {
return s;
}
OpenEventSpace space = Mockito.mock(OpenEventSpace.class);
Mockito.when(space.getID()).thenReturn((SpaceID)invocation.getArguments()[0]);
ContextTest.this.spaces.put(((SpaceID)invocation.getArguments()[0]).getID(), space);
assert(ContextTest.this.privateListener!=null);
ContextTest.this.privateListener.spaceCreated(space, true);
return space;
}
});
Mockito.when(this.spaceRepository.getOrCreateSpaceWithID(Matchers.any(SpaceID.class), Matchers.any(Class.class)))
.thenAnswer(new Answer<Space>() {
@Override
public Space answer(InvocationOnMock invocation)
throws Throwable {
for(Space s : ContextTest.this.spaces.values()) {
if (s.getID().equals(invocation.getArguments()[0])) {
return s;
}
OpenEventSpace space = Mockito.mock(OpenEventSpace.class);
Mockito.when(space.getID()).thenReturn((SpaceID)invocation.getArguments()[1]);
ContextTest.this.spaces.put(((SpaceID)invocation.getArguments()[1]).getID(), space);
assert(ContextTest.this.privateListener!=null);
ContextTest.this.privateListener.spaceCreated(space, true);
return space;
}
});
OpenEventSpace space = Mockito.mock(OpenEventSpace.class);
Mockito.when(space.getID()).thenReturn((SpaceID)invocation.getArguments()[0]);
ContextTest.this.spaces.put(((SpaceID)invocation.getArguments()[0]).getID(), space);
assert(ContextTest.this.privateListener!=null);
ContextTest.this.privateListener.spaceCreated(space, true);
return space;
}
});
Mockito.when(this.spaceRepository.getSpaces(Matchers.any(Class.class)))
.thenAnswer(new Answer<SynchronizedCollection<? extends Space>>() {
@Override
Expand Down Expand Up @@ -164,7 +182,7 @@ protected SpaceRepository newInstanceWithPrivateSpaceListener(
this.contextId, this.spaceId, spaceRepoFactory, this.spaceListener);
this.context.postConstruction();
}

@After
public void tearDown() {
this.contextId = null;
Expand All @@ -189,7 +207,7 @@ public void twoStepConstruction() throws Exception {
}
}
}

@Test
public void postConstruction() {
ArgumentCaptor<SpaceID> argument1 = ArgumentCaptor.forClass(SpaceID.class);
Expand Down Expand Up @@ -248,10 +266,10 @@ public void createSpace() {
}

@Test
public void getOrCreateSpace() {
public void getOrCreateSpaceWithSpec() {
Collection<? extends Space> c;
UUID id = UUID.randomUUID();
OpenEventSpace space = this.context.getOrCreateSpace(OpenEventSpaceSpecification.class, id);
OpenEventSpace space = this.context.getOrCreateSpaceWithSpec(OpenEventSpaceSpecification.class, id);
//
assertNotNull(space);
assertEquals(id, space.getID().getID());
Expand Down Expand Up @@ -281,9 +299,47 @@ public void getOrCreateSpace() {
assertThat(argument3.getValue(), new IsInstanceOf(SpaceCreated.class));
assertEquals(id, ((SpaceCreated)argument3.getValue()).spaceID.getID());
//
OpenEventSpace space2 = this.context.getOrCreateSpace(OpenEventSpaceSpecification.class, id);
OpenEventSpace space2 = this.context.getOrCreateSpaceWithSpec(OpenEventSpaceSpecification.class, id);
assertSame(space, space2);
}

@Test
public void getOrCreateSpaceWithID() {
Collection<? extends Space> c;
UUID id = UUID.randomUUID();
OpenEventSpace space = this.context.getOrCreateSpaceWithID(id, OpenEventSpaceSpecification.class);
//
assertNotNull(space);
assertEquals(id, space.getID().getID());
c = this.context.getSpaces();
assertNotNull(c);
assertEquals(2, c.size());
Collection<UUID> ids = new ArrayList<>();
ids.add(this.spaceId);
ids.add(id);
for(Space sp: c) {
ids.remove(sp.getID().getID());
}
assertTrue(ids.isEmpty());
//
assertNotNull(this.privateListener);
ArgumentCaptor<Space> argument1 = ArgumentCaptor.forClass(Space.class);
ArgumentCaptor<Boolean> argument2 = ArgumentCaptor.forClass(Boolean.class);
// CAUTION: invoked two times due to the default space and the created space.
Mockito.verify(this.spaceListener, new Times(2)).spaceCreated(argument1.capture(), argument2.capture());
assertSame(space, argument1.getValue());
assertTrue(argument2.getValue());
//
OpenEventSpace defSpace = this.spaces.get(this.spaceId);
assertNotNull(defSpace);
ArgumentCaptor<Event> argument3 = ArgumentCaptor.forClass(Event.class);
Mockito.verify(defSpace, new Times(1)).emit(argument3.capture());
assertThat(argument3.getValue(), new IsInstanceOf(SpaceCreated.class));
assertEquals(id, ((SpaceCreated)argument3.getValue()).spaceID.getID());
//
OpenEventSpace space2 = this.context.getOrCreateSpaceWithID(id, OpenEventSpaceSpecification.class);
assertSame(space, space2);
}

@Test
public void getSpaces() {
Expand Down Expand Up @@ -342,5 +398,5 @@ public void destroy() {
this.context.destroy();
Mockito.verify(this.spaceRepository, new Times(1)).destroy();
}

}
Loading

0 comments on commit 2990867

Please sign in to comment.