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 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 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 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(); }
@Before public void recreateSchema() { logger.info("**RECREATION SCHEMA**"); factory.dropDatabaseSchema(); factory.createDatabaseSchema(); createDatabaseDefaults(); if (printSql) logger.info("*************BEGIN***************"); }
public static void main(String[] args) { FileSystemXmlApplicationContext appContext = new FileSystemXmlApplicationContext("database/spring/applicationContext.xml"); LocalSessionFactoryBean sessionFactory = (LocalSessionFactoryBean) appContext.getBean("&sessionFactory", LocalSessionFactoryBean.class); sessionFactory.updateDatabaseSchema(); }
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 } }
/** * 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[] {})); }
/** @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#destroy() */ @Override public void destroy() throws HibernateException { try { super.destroy(); } catch (IllegalStateException e) { // ignore errors sometimes thrown by the CacheManager trying to shut down twice // see net.sf.ehcache.CacheManager#removeShutdownHook() } }
@Override public void configure(Properties props) throws HibernateException { this.dataSource = LocalSessionFactoryBean.getConfigTimeDataSource(); // absolutely needs thread-bound DataSource to initialize if (this.dataSource == null) { throw new HibernateException( "No local DataSource found for configuration - " + "'dataSource' property must be set on LocalSessionFactoryBean"); } this.dataSourceToUse = getDataSourceToUse(this.dataSource); }
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 static ProcessEngine create(final ConfigurationImpl configuration) { CharmsProcessEngine processEngine = null; ApplicationContext applicationContext = null; applicationContext = (ApplicationContext) configuration.getApplicationContext(); processEngine = new CharmsProcessEngine(); processEngine.applicationContext = applicationContext; processEngine.initializeProcessEngine(configuration); final LocalSessionFactoryBean localSessionFactoryBean = processEngine.get(LocalSessionFactoryBean.class); final Configuration hibernateConfiguration = localSessionFactoryBean.getConfiguration(); processEngine .processEngineWireContext .getWireDefinition() .addDescriptor(new ProvidedObjectDescriptor(hibernateConfiguration, true)); // processEngine.checkDb(configuration); return processEngine; }
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())); } }
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 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 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 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); }
/** * Used by the module testing framework to set the dependent modules in the hibernate session * factory * * @see * org.springframework.orm.hibernate3.LocalSessionFactoryBean#setMappingJarLocations(org.springframework.core.io.Resource[]) */ @Override public void setMappingJarLocations(Resource[] mappingJarLocations) { super.setMappingJarLocations(mappingJarLocations); }
private String getSchema() { LocalSessionFactoryBean sessionFactory = (LocalSessionFactoryBean) appContext.getBean("&sessionFactory"); Properties props = sessionFactory.getConfiguration().getProperties(); return props.getProperty("hibernate.default_schema"); }