View Javadoc
1   /**
2    * Copyright 2005-2015 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.krad;
17  
18  import org.junit.Test;
19  import org.kuali.rice.krad.bo.DocumentAttachment;
20  import org.kuali.rice.krad.document.DocumentBase;
21  import org.kuali.rice.krad.maintenance.MaintenanceLock;
22  import org.kuali.rice.krad.messages.Message;
23  import org.reflections.Reflections;
24  
25  import javax.persistence.Embeddable;
26  import javax.persistence.Entity;
27  import javax.persistence.MappedSuperclass;
28  import java.lang.reflect.Method;
29  import java.util.Set;
30  
31  import static org.junit.Assert.fail;
32  
33  /**
34   * This test verifies that all of the JPA objects in this module are statically weaved.
35   *
36   * <p>If one executes this test from within an IDE environment, it very well may not pass if one of the JPA objects
37   * under examination was modified and then recompiled by the IDE. The static weaving process is handled by Maven,
38   * so without executing the appropriate Maven lifecycle phase, the class will not get weaved. Regardless, this test
39   * should *always* pass when executed from the command line.</p>
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public class StaticWeavingTest {
44  
45      // because a sample app package conflicts with our main package we are scanning, let's exclude those from
46      // this analysis
47      private static final String TEST_PACKAGE_PREFIX = "org.kuali.rice.krad.sampleapp";
48  
49      @Test
50      public void testStaticWeaving() {
51          // first, scan for all files on the classpath with an @Entity or @MappedSuperClass annotation
52          Reflections reflections = new Reflections(
53                  DocumentAttachment.class.getPackage().getName(),
54                  DocumentBase.class.getPackage().getName(),
55                  MaintenanceLock.class.getPackage().getName(),
56                  Message.class.getPackage().getName());
57          Set<Class<?>> entityTypes = reflections.getTypesAnnotatedWith(Entity.class);
58          Set<Class<?>> superTypes = reflections.getTypesAnnotatedWith(MappedSuperclass.class);
59          Set<Class<?>> embeddableTypes = reflections.getTypesAnnotatedWith(Embeddable.class);
60  
61          // next, let's assert that they have been statically weaved
62          assertStaticWeaved(entityTypes, superTypes, embeddableTypes);
63      }
64  
65      private void assertStaticWeaved(Set<Class<?>>... types) {
66          for (Set<Class<?>> typeSet : types) {
67              for (Class<?> type : typeSet) {
68                  if (type.getName().startsWith(TEST_PACKAGE_PREFIX)) {
69                      continue;
70                  }
71                  boolean foundWeaved = false;
72                  Method[] methods = type.getDeclaredMethods();
73                  for (Method method : methods) {
74                      if (method.getName().startsWith("_persistence")) {
75                          foundWeaved = true;
76                          break;
77                      }
78                  }
79                  if (!foundWeaved) {
80                      fail("(NOTE: it is expected this test may fail if executed from the IDE instead of command line "
81                              + "since the IDE will not execute the static weaving automatically). Found a class which is "
82                              + "not bytecode weaved (contains no methods starting with '_persistence'): " + type + " "
83                              + "In order to resolve this, please ensure that this type is included in "
84                              + "META-INF/persistence-weaving.xml");
85                  }
86              }
87          }
88      }
89  
90  }