public void testLocalSessionFactoryBeanWithEventListeners() throws Exception {
    final Map registeredListeners = new HashMap();
    LocalSessionFactoryBean sfb =
        new LocalSessionFactoryBean() {
          protected Configuration newConfiguration() {
            return new Configuration() {
              public void setListener(String type, Object listener) {
                registeredListeners.put(type, listener);
              }
            };
          }

          protected SessionFactory newSessionFactory(Configuration config) {
            return null;
          }
        };
    sfb.setMappingResources(new String[0]);
    sfb.setDataSource(new DriverManagerDataSource());
    Map listeners = new HashMap();
    listeners.put("flush", "myListener");
    listeners.put("create", "yourListener");
    sfb.setEventListeners(listeners);
    sfb.afterPropertiesSet();
    assertEquals(listeners, registeredListeners);
  }
  public void testLocalSessionFactoryBeanWithEventListenerSet() throws Exception {
    final Map registeredListeners = new HashMap();
    LocalSessionFactoryBean sfb =
        new LocalSessionFactoryBean() {
          protected Configuration newConfiguration() {
            return new Configuration() {
              public void setListeners(String type, Object[] listeners) {
                assertTrue(listeners instanceof MergeEventListener[]);
                registeredListeners.put(type, new HashSet(Arrays.asList(listeners)));
              }
            };
          }

          protected SessionFactory newSessionFactory(Configuration config) {
            return null;
          }
        };
    sfb.setMappingResources(new String[0]);
    sfb.setDataSource(new DriverManagerDataSource());
    Map listeners = new HashMap();
    Set mergeSet = new HashSet();
    mergeSet.add(new DummyMergeEventListener());
    mergeSet.add(new DummyMergeEventListener());
    listeners.put("merge", mergeSet);
    sfb.setEventListeners(listeners);
    sfb.afterPropertiesSet();
    assertEquals(listeners, registeredListeners);
  }
 public void testLocalSessionFactoryBeanWithEntityInterceptor() throws Exception {
   LocalSessionFactoryBean sfb =
       new LocalSessionFactoryBean() {
         protected Configuration newConfiguration() {
           return new Configuration() {
             public Configuration setInterceptor(Interceptor interceptor) {
               throw new IllegalArgumentException(interceptor.toString());
             }
           };
         }
       };
   sfb.setMappingResources(new String[0]);
   sfb.setDataSource(new DriverManagerDataSource());
   MockControl interceptorControl = MockControl.createControl(Interceptor.class);
   Interceptor entityInterceptor = (Interceptor) interceptorControl.getMock();
   interceptorControl.replay();
   sfb.setEntityInterceptor(entityInterceptor);
   try {
     sfb.afterPropertiesSet();
     fail("Should have thrown IllegalArgumentException");
   } catch (IllegalArgumentException ex) {
     // expected
     assertTrue("Correct exception", ex.getMessage().equals(entityInterceptor.toString()));
   }
 }
  /**
   * Collect the mapping resources for future use because the mappingResources object is defined as
   * 'private' instead of 'protected'
   *
   * @see
   *     org.springframework.orm.hibernate3.LocalSessionFactoryBean#setMappingResources(java.lang.String[])
   */
  @Override
  public void setMappingResources(String[] mappingResources) {
    for (String resource : mappingResources) {
      tmpMappingResources.add(resource);
    }

    super.setMappingResources(tmpMappingResources.toArray(new String[] {}));
  }
 public void testLocalSessionFactoryBeanWithInvalidMappings() throws Exception {
   LocalSessionFactoryBean sfb = new LocalSessionFactoryBean();
   sfb.setMappingResources(new String[] {"mapping.hbm.xml"});
   try {
     sfb.afterPropertiesSet();
   } catch (IOException ex) {
     // expected, mapping resource not found
   }
 }
  /* (non-Javadoc)
   * @see org.springframework.orm.hibernate3.AbstractSessionFactoryBean#afterPropertiesSet()
   */
  @Override
  public void afterPropertiesSet() throws Exception {
    // adding each module's mapping file to the list of mapping resources
    super.setMappingResources(getModuleMappingResources().toArray(new String[] {}));

    // just check for testing module's hbm files here?

    super.afterPropertiesSet();
  }
 public void testLocalSessionFactoryBeanWithInvalidProperties() throws Exception {
   LocalSessionFactoryBean sfb = new LocalSessionFactoryBean();
   sfb.setMappingResources(new String[0]);
   Properties prop = new Properties();
   prop.setProperty(Environment.CONNECTION_PROVIDER, "myClass");
   sfb.setHibernateProperties(prop);
   try {
     sfb.afterPropertiesSet();
   } catch (HibernateException ex) {
     // expected, provider class not found
   }
 }
  public void testLocalSessionFactoryBeanWithDataSourceAndMappingResources() throws Exception {
    final DriverManagerDataSource ds = new DriverManagerDataSource();
    MockControl tmControl = MockControl.createControl(TransactionManager.class);
    final TransactionManager tm = (TransactionManager) tmControl.getMock();
    final List invocations = new ArrayList();
    LocalSessionFactoryBean sfb =
        new LocalSessionFactoryBean() {
          protected Configuration newConfiguration() {
            return new Configuration() {
              public Configuration addInputStream(InputStream is) {
                try {
                  is.close();
                } catch (IOException ex) {
                }
                invocations.add("addResource");
                return this;
              }
            };
          }

          protected SessionFactory newSessionFactory(Configuration config) {
            assertEquals(
                LocalDataSourceConnectionProvider.class.getName(),
                config.getProperty(Environment.CONNECTION_PROVIDER));
            assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource());
            assertEquals(
                LocalTransactionManagerLookup.class.getName(),
                config.getProperty(Environment.TRANSACTION_MANAGER_STRATEGY));
            assertEquals(tm, LocalSessionFactoryBean.getConfigTimeTransactionManager());
            invocations.add("newSessionFactory");
            return null;
          }
        };
    sfb.setMappingResources(
        new String[] {
          "/org/springframework/beans/factory/xml/test.xml",
          "/org/springframework/beans/factory/xml/child.xml"
        });
    sfb.setDataSource(ds);
    sfb.setJtaTransactionManager(tm);
    sfb.afterPropertiesSet();
    assertTrue(sfb.getConfiguration() != null);
    assertEquals("addResource", invocations.get(0));
    assertEquals("addResource", invocations.get(1));
    assertEquals("newSessionFactory", invocations.get(2));
  }
 public void testLocalSessionFactoryBeanWithCustomSessionFactory() throws Exception {
   MockControl factoryControl = MockControl.createControl(SessionFactory.class);
   final SessionFactory sessionFactory = (SessionFactory) factoryControl.getMock();
   sessionFactory.close();
   factoryControl.setVoidCallable(1);
   factoryControl.replay();
   LocalSessionFactoryBean sfb =
       new LocalSessionFactoryBean() {
         protected SessionFactory newSessionFactory(Configuration config) {
           return sessionFactory;
         }
       };
   sfb.setMappingResources(new String[0]);
   sfb.setDataSource(new DriverManagerDataSource());
   sfb.setExposeTransactionAwareSessionFactory(false);
   sfb.afterPropertiesSet();
   assertTrue(sessionFactory == sfb.getObject());
   sfb.destroy();
   factoryControl.verify();
 }
  public void testLocalSessionFactoryBeanWithCacheStrategies() throws Exception {
    final Properties registeredClassCache = new Properties();
    final Properties registeredCollectionCache = new Properties();
    LocalSessionFactoryBean sfb =
        new LocalSessionFactoryBean() {
          protected Configuration newConfiguration() {
            return new Configuration() {
              public Configuration setCacheConcurrencyStrategy(
                  String clazz, String concurrencyStrategy) {
                registeredClassCache.setProperty(clazz, concurrencyStrategy);
                return this;
              }

              public Configuration setCollectionCacheConcurrencyStrategy(
                  String collectionRole, String concurrencyStrategy) {
                registeredCollectionCache.setProperty(collectionRole, concurrencyStrategy);
                return this;
              }
            };
          }

          protected SessionFactory newSessionFactory(Configuration config) {
            return null;
          }
        };

    sfb.setMappingResources(new String[0]);
    sfb.setDataSource(new DriverManagerDataSource());
    Properties classCache = new Properties();
    classCache.setProperty("org.springframework.beans.TestBean", "read-write");
    sfb.setEntityCacheStrategies(classCache);
    Properties collectionCache = new Properties();
    collectionCache.setProperty("org.springframework.beans.TestBean.friends", "read-only");
    sfb.setCollectionCacheStrategies(collectionCache);
    sfb.afterPropertiesSet();

    assertEquals(classCache, registeredClassCache);
    assertEquals(collectionCache, registeredCollectionCache);
  }
 public void testLocalSessionFactoryBeanWithNamingStrategy() throws Exception {
   LocalSessionFactoryBean sfb =
       new LocalSessionFactoryBean() {
         protected Configuration newConfiguration() {
           return new Configuration() {
             public Configuration setNamingStrategy(NamingStrategy namingStrategy) {
               throw new IllegalArgumentException(namingStrategy.toString());
             }
           };
         }
       };
   sfb.setMappingResources(new String[0]);
   sfb.setDataSource(new DriverManagerDataSource());
   sfb.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
   try {
     sfb.afterPropertiesSet();
     fail("Should have thrown IllegalArgumentException");
   } catch (IllegalArgumentException ex) {
     // expected
     assertTrue(
         "Correct exception", ex.getMessage().equals(ImprovedNamingStrategy.INSTANCE.toString()));
   }
 }