001package org.apache.turbine.services.schedule;
002
003/*
004* Licensed to the Apache Software Foundation (ASF) under one
005* or more contributor license agreements.  See the NOTICE file
006* distributed with this work for additional information
007* regarding copyright ownership.  The ASF licenses this file
008* to you under the Apache License, Version 2.0 (the
009* "License"); you may not use this file except in compliance
010* with the License.  You may obtain a copy of the License at
011*
012*   http://www.apache.org/licenses/LICENSE-2.0
013*
014* Unless required by applicable law or agreed to in writing,
015* software distributed under the License is distributed on an
016* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017* KIND, either express or implied.  See the License for the
018* specific language governing permissions and limitations
019* under the License.
020*/
021
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertNotNull;
024import static org.junit.Assert.assertNull;
025
026import org.apache.turbine.util.TurbineException;
027import org.junit.Before;
028import org.junit.Test;
029
030/**
031 * Unit testing for JobQueue.
032 *
033 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
034 */
035public class JobQueueTest
036{
037    private JobQueue<JobEntryNonPersistent> queue;
038    private JobEntryNonPersistent je1;
039    private JobEntryNonPersistent je2;
040
041    @Before
042    public void setUpBefore() throws Exception
043    {
044        queue = new JobQueue<>();
045
046        // Add a new job entry
047        je1 = new JobEntryNonPersistent(1,2,3,4,5,"je1");
048        je1.setJobId(1);
049
050        je2 = new JobEntryNonPersistent(0,2,3,4,5,"je2");
051        je2.setJobId(2);
052    }
053
054    /**
055     * Test job queue functions
056     * @throws TurbineException if the queue update fails
057     */
058    @Test
059    public void testJobQueue() throws TurbineException
060    {
061        assertNull(queue.getNext());
062
063        queue.add(je2);
064        queue.add(je1);
065        assertEquals(2, queue.list().size());
066
067        JobEntryNonPersistent je_a = queue.getNext();
068        assertNotNull(je_a);
069        assertEquals(je2, je_a);
070        assertEquals(1, queue.list().size());
071
072        JobEntryNonPersistent je_b = queue.getJob(je2);
073        assertNull(je_b);
074        JobEntryNonPersistent je_c = queue.getJob(je1);
075        assertNotNull(je_c);
076        assertEquals(je1, je_c);
077
078        je_c.setSecond(2);
079        queue.updateQueue(je_c);
080        assertEquals(1, queue.list().size());
081
082        je2.setSecond(3);
083        queue.updateQueue(je2);
084        JobEntryNonPersistent je_d = queue.getNext();
085        assertNotNull(je_d);
086        assertEquals(je1, je_d);
087        JobEntryNonPersistent je_e = queue.getNext();
088        assertNotNull(je_e);
089        assertEquals(je2, je_e);
090
091        // queue should now be empty
092        assertNull(queue.list());
093    }
094
095}