Java Code Examples for org.springframework.context.support.GenericXmlApplicationContext

The following examples show how to use org.springframework.context.support.GenericXmlApplicationContext. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void configuredThroughNamespace() {
	GenericXmlApplicationContext context = new GenericXmlApplicationContext();
	context.load(new ClassPathResource("taskNamespaceTests.xml", getClass()));
	context.refresh();
	ITestBean testBean = context.getBean("target", ITestBean.class);
	testBean.test();
	testBean.await(3000);
	Thread asyncThread = testBean.getThread();
	assertTrue(asyncThread.getName().startsWith("testExecutor"));

	TestableAsyncUncaughtExceptionHandler exceptionHandler =
			context.getBean("exceptionHandler", TestableAsyncUncaughtExceptionHandler.class);
	assertFalse("handler should not have been called yet", exceptionHandler.isCalled());

	testBean.failWithVoid();
	exceptionHandler.await(3000);
	Method m = ReflectionUtils.findMethod(TestBean.class, "failWithVoid");
	exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class);
	context.close();
}
 
Example 2
Source Project: pinpoint   Source File: AlarmJobTest.java    License: Apache License 2.0 7 votes vote down vote up
public static void main(String[] args) throws Exception{
     GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext("/applicationContext-test.xml");
     JobLauncherTestUtils testLauncher = applicationContext.getBean(JobLauncherTestUtils.class);
     
     JobExecution jobExecution = testLauncher.launchJob(getParameters());
     BatchStatus status = jobExecution.getStatus();
     assertEquals(BatchStatus.COMPLETED, status);
     
     applicationContext.close();
}
 
Example 3
Source Project: Easy-Cassandra-samples   Source File: App.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException, ParseException {
	@SuppressWarnings("resource")
	ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");
	Musics musicas = ctx.getBean(Musics.class);
	MusicService service = ctx.getBean(MusicService.class);
	for (Music musica: musicas.getMusics()) {
		service.save(musica);	
	}
	LuceneUtil.INSTANCE.backupToHD();
	System.out.println(service.findMusicByLyric("lepo lepo"));
	System.out.println(service.findMusicByLyric("aMoR"));
	
	System.out.println(service.findMusicByAuthor("Luan Santana"));
	System.out.println(service.findMusicByAuthor("Santana"));
	System.out.println(service.findMusicByAuthor("Psirico"));
}
 
Example 4
@Test(timeout = 5000)
@SuppressWarnings("resource")
public void checkTarget() {
	Task task =
		new GenericXmlApplicationContext(
				LazyScheduledTasksBeanDefinitionParserTests.class,
				"lazyScheduledTasksContext.xml")
			.getBean(Task.class);

	while (!task.executed) {
		try {
			Thread.sleep(10);
		}
		catch (Exception ex) { /* Do Nothing */ }
	}
}
 
Example 5
@Test
public void testJavaBean() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-java-context.xml");
	TestService bean = context.getBean("javaBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (RuntimeException ex) {
		assertEquals("TestServiceImpl", ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountThrows());

}
 
Example 6
@Test
public void testGroovyBeanInterface() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-interface-context.xml");
	TestService bean = context.getBean("groovyBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (RuntimeException ex) {
		assertEquals("GroovyServiceImpl", ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountThrows());
}
 
Example 7
@Test
public void testGroovyBeanProxyTargetClass() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-proxy-target-class-context.xml");
	TestService bean = context.getBean("groovyBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (TestException ex) {
		assertEquals("GroovyServiceImpl", ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountBefore());
	assertEquals(1, logAdvice.getCountThrows());
}
 
