Skip to content

Commit

Permalink
Merge pull request #269 from Xceptance/#262-bug-browser-configuration…
Browse files Browse the repository at this point in the history
…-of-super-class-is-not-overwritten

[#262] fix overwriting Browser configuration of super class
  • Loading branch information
wurzelkuchen authored Jul 11, 2024
2 parents 2248463 + d7e74bf commit f87d1b2
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.xceptance.neodymium.common.browser;

import java.lang.annotation.Annotation;
import java.lang.annotation.Repeatable;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.Arrays;
Expand Down Expand Up @@ -37,14 +40,74 @@ public void initClassAnnotationsFor(Class<?> testClass)

if (getAnnotations(testClass, SuppressBrowsers.class).isEmpty())
{
classBrowsers = getAnnotations(testClass, Browser.class).stream().map(annotation -> annotation.value()).distinct().collect(Collectors.toList());
classBrowsers = findBrowserRelatedClassAnnotation(testClass, Browser.class).stream().map(annotation -> annotation.value()).distinct()
.collect(Collectors.toList());
}
else
{
classBrowsers = new LinkedList<>();
}
}

public <T extends Annotation> List<T> findBrowserRelatedClassAnnotation(final Class<?> clazz, final Class<T> annotationToFind)
{
// this function is used to find the first (!) @Browser annotation on class level in the hierarchy
// furthermore its not the first but also the first that doesn't have @SuppressBrowsers annotated

if (clazz == null)
return new LinkedList<>();

// check class for browser annotation
// if class has browser annotation and no suppress browsers its fine, else take the super class and check again
List<T> browserAnnotations = getDeclaredAnnotations(clazz, annotationToFind);
List<SuppressBrowsers> suppressBrowsersAnnotations = getDeclaredAnnotations(clazz, SuppressBrowsers.class);

if (!suppressBrowsersAnnotations.isEmpty() || browserAnnotations.isEmpty())
{
return findBrowserRelatedClassAnnotation(clazz.getSuperclass(), annotationToFind);
}
else
{
return browserAnnotations;
}
}

@SuppressWarnings("unchecked")
public static <T extends Annotation> List<T> getDeclaredAnnotations(AnnotatedElement object, Class<T> annotationClass)
{
List<T> annotations = new LinkedList<>();
if (object == null || annotationClass == null)
{
return annotations;
}

// check if the annotation is repeatable
Repeatable repeatingAnnotation = annotationClass.getAnnotation(Repeatable.class);
Annotation annotation = (repeatingAnnotation == null) ? null : object.getDeclaredAnnotation(repeatingAnnotation.value());

if (annotation != null)
{
try
{
annotations.addAll(Arrays.asList((T[]) annotation.getClass().getMethod("value").invoke(annotation)));
}
catch (ReflectiveOperationException e)
{
throw new RuntimeException(e);
}
}
else
{
T anno = object.getDeclaredAnnotation(annotationClass);
if (anno != null)
{
annotations.add(anno);
}
}

return annotations;
}

public BrowserData()
{
populateBrowserDataWithGlobalInformation();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.xceptance.neodymium.junit4.testclasses.browser.inheritance;

import org.junit.Test;

import com.xceptance.neodymium.common.browser.Browser;

@Browser("Chrome_1024x768")
public class BrowserOverwrittingChild extends BrowserParent
{
@Test
public void test()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.xceptance.neodymium.junit4.testclasses.browser.inheritance;

import org.junit.runner.RunWith;

import com.xceptance.neodymium.common.browser.Browser;
import com.xceptance.neodymium.junit4.NeodymiumRunner;

@Browser("Chrome_1024x768")
@Browser("Chrome_1500x1000")
@RunWith(NeodymiumRunner.class)
public abstract class BrowserParent
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.xceptance.neodymium.junit4.testclasses.browser.classonly.RandomBrowserClassLevel;
import com.xceptance.neodymium.junit4.testclasses.browser.classonly.TwoClassBrowserOneMethod;
import com.xceptance.neodymium.junit4.testclasses.browser.classonly.TwoSameClassBrowserOneMethod;
import com.xceptance.neodymium.junit4.testclasses.browser.inheritance.BrowserOverwrittingChild;
import com.xceptance.neodymium.junit4.testclasses.browser.inheritance.RandomBrowsersChild;
import com.xceptance.neodymium.junit4.testclasses.browser.inheritance.RandomBrowsersOverwritingChild;
import com.xceptance.neodymium.junit4.testclasses.browser.methodonly.MethodBrowserSuppressNoBrowserAnnotation;
Expand Down Expand Up @@ -321,6 +322,18 @@ public void testMultibrowserConfiguration() throws Throwable
checkTestEnvironment2(browserProfiles.get("testEnvironmentFlags2"));
}

@Test
public void testBrowserOverwrittingInheritance() throws Throwable
{
String[] expected = new String[]
{
"test :: Browser Chrome_1024x768"
};
checkDescription(BrowserOverwrittingChild.class, expected);
Result result = JUnitCore.runClasses(BrowserOverwrittingChild.class);
checkPass(result, 1, 0);
}

private void checkChrome(BrowserConfiguration config)
{
Assert.assertNotNull(config);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.xceptance.neodymium.junit5.testclasses.browser.inheritance;

import com.xceptance.neodymium.common.browser.Browser;
import com.xceptance.neodymium.junit5.NeodymiumTest;

@Browser("Chrome_1024x768")
public class BrowserOverwrittingChild extends BrowserParent
{
@NeodymiumTest
public void test()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.xceptance.neodymium.junit5.testclasses.browser.inheritance;

import com.xceptance.neodymium.common.browser.Browser;

@Browser("Chrome_1024x768")
@Browser("Chrome_1500x1000")
public abstract class BrowserParent
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.xceptance.neodymium.junit5.testclasses.browser.classonly.RandomBrowserClassLevel;
import com.xceptance.neodymium.junit5.testclasses.browser.classonly.TwoClassBrowserOneMethod;
import com.xceptance.neodymium.junit5.testclasses.browser.classonly.TwoSameClassBrowserOneMethod;
import com.xceptance.neodymium.junit5.testclasses.browser.inheritance.BrowserOverwrittingChild;
import com.xceptance.neodymium.junit5.testclasses.browser.inheritance.RandomBrowsersChild;
import com.xceptance.neodymium.junit5.testclasses.browser.inheritance.RandomBrowsersOverwritingChild;
import com.xceptance.neodymium.junit5.testclasses.browser.methodonly.MethodBrowserSuppressNoBrowserAnnotation;
Expand Down Expand Up @@ -302,6 +303,18 @@ public void testMultibrowserConfiguration() throws Throwable
checkTestEnvironment2(browserProfiles.get("testEnvironmentFlags2"));
}

@Test
public void testBrowserOverwrittingInheritance() throws Throwable
{
String[] expected = new String[]
{
"test :: Browser Chrome_1024x768"
};
checkDescription(BrowserOverwrittingChild.class, expected);
NeodymiumTestExecutionSummary summary = run(BrowserOverwrittingChild.class);
checkPass(summary, 1, 0);
}

private void checkChrome(BrowserConfiguration config)
{
Assertions.assertNotNull(config);
Expand Down

0 comments on commit f87d1b2

Please sign in to comment.