Skip to content

Commit

Permalink
Added repeatable Tag annotation and TestDescriptor.getTags()
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Nov 7, 2015
1 parent 40aff3a commit 133d830
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 1 deletion.
Expand Up @@ -10,6 +10,7 @@

package org.junit.gen5.engine;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

Expand Down Expand Up @@ -57,6 +58,11 @@ public final Set<AbstractTestDescriptor> getChildren() {
return this.children;
}

@Override
public Set<TestTag> getTags() {
return Collections.emptySet();
}

@Override
public final boolean equals(Object other) {
if (other == null) {
Expand Down
Expand Up @@ -10,6 +10,8 @@

package org.junit.gen5.engine;

import java.util.Set;

/**
* @author Sam Brannen
* @since 5.0
Expand All @@ -30,4 +32,5 @@ public interface TestDescriptor {

boolean isTest();

Set<TestTag> getTags();
}
25 changes: 25 additions & 0 deletions junit-engine-api/src/main/java/org/junit/gen5/engine/TestTag.java
@@ -0,0 +1,25 @@
/*
* Copyright 2015 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;

public class TestTag {

private final String name;

public TestTag(String name) {
this.name = name;
}

public String getName() {
return name;
}

}
Expand Up @@ -10,9 +10,13 @@

package org.junit.gen5.engine.junit4;

import java.util.Collections;
import java.util.Set;

import lombok.Data;

import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.TestTag;
import org.junit.runner.Description;

@Data
Expand All @@ -26,6 +30,11 @@ public boolean isTest() {
return description.isTest();
}

@Override
public Set<TestTag> getTags() {
return Collections.emptySet();
}

@Override
public String getUniqueId() {
// TODO Use unique ID if set, too
Expand Down
28 changes: 28 additions & 0 deletions junit5-api/src/main/java/org/junit/gen5/api/Tag.java
@@ -0,0 +1,28 @@
/*
* Copyright 2015 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.api;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @since 5.0
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Tags.class)
public @interface Tag {

String value();
}
26 changes: 26 additions & 0 deletions junit5-api/src/main/java/org/junit/gen5/api/Tags.java
@@ -0,0 +1,26 @@
/*
* Copyright 2015 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.api;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @since 5.0
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Tags {

Tag[]value();
}
Expand Up @@ -10,12 +10,18 @@

package org.junit.gen5.engine.junit5.descriptor;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.junit.gen5.api.Name;
import org.junit.gen5.api.Tag;
import org.junit.gen5.commons.util.AnnotationUtils;
import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.commons.util.StringUtils;
import org.junit.gen5.engine.AbstractTestDescriptor;
import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.TestTag;

/**
* {@link TestDescriptor} for tests based on Java classes.
Expand Down Expand Up @@ -46,7 +52,25 @@ public String getDisplayName() {
return displayName;
}

@Override
public Set<TestTag> getTags() {
//Todo: Remove duplication with MethodDescriptor.getTags
Set<TestTag> tags = new HashSet<>();
// @formatter:off
Tag[] tagAnnotations = this.testClass.getAnnotationsByType(Tag.class);
// Todo: Implement AnnotationUtils.findAnnotations and use it to support meta annotations
Arrays.stream(tagAnnotations)
.map(Tag::value)
.filter(name -> !StringUtils.isBlank(name))
.forEach( tagName -> tags.add(new TestTag(tagName)));
// @formatter:on

return tags;
}

private String determineDisplayName() {
//Todo: Remove duplication with MethodDescriptor.determineDisplayName

// @formatter:off
return AnnotationUtils.findAnnotation(this.testClass, Name.class)
.map(Name::value)
Expand Down
Expand Up @@ -11,13 +11,18 @@
package org.junit.gen5.engine.junit5.descriptor;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.junit.gen5.api.Name;
import org.junit.gen5.api.Tag;
import org.junit.gen5.commons.util.AnnotationUtils;
import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.commons.util.StringUtils;
import org.junit.gen5.engine.AbstractTestDescriptor;
import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.TestTag;

/**
* {@link TestDescriptor} for tests based on Java methods.
Expand All @@ -40,6 +45,7 @@ public MethodTestDescriptor(String uniqueId, Method testMethod) {
}

private String determineDisplayName() {
//Todo: Remove duplication with ClassTestDescriptor.determineDisplayName
// @formatter:off
return AnnotationUtils.findAnnotation(this.testMethod, Name.class)
.map(Name::value)
Expand All @@ -48,6 +54,22 @@ private String determineDisplayName() {
// @formatter:on
}

@Override
public Set<TestTag> getTags() {
//Todo: Remove duplication with ClassTestDescriptor.getTags
Set<TestTag> tags = new HashSet<>();
// @formatter:off
Tag[] tagAnnotations = this.testMethod.getAnnotationsByType(Tag.class);
// Todo: Implement AnnotationUtils.findAnnotations and use it to support meta annotations
Arrays.stream(tagAnnotations)
.map(Tag::value)
.filter(name -> !StringUtils.isBlank(name))
.forEach( tagName -> tags.add(new TestTag(tagName)));
// @formatter:on

return tags;
}

@Override
public String getDisplayName() {
return displayName;
Expand Down
Expand Up @@ -18,9 +18,13 @@
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.gen5.api.Name;
import org.junit.gen5.api.Tag;
import org.junit.gen5.api.Test;
import org.junit.gen5.engine.TestTag;

/**
* Unit tests for {@link MethodTestDescriptor}.
Expand All @@ -29,6 +33,9 @@
* @author Stefan Bechtold
* @since 5.0
*/
@Tag("classTag1")
@Tag("classTag2")
@Name("custom class name")
public class MethodTestDescriptorTests {

@org.junit.Test
Expand All @@ -43,12 +50,32 @@ public void constructFromMethod() throws Exception {
}

@org.junit.Test
public void constructFromMethodWithCustomDisplayName() throws Exception {
public void constructFromMethodWithAnnotations() throws Exception {
Method testMethod = getClass().getDeclaredMethod("foo");
MethodTestDescriptor descriptor = new MethodTestDescriptor("any id", testMethod);

assertEquals(testMethod, descriptor.getTestMethod());
assertEquals("custom test name", descriptor.getDisplayName(), "display name:");

List<String> tags = descriptor.getTags().stream().map(TestTag::getName).collect(Collectors.toList());
assertEquals(2, descriptor.getTags().size());
assertTrue(tags.contains("methodTag1"));
assertTrue(tags.contains("methodTag2"));

}

@org.junit.Test
public void constructClassDescriptorWithAnnotations() throws Exception {
ClassTestDescriptor descriptor = new ClassTestDescriptor("any id", getClass());

assertEquals(getClass(), descriptor.getTestClass());
assertEquals("custom class name", descriptor.getDisplayName(), "display name:");

List<String> tags = descriptor.getTags().stream().map(TestTag::getName).collect(Collectors.toList());
assertEquals(2, descriptor.getTags().size());
assertTrue(tags.contains("classTag1"));
assertTrue(tags.contains("classTag2"));

}

@org.junit.Test
Expand Down Expand Up @@ -77,6 +104,8 @@ void test(String txt, BigDecimal sum) {

@Test
@Name("custom test name")
@Tag("methodTag1")
@Tag("methodTag2")
void foo() {
}

Expand Down

0 comments on commit 133d830

Please sign in to comment.