Skip to content

Commit

Permalink
TestSources implement Serializable
Browse files Browse the repository at this point in the history
Issue: #79

------------------------------------------------------------------------
On behalf of the community, the JUnit Lambda Team thanks
Samuraism, Inc. (http://samuraism.com/) for supporting the
JUnit crowdfunding campaign!
------------------------------------------------------------------------
  • Loading branch information
marcphilipp committed Jan 2, 2016
1 parent 5df9d78 commit 2df8323
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 23 deletions.
Expand Up @@ -11,19 +11,24 @@
package org.junit.gen5.engine;

import java.io.File;
import java.io.Serializable;
import java.util.Optional;

import org.junit.gen5.commons.util.Preconditions;

public class FileSystemSource implements TestSource {

final private File sourceFileOrDirectory;
private static final long serialVersionUID = 1L;

final private FilePosition positionInFile;
private final File sourceFileOrDirectory;
private final FilePosition positionInFile;

public FileSystemSource(File sourceFileOrDirectory) {
this(sourceFileOrDirectory, null);
}

public FileSystemSource(File sourceFileOrDirectory, FilePosition positionInFile) {
Preconditions.notNull(sourceFileOrDirectory, "source file or directory must not be null");
this.sourceFileOrDirectory = sourceFileOrDirectory;
this.positionInFile = positionInFile;
}
Expand Down Expand Up @@ -53,7 +58,9 @@ public boolean isFilePosition() {
return getPosition().isPresent();
}

static public class FilePosition {
static public class FilePosition implements Serializable {

private static final long serialVersionUID = 1L;

private final int line;
private final int column;
Expand All @@ -73,11 +80,24 @@ public int getColumn() {

}

public Optional<File> getFile() {
return Optional.ofNullable(sourceFileOrDirectory);
public File getFile() {
return sourceFileOrDirectory;
}

public Optional<FilePosition> getPosition() {
return Optional.ofNullable(positionInFile);
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder(sourceFileOrDirectory.getAbsolutePath());
getPosition().ifPresent(position -> {
builder.append(" [");
builder.append(position.getLine());
builder.append(':');
builder.append(position.getColumn());
builder.append(']');
});
return builder.toString();
}
}
Expand Up @@ -10,33 +10,39 @@

package org.junit.gen5.engine;

import static org.junit.gen5.commons.util.ObjectUtils.nullSafeToString;

import java.lang.reflect.Method;
import java.util.Optional;

public class JavaSource implements TestSource {

private final Class<?> javaClass;
private static final long serialVersionUID = 1L;

private final Method javaMethod;
private final Class<?> javaClass;
private final String javaMethodName;
private final Class<?>[] javaMethodParameterTypes;

public JavaSource(Class<?> clazz) {
javaClass = clazz;
javaMethod = null;
javaMethodName = null;
javaMethodParameterTypes = null;
}

public JavaSource(Method method) {
javaClass = method.getDeclaringClass();
javaMethod = method;
javaMethodName = method.getName();
javaMethodParameterTypes = method.getParameterTypes();
}

@Override
public boolean isJavaClass() {
return javaClass != null && javaMethod == null;
return javaClass != null && javaMethodName == null;
}

@Override
public boolean isJavaMethod() {
return javaMethod != null;
return javaMethodName != null;
}

@Override
Expand All @@ -58,18 +64,23 @@ public Optional<Class<?>> getJavaClass() {
return Optional.ofNullable(javaClass);
}

public Optional<Method> getJavaMethod() {
return Optional.ofNullable(javaMethod);
public Optional<String> getJavaMethodName() {
return Optional.ofNullable(javaMethodName);
}

public Optional<Class<?>[]> getJavaMethodParameterTypes() {
return Optional.ofNullable(javaMethodParameterTypes);
}

@Override
public String toString() {
// TODO Add parameters to method string
StringBuilder builder = new StringBuilder();
getJavaClass().ifPresent(clazz -> builder.append(clazz.getName()));
getJavaMethod().ifPresent(method -> {
builder.append('#');
builder.append(method.getName());
getJavaMethodName().ifPresent(method -> {
builder.append('#').append(method);
});
getJavaMethodParameterTypes().ifPresent(parameterTypes -> {
builder.append('(').append(nullSafeToString(parameterTypes)).append(')');
});
return builder.toString();
}
Expand Down
Expand Up @@ -10,7 +10,9 @@

package org.junit.gen5.engine;

public interface TestSource {
import java.io.Serializable;

public interface TestSource extends Serializable {

boolean isJavaClass();

Expand Down
@@ -0,0 +1,81 @@
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.gen5.engine;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.gen5.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.io.File;

import org.junit.gen5.api.Test;
import org.junit.gen5.engine.FileSystemSource.FilePosition;

class FileSystemSourceTests {

@Test
void nullSourceFileOrDirectoryYieldsException() {
assertThrows(IllegalArgumentException.class, () -> {
new FileSystemSource(null);
});
}

@Test
void directory() {
File directory = new File(".");
FileSystemSource source = new FileSystemSource(directory);

assertTrue(source.isDirectory());
assertFalse(source.isFile());
assertFalse(source.isFilePosition());
assertFalse(source.isJavaClass());
assertFalse(source.isJavaMethod());

assertEquals(directory, source.getFile());
assertThat(source.getPosition()).isEmpty();

assertEquals(directory.getAbsolutePath(), source.toString());
}

@Test
void fileWithoutPosition() throws Exception {
File file = spy(new File("test.txt"));
when(file.isDirectory()).thenReturn(false);
when(file.isFile()).thenReturn(true);

FileSystemSource source = new FileSystemSource(file);

assertFalse(source.isDirectory());
assertTrue(source.isFile());
assertFalse(source.isFilePosition());
assertFalse(source.isJavaClass());
assertFalse(source.isJavaMethod());

assertEquals(file, source.getFile());
assertThat(source.getPosition()).isEmpty();

assertEquals(file.getAbsolutePath(), source.toString());
}

@Test
void fileWithPosition() throws Exception {
File file = new File("test.txt");
FilePosition position = new FilePosition(42, 23);

FileSystemSource source = new FileSystemSource(file, position);

assertTrue(source.isFilePosition());
assertEquals(file, source.getFile());
assertThat(source.getPosition()).hasValue(position);
assertEquals(file.getAbsolutePath() + " [42:23]", source.toString());
}

}
@@ -0,0 +1,59 @@
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.gen5.engine;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.gen5.api.Assertions.*;

import java.lang.reflect.Method;

import org.junit.gen5.api.Test;
import org.junit.gen5.api.TestName;

class JavaSourceTests {

@Test
void classSource() {
Class<JavaSourceTests> testClass = JavaSourceTests.class;
JavaSource source = new JavaSource(testClass);

assertTrue(source.isJavaClass());
assertFalse(source.isJavaMethod());
assertFalse(source.isDirectory());
assertFalse(source.isFile());
assertFalse(source.isFilePosition());

assertThat(source.getJavaClass()).hasValue(testClass);
assertThat(source.getJavaMethodName()).isEmpty();
assertThat(source.getJavaMethodParameterTypes()).isEmpty();

assertEquals(testClass.getName(), source.toString());
}

@Test
void methodSource(@TestName String testName) throws Exception {
Class<JavaSourceTests> testClass = JavaSourceTests.class;
Method testMethod = testClass.getDeclaredMethod(testName, String.class);
JavaSource source = new JavaSource(testMethod);

assertTrue(source.isJavaMethod());
assertFalse(source.isJavaClass());
assertFalse(source.isDirectory());
assertFalse(source.isFile());
assertFalse(source.isFilePosition());

assertThat(source.getJavaClass()).hasValue(testClass);
assertThat(source.getJavaMethodName()).hasValue(testName);
assertThat(source.getJavaMethodParameterTypes().get()).containsExactly(String.class);

assertEquals(testClass.getName() + "#" + testName + "(" + String.class.getName() + ")", source.toString());
}
}
Expand Up @@ -13,7 +13,6 @@
import static org.apache.maven.surefire.report.SimpleReportEntry.ignored;
import static org.junit.gen5.engine.TestExecutionResult.Status.*;

import java.lang.reflect.Method;
import java.util.Optional;

import org.apache.maven.surefire.report.PojoStackTraceWriter;
Expand Down Expand Up @@ -65,19 +64,19 @@ private SimpleReportEntry createReportEntry(TestIdentifier testIdentifier, Optio
Optional<JavaSource> javaSource = getJavaSource(testIdentifier);
if (javaSource.isPresent() && javaSource.get().getJavaClass().isPresent()) {
Class<?> sourceClass = javaSource.get().getJavaClass().get();
Optional<Method> sourceMethod = javaSource.get().getJavaMethod();
StackTraceWriter stackTraceWriter = getStackTraceWriter(sourceClass, sourceMethod, throwable);
Optional<String> sourceMethodName = javaSource.get().getJavaMethodName();
StackTraceWriter stackTraceWriter = getStackTraceWriter(sourceClass, sourceMethodName, throwable);
return new SimpleReportEntry(getClassNameOrUniqueId(testIdentifier), testIdentifier.getDisplayName(),
stackTraceWriter, null);
}
return ignored(getClassNameOrUniqueId(testIdentifier), testIdentifier.getDisplayName(),
throwable.map(Throwable::getMessage).orElse(null));
}

private StackTraceWriter getStackTraceWriter(Class<?> sourceClass, Optional<Method> sourceMethod,
private StackTraceWriter getStackTraceWriter(Class<?> sourceClass, Optional<String> sourceMethodName,
Optional<Throwable> throwable) {
String className = sourceClass.getName();
String methodName = sourceMethod.map(Method::getName).orElse("");
String methodName = sourceMethodName.orElse("");
return new PojoStackTraceWriter(className, methodName, throwable.orElse(null));
}

Expand Down

0 comments on commit 2df8323

Please sign in to comment.