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 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 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"));
 }
Esempio n. 6
0
  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;
  }
Esempio n. 7
0
 private String getSchema() {
   LocalSessionFactoryBean sessionFactory =
       (LocalSessionFactoryBean) appContext.getBean("&sessionFactory");
   Properties props = sessionFactory.getConfiguration().getProperties();
   return props.getProperty("hibernate.default_schema");
 }