Example 8
Source Project: spring-analysis-note   Source File: DestroyMethodInferenceTests.java    License: MIT License 6 votes vote down vote up
@Test
public void xml() {
	ConfigurableApplicationContext ctx = new GenericXmlApplicationContext(
			getClass(), "DestroyMethodInferenceTests-context.xml");
	WithLocalCloseMethod x1 = ctx.getBean("x1", WithLocalCloseMethod.class);
	WithLocalCloseMethod x2 = ctx.getBean("x2", WithLocalCloseMethod.class);
	WithLocalCloseMethod x3 = ctx.getBean("x3", WithLocalCloseMethod.class);
	WithNoCloseMethod x4 = ctx.getBean("x4", WithNoCloseMethod.class);
	WithInheritedCloseMethod x8 = ctx.getBean("x8", WithInheritedCloseMethod.class);

	assertThat(x1.closed, is(false));
	assertThat(x2.closed, is(false));
	assertThat(x3.closed, is(false));
	assertThat(x4.closed, is(false));
	ctx.close();
	assertThat(x1.closed, is(false));
	assertThat(x2.closed, is(true));
	assertThat(x3.closed, is(true));
	assertThat(x4.closed, is(false));
	assertThat(x8.closed, is(false));
}
 
Example 9
@Test
public void configuredThroughNamespace() {
	GenericXmlApplicationContext context = new GenericXmlApplicationContext();
	context.load(new ClassPathResource("taskNamespaceTests.xml", getClass()));
	context.refresh();
	ITestBean testBean = context.getBean("target", ITestBean.class);
	testBean.test();
	testBean.await(3000);
	Thread asyncThread = testBean.getThread();
	assertTrue(asyncThread.getName().startsWith("testExecutor"));

	TestableAsyncUncaughtExceptionHandler exceptionHandler =
			context.getBean("exceptionHandler", TestableAsyncUncaughtExceptionHandler.class);
	assertFalse("handler should not have been called yet", exceptionHandler.isCalled());

	testBean.failWithVoid();
	exceptionHandler.await(3000);
	Method m = ReflectionUtils.findMethod(TestBean.class, "failWithVoid");
	exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class);
	context.close();
}
 
Example 10
@Test
public void testJavaBean() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-java-context.xml");
	TestService bean = context.getBean("javaBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (RuntimeException ex) {
		assertEquals("TestServiceImpl", ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountThrows());

}
 
Example 11
@Test
public void testGroovyBeanInterface() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-interface-context.xml");
	TestService bean = context.getBean("groovyBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (RuntimeException ex) {
		assertEquals("GroovyServiceImpl", ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountThrows());
}
 
Example 12
@Test
public void testGroovyBeanDynamic() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-dynamic-context.xml");
	TestService bean = context.getBean("groovyBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (RuntimeException ex) {
		assertEquals("GroovyServiceImpl", ex.getMessage());
	}
	// No proxy here because the pointcut only applies to the concrete class, not the interface
	assertEquals(0, logAdvice.getCountThrows());
	assertEquals(0, logAdvice.getCountBefore());
}
 
Example 13
@Test
public void testGroovyBeanProxyTargetClass() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-proxy-target-class-context.xml");
	TestService bean = context.getBean("groovyBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (TestException ex) {
		assertEquals("GroovyServiceImpl", ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountBefore());
	assertEquals(1, logAdvice.getCountThrows());
}
 
Example 14
public static ProcessEngine buildProcessEngine(URL resource) {
  log.fine("==== BUILDING SPRING APPLICATION CONTEXT AND PROCESS ENGINE =========================================");
  
  ApplicationContext applicationContext = new GenericXmlApplicationContext(new UrlResource(resource));
  Map<String, ProcessEngine> beansOfType = applicationContext.getBeansOfType(ProcessEngine.class);
  if ( (beansOfType==null)
       || (beansOfType.isEmpty())
     ) {
    throw new ProcessEngineException("no "+ProcessEngine.class.getName()+" defined in the application context "+resource.toString());
  }
  
  ProcessEngine processEngine = beansOfType.values().iterator().next();

  log.fine("==== SPRING PROCESS ENGINE CREATED ==================================================================");
  return processEngine;
}
 
Example 15
@Test
public void testJavaBean() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-java-context.xml");
	TestService bean = context.getBean("javaBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (RuntimeException ex) {
		assertEquals("TestServiceImpl", ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountThrows());

}
 
