-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Refactor out inner classes - Add logging to events
- Loading branch information
Showing
6 changed files
with
221 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...ce-spring-core/src/test/java/com/oracle/coherence/spring/event/mapevent/TestListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Universal Permissive License v 1.0 as shown at | ||
* https://oss.oracle.com/licenses/upl. | ||
*/ | ||
package com.oracle.coherence.spring.event.mapevent; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.oracle.coherence.common.collections.ConcurrentHashMap; | ||
import com.oracle.coherence.spring.annotation.PropertyExtractor; | ||
import com.oracle.coherence.spring.annotation.WhereFilter; | ||
import com.oracle.coherence.spring.annotation.event.CacheName; | ||
import com.oracle.coherence.spring.annotation.event.Deleted; | ||
import com.oracle.coherence.spring.annotation.event.Inserted; | ||
import com.oracle.coherence.spring.annotation.event.MapName; | ||
import com.oracle.coherence.spring.annotation.event.ScopeName; | ||
import com.oracle.coherence.spring.annotation.event.ServiceName; | ||
import com.oracle.coherence.spring.annotation.event.Synchronous; | ||
import com.oracle.coherence.spring.annotation.event.Updated; | ||
import com.oracle.coherence.spring.event.CoherenceEventListener; | ||
import com.tangosol.util.MapEvent; | ||
import data.Person; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
/** | ||
* @author Gunnar Hillert | ||
*/ | ||
public class TestListener { | ||
|
||
private static final Log logger = LogFactory.getLog(TestListener.class); | ||
|
||
private final Map<Integer, Integer> events = new ConcurrentHashMap<>(); | ||
|
||
private final List<MapEvent<String, Person>> filteredEvents = Collections.synchronizedList(new ArrayList<>()); | ||
|
||
private final List<MapEvent<String, String>> transformedEvents = Collections.synchronizedList(new ArrayList<>()); | ||
|
||
private final List<MapEvent<String, String>> transformedWithExtractorEvents = Collections.synchronizedList(new ArrayList<>()); | ||
|
||
Integer getEvents(int id) { | ||
return this.events.get(id); | ||
} | ||
|
||
public List<MapEvent<String, Person>> getFilteredEvents() { | ||
return this.filteredEvents; | ||
} | ||
|
||
public List<MapEvent<String, String>> getTransformedEvents() { | ||
return this.transformedEvents; | ||
} | ||
|
||
public List<MapEvent<String, String>> getTransformedWithExtractorEvents() { | ||
return this.transformedWithExtractorEvents; | ||
} | ||
|
||
@Synchronous | ||
@WhereFilter("firstName = 'Bart' and lastName = 'Simpson'") | ||
@CoherenceEventListener | ||
void onHomer(@CacheName("people") MapEvent<String, Person> event) { | ||
this.filteredEvents.add(event); | ||
} | ||
|
||
@Synchronous | ||
@CoherenceEventListener | ||
void onPersonDeleted(@Deleted @CacheName("people") MapEvent<String, Person> event) { | ||
logger.info("onPersonDeleted: " + event.getKey()); | ||
record(event); | ||
} | ||
|
||
@Synchronous | ||
@CoherenceEventListener | ||
void onPersonInserted(@Inserted @ScopeName("Test") @MapName("people") MapEvent<String, Person> event) { | ||
logger.info("onPersonInserted: " + event.getKey()); | ||
record(event); | ||
assertThat(event.getNewValue().getLastName(), is("Simpson")); | ||
} | ||
|
||
@Synchronous | ||
@PropertyExtractor("firstName") | ||
@CoherenceEventListener | ||
void onPersonInsertedTransformedWithExtractor(@Inserted @MapName("people") MapEvent<String, String> event) { | ||
this.transformedWithExtractorEvents.add(event); | ||
} | ||
|
||
@Synchronous | ||
@UppercaseName | ||
@CoherenceEventListener | ||
void onPersonInsertedTransformed(@Inserted @MapName("people") MapEvent<String, String> event) { | ||
this.transformedEvents.add(event); | ||
} | ||
|
||
@Synchronous | ||
@CoherenceEventListener | ||
void onPersonUpdated(@Updated @ServiceName("StorageService") @MapName("people") MapEvent<String, Person> event) { | ||
logger.info("onPersonUpdated: " + event.getKey()); | ||
record(event); | ||
assertThat(event.getOldValue().getLastName(), is("Simpson")); | ||
assertThat(event.getNewValue().getLastName(), is("SIMPSON")); | ||
} | ||
|
||
private void record(MapEvent<String, Person> event) { | ||
this.events.compute(event.getId(), (k, v) -> (v != null) ? v + 1 : 1); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ore/src/test/java/com/oracle/coherence/spring/event/mapevent/UppercaseEntryProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Universal Permissive License v 1.0 as shown at | ||
* https://oss.oracle.com/licenses/upl. | ||
*/ | ||
package com.oracle.coherence.spring.event.mapevent; | ||
|
||
import com.tangosol.util.InvocableMap; | ||
import data.Person; | ||
|
||
/** | ||
* @author Gunnar Hillert | ||
*/ | ||
public class UppercaseEntryProcessor | ||
implements InvocableMap.EntryProcessor<String, Person, Object> { | ||
@Override | ||
public Object process(InvocableMap.Entry<String, Person> entry) { | ||
Person p = entry.getValue(); | ||
p.setLastName(p.getLastName().toUpperCase()); | ||
entry.setValue(p); | ||
return null; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...e-spring-core/src/test/java/com/oracle/coherence/spring/event/mapevent/UppercaseName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Universal Permissive License v 1.0 as shown at | ||
* https://oss.oracle.com/licenses/upl. | ||
*/ | ||
package com.oracle.coherence.spring.event.mapevent; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
import com.oracle.coherence.spring.annotation.MapEventTransformerBinding; | ||
|
||
/** | ||
* @author Gunnar Hillert | ||
*/ | ||
@Inherited | ||
@MapEventTransformerBinding | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface UppercaseName { | ||
} |
34 changes: 34 additions & 0 deletions
34
...re/src/test/java/com/oracle/coherence/spring/event/mapevent/UppercaseNameTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Universal Permissive License v 1.0 as shown at | ||
* https://oss.oracle.com/licenses/upl. | ||
*/ | ||
package com.oracle.coherence.spring.event.mapevent; | ||
|
||
import com.tangosol.util.MapEvent; | ||
import com.tangosol.util.MapEventTransformer; | ||
import data.Person; | ||
|
||
/** | ||
* A custom implementation of a {@link MapEventTransformer}. | ||
* @author Gunnar Hillert | ||
*/ | ||
class UppercaseNameTransformer implements MapEventTransformer<String, Person, String> { | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public MapEvent<String, String> transform(MapEvent<String, Person> event) { | ||
String sOldName = transform(event.getOldValue()); | ||
String sNewName = transform(event.getNewValue()); | ||
return new MapEvent<String, String>(event.getMap(), event.getId(), event.getKey(), sOldName, sNewName); | ||
} | ||
|
||
String transform(Person person) { | ||
if (person == null) { | ||
return null; | ||
} | ||
String name = person.getFirstName(); | ||
return (name != null) ? name.toUpperCase() : null; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...src/test/java/com/oracle/coherence/spring/event/mapevent/UppercaseTransformerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Universal Permissive License v 1.0 as shown at | ||
* https://oss.oracle.com/licenses/upl. | ||
*/ | ||
package com.oracle.coherence.spring.event.mapevent; | ||
|
||
import com.oracle.coherence.spring.annotation.MapEventTransformerFactory; | ||
import com.tangosol.util.MapEventTransformer; | ||
import data.Person; | ||
|
||
/** | ||
* @author Gunnar Hillert | ||
*/ | ||
@UppercaseName | ||
public class UppercaseTransformerFactory implements MapEventTransformerFactory<UppercaseName, String, Person, String> { | ||
@Override | ||
public MapEventTransformer<String, Person, String> create(UppercaseName annotation) { | ||
return new UppercaseNameTransformer(); | ||
} | ||
} |