Skip to content

Commit

Permalink
WELD-2165 Detect non-unique BeanDeploymentArchive/BeanManager identifier
Browse files Browse the repository at this point in the history
- also upgrade TestNG to 6.8.8
  • Loading branch information
mkouba committed Jun 1, 2016
1 parent 7b7b723 commit 988a101
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 10 deletions.
15 changes: 15 additions & 0 deletions impl/src/main/java/org/jboss/weld/bootstrap/BeanDeployment.java
Expand Up @@ -264,4 +264,19 @@ public void afterBeanDiscovery(Environment environment) {
beanDeployer.doAfterBeanDiscovery(beanManager.getInterceptors());
beanDeployer.registerCdiInterceptorsForMessageDrivenBeans();
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("BeanDeployment ");
builder.append("[beanDeploymentArchiveId=");
builder.append(beanDeploymentArchive.getId());
if (!beanDeploymentArchive.getId().equals(beanManager.getId())) {
builder.append(", beanManagerId=");
builder.append(beanManager.getId());
}
builder.append("]");
return builder.toString();
}

}
Expand Up @@ -2,7 +2,10 @@

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

Expand Down Expand Up @@ -34,4 +37,16 @@ public Collection<BeanDeployment> getBeanDeployments() {
public ConcurrentMap<BeanDeploymentArchive, BeanManagerImpl> getBdaToBeanManagerMap() {
return beanManagers;
}

boolean isNonuniqueIdentifierDetected() {
Set<String> beanDeploymentArchiveIds = new HashSet<>();
Set<String> beanManagerIds = new HashSet<>();
for (Entry<BeanDeploymentArchive, BeanDeployment> entry : beanDeployments.entrySet()) {
if (!beanDeploymentArchiveIds.add(entry.getKey().getId()) || !beanManagerIds.add(entry.getValue().getBeanManager().getId())) {
return true;
}
}
return false;
}

}
Expand Up @@ -14,6 +14,7 @@
import org.jboss.weld.logging.BootstrapLogger;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.resources.spi.ResourceLoader;
import org.jboss.weld.util.collections.WeldCollections;

/**
* A Deployment visitor which can find the transitive closure of Bean Deployment Archives
Expand Down Expand Up @@ -45,6 +46,10 @@ public void visit() {
visit(archive, seenBeanDeploymentArchives);
}
}
// Alhough it's the responsibility of an integrator, check the uniqueness to avoid weird bugs
if (bdaMapping.isNonuniqueIdentifierDetected()) {
throw BootstrapLogger.LOG.nonuniqueBeanDeploymentIdentifier(WeldCollections.toMultiRowString(bdaMapping.getBeanDeployments()));
}
}

private BeanDeployment visit(BeanDeploymentArchive bda, Set<BeanDeploymentArchive> seenBeanDeploymentArchives) {
Expand Down
Expand Up @@ -263,4 +263,7 @@ public interface BootstrapLogger extends WeldLogger {
@Message(id = 162, value = "BeforeBeanDiscovery.addAnnotatedType() called by {0} for {1}", format = Format.MESSAGE_FORMAT)
void addAnnotatedTypeCalledInBBD(Object extensionName, Object type);

@Message(id = 163, value = "Non-unique bean deployment identifier detected: {0}", format = Format.MESSAGE_FORMAT)
DeploymentException nonuniqueBeanDeploymentIdentifier(Object info);

}
9 changes: 1 addition & 8 deletions pom.xml
Expand Up @@ -71,7 +71,7 @@
<shade.plugin.version>2.3</shade.plugin.version>
<shrinkwrap.version>1.1.3</shrinkwrap.version>
<shrinkwrap.descriptors.version>1.1.0-beta-1</shrinkwrap.descriptors.version>
<testng.version>5.10</testng.version>
<testng.version>6.8.8</testng.version>
<weld.api.version>3.0.Alpha19</weld.api.version>
<weld.logging.tools.version>1.0.1.Final</weld.logging.tools.version>
<wildfly.arquillian.version>1.0.1.Final</wildfly.arquillian.version>
Expand Down Expand Up @@ -431,13 +431,6 @@
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<classifier>jdk15</classifier>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down
3 changes: 1 addition & 2 deletions tests/pom.xml
Expand Up @@ -26,7 +26,6 @@
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<classifier>jdk15</classifier>
</dependency>

<dependency>
Expand Down Expand Up @@ -54,7 +53,7 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.jboss.spec.javax.jms</groupId>
<artifactId>jboss-jms-api_1.1_spec</artifactId>
Expand Down
@@ -0,0 +1,28 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, 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.tests.unit.deployment.structure.duplicit;

import javax.enterprise.inject.Produces;

public class Bar {

@Produces
public String get() {
return "Foo!";
}

}
@@ -0,0 +1,74 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 201, 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.tests.unit.deployment.structure.duplicit;

import org.jboss.arquillian.container.weld.ee.embedded_1_1.mock.BeanDeploymentArchiveImpl;
import org.jboss.arquillian.container.weld.ee.embedded_1_1.mock.TestContainer;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.bootstrap.spi.Deployment;
import org.jboss.weld.exceptions.DeploymentException;
import org.jboss.weld.mock.AbstractDeployment;
import org.testng.annotations.Test;

/**
* https://issues.jboss.org/browse/WELD-2165
*
* @author Martin Kouba
*/
public class DuplicateBeanArchiveIdTest {

@Test(expectedExceptions = { DeploymentException.class })
public void test() {
// Override equals/hashcode - bda is used in a map
final BeanDeploymentArchive bda1 = new BeanDeploymentArchiveImpl("1", Foo.class) {

@Override
public boolean equals(Object obj) {
return (this == obj);
}

@Override
public int hashCode() {
return System.identityHashCode(this);
}
};
final BeanDeploymentArchive bda2 = new BeanDeploymentArchiveImpl("1", Bar.class) {

@Override
public boolean equals(Object obj) {
return (this == obj);
}

@Override
public int hashCode() {
return System.identityHashCode(this);
}
};

Deployment deployment = new AbstractDeployment(bda1, bda2) {

@Override
public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass) {
return loadBeanDeploymentArchive(beanClass);
}
};

TestContainer container = new TestContainer(deployment);
container.startContainer();
container.stopContainer();
}
}
@@ -0,0 +1,28 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, 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.tests.unit.deployment.structure.duplicit;

import javax.enterprise.inject.Produces;

public class Foo {

@Produces
public String get() {
return "Foo!";
}

}

0 comments on commit 988a101

Please sign in to comment.