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

mockit.internal.injection.TestDataSource Maven / Gradle / Ivy

Go to download

JMockit is a Java toolkit for automated developer testing. It contains mocking/faking APIs and a code coverage tool, supporting both JUnit and TestNG. The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested in isolation from selected dependencies.

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.injection;

import java.beans.*;
import java.lang.annotation.*;
import java.lang.reflect.*;
import javax.annotation.*;
import javax.annotation.sql.*;
import javax.sql.*;

final class TestDataSource
{
   @Nullable private final String dsName;
   private Class dsClass;
   private CommonDataSource ds;

   TestDataSource(@Nonnull InjectionPoint injectionPoint) { dsName = injectionPoint.name; }

   @Nullable
   CommonDataSource createIfDataSourceDefinitionAvailable(@Nonnull TestedClass testedClass)
   {
      if (dsName == null) {
         return null;
      }

      Class targetClass = testedClass.targetClass;

      do {
         createDataSource(targetClass);

         if (ds != null) {
            return ds;
         }

         targetClass = targetClass.getSuperclass();
      }
      while (targetClass != Object.class);

      throw new IllegalStateException(
         "Missing @DataSourceDefinition of name \"" + dsName + "\" on " + testedClass.nameOfTestedClass);
   }

   private void createDataSource(@Nonnull Class targetClass)
   {
      for (Annotation annotation : targetClass.getDeclaredAnnotations()) {
         String annotationName = annotation.annotationType().getName();

         if ("javax.annotation.sql.DataSourceDefinitions".equals(annotationName)) {
            createDataSource((DataSourceDefinitions) annotation);
         }
         else if ("javax.annotation.sql.DataSourceDefinition".equals(annotationName)) {
            createDataSource((DataSourceDefinition) annotation);
         }

         if (ds != null) {
            return;
         }
      }
   }

   private void createDataSource(@Nonnull DataSourceDefinitions dsDefs)
   {
      for (DataSourceDefinition dsDef : dsDefs.value()) {
         createDataSource(dsDef);

         if (ds != null) {
            return;
         }
      }
   }

   private void createDataSource(@Nonnull DataSourceDefinition dsDef)
   {
      String configuredDataSourceName = dsDef.name();

      if (configuredDataSourceName.equals(dsName)) {
         instantiateConfiguredDataSourceClass(dsDef);
         setDataSourcePropertiesFromConfiguredValues(dsDef);
      }
   }

   private void instantiateConfiguredDataSourceClass(@Nonnull DataSourceDefinition dsDef)
   {
      String className = dsDef.className();

      try {
         //noinspection unchecked
         dsClass = (Class) Class.forName(className);
         ds = dsClass.newInstance();
      }
      catch (ClassNotFoundException e) { throw new RuntimeException(e); }
      catch (InstantiationException e) { throw new RuntimeException(e.getCause()); }
      catch (IllegalAccessException e) { throw new RuntimeException(e); }
   }

   private void setDataSourcePropertiesFromConfiguredValues(@Nonnull DataSourceDefinition dsDef)
   {
      try {
         BeanInfo beanInfo = Introspector.getBeanInfo(dsClass, Object.class);
         PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();

         setProperty(properties, "url", dsDef.url());
         setProperty(properties, "user", dsDef.user());
         setProperty(properties, "password", dsDef.password());
      }
      catch (IntrospectionException e) { throw new RuntimeException(e); }
      catch (IllegalAccessException e) { throw new RuntimeException(e); }
      catch (InvocationTargetException e) { throw new RuntimeException(e); }
   }

   private void setProperty(@Nonnull PropertyDescriptor[] properties, @Nonnull String name, @Nonnull String value)
      throws InvocationTargetException, IllegalAccessException
   {
      for (PropertyDescriptor property : properties) {
         if (property.getName().equals(name)) {
            property.getWriteMethod().invoke(ds, value);
            return;
         }
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy