001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.ksb.messaging.threadpool;
017    
018    import org.junit.Test;
019    import org.kuali.rice.ksb.messaging.MessageServiceInvoker;
020    import org.kuali.rice.ksb.messaging.PersistedMessageBO;
021    
022    import java.sql.Timestamp;
023    
024    import static org.junit.Assert.assertEquals;
025    
026    
027    /**
028     * Tests that the {@link PriorityBlockingQueuePersistedMessageComparator} is sorting by 
029     * priority and date. 
030     * 
031     * @author Kuali Rice Team (rice.collab@kuali.org)
032     *
033     */
034    public class PriorityBlockingQueuePersistedMessageComparatorTest {
035    
036        @Test public void testSortingByPriorityAndDate() throws Exception {
037            PersistedMessageBO message1 = new PersistedMessageBO();
038            message1.setQueuePriority(1);
039            message1.setQueueDate(new Timestamp(System.currentTimeMillis()));
040            
041            PersistedMessageBO message2 = new PersistedMessageBO();
042            message2.setQueuePriority(5);
043            message2.setQueueDate(new Timestamp(System.currentTimeMillis()));
044            
045            PersistedMessageBO message3 = new PersistedMessageBO();
046            message3.setQueuePriority(5);
047            message3.setQueueDate(new Timestamp(System.currentTimeMillis() + 5));
048            
049            assertEquals(PriorityBlockingQueuePersistedMessageComparator.class.getName() + " is sorting incorrectly", -1, new PriorityBlockingQueuePersistedMessageComparator().compare(new MessageServiceInvoker(message1), new MessageServiceInvoker(message2)));
050            assertEquals(PriorityBlockingQueuePersistedMessageComparator.class.getName() + " is sorting incorrectly", 1, new PriorityBlockingQueuePersistedMessageComparator().compare(new MessageServiceInvoker(message2), new MessageServiceInvoker(message1)));
051            assertEquals(PriorityBlockingQueuePersistedMessageComparator.class.getName() + " is sorting incorrectly", -1, new PriorityBlockingQueuePersistedMessageComparator().compare(new MessageServiceInvoker(message2), new MessageServiceInvoker(message3)));
052        }   
053    }