Skip to content

Commit

Permalink
WELD-2453: Use suppressedExceptions for deployment/definition problems
Browse files Browse the repository at this point in the history
- but only if multiple problems occur
  • Loading branch information
mkouba committed Jan 18, 2018
1 parent 350bd64 commit ee4bd3d
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2018, Red Hat, Inc., and individual contributors
* by the @authors tag. 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.weld.environment.se.test.deployment.errors;

import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.enterprise.inject.spi.DeploymentException;
import javax.enterprise.inject.spi.Extension;

import org.jboss.arquillian.container.se.api.ClassPath;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.BeanArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.weld.environment.se.Weld;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
*
* @author Martin Kouba
*/
@RunWith(Arquillian.class)
public class MultipleDeploymentErrorsTest {

@Deployment
public static Archive<?> createTestArchive() {
return ClassPath.builder().add(ShrinkWrap.create(BeanArchive.class).addPackage(MultipleDeploymentErrorsTest.class.getPackage())).build();
}

@Test
public void testErrors() {
try {
new Weld().addExtension(new TestExtension()).initialize();
Assert.fail("The deployment should have failed!");
} catch (Exception e) {
Assert.assertTrue(e instanceof DeploymentException);
Assert.assertEquals(2, e.getSuppressed().length);
Assert.assertTrue(e.getSuppressed()[0] instanceof NullPointerException);
Assert.assertTrue(e.getSuppressed()[1] instanceof IllegalStateException);
}
}

static class TestExtension implements Extension {

void afterDeploymentValidation(@Observes AfterDeploymentValidation event) {
event.addDeploymentProblem(new NullPointerException());
event.addDeploymentProblem(new IllegalStateException());
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2018, Red Hat, Inc., and individual contributors
* by the @authors tag. 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.weld.environment.se.test.deployment.errors;

import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.enterprise.inject.spi.DeploymentException;
import javax.enterprise.inject.spi.Extension;

import org.jboss.arquillian.container.se.api.ClassPath;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.BeanArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.weld.environment.se.Weld;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
*
* @author Martin Kouba
*/
@RunWith(Arquillian.class)
public class SingleDeploymentErrorTest {

@Deployment
public static Archive<?> createTestArchive() {
return ClassPath.builder().add(ShrinkWrap.create(BeanArchive.class).addPackage(SingleDeploymentErrorTest.class.getPackage())).build();
}

@Test
public void testErrors() {
try {
new Weld().addExtension(new TestExtension()).initialize();
Assert.fail("The deployment should have failed!");
} catch (Exception e) {
Assert.assertTrue(e instanceof DeploymentException);
Assert.assertEquals(0, e.getSuppressed().length);
Assert.assertTrue(e.getCause() instanceof IllegalStateException);
}
}

static class TestExtension implements Extension {

void afterDeploymentValidation(@Observes AfterDeploymentValidation event) {
event.addDeploymentProblem(new IllegalStateException());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ protected AbstractDeploymentContainerEvent(BeanManagerImpl beanManager, Type raw
public void fire() {
super.fire();
if (!getErrors().isEmpty()) {
throw new DeploymentException(getErrors());
if (getErrors().size() == 1) {
throw new DeploymentException(getErrors().get(0));
} else {
throw new DeploymentException(getErrors());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public DefinitionException(Throwable throwable) {
public DefinitionException(List<Throwable> errors) {
super(DefinitionException.class.getName()); // the no-args constructor is missing
this.message = new WeldExceptionListMessage(errors);
for (Throwable error : errors) {
addSuppressed(error);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public DeploymentException(Throwable throwable) {
public DeploymentException(List<? extends Throwable> errors) {
super(DeploymentException.class.getName()); // the no-args constructor is missing
this.message = new WeldExceptionListMessage(errors);
for (Throwable error : errors) {
addSuppressed(error);
}
}

/**
Expand Down

0 comments on commit ee4bd3d

Please sign in to comment.