Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Commit

Permalink
Merge pull request #157 from Cognifide/155
Browse files Browse the repository at this point in the history
#155 - changed logic for registering steps implementation classes to …
  • Loading branch information
mmanski authored Apr 4, 2017
2 parents e5853bd + f0625dc commit b3a43e4
Showing 1 changed file with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
/*
* Copyright 2016 Cognifide Ltd..
*
* 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
* 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
* 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.
* 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.cognifide.qa.bb.loadable.mapper;

import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.spi.TypeListener;

import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.runtime.java.StepDefAnnotation;
import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.runtime.java.guice.ScenarioScoped;

import java.lang.annotation.Annotation;
import java.util.Arrays;

public class TestObjectTypeListener implements TypeListener {

@Override
Expand All @@ -36,11 +40,32 @@ public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {

private <I> boolean isApplicable(Class<? super I> rawType) {
boolean result;
if (rawType.isAnnotationPresent(RunWith.class) && !rawType.isAnnotationPresent(CucumberOptions.class)) {
if (rawType.isAnnotationPresent(RunWith.class)
&& !rawType.isAnnotationPresent(CucumberOptions.class)) {
result = true;
} else {
result = rawType.isAnnotationPresent(ScenarioScoped.class);
result = isStepsImplementationClass(rawType);
}
return result;
}

private boolean isStepsImplementationClass(Class clazz) {
return clazz.isAnnotationPresent(ScenarioScoped.class)
|| Arrays.stream(clazz.getDeclaredMethods()).anyMatch(method ->
Arrays.stream(method.getAnnotations())
.anyMatch(annotation ->
isHookAnnotation(annotation) || isStepDefinitionAnnotation(annotation)
));
}

private boolean isHookAnnotation(Annotation annotation) {
Class annotationType = annotation.annotationType();
return annotationType.equals(Before.class) || annotationType.equals(After.class);
}

private boolean isStepDefinitionAnnotation(Annotation annotation) {
Class annotationType = annotation.annotationType();
return annotationType.getAnnotation(StepDefAnnotation.class) != null;
}

}

0 comments on commit b3a43e4

Please sign in to comment.