private void loadBeanDefinitionsFromImportedResources(Map<String, Class<?>> importedResources) {
    Map<Class<?>, BeanDefinitionReader> readerInstanceCache =
        new HashMap<Class<?>, BeanDefinitionReader>();
    for (Map.Entry<String, Class<?>> entry : importedResources.entrySet()) {
      String resource = entry.getKey();
      Class<?> readerClass = entry.getValue();
      if (!readerInstanceCache.containsKey(readerClass)) {
        try {
          // Instantiate the specified BeanDefinitionReader
          BeanDefinitionReader readerInstance =
              (BeanDefinitionReader)
                  readerClass
                      .getConstructor(BeanDefinitionRegistry.class)
                      .newInstance(this.registry);

          // Delegate the current ResourceLoader to it if possible
          if (readerInstance instanceof AbstractBeanDefinitionReader) {
            ((AbstractBeanDefinitionReader) readerInstance).setResourceLoader(this.resourceLoader);
          }

          readerInstanceCache.put(readerClass, readerInstance);
        } catch (Exception ex) {
          throw new IllegalStateException(
              "Could not instantiate BeanDefinitionReader class [" + readerClass.getName() + "]");
        }
      }
      BeanDefinitionReader reader = readerInstanceCache.get(readerClass);
      // TODO SPR-6310: qualify relatively pathed locations as done in
      // AbstractContextLoader.modifyLocations
      reader.loadBeanDefinitions(resource);
    }
  }
  /** @see DATAMONGO-280 */
  @Test
  @SuppressWarnings("deprecation")
  public void parsesMaxAutoConnectRetryTimeCorrectly() {

    reader.loadBeanDefinitions(new ClassPathResource("namespace/db-factory-bean.xml"));
    Mongo mongo = factory.getBean(Mongo.class);
    assertThat(
        ReflectiveMongoOptionsInvokerTestUtil.getMaxAutoConnectRetryTime(mongo.getMongoOptions()),
        is(27L));
  }
  /** @see DATAMONGO-1218 */
  @Test
  public void setsUpMongoDbFactoryUsingAMongoClientUri() {

    reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-client-uri.xml"));
    BeanDefinition definition = factory.getBeanDefinition("mongoDbFactory");
    ConstructorArgumentValues constructorArguments = definition.getConstructorArgumentValues();

    assertThat(constructorArguments.getArgumentCount(), is(1));
    ValueHolder argument = constructorArguments.getArgumentValue(0, MongoClientURI.class);
    assertThat(argument, is(notNullValue()));
  }
  /** @see DATAMONGO-306 */
  @Test
  public void setsUpMongoDbFactoryUsingAMongoUriWithoutCredentials() {

    reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-uri-no-credentials.xml"));
    BeanDefinition definition = factory.getBeanDefinition("mongoDbFactory");
    ConstructorArgumentValues constructorArguments = definition.getConstructorArgumentValues();

    assertThat(constructorArguments.getArgumentCount(), is(1));
    ValueHolder argument = constructorArguments.getArgumentValue(0, MongoURI.class);
    assertThat(argument, is(notNullValue()));

    MongoDbFactory dbFactory = factory.getBean("mongoDbFactory", MongoDbFactory.class);
    DB db = dbFactory.getDb();
    assertThat(db.getName(), is("database"));
  }
  private void loadBeanDefinitionsFromImportedResources(
      Map<String, Class<? extends BeanDefinitionReader>> importedResources) {

    Map<Class<?>, BeanDefinitionReader> readerInstanceCache =
        new HashMap<Class<?>, BeanDefinitionReader>();

    for (Map.Entry<String, Class<? extends BeanDefinitionReader>> entry :
        importedResources.entrySet()) {
      String resource = entry.getKey();
      Class<? extends BeanDefinitionReader> readerClass = entry.getValue();

      // Default reader selection necessary?
      if (BeanDefinitionReader.class == readerClass) {
        if (StringUtils.endsWithIgnoreCase(resource, ".groovy")) {
          // When clearly asking for Groovy, that's what they'll get...
          readerClass = GroovyBeanDefinitionReader.class;
        } else {
          // Primarily ".xml" files but for any other extension as well
          readerClass = XmlBeanDefinitionReader.class;
        }
      }

      BeanDefinitionReader reader = readerInstanceCache.get(readerClass);
      if (reader == null) {
        try {
          // Instantiate the specified BeanDefinitionReader
          reader =
              readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(this.registry);
          // Delegate the current ResourceLoader to it if possible
          if (reader instanceof AbstractBeanDefinitionReader) {
            AbstractBeanDefinitionReader abdr = ((AbstractBeanDefinitionReader) reader);
            abdr.setResourceLoader(this.resourceLoader);
            abdr.setEnvironment(this.environment);
          }
          readerInstanceCache.put(readerClass, reader);
        } catch (Exception ex) {
          throw new IllegalStateException(
              "Could not instantiate BeanDefinitionReader class [" + readerClass.getName() + "]");
        }
      }

      // TODO SPR-6310: qualify relative path locations as done in
      // AbstractContextLoader.modifyLocations
      reader.loadBeanDefinitions(resource);
    }
  }
 /** @see DATAMONGO-1218 */
 @Test(expected = BeanDefinitionParsingException.class)
 public void rejectsClientUriPlusDetailedConfiguration() {
   reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-client-uri-and-details.xml"));
 }
  @Test
  public void createsDbFactoryBean() {

    reader.loadBeanDefinitions(new ClassPathResource("namespace/db-factory-bean.xml"));
    factory.getBean("first");
  }