Example 16
@Test
public void testGroovyBeanDynamic() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-dynamic-context.xml");
	TestService bean = context.getBean("groovyBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (RuntimeException ex) {
		assertEquals("GroovyServiceImpl", ex.getMessage());
	}
	// No proxy here because the pointcut only applies to the concrete class, not the interface
	assertEquals(0, logAdvice.getCountThrows());
	assertEquals(0, logAdvice.getCountBefore());
}
 
Example 17
@Test
public void testGroovyBeanProxyTargetClass() {
	context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-proxy-target-class-context.xml");
	TestService bean = context.getBean("groovyBean", TestService.class);
	LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (TestException ex) {
		assertEquals("GroovyServiceImpl", ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountBefore());
	assertEquals(1, logAdvice.getCountThrows());
}
 
Example 18
@Test
public void xml() {
	ConfigurableApplicationContext ctx = new GenericXmlApplicationContext(
			getClass(), "DestroyMethodInferenceTests-context.xml");
	WithLocalCloseMethod x1 = ctx.getBean("x1", WithLocalCloseMethod.class);
	WithLocalCloseMethod x2 = ctx.getBean("x2", WithLocalCloseMethod.class);
	WithLocalCloseMethod x3 = ctx.getBean("x3", WithLocalCloseMethod.class);
	WithNoCloseMethod x4 = ctx.getBean("x4", WithNoCloseMethod.class);
	WithInheritedCloseMethod x8 = ctx.getBean("x8", WithInheritedCloseMethod.class);

	assertThat(x1.closed, is(false));
	assertThat(x2.closed, is(false));
	assertThat(x3.closed, is(false));
	assertThat(x4.closed, is(false));
	ctx.close();
	assertThat(x1.closed, is(false));
	assertThat(x2.closed, is(true));
	assertThat(x3.closed, is(true));
	assertThat(x4.closed, is(false));
	assertThat(x8.closed, is(false));
}
 
Example 19
@Test
public void testWriteAndBack() throws NoSuchBeanDefinitionException, RuntimeConfigException, IllegalStateException,
		IOException {
	BeanDefinitionWriterServiceImpl service = new BeanDefinitionWriterServiceImpl();
	String basePath = File.createTempFile("tmp", "tmp").getParentFile().getAbsolutePath() + File.separator;
	service.setBaseResource(new FileSystemResource(basePath));
	List<BeanDefinitionHolder> beans = new ArrayList<BeanDefinitionHolder>();
	beans.add(new BeanDefinitionHolder(context.getBeanFactory().getBeanDefinition("beanA"), "beanA"));
	beans.add(new BeanDefinitionHolder(context.getBeanFactory().getBeanDefinition("beanB"), "beanB"));
	service.persist("test", beans);
	GenericXmlApplicationContext generic = new GenericXmlApplicationContext(service.getBaseResource().createRelative("test.xml"));
	Assert.assertEquals(context.getBean("beanA"), generic.getBean("beanA"));
	Assert.assertEquals(context.getBean("beanB"), generic.getBean("beanB"));
}
 
Example 20
@Test
public void propertyPlaceholderEnvironmentProperties() {
	MockEnvironment env = new MockEnvironment().withProperty("foo", "spam");
	GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext();
	applicationContext.setEnvironment(env);
	applicationContext.load(new ClassPathResource("contextNamespaceHandlerTests-simple.xml", getClass()));
	applicationContext.refresh();
	assertEquals("spam", applicationContext.getBean("string"));
	assertEquals("none", applicationContext.getBean("fallback"));
}
 
Example 21
@Test
public void cacheResolver() {
	ConfigurableApplicationContext context = new GenericXmlApplicationContext(
			"/org/springframework/cache/config/annotationDrivenCacheNamespace-resolver.xml");

	CacheInterceptor ci = context.getBean(CacheInterceptor.class);
	assertSame(context.getBean("cacheResolver"), ci.getCacheResolver());
	context.close();
}
 
Example 22
@Test
public void bothSetOnlyResolverIsUsed() {
	ConfigurableApplicationContext context = new GenericXmlApplicationContext(
			"/org/springframework/cache/config/annotationDrivenCacheNamespace-manager-resolver.xml");

	CacheInterceptor ci = context.getBean(CacheInterceptor.class);
	assertSame(context.getBean("cacheResolver"), ci.getCacheResolver());
	context.close();
}
 
Example 23
Source Project: spring-analysis-note   Source File: CacheAdviceParserTests.java    License: MIT License 5 votes vote down vote up
@Test
public void keyAndKeyGeneratorCannotBeSetTogether() {
	try {
		new GenericXmlApplicationContext("/org/springframework/cache/config/cache-advice-invalid.xml");
		fail("Should have failed to load context, one advise define both a key and a key generator");
	}
	catch (BeanDefinitionStoreException ex) {
		// TODO better exception handling
	}
}
 
Example 24
@Test
public void genericXmlApplicationContext() {
	GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
	assertHasStandardEnvironment(ctx);
	ctx.setEnvironment(prodEnv);
	ctx.load(XML_PATH);
	ctx.refresh();

	assertHasEnvironment(ctx, prodEnv);
	assertEnvironmentBeanRegistered(ctx);
	assertEnvironmentAwareInvoked(ctx, prodEnv);
	assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false));
	assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true));
}
 
