/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.arquillian.impl.core;
import junit.framework.Assert;
import org.jboss.arquillian.impl.core.context.SuiteContextImpl;
import org.jboss.arquillian.spi.core.Event;
import org.jboss.arquillian.spi.core.annotation.Inject;
import org.jboss.arquillian.spi.core.annotation.Observes;
import org.junit.Test;
/**
* TestCase to ensure a nested observes Exception is only handled once within the call chain.
*
* - TestEventFire
* - TestExceptionThrower
* - throws Exception
* - TestExceptionHandler
* - re throw
*
* @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
* @version $Revision: $
*/
public class ExceptionHandlingTestCase
{
{
ManagerImpl.DEBUG = false;
}
@Test
public void shouldOnlyFireSameExceptionOnce() throws Exception
{
ManagerImpl manager = ManagerBuilder.from()
.context(SuiteContextImpl.class)
.extensions(TestEventFire.class, TestExceptionThrower.class, TestExceptionHandler.class)
.create();
try
{
manager.fire(new Double(0.0));
}
catch (Exception e)
{
if(!(e instanceof TestException))
{
Assert.fail("Wrong Exception thrown " + e);
}
}
Assert.assertEquals(new Integer(1), TestEventFire.called);
Assert.assertEquals(new Integer(1), TestExceptionThrower.called);
Assert.assertEquals(new Integer(1), TestExceptionHandler.called);
}
public static class TestEventFire
{
public static Integer called = 0;
@Inject
private Event<Integer> integer;
public void handle(@Observes Double event) throws Exception
{
called++;
integer.fire(new Integer(10));
}
}
public static class TestExceptionThrower
{
public static Integer called = 0;
public void handle(@Observes Integer event) throws Exception
{
called++;
throw new TestException("_TEST_");
}
}
public static class TestExceptionHandler
{
public static Integer called = 0;
public void handle(@Observes TestException event) throws Exception
{
called++;
// Handles exception but re-throws it to specify it's a IllegalException
throw event;
}
}
public static class TestException extends Exception
{
private static final long serialVersionUID = 1L;
public TestException(String message)
{
super(message);
}
}
}