Search in sources :

Example 1 with ClassLoaderServiceImpl

use of org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl in project hibernate-orm by hibernate.

the class AbstractEnhancerTestTask method prepare.

public final void prepare(Configuration config) {
    config.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
    Class<?>[] resources = getAnnotatedClasses();
    for (Class<?> resource : resources) {
        config.addAnnotatedClass(resource);
    }
    StandardServiceRegistryBuilder serviceBuilder = new StandardServiceRegistryBuilder();
    serviceBuilder.addService(ClassLoaderService.class, new ClassLoaderServiceImpl(Thread.currentThread().getContextClassLoader()));
    serviceBuilder.applySettings(config.getProperties());
    serviceRegistry = serviceBuilder.build();
    factory = config.buildSessionFactory(serviceRegistry);
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) ClassLoaderServiceImpl(org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl)

Example 2 with ClassLoaderServiceImpl

use of org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl in project hibernate-orm by hibernate.

the class ClassLoaderServiceImplTest method testSystemClassLoaderNotOverriding.

/**
 * Test for bug: HHH-7084
 */
@Test
public void testSystemClassLoaderNotOverriding() throws IOException, ClassNotFoundException {
    Class<?> testClass = Entity.class;
    // Check that class is accessible by SystemClassLoader.
    ClassLoader.getSystemClassLoader().loadClass(testClass.getName());
    // Create ClassLoader with overridden class.
    TestClassLoader anotherLoader = new TestClassLoader();
    anotherLoader.overrideClass(testClass);
    Class<?> anotherClass = anotherLoader.loadClass(testClass.getName());
    Assert.assertNotSame(testClass, anotherClass);
    // Check ClassLoaderServiceImpl().classForName() returns correct class (not from current ClassLoader).
    ClassLoaderServiceImpl loaderService = new ClassLoaderServiceImpl(anotherLoader);
    Class<Object> objectClass = loaderService.classForName(testClass.getName());
    Assert.assertSame("Should not return class loaded from the parent classloader of ClassLoaderServiceImpl", objectClass, anotherClass);
}
Also used : Entity(javax.persistence.Entity) ClassLoaderServiceImpl(org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl) Test(org.junit.Test)

Example 3 with ClassLoaderServiceImpl

use of org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl in project hibernate-orm by hibernate.

the class AbstractCachingTestTask method prepare.

@Override
public void prepare() {
    StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder();
    registryBuilder.applySetting(AvailableSettings.GENERATE_STATISTICS, "true");
    registryBuilder.applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop");
    registryBuilder.applySetting(AvailableSettings.USE_SECOND_LEVEL_CACHE, "true");
    registryBuilder.addService(ClassLoaderService.class, new ClassLoaderServiceImpl(Thread.currentThread().getContextClassLoader()));
    StandardServiceRegistry registry = registryBuilder.build();
    MetadataSources metadataSources = new MetadataSources(registry);
    metadataSources.addAnnotatedClass(Document.class);
    sessionFactory = (SessionFactoryImplementor) metadataSources.buildMetadata().buildSessionFactory();
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) ClassLoaderServiceImpl(org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry)

Example 4 with ClassLoaderServiceImpl

use of org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl in project hibernate-orm by hibernate.

the class MapProxyTest method shouldGenerateClassWithAppropriateAccessorsForBoolean.

@Test
public void shouldGenerateClassWithAppropriateAccessorsForBoolean() throws Exception {
    //given
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("checkbox", true);
    Map<String, Class<?>> properties = new HashMap<String, Class<?>>();
    properties.put("checkbox", Boolean.class);
    //when
    Class testClass = MapProxyTool.classForName("TestClass3", properties, new ClassLoaderServiceImpl());
    Object testClassInstance = testClass.getConstructor(Map.class).newInstance(map);
    //then
    Getter getter = ReflectionTools.getGetter(testClass, "checkbox", "property", serviceRegistry);
    Assert.assertTrue((Boolean) getter.get(testClassInstance));
}
Also used : HashMap(java.util.HashMap) Getter(org.hibernate.property.access.spi.Getter) ClassLoaderServiceImpl(org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 5 with ClassLoaderServiceImpl

use of org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl in project hibernate-orm by hibernate.

the class MapProxyTest method shouldGenerateClassWithAppropriateSetter.

@Test
public void shouldGenerateClassWithAppropriateSetter() throws Exception {
    //given
    Map<String, Object> map = new HashMap<String, Object>();
    Map<String, Class<?>> properties = new HashMap<String, Class<?>>();
    properties.put("age", Integer.class);
    //when
    Class testClass = MapProxyTool.classForName("TestClass2", properties, new ClassLoaderServiceImpl());
    Object testClassInstance = testClass.getConstructor(Map.class).newInstance(map);
    //then
    Setter setter = ReflectionTools.getSetter(testClass, "age", "property", serviceRegistry);
    int ageExpected = 14;
    setter.set(testClassInstance, ageExpected, null);
    Object age = map.get("age");
    Assert.assertEquals(ageExpected, age);
}
Also used : HashMap(java.util.HashMap) Setter(org.hibernate.property.access.spi.Setter) ClassLoaderServiceImpl(org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

ClassLoaderServiceImpl (org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl)7 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)2 Getter (org.hibernate.property.access.spi.Getter)2 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Entity (javax.persistence.Entity)1 MetadataSources (org.hibernate.boot.MetadataSources)1 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)1 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)1 BootstrapServiceRegistryImpl (org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl)1 IntegratorServiceImpl (org.hibernate.integrator.internal.IntegratorServiceImpl)1 Setter (org.hibernate.property.access.spi.Setter)1