001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestBlockingRepository.java 3643 2007-01-12 15:29:45Z gbevin $
007: */
008: package com.uwyn.rife.rep;
009:
010: import com.uwyn.rife.config.Config;
011: import com.uwyn.rife.rep.exceptions.BlockingParticipantExpectedException;
012: import com.uwyn.rife.resources.ResourceFinderClasspath;
013: import java.util.Map;
014: import java.util.Set;
015: import java.util.Stack;
016: import junit.framework.TestCase;
017:
018: public class TestBlockingRepository extends TestCase {
019: public TestBlockingRepository(String name) {
020: super (name);
021: }
022:
023: public void testProperties() throws Exception {
024: Repository rep = new BlockingRepository();
025: assertEquals(rep.getProperties().size(), System.getProperties()
026: .size());
027: for (Map.Entry<Object, Object> entry : (Set<Map.Entry<Object, Object>>) System
028: .getProperties().entrySet()) {
029: assertTrue(rep.getProperties().contains(
030: (String) entry.getKey()));
031: assertEquals(rep.getProperties().get(
032: (String) entry.getKey()).getValueString(), entry
033: .getValue());
034: }
035: }
036:
037: public void testPropertiesSeperatedCollection() throws Exception {
038: Repository rep = new BlockingRepository();
039: rep.getProperties().put("os.name", "plif");
040: assertFalse(System.getProperty("os.name").equals(
041: rep.getProperties().get("os.name").getValueString()));
042: }
043:
044: public static class TestParticipant extends SingleObjectParticipant {
045: public Object getObject() {
046: Config config = new Config();
047: config.setParameter("param1", "value1");
048: return config;
049: }
050: }
051:
052: public void testAddRegularParticipant() {
053: BlockingRepository rep = new BlockingRepository();
054: rep.addParticipant(TestParticipant.class, "ParticipantConfig",
055: true, null);
056: rep.runParticipants(ResourceFinderClasspath.getInstance());
057:
058: assertNotNull(rep.getParticipant("ParticipantConfig"));
059:
060: Participant participant = rep
061: .getParticipant("ParticipantConfig");
062: assertSame(participant, rep
063: .getParticipant(TestParticipant.class.getName()));
064:
065: assertNotNull(participant.getObject());
066: assertEquals("value1", ((Config) participant.getObject())
067: .getString("param1"));
068: }
069:
070: public void testAddNotParticipant() {
071: BlockingRepository rep = new BlockingRepository();
072: try {
073: rep.addParticipant(Object.class, "ParticipantConfig", true,
074: null);
075: fail();
076: } catch (BlockingParticipantExpectedException e) {
077: assertEquals(Object.class.getName(), e.getClassName());
078: }
079: }
080:
081: public void testCleanup() {
082: CleanupRepository rep = new CleanupRepository();
083:
084: assertEquals(0, rep.mParticipants.size());
085:
086: rep.addParticipant(CleanupParticipant.class,
087: "CleanupParticipant1", true, null);
088: rep.addParticipant(CleanupParticipant.class,
089: "CleanupParticipant2", true, null);
090: rep.addParticipant(CleanupParticipant.class,
091: "CleanupParticipant3", true, null);
092: rep.addParticipant(CleanupParticipant.class,
093: "CleanupParticipant4", true, null);
094:
095: rep.runParticipants(ResourceFinderClasspath.getInstance());
096:
097: while (!rep.isFinished()) {
098: try {
099: Thread.sleep(100);
100: } catch (InterruptedException e) {
101: }
102: }
103:
104: assertEquals(4, rep.mParticipants.size());
105:
106: rep.cleanup();
107:
108: assertEquals(0, rep.mParticipants.size());
109: }
110: }
111:
112: class CleanupRepository extends BlockingRepository {
113: public Stack<String> mParticipants = new Stack<String>();
114: }
115:
116: class CleanupParticipant extends BlockingParticipant {
117: protected void initialize() {
118: ((CleanupRepository) getRepository()).mParticipants
119: .push(getName());
120: }
121:
122: protected Object _getObject() {
123: return null;
124: }
125:
126: protected void cleanup() {
127: Stack<String> participants = ((CleanupRepository) getRepository()).mParticipants;
128: if (participants.peek().equals(getName())) {
129: participants.pop();
130: }
131: }
132: }
|