/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY 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 along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.cache.test;

import org.xwiki.cache.event.CacheEntryEvent;
import org.xwiki.cache.event.CacheEntryListener;

/**
 * Class used to test cache event management.
 * 
 * @version $Id$
 */
public class CacheEntryListenerTest implements CacheEntryListener<Object>
{
    /**
     * 
     */
    public enum EventType
    {
        /**
         * Add event.
         */
        ADD,

        /**
         * Modify event.
         */
        MODIFY,

        /**
         * Remove event.
         */
        REMOVE
    }

    /**
     * Event object received with last insertion.
     */
    private CacheEntryEvent<Object> addedEvent;

    /**
     * Event object received with last modification.
     */
    private CacheEntryEvent<Object> modifiedEvent;

    /**
     * Event object received with last remove.
     */
    private CacheEntryEvent<Object> removedEvent;

    /**
     * @return event object received with last insertion.
     */
    public CacheEntryEvent<Object> getAddedEvent()
    {
        return this.addedEvent;
    }

    /**
     * Set add event to null.
     */
    public void reinitAddEvent()
    {
        this.addedEvent = null;
    }

    /**
     * @return event object received with last modification.
     */
    public CacheEntryEvent<Object> getModifiedEvent()
    {
        return this.modifiedEvent;
    }

    /**
     * Set modified event to null.
     */
    public void reinitModifiedEvent()
    {
        this.modifiedEvent = null;
    }

    /**
     * @return event object received with last remove.
     */
    public CacheEntryEvent<Object> getRemovedEvent()
    {
        return this.removedEvent;
    }

    /**
     * Set removed event to null.
     */
    public void reinitRemovedEvent()
    {
        this.removedEvent = null;
    }

    @Override
    public void cacheEntryAdded(CacheEntryEvent<Object> event)
    {
        this.addedEvent = event;
    }

    @Override
    public void cacheEntryModified(CacheEntryEvent<Object> event)
    {
        this.modifiedEvent = event;
    }

    @Override
    public void cacheEntryRemoved(CacheEntryEvent<Object> event)
    {
        this.removedEvent = event;
    }

    /**
     * @param eventType event type.
     * @return wait until it receive a entry removed event.
     * @throws InterruptedException error
     */
    public boolean waitForEntryEvent(EventType eventType) throws InterruptedException
    {
        EventWaiter eventWaiter = new EventWaiter(eventType);

        Thread thread = new Thread(eventWaiter);
        thread.start();
        thread.join(100000);

        if (thread.isAlive()) {
            eventWaiter.stop();

            return false;
        }

        return true;
    }

    /**
     * Event waiter.
     */
    class EventWaiter implements Runnable
    {
        /**
         * Indicate that the thread should continue to run or have to stop.
         */
        private boolean run = true;

        /**
         * The event type.
         */
        private EventType eventType;

        /**
         * @param eventType the event type.
         */
        EventWaiter(EventType eventType)
        {
            this.eventType = eventType;
        }

        /**
         * Stop.
         */
        void stop()
        {
            this.run = false;
        }

        @Override
        public void run()
        {
            CacheEntryEvent<Object> event;

            while (this.run) {
                if (this.eventType == EventType.ADD) {
                    event = addedEvent;
                } else if (this.eventType == EventType.MODIFY) {
                    event = modifiedEvent;
                } else {
                    event = removedEvent;
                }

                if (event != null) {
                    break;
                }

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    // Ignore but properly restore the interrupted status
                    Thread.currentThread().interrupt();
                }
            }
        }
    }
}

@@ -4291,24 +4291,46 @@
 ct%3E event;%0A%0A
+            try %7B%0A    
             
@@ -4344,24 +4344,28 @@
 this.run) %7B%0A
+    
             
@@ -4403,24 +4403,28 @@
 Type.ADD) %7B%0A
+    
             
@@ -4463,24 +4463,28 @@
             
+    
 %7D else if (t
@@ -4532,32 +4532,36 @@
                 
+    
 event = modified
@@ -4579,24 +4579,28 @@
             
+    
 %7D else %7B%0A   
@@ -4608,32 +4608,36 @@
                 
+   
+ 
 event = removedE
@@ -4650,35 +4650,42 @@
                 
-%7D%0A%0A
+    %7D%0A%0A   
                 
@@ -4680,24 +4680,25 @@
             
+ 
 if (event !=
@@ -4702,24 +4702,28 @@
  != null) %7B%0A
+    
             
@@ -4757,32 +4757,13 @@
     
-%7D%0A%0A                try %7B
+    %7D
 %0A   
@@ -4815,16 +4815,30 @@
        %7D
+%0A            %7D
  catch (
@@ -4859,28 +4859,24 @@
 eption e) %7B%0A
-    
             
@@ -4945,28 +4945,24 @@
             
-    
 Thread.curre
@@ -4985,34 +4985,16 @@
 rupt();%0A
-                %7D%0A
         

Apply best practice for InterruptedException thanks to SonarQube (better fix)