Debian Codegraph @

src/test/java/org/jboss/modules/ref/util/Assert.java

raw
1 /*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2014 Red Hat, Inc., and individual contributors
4 * as indicated by the @author tags.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.jboss.modules.ref.util;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertSame;
24 import static org.junit.Assert.fail;
25
26 import org.jboss.modules.ref.Reaper;
27 import org.jboss.modules.ref.Reference;
28 import org.jboss.modules.ref.Reference.Type;
29
30 /**
31 * Assertion methods used by reference tests.
32 *
33 * @author <a href="mailto:[email protected]">Flavia Rainone</a>
34 */
35 public class Assert {
36
37 /**
38 * Assert that {@code reference} contains {@code referent}, {@code attachment}, and that its type is {@code type}.
39 *
40 * @param <T> the type of referent
41 * @param <A> the type of attachment
42 * @param reference the reference that needs to be checked
43 * @param referent the expected content for {@code reference}
44 * @param attachment the expected attachment contained in {@code reference}
45 * @param type the expected type of {@code reference}
46 */
47 public static final <T, A> void assertReference(Reference<T, A> reference, T referent, A attachment, Type type) {
48 assertEquals(referent, reference.get());
49 assertEquals(attachment, reference.getAttachment());
50 assertSame(type, reference.getType());
51 assertNotNull(reference.toString()); // make sure that no exception is thrown and that null is never returned
52 }
53
54 /**
55 * Assert that {@code reference} contains {@code referent}, {@code attachment}, and that its type is {@code type}.
56 * Also asserts that {@code reaper} is the reaper used by {@code referent}.
57 * <p>
58 * Call this method only for {@link org.jboss.msc.ref.Reapable Reapable} references. This method assumes that
59 * {@code reference} has a method called {@code getReaper} that will return the reaper.
60 *
61 * @param <T> the type of referent
62 * @param <A> the type of attachment
63 * @param reference the {@code Reapable} reference that needs to be checked
64 * @param referent the expected content for {@code reference}
65 * @param attachment the expected attachment contained in {@code reference}
66 * @param type the expected type of {@code reference}
67 * @param reaper the expected reaper
68 */
69 public static final <T, A> void assertReference(Reference<T, A> reference, T referent, A attachment, Type type, Reaper<T, A> reaper) {
70 assertReference(reference, referent, attachment, type);
71 try {
72 assertEquals(reaper, reference.getClass().getMethod("getReaper").invoke(reference));
73 } catch (NoSuchMethodException e) {
74 fail("Can't find getReaper method at clas " + reference.getClass());
75 } catch (RuntimeException e) {
76 throw e;
77 }
78 catch (Exception e) {
79 fail(e.getMessage());
80 }
81 }
82 }