From ac8d47bec45e949440b2d30ede069ea2f95c013b Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 13 Feb 2020 00:13:38 -0800 Subject: [PATCH] Add raw Apache Geode ClientCache application verifying the log-level property has no effect. Resolves gh-73. --- .../app/ApacheGeodeClientApplication.java | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 apache-geode-extensions/src/test/java/org/springframework/geode/app/ApacheGeodeClientApplication.java diff --git a/apache-geode-extensions/src/test/java/org/springframework/geode/app/ApacheGeodeClientApplication.java b/apache-geode-extensions/src/test/java/org/springframework/geode/app/ApacheGeodeClientApplication.java new file mode 100644 index 00000000..c3204073 --- /dev/null +++ b/apache-geode-extensions/src/test/java/org/springframework/geode/app/ApacheGeodeClientApplication.java @@ -0,0 +1,110 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +package org.springframework.geode.app; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Optional; +import java.util.Properties; + +import org.apache.geode.cache.Region; +import org.apache.geode.cache.client.ClientCache; +import org.apache.geode.cache.client.ClientCacheFactory; +import org.apache.geode.cache.client.ClientRegionShortcut; + +/** + * An example Apache Geode {@link ClientCache} application. + * + * @author John Blum + * @see java.util.Properties + * @see org.apache.geode.cache.Region + * @see org.apache.geode.cache.client.ClientCache + * @since 1.3.0 + */ +@SuppressWarnings("unused") +public class ApacheGeodeClientApplication implements Runnable { + + private static final ClientRegionShortcut CLIENT_REGION_SHORTCUT = ClientRegionShortcut.LOCAL; + + private static final String APPLICATION_NAME = ApacheGeodeClientApplication.class.getSimpleName(); + private static final String GEMFIRE_LOG_LEVEL = "info"; + + private static final String[] EMPTY_ARGUMENTS = {}; + + public static void main(String[] args) { + new ApacheGeodeClientApplication(args).run(); + } + + private final String[] arguments; + + public ApacheGeodeClientApplication(String[] arguments) { + this.arguments = arguments != null ? arguments : EMPTY_ARGUMENTS; + } + + protected String[] getArguments() { + return this.arguments; + } + + @Override + public void run() { + run(getArguments()); + } + + public void run(String[] arguments) { + + ClientCache clientCache = registerShutdownHook(newClientCache(gemfireProperties())); + + Region example = newClientRegion(clientCache, "Example"); + + doDataAccessOperationsTest(example); + } + + protected Properties gemfireProperties() { + + Properties gemfireProperties = new Properties(); + + gemfireProperties.setProperty("name", APPLICATION_NAME); + gemfireProperties.setProperty("log-level", GEMFIRE_LOG_LEVEL); + + return gemfireProperties; + } + + protected ClientCache newClientCache(Properties gemfireProperties) { + return new ClientCacheFactory(gemfireProperties).create(); + } + + protected Region newClientRegion(ClientCache clientCache, String regionName) { + return clientCache.createClientRegionFactory(CLIENT_REGION_SHORTCUT).create(regionName); + } + + @SuppressWarnings("unchecked") + protected Region doDataAccessOperationsTest(Region region) { + + assertThat(region.put(1, "TEST")).isNull(); + assertThat(region.get(1)).isEqualTo("TEST"); + + return (Region) region; + } + + protected ClientCache registerShutdownHook(ClientCache clientCache) { + + Runtime.getRuntime().addShutdownHook(new Thread(() -> + Optional.ofNullable(clientCache).ifPresent(ClientCache::close), + "ClientCache Shutdown Hook")); + + return clientCache; + } +}