Permalink
Browse files

Fixed issue #127

  • Loading branch information...
oshoukry committed Jan 4, 2019
1 parent a64e1c0 commit 75a473177366b7331446e16e18e039987db78720
@@ -29,36 +29,36 @@
import com.openpojo.validation.affirm.Affirm;
import com.openpojo.validation.rule.Rule;


/**
* There are three default accepted naming schemes for test classes.
* <ul>
* <li> Test ends with Test
* <li> Test begins with Test
* <li> Test ends with TestCase
* </ul>
*
* To override the accepted list use the {@link #TestClassMustBeProperlyNamedRule(Collection prefix, Collection postfix)
* TestClassMustBeProperlyNamedRule}
* <p>
* To override the accepted list use the {@link #TestClassMustBeProperlyNamedRule(Collection, Collection, Collection)}
* TestClassMustBeProperlyNamedRule
*
* @author oshoukry
*/
public class TestClassMustBeProperlyNamedRule implements Rule {

public static final String[] DEFAULT_PREFIX_TOKENS = { "Test" };
public static final String[] DEFAULT_SUFFIX_TOKENS = { "Test", "TestCase" };
public static final String[] DEFAULT_ANNOTATIONS = { "org.testng.annotations.Test", "org.junit.Test",
"org.junit.jupiter.api.Test" };

private final Collection<String> prefixes;
private final Collection<String> suffixes;

public static final String DEFAULT_ANNOTATIONS[] = { "org.testng.annotations.Test", "org.junit.Test" };
private final Collection<Class<? extends Annotation>> loadedAnnotations = new ArrayList<Class<? extends Annotation>>();

/**
* This constructor used the default is prefix "Test", suffixes "Test" &amp; "TestCase".
*/
public TestClassMustBeProperlyNamedRule() {
this(Arrays.asList(DEFAULT_PREFIX_TOKENS), Arrays.asList(DEFAULT_SUFFIX_TOKENS));
this(Arrays.asList(DEFAULT_PREFIX_TOKENS), Arrays.asList(DEFAULT_SUFFIX_TOKENS), Arrays.asList(DEFAULT_ANNOTATIONS));
}

/**
@@ -71,18 +71,23 @@ public TestClassMustBeProperlyNamedRule() {
* the suffix list to use
*/
@SuppressWarnings("unchecked")
public TestClassMustBeProperlyNamedRule(Collection<String> prefixes, Collection<String> suffixes) {
public TestClassMustBeProperlyNamedRule(Collection<String> prefixes,
Collection<String> suffixes,
Collection<String> annotations) {
this.prefixes = prefixes;
this.suffixes = suffixes;
for (String annotation : DEFAULT_ANNOTATIONS) {
for (String annotation : annotations) {
Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) ClassUtil.loadClass(annotation);
if (annotationClass != null) {
loadedAnnotations.add(annotationClass);
}
}

if (loadedAnnotations.size() == 0)
throw new IllegalStateException("No annotations loaded, expected any of " + Arrays.toString(DEFAULT_ANNOTATIONS));
if (loadedAnnotations.size() == 0) {
ArrayList<String> namedAnnotations = new ArrayList<String>();
namedAnnotations.addAll(annotations);
throw new IllegalStateException("No annotations loaded, expected any of " + namedAnnotations);
}
}

@SuppressWarnings("unchecked")
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2010-2019 Osman Shoukry
*
* Licensed 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 com.openpojo.validation.rule.impl;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertThat;

public class TestClassMustBeProperlyNamedRuleTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void shouldThrowExceptionIfNoAnnotationsLoaded() {
String noneExistentClass = this.getClass().getName() + "DoesNotExist";

thrown.expect(IllegalStateException.class);
thrown.expectMessage("No annotations loaded, expected any of [" + noneExistentClass + "]");

new TestClassMustBeProperlyNamedRule(getEmptyList(), getEmptyList(), Arrays.asList(noneExistentClass));
}

@Test
public void assertDefaultAnnotation() {
String[] expectedList = { "org.testng.annotations.Test", "org.junit.Test", "org.junit.jupiter.api.Test" };
assertThat(Arrays.asList(TestClassMustBeProperlyNamedRule.DEFAULT_ANNOTATIONS),
containsInAnyOrder(expectedList));
}

private List<String> getEmptyList() {
return Collections.<String>emptyList();
}
}

0 comments on commit 75a4731

Please sign in to comment.