Skip to content

Commit

Permalink
WELD-1560 - CustomCDIProviderTest based on incorrect assumption
Browse files Browse the repository at this point in the history
  • Loading branch information
bafco authored and jharting committed Dec 2, 2013
1 parent 9d6b004 commit 1bca3e7
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 87 deletions.
@@ -0,0 +1,43 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2012, 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.provider.custom;

import javax.enterprise.inject.Vetoed;
import javax.enterprise.inject.spi.CDI;
import javax.enterprise.inject.spi.CDIProvider;

/**
* @author Martin Kouba
*
*/
@Vetoed
public class CustomCDIProvider implements CDIProvider {

public static boolean isCalled = false;

@Override
public CDI<Object> getCDI() {
isCalled = true;
return null;
}

public static void reset() {
isCalled = false;
}

}
Expand Up @@ -16,6 +16,7 @@
*/
package org.jboss.weld.environment.se.test.provider.custom;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import javax.enterprise.inject.spi.CDI;
Expand All @@ -24,18 +25,20 @@
import org.junit.Test;

/**
* @author Matus Abaffy
* @author Martin Kouba
*/
public class CustomCDIProviderTest extends WeldSETest {

@Test
public void testCustomCDIProvider() {
MyCDI.reset();
// Setting CDI provider may affect other tests in weld-se-core,
// as they will use this CDI provider if they run after this test
CDI.setCDIProvider(MyCDI.INSTANCE);
CDI.current().getBeanManager();
assertTrue(MyCDI.isGetBMCalled);
assertTrue(MyCDI.isGetCDICalled);
try {
CustomCDIProvider.reset();
CDI.setCDIProvider(new CustomCDIProvider());
assertNull(CDI.current());
assertTrue(CustomCDIProvider.isCalled);
} finally {
// Unset the CDIProvider so that other tests are not affected
TestCDI.unsetCDIProvider();
}
}
}

This file was deleted.

This file was deleted.

@@ -0,0 +1,88 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, 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.provider.custom;

import java.lang.annotation.Annotation;
import java.util.Iterator;

import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Vetoed;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.CDI;
import javax.enterprise.util.TypeLiteral;

/**
*
* @author Martin Kouba
*/
@Vetoed
public class TestCDI extends CDI<Object> {

/**
* WORKAROUND - it's not possible to unset the CDIProvider via {@link CDI#setCDIProvider(javax.enterprise.inject.spi.CDIProvider)} but it's possible to set the field
* value directly in a subclass. However, it was probably not intended for the subclass to be able to do this.
*/
public static void unsetCDIProvider() {
configuredProvider = null;
}

@Override
public Instance<Object> select(Annotation... qualifiers) {
throw new UnsupportedOperationException();
}

@Override
public <U> Instance<U> select(Class<U> subtype, Annotation... qualifiers) {
throw new UnsupportedOperationException();
}

@Override
public <U> Instance<U> select(TypeLiteral<U> subtype, Annotation... qualifiers) {
throw new UnsupportedOperationException();
}

@Override
public boolean isUnsatisfied() {
throw new UnsupportedOperationException();
}

@Override
public boolean isAmbiguous() {
throw new UnsupportedOperationException();
}

@Override
public void destroy(Object instance) {
throw new UnsupportedOperationException();
}

@Override
public Iterator<Object> iterator() {
throw new UnsupportedOperationException();
}

@Override
public Object get() {
throw new UnsupportedOperationException();
}

@Override
public BeanManager getBeanManager() {
throw new UnsupportedOperationException();
}

}
@@ -0,0 +1,43 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2012, 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.servlet.test.provider;

import javax.enterprise.inject.Vetoed;
import javax.enterprise.inject.spi.CDI;
import javax.enterprise.inject.spi.CDIProvider;

/**
* @author Martin Kouba
*
*/
@Vetoed
public class CustomCDIProvider implements CDIProvider {

public static boolean isCalled = false;

@Override
public CDI<Object> getCDI() {
isCalled = true;
return null;
}

public static void reset() {
isCalled = false;
}

}
Expand Up @@ -17,6 +17,7 @@
package org.jboss.weld.environment.servlet.test.provider;

import static org.jboss.weld.environment.servlet.test.util.Deployments.baseDeployment;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import javax.enterprise.inject.spi.CDI;
Expand All @@ -32,10 +33,14 @@ public static WebArchive deployment() {

@Test
public void testCustomCDIProvider() {
MyCDIProvider.reset();
CDI<Object> discovered = CDI.current();
CDI.setCDIProvider(new MyCDIProvider(discovered));
CDI.current();
assertTrue(MyCDIProvider.isCalled);
try {
CustomCDIProvider.reset();
CDI.setCDIProvider(new CustomCDIProvider());
assertNull(CDI.current());
assertTrue(CustomCDIProvider.isCalled);
} finally {
// Unset the CDIProvider so that other tests are not affected
TestCDI.unsetCDIProvider();
}
}
}

This file was deleted.

0 comments on commit 1bca3e7

Please sign in to comment.