Skip to content

Commit

Permalink
Fixes powermock#727 Use ByteBuddy to instrument classes instead Javas…
Browse files Browse the repository at this point in the history
…sist:

- Fix all tests failed after last changes
  • Loading branch information
Arthur Zagretdinov committed Apr 29, 2018
1 parent 399cbce commit 8e851a1
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.powermock.configuration;

import org.powermock.core.classloader.ByteCodeFramework;
import org.powermock.utils.ArrayUtil;

/**
Expand All @@ -29,6 +30,7 @@
*/
public class PowerMockConfiguration implements Configuration<PowerMockConfiguration> {
private String[] globalIgnore;
private ByteCodeFramework byteCodeFramework;

public String[] getGlobalIgnore() {
return globalIgnore;
Expand All @@ -38,6 +40,14 @@ public void setGlobalIgnore(final String[] globalIgnore) {
this.globalIgnore = globalIgnore;
}

public ByteCodeFramework getByteCodeFramework() {
return byteCodeFramework;
}

public void setByteCodeFramework(final ByteCodeFramework byteCodeFramework) {
this.byteCodeFramework = byteCodeFramework;
}

@Override
public PowerMockConfiguration merge(final PowerMockConfiguration configuration) {
if (configuration == null) {
Expand All @@ -48,9 +58,13 @@ public PowerMockConfiguration merge(final PowerMockConfiguration configuration)
String[] globalIgnore = ArrayUtil.mergeArrays(this.globalIgnore, configuration.globalIgnore);

powerMockConfiguration.setGlobalIgnore(globalIgnore);
if (byteCodeFramework == null) {
powerMockConfiguration.setByteCodeFramework(configuration.byteCodeFramework);
}else {
powerMockConfiguration.setByteCodeFramework(byteCodeFramework);
}

return powerMockConfiguration;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public class ConfigurationFactoryImpl implements ConfigurationFactory {
private static final String USER_CONFIGURATION = "org/powermock/extensions/configuration.properties";
private static final String DEFAULT_CONFIGURATION = "org/powermock/default.properties";

@Override public <T extends Configuration<T>> T create(final Class<T> configurationType) {
@Override
public <T extends Configuration<T>> T create(final Class<T> configurationType) {
T configuration = readUserConfiguration(configurationType);
T defaultConfiguration = readDefault(configurationType);
return defaultConfiguration.merge(configuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public void map(final Properties properties) {
}
}

private void mapProperty(final PropertyDescriptor propertyDescriptor, final Properties properties)
throws IllegalAccessException, InvocationTargetException {
private void mapProperty(final PropertyDescriptor propertyDescriptor, final Properties properties) {

final ConfigurationKey key = new ConfigurationKey(ConfigurationType.forClass(configurationClass), propertyDescriptor.getName());
final String value = aliases.findValue((String) properties.get(key.toString()));
Expand Down Expand Up @@ -97,14 +96,15 @@ public String toString() {
}
}

@SuppressWarnings("unchecked")
private enum PropertyWriter {
ArrayWriter {
@Override
public void writeProperty(final PropertyDescriptor propertyDescriptor, final Object target, final String value) {
public void writeProperty(final PropertyDescriptor pd, final Object target, final String value) {
try {
if (value != null) {
String[] array = value.split(",");
propertyDescriptor.getWriteMethod().invoke(target, (Object) array);
pd.getWriteMethod().invoke(target, (Object) array);
}
} catch (Exception e) {
throw new PowerMockInternalException(e);
Expand All @@ -113,25 +113,49 @@ public void writeProperty(final PropertyDescriptor propertyDescriptor, final Obj
},
StringWriter {
@Override
public void writeProperty(final PropertyDescriptor propertyDescriptor, final Object target, final String value) {
public void writeProperty(final PropertyDescriptor pd, final Object target, final String value) {
try {
if (value != null) {
propertyDescriptor.getWriteMethod().invoke(target, value);
pd.getWriteMethod().invoke(target, value);
}
} catch (Exception e) {
throw new PowerMockInternalException(e);
}
}
},
EnumWriter {
@Override
public void writeProperty(final PropertyDescriptor pd, final Object target, final String value) {
try {
if (value != null) {
final Class<Enum<?>> enumClass = (Class<Enum<?>>) pd.getPropertyType();
final Enum<?>[] constants = enumClass.getEnumConstants();
for (Enum<?> constant : constants) {
if(value.equals(constant.name())){
pd.getWriteMethod().invoke(target, constant);
return;
}
}
throw new PowerMockInternalException(String.format(
"Find unknown enum constant `%s` for type `%s` during reading configuration.", value, enumClass
));
}
} catch (Exception e) {
throw new PowerMockInternalException(e);
}
}
};
public static PropertyWriter forProperty(final PropertyDescriptor propertyDescriptor) {
if (String[].class.isAssignableFrom(propertyDescriptor.getPropertyType())) {

private static PropertyWriter forProperty(final PropertyDescriptor pd) {
if (String[].class.isAssignableFrom(pd.getPropertyType())) {
return ArrayWriter;
} else if (Enum.class.isAssignableFrom(pd.getPropertyType())) {
return EnumWriter;
} else {
return StringWriter;
}
}

public abstract void writeProperty(final PropertyDescriptor propertyDescriptor, final Object target, final String value);
abstract void writeProperty(final PropertyDescriptor propertyDescriptor, final Object target, final String value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@

public class PowerMockInternalException extends RuntimeException{

private static final String MESSAGE = "PowerMock internal error has happened. This exception is thrown in unexpected cases, that normally should never happen. Please, report about the issue to PowerMock issues tacker. ";

public PowerMockInternalException(final Throwable cause) {
super("PowerMock internal error has happened. This exception is thrown in unexpected cases, that normally should never happen. Please, report about the issue to PowerMock issues tacker.", cause);
super(MESSAGE, cause);
}

public PowerMockInternalException(final String message) {
super(MESSAGE + message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ public MockClassLoaderBuilder addClassesToModify(String[] classesToModify) {

public MockClassLoaderBuilder addExtraMockTransformers(MockTransformer... mockTransformers) {
if (mockTransformers != null) {
extraMockTransformers.addAll(asList(mockTransformers));
for (MockTransformer mockTransformer : mockTransformers) {
if (mockTransformer != null) {
extraMockTransformers.add(mockTransformer);
}
}
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.powermock.core.classloader;

import org.powermock.configuration.GlobalConfiguration;
import org.powermock.core.classloader.annotations.MockPolicy;
import org.powermock.core.classloader.annotations.PrepareEverythingForTest;
import org.powermock.core.classloader.annotations.PrepareForTest;
Expand Down Expand Up @@ -100,14 +101,22 @@ private ByteCodeFramework getByteCodeFrameworkFor(final Method method) {
}

private ByteCodeFramework getByteCodeFramework() {
return getByteCodeFramework(testClass);
ByteCodeFramework byteCodeFramework = getByteCodeFramework(testClass);

if (byteCodeFramework == null){
byteCodeFramework = GlobalConfiguration.powerMockConfiguration().getByteCodeFramework();
}

return byteCodeFramework;
}

private ByteCodeFramework getByteCodeFramework(final AnnotatedElement element) {
if (element.isAnnotationPresent(PrepareForTest.class)) {
return element.getAnnotation(PrepareForTest.class).byteCodeFramework();
} else if (element.isAnnotationPresent(PrepareEverythingForTest.class)) {
return element.getAnnotation(PrepareEverythingForTest.class).byteCodeFramework();
} else if (element.isAnnotationPresent(SuppressStaticInitializationFor.class)){
return element.getAnnotation(SuppressStaticInitializationFor.class).byteCodeFramework();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.powermock.core.classloader.annotations;

import org.powermock.core.classloader.ByteCodeFramework;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
Expand All @@ -40,4 +42,6 @@
@Inherited
public @interface SuppressStaticInitializationFor {
String[] value() default "";

ByteCodeFramework byteCodeFramework() default ByteCodeFramework.Javassist;
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# suppress inspection "UnusedProperty" for whole file
powermock.global-ignore=org.powermock.core*
powermock.global-ignore=org.powermock.core*
powermock.byte-code-framework=Javassist
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import org.junit.Test;
import org.powermock.api.mockito.ConfigurationTestUtils;
import org.powermock.configuration.support.ConfigurationFactoryImpl;
import org.powermock.core.classloader.ByteCodeFramework;

import java.io.IOException;
import java.net.URISyntaxException;

import static org.assertj.core.api.Java6Assertions.assertThat;

Expand All @@ -32,13 +36,13 @@ public class ConfigurationFactoryImplTest {
private ConfigurationTestUtils util;

@Before
public void setUp() throws Exception {
public void setUp() {
configurationFactory = new ConfigurationFactoryImpl();
util = new ConfigurationTestUtils();
}

@After
public void tearDown() throws Exception {
public void tearDown() {
util.clear();
}

Expand Down Expand Up @@ -86,4 +90,20 @@ public void should_return_default_value_for_configuration_if_user_not_defined()
.as("Configuration is read correctly")
.contains("org.powermock.core*");
}

@Test
public void should_read_enum_values() throws IOException, URISyntaxException {

util.copyTemplateToPropertiesFile();

PowerMockConfiguration configuration = configurationFactory.create(PowerMockConfiguration.class);

assertThat(configuration)
.as("Configuration is created")
.isNotNull();

assertThat(configuration.getByteCodeFramework())
.as("Enum from configuration is read correctly")
.isEqualTo(ByteCodeFramework.Javassist);
}
}
Loading

0 comments on commit 8e851a1

Please sign in to comment.