Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 112   Methods: 6
NCLOC: 69   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
TxGauranteedListener.java 66.7% 93.8% 100% 89.3%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source
 3    * Copyright 2005, JBoss Inc., and individual contributors as indicated
 4    * by the @authors tag. See the copyright.txt in the distribution for a
 5    * full listing of individual contributors.
 6    *
 7    * This is free software; you can redistribute it and/or modify it
 8    * under the terms of the GNU Lesser General Public License as
 9    * published by the Free Software Foundation; either version 2.1 of
 10    * the License, or (at your option) any later version.
 11    *
 12    * This software is distributed in the hope that it will be useful,
 13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15    * Lesser General Public License for more details.
 16    *
 17    * You should have received a copy of the GNU Lesser General Public
 18    * License along with this software; if not, write to the Free
 19    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21    */
 22   
 23    package org.jboss.cache.pojo.notification;
 24   
 25    import java.util.Queue;
 26    import java.util.concurrent.ConcurrentHashMap;
 27    import java.util.concurrent.ConcurrentLinkedQueue;
 28    import java.util.concurrent.ConcurrentMap;
 29   
 30    import javax.transaction.Transaction;
 31   
 32    import org.jboss.cache.pojo.notification.annotation.Attached;
 33    import org.jboss.cache.pojo.notification.annotation.Detached;
 34    import org.jboss.cache.pojo.notification.annotation.FieldModified;
 35    import org.jboss.cache.pojo.notification.annotation.ListModified;
 36    import org.jboss.cache.pojo.notification.annotation.MapModified;
 37    import org.jboss.cache.pojo.notification.annotation.PojoCacheListener;
 38    import org.jboss.cache.pojo.notification.annotation.SetModified;
 39    import org.jboss.cache.pojo.notification.annotation.TransactionCompleted;
 40    import org.jboss.cache.pojo.notification.event.Event;
 41    import org.jboss.cache.pojo.notification.event.TransactionCompletedEvent;
 42   
 43    // $Id: TxGauranteedListener.java,v 1.1 2007/07/03 01:46:32 jgreene Exp $
 44   
 45    /**
 46    * A recoding Listener for notification test package.
 47    *
 48    * @author Jason T. Greene
 49    */
 50    @PojoCacheListener
 51    public class TxGauranteedListener
 52    {
 53    private class TxEventQueue
 54    {
 55    private ConcurrentMap<Transaction, Queue<Event>> map = new ConcurrentHashMap<Transaction, Queue<Event>>();
 56   
 57  9 public void offer(Event event)
 58    {
 59  9 Queue<Event> queue = getQueue(event.getContext().getTransaction());
 60  9 queue.offer(event);
 61    }
 62   
 63  9 private Queue<Event> getQueue(Transaction transaction)
 64    {
 65  9 Queue<Event> queue = map.get(transaction);
 66  9 if (queue == null)
 67    {
 68  5 queue = new ConcurrentLinkedQueue<Event>();
 69  5 map.putIfAbsent(transaction, queue);
 70    }
 71   
 72  9 return queue;
 73    }
 74   
 75  5 public Queue<Event> takeAll(Transaction transaction)
 76    {
 77  5 return map.remove(transaction);
 78    }
 79    }
 80   
 81    private TxEventQueue events = new TxEventQueue();
 82    private Queue<Event> committed = new ConcurrentLinkedQueue<Event>();
 83   
 84  9 @SuppressWarnings("unchecked")
 85    public <T extends Event> T take(Class<T> t)
 86    {
 87  9 Event notification = committed.remove();
 88  9 if (!t.isInstance(notification))
 89  0 throw new IllegalStateException("Expected notification type: " + t.getSimpleName() + " but was: " + notification.getClass().getSimpleName());
 90   
 91  9 return (T) notification;
 92    }
 93   
 94  9 @Attached
 95    @Detached
 96    @FieldModified
 97    @ListModified
 98    @SetModified
 99    @MapModified
 100    public void handle(Event event)
 101    {
 102  9 events.offer(event);
 103    }
 104   
 105  5 @TransactionCompleted
 106    public void handleTx(TransactionCompletedEvent event)
 107    {
 108  5 Queue<Event> completed = events.takeAll(event.getContext().getTransaction());
 109  5 if (completed != null && event.isSuccessful())
 110  5 committed.addAll(completed);
 111    }
 112    }