All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mockit.internal.expectations.injection.JPADependencies Maven / Gradle / Ivy

Go to download

JMockit is a Java toolkit for automated developer testing. It contains APIs for the creation of the objects to be tested, for mocking dependencies, and for faking external APIs; JUnit (4 & 5) and TestNG test runners are supported. It also contains an advanced code coverage tool.

There is a newer version: 1.49
Show newest version
/*
 * Copyright (c) 2006 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.expectations.injection;

import java.io.*;
import java.lang.annotation.*;
import javax.annotation.*;
import javax.persistence.*;
import javax.xml.parsers.*;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

/**
 * Detects and resolves dependencies belonging to the {@code javax.persistence} API, namely {@code EntityManagerFactory}
 * and {@code EntityManager}.
 */
final class JPADependencies
{
   static boolean isApplicable(@Nonnull Class dependencyType)
   {
      return dependencyType == EntityManager.class || dependencyType == EntityManagerFactory.class;
   }

   @Nullable
   static String getDependencyIdIfAvailable(@Nonnull Annotation annotation)
   {
      Class annotationType = annotation.annotationType();

      if (annotationType == PersistenceUnit.class) {
         return ((PersistenceUnit) annotation).unitName();
      }
      else if (annotationType == PersistenceContext.class) {
         return  ((PersistenceContext) annotation).unitName();
      }

      return null;
   }

   @Nonnull private final InjectionState injectionState;
   @Nullable private String defaultPersistenceUnitName;

   JPADependencies(@Nonnull InjectionState injectionState) { this.injectionState = injectionState; }

   @Nullable
   Object newInstanceIfApplicable(@Nonnull Class dependencyType, @Nonnull Object dependencyKey)
   {
      if (dependencyType == EntityManagerFactory.class) {
         String persistenceUnitName;

         if (dependencyKey instanceof String) {
            persistenceUnitName = extractIdFromDependencyKey((String) dependencyKey);
         }
         else {
            persistenceUnitName = discoverNameOfDefaultPersistenceUnit();
         }

         EntityManagerFactory emFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
         injectionState.saveGlobalDependency(dependencyKey, emFactory);
         return emFactory;
      }

      if (dependencyType == EntityManager.class) {
         return findOrCreateEntityManager(dependencyKey);
      }

      return null;
   }

   @Nonnull
   private static String extractIdFromDependencyKey(@Nonnull String dependencyKey)
   {
      int p = dependencyKey.indexOf(':');
      return dependencyKey.substring(p + 1);
   }

   @Nonnull
   private String discoverNameOfDefaultPersistenceUnit()
   {
      if (defaultPersistenceUnitName != null) {
         return defaultPersistenceUnitName;
      }

      defaultPersistenceUnitName = "";
      InputStream xmlFile = getClass().getResourceAsStream("/META-INF/persistence.xml");

      if (xmlFile != null) {
         try {
            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
            parser.parse(xmlFile, new DefaultHandler() {
               @Override
               public void startElement(String uri, String localName, String qName, Attributes attributes)
               {
                  if ("persistence-unit".equals(qName)) {
                     defaultPersistenceUnitName = attributes.getValue("name");
                  }
               }
            });
            xmlFile.close();
         }
         catch (ParserConfigurationException ignore) {}
         catch (SAXException ignore) {}
         catch (IOException ignore) {}
      }

      return defaultPersistenceUnitName;
   }

   @Nullable
   private EntityManager findOrCreateEntityManager(@Nonnull Object dependencyKey)
   {
      String persistenceUnitName;
      Object emFactoryKey;

      if (dependencyKey instanceof String) {
         persistenceUnitName = extractIdFromDependencyKey((String) dependencyKey);
         emFactoryKey = EntityManagerFactory.class.getName() + ':' + persistenceUnitName;
      }
      else {
         persistenceUnitName = null;
         emFactoryKey = EntityManagerFactory.class;
      }

      EntityManagerFactory emFactory = injectionState.getGlobalDependency(emFactoryKey);

      if (emFactory == null) {
         if (persistenceUnitName == null) {
            persistenceUnitName = discoverNameOfDefaultPersistenceUnit();
         }

         emFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
         injectionState.saveGlobalDependency(emFactoryKey, emFactory);
      }

      return emFactory.createEntityManager();
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy