[savara-commits] savara SVN: r590 - in branches/experimental/2.0.x/bundles/org.savara.monitor/src: test/java/org/savara/monitor/impl and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Mon Jan 17 18:21:50 EST 2011


Author: objectiser
Date: 2011-01-17 18:21:49 -0500 (Mon, 17 Jan 2011)
New Revision: 590

Added:
   branches/experimental/2.0.x/bundles/org.savara.monitor/src/main/java/org/savara/monitor/impl/InMemoryProtocolRepository.java
   branches/experimental/2.0.x/bundles/org.savara.monitor/src/test/java/org/savara/monitor/impl/DefaultMonitorTest.java
Modified:
   branches/experimental/2.0.x/bundles/org.savara.monitor/src/main/java/org/savara/monitor/impl/DefaultMonitor.java
   branches/experimental/2.0.x/bundles/org.savara.monitor/src/main/java/org/savara/monitor/impl/InMemorySessionStore.java
Log:
Added some tests for the monitor.

Modified: branches/experimental/2.0.x/bundles/org.savara.monitor/src/main/java/org/savara/monitor/impl/DefaultMonitor.java
===================================================================
--- branches/experimental/2.0.x/bundles/org.savara.monitor/src/main/java/org/savara/monitor/impl/DefaultMonitor.java	2011-01-17 21:04:43 UTC (rev 589)
+++ branches/experimental/2.0.x/bundles/org.savara.monitor/src/main/java/org/savara/monitor/impl/DefaultMonitor.java	2011-01-17 23:21:49 UTC (rev 590)
@@ -102,6 +102,48 @@
 	public boolean sent(ProtocolId pid, ConversationInstanceId cid, Message mesg)
 							throws ProtocolUnknownException,
 									java.io.IOException {
+		return(processMessage(pid, cid, mesg, true));
+	}
+	
+	/**
+	 * This method is used to indicate that a message has been
+	 * received and should be monitored against the configured
+	 * behaviour.
+	 * 
+	 * If the conversation instance id is not explicitly
+	 * specified, then the protocol monitor will be responsible
+	 * for deriving the appropriate value.
+	 * 
+	 * @param pid The protocol id
+	 * @param cid The optional conversation instance id
+	 * @param mesg The message
+	 * @return Whether the message was valid
+	 * @throws ProtocolUnknownException Unknown protocol name or role
+	 * @throws IOException Failed to create or retrieve session
+	 */
+	public boolean received(ProtocolId pid, ConversationInstanceId cid, Message mesg)
+							throws ProtocolUnknownException,
+							java.io.IOException {
+		return(processMessage(pid, cid, mesg, false));
+	}
+	
+	/**
+	 * This method is used to process a message against a protocol monitor.
+	 * 
+	 * If the conversation instance id is not explicitly
+	 * specified, then the protocol monitor will be responsible
+	 * for deriving the appropriate value.
+	 * 
+	 * @param pid The protocol id
+	 * @param cid The optional conversation instance id
+	 * @param mesg The message
+	 * @param sent Whether to process the message as a sent message
+	 * @return Whether the message was valid
+	 * @throws ProtocolUnknownException Unknown protocol name or role
+	 * @throws IOException Failed to create or retrieve session
+	 */
+	protected boolean processMessage(ProtocolId pid, ConversationInstanceId cid, Message mesg, boolean sent)
+						throws ProtocolUnknownException, java.io.IOException {
 		boolean ret=false;
 		
 		if (pid == null) {
@@ -128,11 +170,11 @@
 		
 		if (session == null) {
 			session = m_sessionStore.create(pid, cid);
-			
+		
 			// Try to create new session
 			if (session instanceof Session) {
 				m_monitor.initialize(context, desc, (Session)session);
-				
+			
 				if (m_monitorListener != null) {
 					m_monitorListener.sessionStarted(pid, cid);
 				}
@@ -148,12 +190,12 @@
 			if (m_monitorListener != null) {
 				m_monitorListener.messageSent(pid, cid, mesg, result);
 			}
-			
+		
 			if (((Session)session).isFinished()) {
 				if (m_monitorListener != null) {
 					m_monitorListener.sessionFinished(pid, cid);
 				}
-
+		
 				m_sessionStore.remove(pid, cid);
 			} else {
 				m_sessionStore.update(pid, cid, session);
@@ -162,41 +204,7 @@
 			throw new java.io.IOException("Inappropriate session type returned");
 		}
 		
-		// Should instance id be derived from message, or supplied
-		// with message? If simulating, then identity should be
-		// fixed.
-		
-		// Protocol name and role needs to be distinguished when
-		// storing session, as a single conversation, with a
-		// particular business id, may be monitored against
-		// multiple protocols/roles
-		
-		
-		
 		return(ret);
 	}
 	
-	/**
-	 * This method is used to indicate that a message has been
-	 * received and should be monitored against the configured
-	 * behaviour.
-	 * 
-	 * If the conversation instance id is not explicitly
-	 * specified, then the protocol monitor will be responsible
-	 * for deriving the appropriate value.
-	 * 
-	 * @param pid The protocol id
-	 * @param cid The optional conversation instance id
-	 * @param mesg The message
-	 * @return Whether the message was valid
-	 * @throws ProtocolUnknownException Unknown protocol name or role
-	 * @throws IOException Failed to create or retrieve session
-	 */
-	public boolean received(ProtocolId pid, ConversationInstanceId cid, Message mesg)
-							throws ProtocolUnknownException,
-							java.io.IOException {
-		boolean ret=false;
-		
-		return(ret);
-	}
 }

Added: branches/experimental/2.0.x/bundles/org.savara.monitor/src/main/java/org/savara/monitor/impl/InMemoryProtocolRepository.java
===================================================================
--- branches/experimental/2.0.x/bundles/org.savara.monitor/src/main/java/org/savara/monitor/impl/InMemoryProtocolRepository.java	                        (rev 0)
+++ branches/experimental/2.0.x/bundles/org.savara.monitor/src/main/java/org/savara/monitor/impl/InMemoryProtocolRepository.java	2011-01-17 23:21:49 UTC (rev 590)
@@ -0,0 +1,48 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA  02110-1301, USA.
+ */
+package org.savara.monitor.impl;
+
+import org.savara.monitor.ProtocolId;
+import org.savara.monitor.ProtocolRepository;
+import org.savara.monitor.ProtocolUnknownException;
+import org.scribble.protocol.monitor.model.Description;
+
+/**
+ * This is the in-memory implementation of the protocol repository.
+ *
+ */
+public class InMemoryProtocolRepository implements ProtocolRepository {
+
+	private java.util.Map<ProtocolId,Description> m_descriptions=
+						new java.util.HashMap<ProtocolId,Description>();
+	
+	public InMemoryProtocolRepository() {
+	}
+
+	public void addProtocol(ProtocolId pid, Description desc) {
+		m_descriptions.put(pid, desc);
+	}
+	
+	public Description getProtocol(ProtocolId pid) throws ProtocolUnknownException {
+		if (m_descriptions.containsKey(pid) == false) {
+			throw new ProtocolUnknownException("Protocol '"+pid+"' not known");
+		}
+		
+		return(m_descriptions.get(pid));
+	}
+}

Modified: branches/experimental/2.0.x/bundles/org.savara.monitor/src/main/java/org/savara/monitor/impl/InMemorySessionStore.java
===================================================================
--- branches/experimental/2.0.x/bundles/org.savara.monitor/src/main/java/org/savara/monitor/impl/InMemorySessionStore.java	2011-01-17 21:04:43 UTC (rev 589)
+++ branches/experimental/2.0.x/bundles/org.savara.monitor/src/main/java/org/savara/monitor/impl/InMemorySessionStore.java	2011-01-17 23:21:49 UTC (rev 590)
@@ -27,6 +27,21 @@
 	private java.util.Map<ProtocolId,java.util.Map<ConversationInstanceId,java.io.Serializable>> m_sessions=
 			new java.util.HashMap<ProtocolId,java.util.Map<ConversationInstanceId, java.io.Serializable>>();
 	
+	protected void addSession(ProtocolId pid, ConversationInstanceId cid, java.io.Serializable session) {
+		java.util.Map<ConversationInstanceId,java.io.Serializable> sessions=
+						m_sessions.get(pid);
+
+		if (sessions == null) {
+			sessions = new java.util.HashMap<ConversationInstanceId,java.io.Serializable>();
+			m_sessions.put(pid, sessions);
+		
+		} else if (sessions.containsKey(cid)) {
+			throw new IllegalArgumentException("Conversation instance id already in use");
+		}
+		
+		sessions.put(cid, session);	
+	}
+	
 	/**
 	 * This method returns a new session associated with
 	 * the supplied protocol and conversation instance id.
@@ -45,20 +60,9 @@
 			throw new IllegalArgumentException("Conversation instance id not specified");
 		}
 		
-		java.util.Map<ConversationInstanceId,java.io.Serializable> sessions=
-							m_sessions.get(pid);
-		
-		if (sessions == null) {
-			sessions = new java.util.HashMap<ConversationInstanceId,java.io.Serializable>();
-			m_sessions.put(pid, sessions);
-			
-		} else if (sessions.containsKey(cid)) {
-			throw new IllegalArgumentException("Conversation instance id already in use");
-		}
-		
 		java.io.Serializable ret=new DefaultSession();
 		
-		sessions.put(cid, ret);
+		addSession(pid, cid, ret);
 		
 		return(ret);
 	}

Added: branches/experimental/2.0.x/bundles/org.savara.monitor/src/test/java/org/savara/monitor/impl/DefaultMonitorTest.java
===================================================================
--- branches/experimental/2.0.x/bundles/org.savara.monitor/src/test/java/org/savara/monitor/impl/DefaultMonitorTest.java	                        (rev 0)
+++ branches/experimental/2.0.x/bundles/org.savara.monitor/src/test/java/org/savara/monitor/impl/DefaultMonitorTest.java	2011-01-17 23:21:49 UTC (rev 590)
@@ -0,0 +1,237 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA  02110-1301, USA.
+ */
+package org.savara.monitor.impl;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+import org.savara.monitor.ConversationInstanceId;
+import org.savara.monitor.Message;
+import org.savara.monitor.MonitorListener;
+import org.savara.monitor.ProtocolId;
+import org.scribble.protocol.monitor.DefaultSession;
+import org.scribble.protocol.monitor.ProtocolMonitorFactory;
+import org.scribble.protocol.monitor.Result;
+import org.scribble.protocol.monitor.Session;
+import org.scribble.protocol.monitor.model.Description;
+
+public class DefaultMonitorTest {
+
+	@Test
+	public void testNoProtocolRepository() {
+		DefaultMonitor mon=new DefaultMonitor();
+		
+		DefaultMessage mesg=new DefaultMessage();
+		
+		ProtocolId pid=new ProtocolId("name", "role");
+		
+		ConversationInstanceId cid=new ConversationInstanceId("cid");
+		
+		mon.setProtocolMonitor(ProtocolMonitorFactory.createProtocolMonitor());
+		mon.setSessionStore(new InMemorySessionStore());
+		
+		try {
+			mon.processMessage(pid, cid, mesg, true);
+			fail("Should have generated illegal state exception");
+		} catch(IllegalStateException ise) {
+			// Expected
+		} catch(Exception e) {
+			fail("Unexpected: "+e);
+		}
+	}
+
+	@Test
+	public void testNoSessionStore() {
+		DefaultMonitor mon=new DefaultMonitor();
+		
+		DefaultMessage mesg=new DefaultMessage();
+		
+		ProtocolId pid=new ProtocolId("name", "role");
+		
+		ConversationInstanceId cid=new ConversationInstanceId("cid");
+		
+		mon.setProtocolMonitor(ProtocolMonitorFactory.createProtocolMonitor());
+		
+		InMemoryProtocolRepository rep=new InMemoryProtocolRepository();
+		mon.setProtocolRepository(rep);
+		
+		try {
+			mon.processMessage(pid, cid, mesg, true);
+			fail("Should have generated illegal state exception");
+		} catch(IllegalStateException ise) {
+			// Expected
+		} catch(Exception e) {
+			fail("Unexpected: "+e);
+		}
+	}
+
+	@Test
+	public void testProcessMessage() {
+		DefaultMonitor mon=new DefaultMonitor();
+		
+		DefaultMessage mesg=new DefaultMessage();
+		
+		ProtocolId pid=new ProtocolId("name", "role");
+		
+		ConversationInstanceId cid=new ConversationInstanceId("cid");
+		
+		mon.setProtocolMonitor(ProtocolMonitorFactory.createProtocolMonitor());
+		
+		InMemoryProtocolRepository rep=new InMemoryProtocolRepository();
+		rep.addProtocol(pid, new Description());
+		mon.setProtocolRepository(rep);
+
+		InMemorySessionStore store=new InMemorySessionStore();		
+		mon.setSessionStore(store);
+		
+		try {
+			mon.processMessage(pid, cid, mesg, true);
+		} catch(Exception e) {
+			fail("Unexpected: "+e);
+		}
+	}
+
+	@Test
+	public void testProcessMessageWithExistingSession() {
+		DefaultMonitor mon=new DefaultMonitor();
+		
+		DefaultMessage mesg=new DefaultMessage();
+		
+		ProtocolId pid=new ProtocolId("name", "role");
+		
+		ConversationInstanceId cid=new ConversationInstanceId("cid");
+		
+		mon.setProtocolMonitor(ProtocolMonitorFactory.createProtocolMonitor());
+		
+		InMemoryProtocolRepository rep=new InMemoryProtocolRepository();
+		rep.addProtocol(pid, new Description());
+		mon.setProtocolRepository(rep);
+
+		InMemorySessionStore store=new InMemorySessionStore();
+		
+		DefaultSession s1=new DefaultSession();
+		
+		try {
+			store.create(pid, cid);
+			store.update(pid, cid, s1);
+		} catch(Exception e) {
+			fail("Failed: "+e);
+		}
+		
+		mon.setSessionStore(store);
+		
+		try {
+			mon.processMessage(pid, cid, mesg, true);
+		} catch(Exception e) {
+			fail("Unexpected: "+e);
+		}
+	}
+
+	@Test
+	public void testSingleMessageSession() {
+		DefaultMonitor mon=new DefaultMonitor();
+		
+		DefaultMessage mesg=new DefaultMessage();
+		
+		ProtocolId pid=new ProtocolId("name", "role");
+		
+		ConversationInstanceId cid=new ConversationInstanceId("cid");
+		
+		mon.setProtocolMonitor(ProtocolMonitorFactory.createProtocolMonitor());
+		
+		InMemoryProtocolRepository rep=new InMemoryProtocolRepository();
+		rep.addProtocol(pid, new Description());
+		mon.setProtocolRepository(rep);
+
+		InMemorySessionStore store=new InMemorySessionStore() {
+			public java.io.Serializable create(ProtocolId pid, ConversationInstanceId cid)
+						throws IllegalArgumentException, java.io.IOException {
+			
+				DefaultSession s1=new DefaultSession() {
+					public boolean isFinished() {
+						return(true);
+					}
+				};
+				
+				addSession(pid, cid, s1);
+				
+				return(s1);
+			}
+		};
+				
+		TestMonitorListener tl=new TestMonitorListener();
+		
+		mon.setMonitorListener(tl);
+		
+		mon.setSessionStore(store);
+		
+		try {
+			mon.processMessage(pid, cid, mesg, true);
+		} catch(Exception e) {
+			fail("Unexpected: "+e);
+		}
+		
+		if (tl.hasSessionStarted(pid) == false) {
+			fail("Session not started");
+		}
+		
+		if (tl.hasSessionFinished(pid) == false) {
+			fail("Session not finished");
+		}
+	}
+	
+	public class TestMonitorListener implements MonitorListener {
+		
+		private java.util.List<ProtocolId> m_started=new java.util.Vector<ProtocolId>();
+		private java.util.List<ProtocolId> m_finished=new java.util.Vector<ProtocolId>();
+
+		public boolean hasSessionStarted(ProtocolId pid) {
+			return(m_started.contains(pid));
+		}
+		
+		@Override
+		public void sessionStarted(ProtocolId pid, ConversationInstanceId cid) {
+			m_started.add(pid);
+		}
+
+		public boolean hasSessionFinished(ProtocolId pid) {
+			return(m_finished.contains(pid));
+		}
+		
+		@Override
+		public void sessionFinished(ProtocolId pid, ConversationInstanceId cid) {
+			m_finished.add(pid);
+		}
+
+		@Override
+		public void messageSent(ProtocolId pid, ConversationInstanceId cid,
+				Message mesg, Result result) {
+			// TODO Auto-generated method stub
+			
+		}
+
+		@Override
+		public void messageReceived(ProtocolId pid, ConversationInstanceId cid,
+				Message mesg, Result result) {
+			// TODO Auto-generated method stub
+			
+		}
+		
+	}
+}
+



More information about the savara-commits mailing list