View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.location.impl;
17  
18  import org.junit.Test;
19  import org.reflections.Reflections;
20  
21  import javax.persistence.Embeddable;
22  import javax.persistence.Entity;
23  import javax.persistence.MappedSuperclass;
24  import java.lang.reflect.Method;
25  import java.util.Set;
26  
27  import static org.junit.Assert.fail;
28  
29  /**
30   * This test verifies that all of the JPA objects in this module are statically weaved.
31   *
32   * <p>If one executes this test from within an IDE environment, it very well may not pass if one of the JPA objects
33   * under examination was modified and then recompiled by the IDE. The static weaving process is handled by Maven,
34   * so without executing the appropriate Maven lifecycle phase, the class will not get weaved. Regardless, this test
35   * should *always* pass when executed from the command line.</p>
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  public class StaticWeavingTest {
40  
41      @Test
42      public void testStaticWeaving() {
43          // first, scan for all files on the classpath with an @Entity or @MappedSuperClass annotation
44          Reflections reflections = new Reflections(getClass().getPackage().getName());
45          Set<Class<?>> entityTypes = reflections.getTypesAnnotatedWith(Entity.class);
46          Set<Class<?>> superTypes = reflections.getTypesAnnotatedWith(MappedSuperclass.class);
47          Set<Class<?>> embeddableTypes = reflections.getTypesAnnotatedWith(Embeddable.class);
48  
49          // next, let's assert that they have been statically weaved
50          assertStaticWeaved(entityTypes, superTypes, embeddableTypes);
51      }
52  
53      private void assertStaticWeaved(Set<Class<?>>... types) {
54          for (Set<Class<?>> typeSet : types) {
55              for (Class<?> type : typeSet) {
56                  boolean foundWeaved = false;
57                  Method[] methods = type.getDeclaredMethods();
58                  for (Method method : methods) {
59                      if (method.getName().startsWith("_persistence")) {
60                          foundWeaved = true;
61                          break;
62                      }
63                  }
64                  if (!foundWeaved) {
65                      fail("(NOTE: it is expected this test may fail if executed from the IDE instead of command line "
66                              + "since the IDE will not execute the static weaving automatically). Found a class which is "
67                              + "not bytecode weaved (contains no methods starting with '_persistence'): " + type + " "
68                              + "In order to resolve this, please ensure that this type is included in "
69                              + "META-INF/persistence-weaving.xml");
70                  }
71              }
72          }
73      }
74  
75  }