/*
 * 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.observation;

import junit.framework.Assert;

import org.hamcrest.core.IsNot;
import org.jmock.Expectations;
import org.junit.Test;
import org.xwiki.observation.event.BeginEvent;
import org.xwiki.observation.event.EndEvent;
import org.xwiki.observation.internal.DefaultObservationContext;
import org.xwiki.test.AbstractComponentTestCase;

/**
 * Validate {@link DefaultObservationContext}.
 * 
 * @version $Id$
 */
public class DefaultObservationContextTest extends AbstractComponentTestCase
{
    private ObservationManager manager;

    private ObservationContext observationContext;

    @Override
    public void setUp() throws Exception
    {
        super.setUp();

        this.manager = getComponentManager().lookup(ObservationManager.class);
        this.observationContext = getComponentManager().lookup(ObservationContext.class);
    }

    @Test
    public void test()
    {
        final BeginEvent beginEvent1 = getMockery().mock(BeginEvent.class, "begin1");
        final BeginEvent beginEvent2 = getMockery().mock(BeginEvent.class, "begin2");
        final EndEvent endEvent1 = getMockery().mock(EndEvent.class, "end1");
        final EndEvent endEvent2 = getMockery().mock(EndEvent.class, "end2");

        getMockery().checking(new Expectations()
        {
            {
                allowing(beginEvent1).matches(with(same(beginEvent1)));
                will(returnValue(true));
                allowing(beginEvent1).matches(with(IsNot.not(same(beginEvent1))));
                will(returnValue(false));

                allowing(beginEvent2).matches(with(same(beginEvent2)));
                will(returnValue(true));
                allowing(beginEvent2).matches(with(IsNot.not(same(beginEvent2))));
                will(returnValue(false));

                allowing(endEvent1).matches(with(same(endEvent1)));
                will(returnValue(true));
                allowing(endEvent1).matches(with(IsNot.not(same(endEvent1))));
                will(returnValue(false));

                allowing(endEvent2).matches(with(same(endEvent2)));
                will(returnValue(true));
                allowing(endEvent2).matches(with(IsNot.not(same(endEvent2))));
                will(returnValue(false));
            }
        });

        Assert.assertFalse(this.observationContext.isIn(beginEvent1));
        Assert.assertFalse(this.observationContext.isIn(beginEvent2));

        this.manager.notify(beginEvent1, null);

        Assert.assertTrue(this.observationContext.isIn(beginEvent1));

        this.manager.notify(beginEvent2, null);

        Assert.assertTrue(this.observationContext.isIn(beginEvent1));
        Assert.assertTrue(this.observationContext.isIn(beginEvent2));

        this.manager.notify(endEvent2, null);

        Assert.assertTrue(this.observationContext.isIn(beginEvent1));
        Assert.assertFalse(this.observationContext.isIn(beginEvent2));

        this.manager.notify(endEvent1, null);

        Assert.assertFalse(this.observationContext.isIn(beginEvent1));
        Assert.assertFalse(this.observationContext.isIn(beginEvent2));
    }
}

@@ -2173,37 +2173,12 @@
 ns()
-%0A        %7B%0A            %7B%0A    
+ %7B%7B%0A
     
@@ -2240,32 +2240,16 @@
 ent1)));
-%0A               
  will(re
@@ -2258,36 +2258,32 @@
 rnValue(true));%0A
-    
             allo
@@ -2344,32 +2344,16 @@
 nt1))));
-%0A               
  will(re
@@ -2364,36 +2364,32 @@
 Value(false));%0A%0A
-    
             allo
@@ -2439,32 +2439,16 @@
 ent2)));
-%0A               
  will(re
@@ -2457,36 +2457,32 @@
 rnValue(true));%0A
-    
             allo
@@ -2543,32 +2543,16 @@
 nt2))));
-%0A               
  will(re
@@ -2563,36 +2563,32 @@
 Value(false));%0A%0A
-    
             allo
@@ -2634,32 +2634,16 @@
 ent1)));
-%0A               
  will(re
@@ -2664,36 +2664,32 @@
 ));%0A            
- 
-   
 allowing(endEven
@@ -2734,32 +2734,16 @@
 nt1))));
-%0A               
  will(re
@@ -2754,36 +2754,32 @@
 Value(false));%0A%0A
-    
             allo
@@ -2825,32 +2825,16 @@
 ent2)));
-%0A               
  will(re
@@ -2843,36 +2843,32 @@
 rnValue(true));%0A
-    
             allo
@@ -2929,24 +2929,8 @@
 )));
-%0A               
  wil
@@ -2964,22 +2964,9 @@
     
-    %7D%0A        
+%7D
 %7D);%0A

Introduce begin/end events standard Fix codestyle