Skip to content

Commit

Permalink
Added missing tests. Apply integration tests in Gradle if Maven profi…
Browse files Browse the repository at this point in the history
…le is set.
  • Loading branch information
raphw committed Aug 31, 2016
1 parent 06df257 commit f24fe98
Show file tree
Hide file tree
Showing 42 changed files with 1,651 additions and 21 deletions.
1 change: 1 addition & 0 deletions byte-buddy-gradle-plugin/build.gradle
Expand Up @@ -20,6 +20,7 @@ dependencies {
compile group: group, name: 'byte-buddy', version: version
testCompile gradleTestKit()
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.mockito', name: 'mockito-core', version: '1.10.19'
}

task javadocJar(type: Jar, dependsOn: javadoc) {
Expand Down
@@ -1,4 +1,4 @@
#Wed Aug 31 09:16:34 EEST 2016
#Wed Aug 31 15:32:56 CEST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
3 changes: 3 additions & 0 deletions byte-buddy-gradle-plugin/pom.xml
Expand Up @@ -83,6 +83,9 @@
<task>build</task>
<task>javadocJar</task>
</tasks>
<jvmArgs>
<jvmArg>-Dnet.bytebuddy.test.integration=${bytebuddy.integration}</jvmArg>
</jvmArgs>
</configuration>
</execution>
</executions>
Expand Down
Expand Up @@ -8,6 +8,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
* Representation of a Gradle configuration for Byte Buddy.
Expand Down Expand Up @@ -39,7 +40,10 @@ public class ByteBuddyExtension {
*/
private boolean failOnLiveInitializer;

private List<String> tasks;
/**
* A list of task names for which to apply a transformation or {@code null} if the task should apply to all tasks.
*/
private Set<String> tasks;

/**
* Creates a new Byte Buddy extension.
Expand Down Expand Up @@ -156,7 +160,7 @@ public boolean implies(Task task) {
*
* @param tasks The tasks to explicitly append a transformation to.
*/
public void setTasks(List<String> tasks) {
public void setTasks(Set<String> tasks) {
this.tasks = tasks;
}
}
@@ -0,0 +1,114 @@
package net.bytebuddy.build.gradle;

import org.gradle.api.Project;

import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
* A log handler for only printing Byte Buddy specific log-messages if debug logging is enabled for a plugin.
*/
public class ByteBuddyLogHandler extends Handler {

private final Project project;

/**
* The Byte Buddy logger target.
*/
private final Logger logger;

/**
* {@code true} if parent handler delegation was originally enabled.
*/
private final boolean useParentHandlers;

/**
* Creates a new log handler.
*
* @param log The Maven logging target.
* @param logger The Byte Buddy logger target.
* @param useParentHandlers {@code true} if parent handler delegation was originally enabled.
*/
protected ByteBuddyLogHandler(Project project, Logger logger, boolean useParentHandlers) {
this.project = project;
this.logger = logger;
this.useParentHandlers = useParentHandlers;
setFormatter(new SimpleFormatter());
}

/**
* Initializes the Byte Buddy log handler.
*
* @param log The Maven logging target.
* @return The registered log handler.
*/
public static ByteBuddyLogHandler initialize(Project project) {
Logger logger = Logger.getLogger("net.bytebuddy");
ByteBuddyLogHandler handler = new ByteBuddyLogHandler(project, logger, logger.getUseParentHandlers());
try {
logger.setUseParentHandlers(false);
logger.addHandler(handler);
} catch (SecurityException exception) {
project.getLogger().warn("Cannot configure Byte Buddy logging", exception);
}
return handler;
}

/**
* Resets the configuration to its original state.
*/
public void reset() {
try {
logger.setUseParentHandlers(useParentHandlers);
logger.removeHandler(this);
} catch (SecurityException exception) {
project.getLogger().warn("Cannot configure Byte Buddy logging", exception);
}
}

@Override
public void publish(LogRecord record) {
if (project.getLogger().isDebugEnabled()) {
project.getLogger().debug(getFormatter().format(record));
}
}

@Override
public void flush() {
/* do nothing */
}

@Override
public void close() {
/* do nothing */
}

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
ByteBuddyLogHandler that = (ByteBuddyLogHandler) object;
return useParentHandlers == that.useParentHandlers
&& logger.equals(that.logger)
&& project.equals(that.project);
}

@Override
public int hashCode() {
int result = project.hashCode();
result = 31 * result + logger.hashCode();
result = 31 * result + (useParentHandlers ? 1 : 0);
return result;
}

@Override
public String toString() {
return "ByteBuddyLogHandler{" +
"project=" + project +
" ,logger=" + logger +
" ,useParentHandlers=" + useParentHandlers +
'}';
}
}
Expand Up @@ -43,7 +43,7 @@ public void setEntryPoint(String entryPoint) {
* @param classPath The class path of the current task.
* @return A resolved entry point.
*/
public EntryPoint toEntryPoint(ClassLoaderResolver classLoaderResolver, File root, Iterable<? extends File> classPath) {
public EntryPoint getEntryPoint(ClassLoaderResolver classLoaderResolver, File root, Iterable<? extends File> classPath) {
if (entryPoint == null || entryPoint.isEmpty()) {
throw new GradleException("Entry point name is not defined");
}
Expand Down
Expand Up @@ -25,7 +25,7 @@ public class PostCompilationAction implements Action<AbstractCompile> {
* @param project The current project.
* @param byteBuddyExtension The Byte Buddy extension of this build.
*/
public PostCompilationAction(Project project, ByteBuddyExtension byteBuddyExtension) {
protected PostCompilationAction(Project project, ByteBuddyExtension byteBuddyExtension) {
this.project = project;
this.byteBuddyExtension = byteBuddyExtension;
}
Expand All @@ -45,7 +45,7 @@ public void execute(AbstractCompile task) {
if (byteBuddyExtension.implies(task)) {
task.doLast(new TransformationAction(project, byteBuddyExtension, task));
} else {
project.getLogger().info("Skipping {}", task.getName());
project.getLogger().info("Skipping non-specified task {}", task.getName());
}
}
}
Expand Up @@ -61,10 +61,13 @@ public TransformationAction(Project project, ByteBuddyExtension extension, Abstr

@Override
public void execute(Task task) {
ByteBuddyLogHandler byteBuddyLogHandler = ByteBuddyLogHandler.initialize(project);
try {
processOutputDirectory(this.task.getDestinationDir(), this.task.getClasspath());
} catch (IOException exception) {
throw new GradleException("Error accessing file system", exception);
} finally {
byteBuddyLogHandler.reset();
}
}

Expand All @@ -83,17 +86,20 @@ private void processOutputDirectory(File root, Iterable<? extends File> classPat
try {
List<Plugin> plugins = new ArrayList<Plugin>(byteBuddyExtension.getTransformations().size());
for (Transformation transformation : byteBuddyExtension.getTransformations()) {
String plugin = transformation.getPlugin();
try {
String plugin = transformation.getPlugin();
plugins.add((Plugin) Class.forName(plugin, false, classLoaderResolver.resolve(transformation.getClassPath(root, classPath)))
.getDeclaredConstructor()
.newInstance());
project.getLogger().info("Created plugin: {}", plugin);
} catch (Exception exception) {
if (exception instanceof GradleException) {
throw (GradleException) exception;
}
throw new GradleException("Cannot create plugin: " + transformation.getRawPlugin(), exception);
}
}
EntryPoint entryPoint = byteBuddyExtension.getInitialization().toEntryPoint(classLoaderResolver, root, classPath);
EntryPoint entryPoint = byteBuddyExtension.getInitialization().getEntryPoint(classLoaderResolver, root, classPath);
project.getLogger().info("Resolved entry point: {}", entryPoint);
transform(root, classPath, entryPoint, plugins);
} finally {
Expand Down
@@ -0,0 +1,45 @@
package net.bytebuddy.build.gradle;

import net.bytebuddy.test.utility.MockitoRule;
import net.bytebuddy.test.utility.ObjectPropertyAssertion;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.mockito.Mock;

import java.io.File;
import java.util.Collections;
import java.util.Iterator;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class AbstractUserConfigurationPrefixIterableTest {

@Rule
public TestRule mockitoRule = new MockitoRule(this);

@Mock
private File primary, other;

@Test
public void testIteration() throws Exception {
Iterator<? extends File> iterator = new AbstractUserConfiguration.PrefixIterable(primary, Collections.singleton(other)).iterator();
assertThat(iterator.hasNext(), is(true));
assertThat(iterator.next(), is(primary));
assertThat(iterator.hasNext(), is(true));
assertThat(iterator.next(), is(other));
assertThat(iterator.hasNext(), is(false));
}

@Test(expected = UnsupportedOperationException.class)
public void testRemoval() throws Exception {
new AbstractUserConfiguration.PrefixIterable(primary, Collections.<File>emptySet()).iterator().remove();
}

@Test
public void testObjectProperties() throws Exception {
ObjectPropertyAssertion.of(AbstractUserConfiguration.PrefixIterable.class).applyBasic();
ObjectPropertyAssertion.of(AbstractUserConfiguration.PrefixIterable.PrefixIterator.class).applyBasic();
}
}

0 comments on commit f24fe98

Please sign in to comment.