Java Code Examples for org.springframework.test.util.ReflectionTestUtils#getField()

The following examples show how to use org.springframework.test.util.ReflectionTestUtils#getField() . 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
Source File: WebConfigurerTest.java    From Spring-5.0-Projects with MIT License 6 votes vote down vote up
@Test
public void testCustomizeServletContainer() {
    env.setActiveProfiles(JHipsterConstants.SPRING_PROFILE_PRODUCTION);
    UndertowServletWebServerFactory container = new UndertowServletWebServerFactory();
    webConfigurer.customize(container);
    assertThat(container.getMimeMappings().get("abs")).isEqualTo("audio/x-mpeg");
    assertThat(container.getMimeMappings().get("html")).isEqualTo("text/html;charset=utf-8");
    assertThat(container.getMimeMappings().get("json")).isEqualTo("text/html;charset=utf-8");
    if (container.getDocumentRoot() != null) {
        assertThat(container.getDocumentRoot().getPath()).isEqualTo(FilenameUtils.separatorsToSystem("target/www"));
    }

    Builder builder = Undertow.builder();
    container.getBuilderCustomizers().forEach(c -> c.customize(builder));
    OptionMap.Builder serverOptions = (OptionMap.Builder) ReflectionTestUtils.getField(builder, "serverOptions");
    assertThat(serverOptions.getMap().get(UndertowOptions.ENABLE_HTTP2)).isNull();
}
 
Example 2
Source File: ConfigFileControllerTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  configFileController = new ConfigFileController(
      configController, namespaceUtil, watchKeysUtil, grayReleaseRulesHolder
  );

  someAppId = "someAppId";
  someClusterName = "someClusterName";
  someNamespace = "someNamespace";
  someDataCenter = "someDataCenter";
  someClientIp = "10.1.1.1";

  when(namespaceUtil.filterNamespaceName(someNamespace)).thenReturn(someNamespace);
  when(namespaceUtil.normalizeNamespace(someAppId, someNamespace)).thenReturn(someNamespace);
  when(grayReleaseRulesHolder.hasGrayReleaseRule(anyString(), anyString(), anyString()))
      .thenReturn(false);

  watchedKeys2CacheKey =
      (Multimap<String, String>) ReflectionTestUtils
          .getField(configFileController, "watchedKeys2CacheKey");
  cacheKey2WatchedKeys =
      (Multimap<String, String>) ReflectionTestUtils
          .getField(configFileController, "cacheKey2WatchedKeys");
}
 
Example 3
Source File: DeferredLogTests.java    From java-cfenv with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void switchTo() {
	List<String> lines = (List<String>) ReflectionTestUtils.getField(this.deferredLog,
			"lines");
	assertThat(lines).isEmpty();

	this.deferredLog.error(this.message, this.throwable);
	assertThat(lines).hasSize(1);

	this.deferredLog.switchTo(this.log);
	assertThat(lines).isEmpty();

	this.deferredLog.info("Message2");
	assertThat(lines).isEmpty();

	verify(this.log).error(this.message, this.throwable);
	verify(this.log).info("Message2", null);
}
 
Example 4
Source File: RefreshableConfigurationTest.java    From memcached-spring-boot with Apache License 2.0 6 votes vote down vote up
@Test
@DirtiesContext
public void whenConfigurationChangedThenMemcachedClientReinitialized() {
    Object beforeRefresh = ReflectionTestUtils.getField(cacheManager, "memcachedClient");
    assertMemcachedClient((IMemcachedClient) beforeRefresh);

    TestPropertyValues.of(
        "memcached.cache.prefix:test-prefix",
        "memcached.cache.protocol:binary"
    ).applyTo(environment);

    refresher.refresh();

    Object expiration = ReflectionTestUtils.getField(cacheManager, "expiration");
    Object prefix = ReflectionTestUtils.getField(cacheManager, "prefix");
    Object afterRefresh = ReflectionTestUtils.getField(cacheManager, "memcachedClient");

    assertThat(expiration).isNotNull();
    assertThat(expiration).isEqualTo(Default.EXPIRATION);
    assertThat(prefix).isNotNull();
    assertThat(prefix).isEqualTo("test-prefix");
    assertMemcachedClient((IMemcachedClient) afterRefresh,
            MemcachedCacheProperties.Protocol.BINARY, Default.OPERATION_TIMEOUT);
}
 
