Skip to content

Commit

Permalink
Added tests for lifecycle event interceptor ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
jharting committed Jun 7, 2012
1 parent 4da2e16 commit b923e48
Show file tree
Hide file tree
Showing 8 changed files with 468 additions and 0 deletions.
@@ -0,0 +1,34 @@
/*
* 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.tests.interceptors.lifecycle;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.interceptor.InterceptorBinding;

@InterceptorBinding
@Inherited
@Target({ TYPE })
@Retention(RUNTIME)
public @interface Bar {

}
@@ -0,0 +1,74 @@
/*
* 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.tests.interceptors.lifecycle;

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

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Interceptor
@Bar
public class BarInterceptor {

private static boolean postConstructCalled;
private static boolean preDestroyCalled;

@PostConstruct
public void postConstruct(InvocationContext ctx) {
assertTrue(FooInterceptor.isPostConstructCalled());
assertFalse(BarInterceptor.isPostConstructCalled());
assertFalse(BazInterceptor.isPostConstructCalled());
assertFalse(Donkey.isPostConstructCalled());
postConstructCalled = true;
try {
ctx.proceed();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

@PreDestroy
public void preDestroy(InvocationContext ctx) {
assertTrue(FooInterceptor.isPreDestroyCalled());
assertFalse(BarInterceptor.isPreDestroyCalled());
assertFalse(BazInterceptor.isPreDestroyCalled());
assertFalse(Donkey.isPreDestroyCalled());
preDestroyCalled = true;
try {
ctx.proceed();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

public static boolean isPostConstructCalled() {
return postConstructCalled;
}

public static boolean isPreDestroyCalled() {
return preDestroyCalled;
}

public static void reset() {
postConstructCalled = false;
preDestroyCalled = false;
}
}
@@ -0,0 +1,34 @@
/*
* 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.tests.interceptors.lifecycle;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.interceptor.InterceptorBinding;

@InterceptorBinding
@Inherited
@Target({ TYPE })
@Retention(RUNTIME)
public @interface Baz {

}
@@ -0,0 +1,74 @@
/*
* 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.tests.interceptors.lifecycle;

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

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Interceptor
@Baz
public class BazInterceptor {

private static boolean postConstructCalled;
private static boolean preDestroyCalled;

@PostConstruct
public void postConstruct(InvocationContext ctx) {
assertTrue(FooInterceptor.isPostConstructCalled());
assertTrue(BarInterceptor.isPostConstructCalled());
assertFalse(BazInterceptor.isPostConstructCalled());
assertFalse(Donkey.isPostConstructCalled());
postConstructCalled = true;
try {
ctx.proceed();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

@PreDestroy
public void preDestroy(InvocationContext ctx) {
assertTrue(FooInterceptor.isPreDestroyCalled());
assertTrue(BarInterceptor.isPreDestroyCalled());
assertFalse(BazInterceptor.isPreDestroyCalled());
assertFalse(Donkey.isPreDestroyCalled());
preDestroyCalled = true;
try {
ctx.proceed();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

public static boolean isPostConstructCalled() {
return postConstructCalled;
}

public static boolean isPreDestroyCalled() {
return preDestroyCalled;
}

public static void reset() {
postConstructCalled = false;
preDestroyCalled = false;
}
}
@@ -0,0 +1,63 @@
/*
* 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.tests.interceptors.lifecycle;

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

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Foo
@Bar
@Baz
public class Donkey {

private static boolean postConstructCalled;
private static boolean preDestroyCalled;

@PostConstruct
public void postConstruct() {
assertTrue(FooInterceptor.isPostConstructCalled());
assertTrue(BarInterceptor.isPostConstructCalled());
assertTrue(BazInterceptor.isPostConstructCalled());
assertFalse(Donkey.isPostConstructCalled());
postConstructCalled = true;
}

@PreDestroy
public void preDestroy() {
assertTrue(FooInterceptor.isPreDestroyCalled());
assertTrue(BarInterceptor.isPreDestroyCalled());
assertTrue(BazInterceptor.isPreDestroyCalled());
assertFalse(Donkey.isPreDestroyCalled());
preDestroyCalled = true;
}

public static boolean isPostConstructCalled() {
return postConstructCalled;
}

public static boolean isPreDestroyCalled() {
return preDestroyCalled;
}

public static void reset() {
postConstructCalled = false;
preDestroyCalled = false;
}
}
@@ -0,0 +1,34 @@
/*
* 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.tests.interceptors.lifecycle;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.interceptor.InterceptorBinding;

@InterceptorBinding
@Inherited
@Target({ TYPE })
@Retention(RUNTIME)
public @interface Foo {

}
@@ -0,0 +1,73 @@
/*
* 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.tests.interceptors.lifecycle;

import static org.junit.Assert.assertFalse;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Interceptor
@Foo
public class FooInterceptor {

private static boolean postConstructCalled;
private static boolean preDestroyCalled;

@PostConstruct
public void postConstruct(InvocationContext ctx) {
assertFalse(FooInterceptor.isPostConstructCalled());
assertFalse(BarInterceptor.isPostConstructCalled());
assertFalse(BazInterceptor.isPostConstructCalled());
assertFalse(Donkey.isPostConstructCalled());
postConstructCalled = true;
try {
ctx.proceed();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

@PreDestroy
public void preDestroy(InvocationContext ctx) {
assertFalse(FooInterceptor.isPreDestroyCalled());
assertFalse(BarInterceptor.isPreDestroyCalled());
assertFalse(BazInterceptor.isPreDestroyCalled());
assertFalse(Donkey.isPreDestroyCalled());
preDestroyCalled = true;
try {
ctx.proceed();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

public static boolean isPostConstructCalled() {
return postConstructCalled;
}

public static boolean isPreDestroyCalled() {
return preDestroyCalled;
}

public static void reset() {
postConstructCalled = false;
preDestroyCalled = false;
}
}

0 comments on commit b923e48

Please sign in to comment.