Skip to content

Commit

Permalink
Last failing tests fixed
Browse files Browse the repository at this point in the history
- Added in tests that had previously been commented from ClassNameTest

Signed-off-by: Jens Deppe <jdeppe@pivotal.io>
  • Loading branch information
Petahhh authored and jdeppe-pivotal committed Jan 17, 2019
1 parent f1ca297 commit aa28a37
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ WHERE <args>:
geode-modules-session-internal.jar
geode-core.jar
geode-json.jar
geode-common.jar
geode-management-api.jar
antlr.jar
log4j-core.jar
log4j-api.jar
Expand Down Expand Up @@ -261,6 +263,8 @@ SESSION_JAR="${LIB_DIR}/geode-modules-session-${VERSION}.jar"
declare -a OTHER_JARS
OTHER_JARS=(${GEODE}/lib/geode-core-${VERSION}.jar \
${GEODE}/lib/geode-json-${VERSION}.jar \
${GEODE}/lib/geode-common-${VERSION}.jar \
${GEODE}/lib/geode-management-api-${VERSION}.jar \
${GEODE}/lib/antlr-@ANTLR_VERSION@.jar \
${GEODE}/lib/log4j-core-@LOG4J_VERSION@.jar \
${GEODE}/lib/log4j-api-@LOG4J_VERSION@.jar \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You 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
*
* http://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.apache.geode.management.internal.cli.domain;

import org.apache.geode.cache.Cache;
import org.apache.geode.cache.Declarable;
import org.apache.geode.cache.configuration.ConfigTypeInstantiator;
import org.apache.geode.internal.ClassPathLoader;

public class ClassNameInstantiator<T> implements ConfigTypeInstantiator<ClassName<?>> {

private final Cache cache;

public ClassNameInstantiator(Cache cache) {
this.cache = cache;
}

@Override
public <V> V newInstance(ClassName<?> type) {
try {
Class<T> loadedClass = (Class<T>) ClassPathLoader.getLatest().forName(type.getClassName());
T object = loadedClass.newInstance();
if (object instanceof Declarable) {
Declarable declarable = (Declarable) object;
declarable.initialize(cache, type.getInitProperties());
declarable.init(type.getInitProperties()); // for backwards compatibility
}
return (V) object;
} catch (Exception e) {
throw new RuntimeException("Error instantiating class: <" + type.getClassName() + ">", e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.junit.Test;



public class ClassNameTest {

@Test
Expand All @@ -42,12 +41,12 @@ public void empty() {
.isEqualTo(ClassName.EMPTY);
}

// @Test
// public void emptyCanNotInstantiate() {
// assertThatThrownBy(() ->
// ClassName.EMPTY.newInstance(null)).isInstanceOf(RuntimeException.class)
// .hasMessageContaining("Error instantiating class");
// }
@Test
public void emptyCanNotInstantiate() {
assertThatThrownBy(() -> ClassName.EMPTY.newInstance(new ClassNameInstantiator(null)))
.isInstanceOf(RuntimeException.class)
.hasMessageContaining("Error instantiating class");
}

@Test
public void constructWithProperties() {
Expand Down Expand Up @@ -98,18 +97,18 @@ public void illegalJson() {
.isInstanceOf(IllegalArgumentException.class);
}

// @Test
// public void getInstance() {
// ClassName<String> klass = new ClassName("java.lang.String");
// String s = klass.newInstance(null);
// assertThat(s.toString()).isEqualTo("");
// }
//
// @Test
// public void getInstanceWithProps() {
// String json = "{\"k\":\"v\"}";
// ClassName<MyCacheWriter> cacheWriter = new ClassName<>(MyCacheWriter.class.getName(), json);
// MyCacheWriter obj = cacheWriter.newInstance(null);
// assertThat(obj.getProperties()).containsEntry("k", "v").containsOnlyKeys("k");
// }
@Test
public void getInstance() {
ClassName<String> klass = new ClassName("java.lang.String");
String s = klass.newInstance(new ClassNameInstantiator<String>(null));
assertThat(s.toString()).isEqualTo("");
}

@Test
public void getInstanceWithProps() {
String json = "{\"k\":\"v\"}";
ClassName<MyCacheWriter> cacheWriter = new ClassName<>(MyCacheWriter.class.getName(), json);
MyCacheWriter obj = cacheWriter.newInstance(new ClassNameInstantiator<MyCacheWriter>(null));
assertThat(obj.getProperties()).containsEntry("k", "v").containsOnlyKeys("k");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;

import org.apache.geode.cache.configuration.ConfigTypeInstantiator;


/**
* This is mostly used for Gfsh command options that need to specify a className for instantiation.
Expand Down Expand Up @@ -56,7 +58,6 @@ public ClassName(String className) {
}

/**
*
* @param className this class needs to have an empty param constructor
* @param jsonInitProperties this class needs to implement Declarable in order for these
* properties to be applied at initialization time
Expand Down Expand Up @@ -113,22 +114,8 @@ public boolean equals(Object o) {
&& this.getInitProperties().equals(that.getInitProperties());
}

// public T newInstance(ConfigTypeInstantiator<DeclarableType> instantiator) {
// return (T) instantiator.newInstance(this);
// }

// public T newInstance(Cache cache) {
// try {
// Class<T> loadedClass = (Class<T>) ClassPathLoader.getLatest().forName(className);
// T object = loadedClass.newInstance();
// if (object instanceof Declarable) {
// Declarable declarable = (Declarable) object;
// declarable.initialize(cache, initProperties);
// declarable.init(initProperties); // for backwards compatibility
// }
// return object;
// } catch (Exception e) {
// throw new RuntimeException("Error instantiating class: <" + className + ">", e);
// }
// }
public T newInstance(ConfigTypeInstantiator<ClassName<?>> instantiator) {
return (T) instantiator.newInstance(this);
}

}

0 comments on commit aa28a37

Please sign in to comment.