Example 5
Source File: NotificationControllerTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  controller = new NotificationController(watchKeysUtil, releaseMessageService, entityManagerUtil, namespaceUtil);

  someAppId = "someAppId";
  someCluster = "someCluster";
  defaultNamespace = ConfigConsts.NAMESPACE_APPLICATION;
  someDataCenter = "someDC";
  someNotificationId = 1;
  someClientIp = "someClientIp";

  when(namespaceUtil.filterNamespaceName(defaultNamespace)).thenReturn(defaultNamespace);

  deferredResults =
      (Multimap<String, DeferredResult<ResponseEntity<ApolloConfigNotification>>>) ReflectionTestUtils
          .getField(controller, "deferredResults");
}
 
Example 6
Source File: EmbeddedCassandraFactoryTests.java    From embedded-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
void testLogger() {
	Logger mylogger = LoggerFactory.getLogger("mylogger");
	this.cassandraFactory.setLogger(mylogger);
	Cassandra cassandra = this.cassandraFactory.create();
	Object database = ReflectionTestUtils.getField(cassandra, "database");
	assertThat(database).hasFieldOrPropertyWithValue("logger", mylogger);
}
 
Example 7
Source File: EmbeddedCassandraFactoryTests.java    From embedded-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
void testRackConfig(@TempDir Path temporaryFolder) throws IOException {
	Path file = Files.createTempFile(temporaryFolder, "", "");
	FileSystemResource config = new FileSystemResource(file);
	this.cassandraFactory.setRackConfig(config);
	assertThat(this.cassandraFactory.getRackConfig()).isEqualTo(config);
	Cassandra cassandra = this.cassandraFactory.create();
	Object database = ReflectionTestUtils.getField(cassandra, "database");
	assertThat(database).hasFieldOrPropertyWithValue("rackConfig", config);
}
 
Example 8
Source File: UndertowSSLConfigurationTest.java    From jhipster with Apache License 2.0 5 votes vote down vote up
@Test
public void testUndertowSSLConfigurationOK() {
    //Prepare
    UndertowServletWebServerFactory undertowServletWebServerFactory = new UndertowServletWebServerFactory();

    //Execute
    UndertowSSLConfiguration undertowSSLConfiguration = new UndertowSSLConfiguration(undertowServletWebServerFactory);

    //Verify
    Undertow.Builder builder = Undertow.builder();
    undertowServletWebServerFactory.getBuilderCustomizers().forEach(c -> c.customize(builder));
    OptionMap.Builder serverOptions = (OptionMap.Builder) ReflectionTestUtils.getField(builder, "socketOptions");
    assertThat(undertowServletWebServerFactory).isNotNull();
    assertThat(serverOptions.getMap().get(UndertowOptions.SSL_USER_CIPHER_SUITES_ORDER)).isTrue();
}
 
Example 9
Source File: WebConfigurerTest.java    From e-commerce-microservice with Apache License 2.0 5 votes vote down vote up
@Test
public void testUndertowHttp2Enabled() {
    props.getHttp().setVersion(JHipsterProperties.Http.Version.V_2_0);
    UndertowServletWebServerFactory container = new UndertowServletWebServerFactory();
    webConfigurer.customize(container);
    Builder builder = Undertow.builder();
    container.getBuilderCustomizers().forEach(c -> c.customize(builder));
    OptionMap.Builder serverOptions = (OptionMap.Builder) ReflectionTestUtils.getField(builder, "serverOptions");
    assertThat(serverOptions.getMap().get(UndertowOptions.ENABLE_HTTP2)).isTrue();
}
 
