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 testLocalSessionFactoryBeanWithTransactionAwareDataSource() throws Exception {
    final DriverManagerDataSource ds = new DriverManagerDataSource();
    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(
                TransactionAwareDataSourceConnectionProvider.class.getName(),
                config.getProperty(Environment.CONNECTION_PROVIDER));
            assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource());
            invocations.add("newSessionFactory");
            return null;
          }
        };
    sfb.setDataSource(ds);
    sfb.setUseTransactionAwareDataSource(true);
    sfb.afterPropertiesSet();
    assertTrue(sfb.getConfiguration() != null);
    assertEquals("newSessionFactory", invocations.get(0));
  }
 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()));
   }
 }
  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 testLocalSessionFactoryBeanWithDataSourceAndMappingJarLocations() throws Exception {
    final DriverManagerDataSource ds = new DriverManagerDataSource();
    final Set invocations = new HashSet();
    LocalSessionFactoryBean sfb =
        new LocalSessionFactoryBean() {
          protected Configuration newConfiguration() {
            return new Configuration() {
              public Configuration addJar(File file) {
                invocations.add("addResource " + file.getPath());
                return this;
              }
            };
          }

          protected SessionFactory newSessionFactory(Configuration config) {
            assertEquals(
                LocalDataSourceConnectionProvider.class.getName(),
                config.getProperty(Environment.CONNECTION_PROVIDER));
            assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource());
            invocations.add("newSessionFactory");
            return null;
          }
        };
    sfb.setMappingJarLocations(
        new Resource[] {
          new FileSystemResource("mapping.hbm.jar"), new FileSystemResource("mapping2.hbm.jar")
        });
    sfb.setDataSource(ds);
    sfb.afterPropertiesSet();
    assertTrue(sfb.getConfiguration() != null);
    assertTrue(invocations.contains("addResource mapping.hbm.jar"));
    assertTrue(invocations.contains("addResource mapping2.hbm.jar"));
    assertTrue(invocations.contains("newSessionFactory"));
  }
 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
   }
 }
 private SessionFactory createTestSessionFactory() throws Exception {
   // create a FactoryBean to help create a Hibernate SessionFactory
   LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
   factoryBean.setDataSource(createTestDataSource());
   Resource[] mappingLocations =
       new ClassPathResource[] {
         new ClassPathResource("Account.hbm.xml", Account.class),
         new ClassPathResource("Beneficiary.hbm.xml", Beneficiary.class)
       };
   factoryBean.setMappingLocations(mappingLocations);
   factoryBean.setHibernateProperties(createHibernateProperties());
   // initialize according to the Spring InitializingBean contract
   factoryBean.afterPropertiesSet();
   // get the created session factory
   return (SessionFactory) factoryBean.getObject();
 }
  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 testLocalSessionFactoryBeanWithDataSourceAndProperties() throws Exception {
    final DriverManagerDataSource ds = new DriverManagerDataSource();
    final Set invocations = new HashSet();
    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("myValue", config.getProperty("myProperty"));
            invocations.add("newSessionFactory");
            return null;
          }
        };
    sfb.setMappingLocations(
        new Resource[] {new ClassPathResource("/org/springframework/beans/factory/xml/test.xml")});
    sfb.setDataSource(ds);
    Properties prop = new Properties();
    prop.setProperty(Environment.CONNECTION_PROVIDER, "myClass");
    prop.setProperty("myProperty", "myValue");
    sfb.setHibernateProperties(prop);
    sfb.afterPropertiesSet();
    assertTrue(sfb.getConfiguration() != null);
    assertTrue(invocations.contains("addResource"));
    assertTrue(invocations.contains("newSessionFactory"));
  }
  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 testLocalSessionFactoryBeanWithValidProperties() throws Exception {
   final Set invocations = new HashSet();
   LocalSessionFactoryBean sfb =
       new LocalSessionFactoryBean() {
         protected SessionFactory newSessionFactory(Configuration config) {
           assertEquals(
               UserSuppliedConnectionProvider.class.getName(),
               config.getProperty(Environment.CONNECTION_PROVIDER));
           assertEquals("myValue", config.getProperty("myProperty"));
           invocations.add("newSessionFactory");
           return null;
         }
       };
   Properties prop = new Properties();
   prop.setProperty(
       Environment.CONNECTION_PROVIDER, UserSuppliedConnectionProvider.class.getName());
   prop.setProperty("myProperty", "myValue");
   sfb.setHibernateProperties(prop);
   sfb.afterPropertiesSet();
   assertTrue(sfb.getConfiguration() != null);
   assertTrue(invocations.contains("newSessionFactory"));
 }
 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()));
   }
 }