@Test public void initializersCreatedOnceForChild() throws Exception { SpringApplicationBuilder application = new SpringApplicationBuilder(ExampleConfig.class).child(ChildConfig.class).web(false); this.context = application.run(); assertEquals(4, application.application().getInitializers().size()); }
@Test public void specificApplicationContextClass() throws Exception { SpringApplicationBuilder application = new SpringApplicationBuilder() .sources(ExampleConfig.class) .contextClass(StaticApplicationContext.class); this.context = application.run(); assertThat(this.context, is(instanceOf(StaticApplicationContext.class))); }
@Test public void parentContextIdentical() throws Exception { SpringApplicationBuilder application = new SpringApplicationBuilder(ExampleConfig.class); application.parent(ExampleConfig.class); application.contextClass(SpyApplicationContext.class); this.context = application.run(); verify(((SpyApplicationContext) this.context).getApplicationContext()) .setParent(any(ApplicationContext.class)); }
@Test public void contextWithClassLoader() throws Exception { SpringApplicationBuilder application = new SpringApplicationBuilder(ExampleConfig.class).contextClass(SpyApplicationContext.class); ClassLoader classLoader = new URLClassLoader(new URL[0], getClass().getClassLoader()); application.resourceLoader(new DefaultResourceLoader(classLoader)); this.context = application.run(); assertThat(((SpyApplicationContext) this.context).getClassLoader(), is(equalTo(classLoader))); }
@Test public void propertiesAsMap() throws Exception { SpringApplicationBuilder application = new SpringApplicationBuilder() .sources(ExampleConfig.class) .contextClass(StaticApplicationContext.class) .properties(Collections.<String, Object>singletonMap("bar", "foo")); this.context = application.run(); assertThat(this.context.getEnvironment().getProperty("bar"), is(equalTo("foo"))); }
@Test public void parentFirstCreation() throws Exception { SpringApplicationBuilder application = new SpringApplicationBuilder(ExampleConfig.class).child(ChildConfig.class); application.contextClass(SpyApplicationContext.class); this.context = application.run(); verify(((SpyApplicationContext) this.context).getApplicationContext()) .setParent(any(ApplicationContext.class)); assertThat(((SpyApplicationContext) this.context).getRegisteredShutdownHook(), equalTo(false)); }
@Test public void propertiesAsProperties() throws Exception { SpringApplicationBuilder application = new SpringApplicationBuilder() .sources(ExampleConfig.class) .contextClass(StaticApplicationContext.class) .properties( StringUtils.splitArrayElementsIntoProperties(new String[] {"bar=foo"}, "=")); this.context = application.run(); assertThat(this.context.getEnvironment().getProperty("bar"), is(equalTo("foo"))); }
@Test public void profileAndProperties() throws Exception { SpringApplicationBuilder application = new SpringApplicationBuilder() .sources(ExampleConfig.class) .contextClass(StaticApplicationContext.class) .profiles("foo") .properties("foo=bar"); this.context = application.run(); assertThat(this.context, is(instanceOf(StaticApplicationContext.class))); assertThat(this.context.getEnvironment().getProperty("foo"), is(equalTo("bucket"))); assertThat(this.context.getEnvironment().acceptsProfiles("foo"), is(true)); }
@Test public void initializersIncludeDefaults() throws Exception { SpringApplicationBuilder application = new SpringApplicationBuilder(ExampleConfig.class) .web(false) .initializers( new ApplicationContextInitializer<ConfigurableApplicationContext>() { @Override public void initialize(ConfigurableApplicationContext applicationContext) {} }); this.context = application.run(); assertEquals(4, application.application().getInitializers().size()); }
@Test public void parentFirstWithDifferentProfile() throws Exception { SpringApplicationBuilder application = new SpringApplicationBuilder(ExampleConfig.class) .profiles("node") .properties("transport=redis") .child(ChildConfig.class) .profiles("admin") .web(false); this.context = application.run(); assertThat(this.context.getEnvironment().acceptsProfiles("node", "admin"), is(true)); assertThat(this.context.getParent().getEnvironment().acceptsProfiles("admin"), is(false)); }
@Test public void parentFirstWithDifferentProfileAndExplicitEnvironment() throws Exception { SpringApplicationBuilder application = new SpringApplicationBuilder(ExampleConfig.class) .environment(new StandardEnvironment()) .profiles("node") .properties("transport=redis") .child(ChildConfig.class) .profiles("admin") .web(false); this.context = application.run(); assertThat(this.context.getEnvironment().acceptsProfiles("node", "admin"), is(true)); // Now they share an Environment explicitly so there's no way to keep the profiles // separate assertThat(this.context.getParent().getEnvironment().acceptsProfiles("admin"), is(true)); }
@Test public void parentFirstCreationWithProfileAndDefaultArgs() throws Exception { SpringApplicationBuilder application = new SpringApplicationBuilder(ExampleConfig.class) .profiles("node") .properties("transport=redis") .child(ChildConfig.class) .web(false); this.context = application.run(); assertThat(this.context.getEnvironment().acceptsProfiles("node"), is(true)); assertThat(this.context.getEnvironment().getProperty("transport"), is(equalTo("redis"))); assertThat(this.context.getParent().getEnvironment().acceptsProfiles("node"), is(true)); assertThat( this.context.getParent().getEnvironment().getProperty("transport"), is(equalTo("redis"))); // only defined in node profile assertThat(this.context.getEnvironment().getProperty("bar"), is(equalTo("spam"))); }