Example 10
Source File: EmbeddedCassandraFactoryTests.java    From embedded-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
void testStoragePortSsl() {
	this.cassandraFactory.setSslStoragePort(7001);
	assertThat(this.cassandraFactory.getSslStoragePort()).isEqualTo(7001);
	Cassandra cassandra = this.cassandraFactory.create();
	Object node = ReflectionTestUtils.getField(ReflectionTestUtils.getField(cassandra, "database"), "node");
	assertThat(node).hasFieldOrPropertyWithValue("systemProperties",
			Collections.singletonMap("cassandra.ssl_storage_port", 7001));
}
 
Example 11
Source File: WebConfigurerTest.java    From jhipster-microservices-example with Apache License 2.0 5 votes vote down vote up
@Test
public void testUndertowHttp2Enabled() {
    props.getHttp().setVersion(JHipsterProperties.Http.Version.V_2_0);
    UndertowEmbeddedServletContainerFactory container = new UndertowEmbeddedServletContainerFactory();
    webConfigurer.customize(container);
    Builder builder = Undertow.builder();
    container.getBuilderCustomizers().forEach(c -> c.customize(builder));
    OptionMap.Builder serverOptions = (OptionMap.Builder) ReflectionTestUtils.getField(builder, "serverOptions");
    assertThat(serverOptions.getMap().get(UndertowOptions.ENABLE_HTTP2)).isTrue();
}
 
Example 12
Source File: EmbeddedCassandraFactoryTests.java    From embedded-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
void getSystemProperties() {
	this.cassandraFactory.getSystemProperties().put("key", "value");
	Cassandra cassandra = this.cassandraFactory.create();
	Object node = ReflectionTestUtils.getField(ReflectionTestUtils.getField(cassandra, "database"), "node");
	assertThat(node).hasFieldOrPropertyWithValue("systemProperties", Collections.singletonMap("key", "value"));
}
 
Example 13
Source File: WebConfigurerTest.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 5 votes vote down vote up
@Test
public void testUndertowHttp2Enabled() {
    props.getHttp().setVersion(JHipsterProperties.Http.Version.V_2_0);
    UndertowServletWebServerFactory container = new UndertowServletWebServerFactory();
    webConfigurer.customize(container);
    Builder builder = Undertow.builder();
    container.getBuilderCustomizers().forEach(c -> c.customize(builder));
    OptionMap.Builder serverOptions = (OptionMap.Builder) ReflectionTestUtils.getField(builder, "serverOptions");
    assertThat(serverOptions.getMap().get(UndertowOptions.ENABLE_HTTP2)).isTrue();
}
 
Example 14
Source File: WebConfigurerTest.java    From jhipster-microservices-example with Apache License 2.0 5 votes vote down vote up
@Test
public void testUndertowHttp2Enabled() {
    props.getHttp().setVersion(JHipsterProperties.Http.Version.V_2_0);
    UndertowEmbeddedServletContainerFactory container = new UndertowEmbeddedServletContainerFactory();
    webConfigurer.customize(container);
    Builder builder = Undertow.builder();
    container.getBuilderCustomizers().forEach(c -> c.customize(builder));
    OptionMap.Builder serverOptions = (OptionMap.Builder) ReflectionTestUtils.getField(builder, "serverOptions");
    assertThat(serverOptions.getMap().get(UndertowOptions.ENABLE_HTTP2)).isTrue();
}
 