Example 25
Source Project: spring-analysis-note   Source File: JCacheNamespaceDrivenTests.java    License: MIT License 5 votes vote down vote up
@Test
public void cacheResolver() {
	ConfigurableApplicationContext context = new GenericXmlApplicationContext(
			"/org/springframework/cache/jcache/config/jCacheNamespaceDriven-resolver.xml");

	DefaultJCacheOperationSource ci = context.getBean(DefaultJCacheOperationSource.class);
	assertSame(context.getBean("cacheResolver"), ci.getCacheResolver());
	context.close();
}
 
Example 26
@Test(timeout = 5000)
public void checkTarget() {
	Task task =
		new GenericXmlApplicationContext(
				LazyScheduledTasksBeanDefinitionParserTests.class,
				"lazyScheduledTasksContext.xml")
			.getBean(Task.class);

	while (!task.executed) {
		try {
			Thread.sleep(10);
		}
		catch (Exception ex) { /* Do Nothing */ }
	}
}
 
Example 27
@Test
public void propertyPlaceholderEnvironmentProperties() throws Exception {
	MockEnvironment env = new MockEnvironment().withProperty("foo", "spam");
	GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext();
	applicationContext.setEnvironment(env);
	applicationContext.load(new ClassPathResource("contextNamespaceHandlerTests-simple.xml", getClass()));
	applicationContext.refresh();
	assertEquals("spam", applicationContext.getBean("string"));
	assertEquals("none", applicationContext.getBean("fallback"));
}
 
Example 28
@Test
public void cacheResolver() {
	ConfigurableApplicationContext context = new GenericXmlApplicationContext(
			"/org/springframework/cache/config/annotationDrivenCacheNamespace-resolver.xml");

	CacheInterceptor ci = context.getBean(CacheInterceptor.class);
	assertSame(context.getBean("cacheResolver"), ci.getCacheResolver());
	context.close();
}
 
Example 29
@Test
public void bothSetOnlyResolverIsUsed() {
	ConfigurableApplicationContext context = new GenericXmlApplicationContext(
			"/org/springframework/cache/config/annotationDrivenCacheNamespace-manager-resolver.xml");

	CacheInterceptor ci = context.getBean(CacheInterceptor.class);
	assertSame(context.getBean("cacheResolver"), ci.getCacheResolver());
	context.close();
}
 
Example 30
Source Project: java-technology-stack   Source File: CacheAdviceParserTests.java    License: MIT License 5 votes vote down vote up
@Test
public void keyAndKeyGeneratorCannotBeSetTogether() {
	try {
		new GenericXmlApplicationContext("/org/springframework/cache/config/cache-advice-invalid.xml");
		fail("Should have failed to load context, one advise define both a key and a key generator");
	}
	catch (BeanDefinitionStoreException ex) {
		// TODO better exception handling
	}
}