Example 15
Source File: WebConfigurerTest.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomizeServletContainer() {
    env.setActiveProfiles(JHipsterConstants.SPRING_PROFILE_PRODUCTION);
    UndertowEmbeddedServletContainerFactory container = new UndertowEmbeddedServletContainerFactory();
    webConfigurer.customize(container);
    assertThat(container.getMimeMappings().get("abs")).isEqualTo("audio/x-mpeg");
    assertThat(container.getMimeMappings().get("html")).isEqualTo("text/html;charset=utf-8");
    assertThat(container.getMimeMappings().get("json")).isEqualTo("text/html;charset=utf-8");

    Builder builder = Undertow.builder();
    container.getBuilderCustomizers().forEach(c -> c.customize(builder));
    OptionMap.Builder serverOptions = (OptionMap.Builder) ReflectionTestUtils.getField(builder, "serverOptions");
    assertThat(serverOptions.getMap().get(UndertowOptions.ENABLE_HTTP2)).isNull();
}
 
Example 16
Source File: WebConfigurerTest.java    From TeamDojo with Apache License 2.0 5 votes vote down vote up
@Test
public void testUndertowHttp2Enabled() {
    props.getHttp().setVersion(JHipsterProperties.Http.Version.V_2_0);
    UndertowServletWebServerFactory container = new UndertowServletWebServerFactory();
    webConfigurer.customize(container);
    Builder builder = Undertow.builder();
    container.getBuilderCustomizers().forEach(c -> c.customize(builder));
    OptionMap.Builder serverOptions = (OptionMap.Builder) ReflectionTestUtils.getField(builder, "serverOptions");
    assertThat(serverOptions.getMap().get(UndertowOptions.ENABLE_HTTP2)).isTrue();
}
 
Example 17
Source File: MemcachedAutoConfigurationIT.java    From memcached-spring-boot with Apache License 2.0 5 votes vote down vote up
@Test
public void whenTextProtocolAndMultipleServerListThenMemcachedLoaded() {
    loadContext(CacheConfiguration.class, String.format("memcached.cache.servers=%s:%d, %s:%d", memcachedHost1, memcachedPort1, memcachedHost2, memcachedPort2),
            "memcached.cache.protocol=text");

    MemcachedCacheManager memcachedCacheManager = this.applicationContext.getBean(MemcachedCacheManager.class);

    IMemcachedClient memcachedClient = (IMemcachedClient) ReflectionTestUtils.getField(memcachedCacheManager, "memcachedClient");

    assertMemcachedClient(memcachedClient, Protocol.TEXT, Default.OPERATION_TIMEOUT, new InetSocketAddress(memcachedHost1, memcachedPort1), new InetSocketAddress(memcachedHost2, memcachedPort2));
    assertMemcachedCacheManager(memcachedCacheManager, Default.EXPIRATION, Collections.emptyMap(), Default.PREFIX, Default.NAMESPACE);
}
 
Example 18
Source File: EmbeddedCassandraBuilderTests.java    From embedded-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
void testWorkingDir(@TempDir Path temporaryFolder) {
	this.builder.withWorkingDirectory(temporaryFolder);
	Cassandra cassandra = this.builder.create();
	Object database = ReflectionTestUtils.getField(cassandra, "database");
	assertThat(database).hasFieldOrPropertyWithValue("workingDirectory", temporaryFolder);
}
 
Example 19
Source File: EmbeddedCassandraFactoryTests.java    From embedded-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
void getEnvironmentVariables() {
	this.cassandraFactory.getEnvironmentVariables().put("key", "value");
	Cassandra cassandra = this.cassandraFactory.create();
	Object node = ReflectionTestUtils.getField(ReflectionTestUtils.getField(cassandra, "database"), "node");
	Map<String, Object> environmentVariables = (Map<String, Object>) ReflectionTestUtils.getField(node,
			"environmentVariables");
	assertThat(environmentVariables).containsEntry("key", "value");
}
 
Example 20
Source File: EmbeddedCassandraBuilderTests.java    From embedded-cassandra with Apache License 2.0 4 votes vote down vote up
@Test
void getProperties() {
	Cassandra cassandra = this.builder.withConfigProperty("key", "value").create();
	Object node = ReflectionTestUtils.getField(ReflectionTestUtils.getField(cassandra, "database"), "node");
	assertThat(node).hasFieldOrPropertyWithValue("properties", Collections.singletonMap("key", "value"));
}