diff --git a/diagrams/PhasedDiagrams.drawio b/diagrams/PhasedDiagrams.drawio index af0ac8ae..36da0ceb 100644 --- a/diagrams/PhasedDiagrams.drawio +++ b/diagrams/PhasedDiagrams.drawio @@ -1,4 +1,4 @@ - + @@ -1366,391 +1366,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/src/main/java/com/adobe/campaign/tests/integro/phased/PhasedEventManager.java b/src/main/java/com/adobe/campaign/tests/integro/phased/PhasedEventManager.java index b99ceddf..d12eb9ba 100644 --- a/src/main/java/com/adobe/campaign/tests/integro/phased/PhasedEventManager.java +++ b/src/main/java/com/adobe/campaign/tests/integro/phased/PhasedEventManager.java @@ -27,8 +27,9 @@ public class PhasedEventManager { private static final Logger log = LogManager.getLogger(); - + static Map events = new HashMap<>(); private static ExecutorService eventExecutor = null; + private static List eventLogs = new ArrayList(); /** * Returns the declared event. if declared on the method. The declarations have the following precedence: @@ -37,11 +38,12 @@ public class PhasedEventManager { *
  • Declaration in @PhasedTest
  • *
  • When the property PHASED.EVENTS.NONINTERRUPTIVE is set
  • * Null is returned if no such declaration is present. + * * @param in_method The method we are examining * @return The event that is declared on the method. Null if there is no event declared for the method */ public static String fetchApplicableEvent(Method in_method) { - if (!PhasedTestManager.isPhasedTest(in_method) ) { + if (!PhasedTestManager.isPhasedTest(in_method)) { return null; } /* @@ -73,23 +75,53 @@ public static String fetchApplicableEvent(Method in_method) { ConfigValueHandlerPhased.EVENTS_NONINTERRUPTIVE.fetchValue(); */ - } - else if (in_method.getDeclaringClass().getDeclaredAnnotation(PhasedTest.class).eventClasses().length > 0) { + } else if (in_method.getDeclaringClass().getDeclaredAnnotation(PhasedTest.class).eventClasses().length > 0) { return in_method.getDeclaringClass().getDeclaredAnnotation(PhasedTest.class).eventClasses()[0]; } else if (ConfigValueHandlerPhased.EVENT_TARGET.isSet()) { - return PhasedTestManager.isPhasedTestTargetOfEvent(in_method) ? ConfigValueHandlerPhased.EVENTS_NONINTERRUPTIVE.fetchValue() : null; + return PhasedTestManager.isPhasedTestTargetOfEvent( + in_method) ? ConfigValueHandlerPhased.EVENTS_NONINTERRUPTIVE.fetchValue() : null; } else if (ConfigValueHandlerPhased.EVENTS_NONINTERRUPTIVE.isSet()) { return ConfigValueHandlerPhased.EVENTS_NONINTERRUPTIVE.fetchValue(); } return null; } - protected static enum EventMode {START, END}; + ; - static Map events = new HashMap<>(); + /** + * Returns the declared event. if declared on the method. The declarations have the following precedence: + *
      + *
    1. Declaration in @PhaseEvent
    2. + *
    3. Declaration in @PhasedTest
    4. + *
    5. When the property PHASED.EVENTS.NONINTERRUPTIVE is set
    6. + *
    Null is returned if no such declaration is present. + * + * @param in_scenario The class/scenario we are examining + * @return The event that is declared on the method. Null if there is no event declared for the method + */ + public static Class fetchApplicableEvent(Class in_scenario) { + Method l_foundMethod = Arrays.stream(in_scenario.getMethods()).filter(m -> fetchApplicableEvent(m) != null) + .findFirst().orElse(null); + if (l_foundMethod == null) { + return null; + } - private static List eventLogs = new ArrayList(); + try { + Class lr_eventClass = Class.forName(fetchApplicableEvent(l_foundMethod)); + + if (!NonInterruptiveEvent.class.isAssignableFrom(lr_eventClass)) { + throw new PhasedTestConfigurationException( + "The given event " + lr_eventClass.getTypeName() + " should extend the abstract class " + + NonInterruptiveEvent.class.getTypeName() + "."); + } + + return lr_eventClass; + } catch (ClassNotFoundException e) { + throw new PhasedTestConfigurationException( + "The given event " + fetchApplicableEvent(l_foundMethod) + " could not be found.", e); + } + } /** * Used for logging events @@ -114,27 +146,33 @@ protected static NonInterruptiveEvent startEvent(String in_event, String in_onAc if (eventExecutor == null) { eventExecutor = Executors.newSingleThreadExecutor(); } - log.info("Starting event {} for step {}.",in_event,in_onAccountOfStep); + log.info("Starting event {} for step {}.", in_event, in_onAccountOfStep); NonInterruptiveEvent nie = instantiateClassFromString(in_event); logEvent(EventMode.START, in_event, in_onAccountOfStep); events.put(in_onAccountOfStep, nie); nie.threadFuture = eventExecutor.submit(nie); + + //Check that the vent really starts while (nie.getState().equals(NonInterruptiveEvent.states.DEFINED)) { try { //log.debug("Waiting for event to start"); Thread.sleep(1); } catch (InterruptedException e) { Thread.currentThread().interrupt(); - throw new RuntimeException(e); + throw new PhasedTestingEventException("Un expected exception at startup",e); } } + //Check if the event had no issues if (nie.getState().equals(NonInterruptiveEvent.states.FAILURE)) { log.error("Event Exception : The event {} for step {} caused an exception during start.", in_event, in_onAccountOfStep); try { nie.threadFuture.get(); - } catch (InterruptedException | ExecutionException ex) { + } catch (ExecutionException ex) { ex.getCause().printStackTrace(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new PhasedTestingEventException("Un expected problem happened when waiting for th event to start.",e); } } @@ -153,14 +191,17 @@ private static NonInterruptiveEvent instantiateClassFromString(String in_event) Class eventClass = Class.forName(in_event); if (!NonInterruptiveEvent.class.isAssignableFrom(eventClass)) { - throw new PhasedTestConfigurationException("The given event "+in_event+ " should be a sub-class of the abstract class "+NonInterruptiveEvent.class.getTypeName()+"."); + throw new PhasedTestConfigurationException( + "The given event " + in_event + " should be a sub-class of the abstract class " + + NonInterruptiveEvent.class.getTypeName() + "."); } nie = (NonInterruptiveEvent) eventClass.newInstance(); } catch (IllegalAccessException | InstantiationException e) { - throw new PhasedTestConfigurationException("We have had a problem instantiating the event "+in_event+".", e); + throw new PhasedTestConfigurationException( + "We have had a problem instantiating the event " + in_event + ".", e); } catch (ClassNotFoundException e) { - throw new PhasedTestConfigurationException("The given event class "+in_event+" could not be found.", e); + throw new PhasedTestConfigurationException("The given event class " + in_event + " could not be found.", e); } return nie; } @@ -176,15 +217,16 @@ protected static NonInterruptiveEvent finishEvent(String in_event, String in_onA log.info("Finishing event {} for step {}.", in_event, in_onAccountOfStep); NonInterruptiveEvent l_activeEvent = events.get(in_onAccountOfStep); if (l_activeEvent == null) { - throw new PhasedTestException("No event of the type "+in_event+" was stored for the test step "+in_onAccountOfStep); + throw new PhasedTestException( + "No event of the type " + in_event + " was stored for the test step " + in_onAccountOfStep); } try { if (Class.forName(in_event) != l_activeEvent.getClass()) { - throw new PhasedTestException("The given class "+in_event+" does not exist."); + throw new PhasedTestException("The given class " + in_event + " does not exist."); } } catch (ClassNotFoundException e) { - throw new PhasedTestConfigurationException("Class "+in_event+" not found.",e); + throw new PhasedTestConfigurationException("Class " + in_event + " not found.", e); } //if (Phases.NON_INTERRUPTIVE.fetchType().startsWith("3")) { @@ -249,6 +291,7 @@ public static Map getEvents() { /** * Extracts the event for a given method. The choice of the event is based on where the event is declared. + * * @param in_testResult A result object for a test containing the annotation {@link PhaseEvent} * @return An event that can be executed with this method. Null if no event is applicable */ @@ -273,9 +316,10 @@ public static String fetchEvent(ITestResult in_testResult) { int l_currentShuffleGroupNr = PhasedTestManager.asynchronousExtractIndex(in_testResult); - int l_currentStep = PhasedTestManager.getMethodMap().get(ClassPathParser.fetchFullName(l_currentMethod)).methodOrderInExecution; + int l_currentStep = PhasedTestManager.getMethodMap() + .get(ClassPathParser.fetchFullName(l_currentMethod)).methodOrderInExecution; - if (l_currentStep == l_currentShuffleGroupNr) { + if (l_currentStep == l_currentShuffleGroupNr) { return fetchApplicableEvent(l_currentMethod); } return null; @@ -287,9 +331,11 @@ public static ExecutorService getEventExecutor() { } public static void stopEventExecutor() { - if (eventExecutor != null) + if (eventExecutor != null) { eventExecutor.shutdown(); + } } + protected static enum EventMode {START, END} } diff --git a/src/main/java/com/adobe/campaign/tests/integro/phased/PhasedTestManager.java b/src/main/java/com/adobe/campaign/tests/integro/phased/PhasedTestManager.java index 5d7779c5..2da8c6db 100644 --- a/src/main/java/com/adobe/campaign/tests/integro/phased/PhasedTestManager.java +++ b/src/main/java/com/adobe/campaign/tests/integro/phased/PhasedTestManager.java @@ -54,7 +54,7 @@ private PhasedTestManager() { static final String STD_PHASED_GROUP_PREFIX = "phased-shuffledGroup_"; static final String STD_PHASED_GROUP_SINGLE = "phased-singleRun"; - static final String STD_PHASED_GROUP_NIE_PREFIX = "phased-shuffledGroupNIE_"; + static final String STD_PHASED_GROUP_NIE_PREFIX = "ni_event_"; public static final String STD_MERGE_STEP_ERROR_PREFIX = "Phased Error: Failure in step "; @@ -553,7 +553,10 @@ public static Object[][] fetchProvidersShuffled(Method in_method, Phases in_phas + "_" + lt_nrAfterPhase; } else { - l_objectArrayPhased[rows][0] = STD_PHASED_GROUP_NIE_PREFIX + (rows+1); + Class l_event = PhasedEventManager.fetchApplicableEvent( + in_method.getDeclaringClass()); + + l_objectArrayPhased[rows][0] = STD_PHASED_GROUP_NIE_PREFIX + (l_event != null ? l_event.getSimpleName()+"_" : "" ) + (rows+1); } } @@ -593,7 +596,11 @@ public static Object[] fetchProvidersSingle(Method in_method) { } if (Phases.ASYNCHRONOUS.isSelected()) { - return new Object[] { STD_PHASED_GROUP_SINGLE }; + Class l_event = PhasedEventManager.fetchApplicableEvent( + in_method.getDeclaringClass()); + + return new Object[] { l_event == null ? STD_PHASED_GROUP_SINGLE : + PhasedTestManager.STD_PHASED_GROUP_NIE_PREFIX + l_event.getSimpleName() }; } return new Object[] {}; diff --git a/src/test/java/com/adobe/campaign/tests/integro/phased/PhasedTestManagerTests.java b/src/test/java/com/adobe/campaign/tests/integro/phased/PhasedTestManagerTests.java index da61c63e..2247d08d 100644 --- a/src/test/java/com/adobe/campaign/tests/integro/phased/PhasedTestManagerTests.java +++ b/src/test/java/com/adobe/campaign/tests/integro/phased/PhasedTestManagerTests.java @@ -14,9 +14,7 @@ import com.adobe.campaign.tests.integro.phased.data.*; import com.adobe.campaign.tests.integro.phased.data.befaft.PhasedSeries_M_SimpleClass; import com.adobe.campaign.tests.integro.phased.data.dp.*; -import com.adobe.campaign.tests.integro.phased.data.events.TestSINGLEWithEvent_eventAsAnnotation; -import com.adobe.campaign.tests.integro.phased.data.events.TestSINGLEWithEvent_eventAsExecProperty; -import com.adobe.campaign.tests.integro.phased.data.events.TestShuffled_eventPassedAsExecutionVariable; +import com.adobe.campaign.tests.integro.phased.data.events.*; import com.adobe.campaign.tests.integro.phased.exceptions.PhasedTestConfigurationException; import com.adobe.campaign.tests.integro.phased.exceptions.PhasedTestException; import com.adobe.campaign.tests.integro.phased.utils.ClassPathParser; @@ -274,7 +272,7 @@ public void exportingDataTwice() throws IOException { } /** - * Testing {@code PROP_PHASED_DATA_PATH} that when the property {@value ConfigValueHandlerPhased#PROP_PHASED_DATA_PATH.systemName} is set, that path is + * Testing {@code PROP_PHASED_DATA_PATH} that when the property is set, that path is * used. * * Author : gandomi @@ -917,6 +915,65 @@ public void testCreateDataProviderData_forNonInterruptive() throws NoSuchMethodE } + @Test + public void testCreateDataProviderData_forNonInterruptiveWithEvent() throws NoSuchMethodException { + Phases.ASYNCHRONOUS.activate(); + + var eventClass = MyNonInterruptiveEvent.class; + ConfigValueHandlerPhased.EVENTS_NONINTERRUPTIVE.activate(eventClass.getTypeName()); + + Map, List> l_myMap = new HashMap<>(); + + Method method1 = PhasedSeries_F_Shuffle.class.getMethod("step1", String.class); + Method method2 = PhasedSeries_F_Shuffle.class.getMethod("step2", String.class); + Method method3 = PhasedSeries_F_Shuffle.class.getMethod("step3", String.class); + + l_myMap.put(PhasedSeries_F_Shuffle.class, + Arrays.asList(ClassPathParser.fetchFullName(method1), ClassPathParser.fetchFullName(method2), + ClassPathParser.fetchFullName(method3))); + + Map l_result = PhasedTestManager.generatePhasedProviders(l_myMap, + Phases.getCurrentPhase()); + + assertThat("we need to have the expected key", l_result.containsKey(ClassPathParser.fetchFullName(method1))); + assertThat("The first method should have three entries", l_result.get(ClassPathParser.fetchFullName(method1)).nrOfProviders, equalTo(3)); + + assertThat("The first method should have two entries", l_result.get(ClassPathParser.fetchFullName(method2)).nrOfProviders, equalTo(3)); + + assertThat("The first method should have one entry", l_result.get(ClassPathParser.fetchFullName(method3)).nrOfProviders, equalTo(3)); + + assertThat("We should have the same amount of total sizes", l_result.get(ClassPathParser.fetchFullName(method1)).totalClassMethods, + equalTo(l_result.get(ClassPathParser.fetchFullName(method2)).totalClassMethods)); + assertThat("We should have the same amount of total sizes", l_result.get(ClassPathParser.fetchFullName(method1)).totalClassMethods, + equalTo(l_result.get(ClassPathParser.fetchFullName(method3)).totalClassMethods)); + + Object[][] l_providerA = PhasedTestManager.fetchProvidersShuffled(method1); + + assertThat(l_providerA[0].length, equalTo(1)); + + var expectedPrefix = PhasedTestManager.STD_PHASED_GROUP_NIE_PREFIX +eventClass.getSimpleName(); + assertThat(l_providerA[0][0], equalTo(expectedPrefix+ "_1")); + assertThat(l_providerA[1][0], equalTo(expectedPrefix+ "_2")); + assertThat(l_providerA[2][0], equalTo(expectedPrefix + "_3")); + + Object[][] l_providerB = PhasedTestManager.fetchProvidersShuffled(method2); + + assertThat(l_providerB[0].length, equalTo(1)); + + assertThat(l_providerB[0][0], equalTo(expectedPrefix + "_1")); + assertThat(l_providerB[1][0], equalTo(expectedPrefix + "_2")); + assertThat(l_providerB[2][0], equalTo(expectedPrefix + "_3")); + + Object[][] l_providerC = PhasedTestManager.fetchProvidersShuffled(method3); + + assertThat(l_providerC[0].length, equalTo(1)); + + assertThat(l_providerC[0][0], equalTo(expectedPrefix + "_1")); + assertThat(l_providerC[1][0], equalTo(expectedPrefix + "_2")); + assertThat(l_providerC[2][0], equalTo(expectedPrefix + "_3")); + + } + @Test public void testPhasedManagerContext() { @@ -1135,7 +1192,7 @@ public void testFetchProvidersSingle_Consumer_InactiveNoExec() //NIE @Test - public void testFetchProvidersSingle_asynchronousNonInterruptive() + public void testFetchProvidersSingle_NIE_eventDefined() throws NoSuchMethodException, SecurityException { Phases.ASYNCHRONOUS.activate(); @@ -1146,14 +1203,50 @@ public void testFetchProvidersSingle_asynchronousNonInterruptive() Object[] l_providerStep1 = PhasedTestManager.fetchProvidersSingle(l_myMethod1); assertThat("We should have a parameter for this method",l_providerStep1.length, equalTo(1)); + //System.out.println(PhasedEventManager.fetchEvent(l_myMethod1)); + var l_expectedGroup = PhasedTestManager.STD_PHASED_GROUP_NIE_PREFIX + "MyNonInterruptiveEvent"; + assertThat("We should have the correct parameter for this method",l_providerStep1[0], equalTo(l_expectedGroup)); - Object[] l_providerStep2 = PhasedTestManager.fetchProvidersSingle(l_myMethod1); + Object[] l_providerStep2 = PhasedTestManager.fetchProvidersSingle(l_myMethod2); assertThat("We should have a parameter for this method",l_providerStep2.length, equalTo(1)); + assertThat("We should have the correct parameter for this method",l_providerStep2[0], equalTo(l_expectedGroup)); + + + Object[] l_providerStep3 = PhasedTestManager.fetchProvidersSingle(l_myMethod3); + + assertThat("We should have a parameter for this method",l_providerStep3.length, equalTo(1)); + assertThat("We should have the correct parameter for this method",l_providerStep3[0], equalTo(l_expectedGroup)); + } + + + @Test + public void testFetchProvidersSingle_NIE_eventSet() + throws NoSuchMethodException, SecurityException { + + Phases.ASYNCHRONOUS.activate(); + ConfigValueHandlerPhased.EVENTS_NONINTERRUPTIVE.activate(NI_Event2.class.getTypeName()); + final Method l_myMethod1 = TestShuffled_eventPassedAsExecutionVariable.class.getMethod("step1", String.class); + final Method l_myMethod2 = TestShuffled_eventPassedAsExecutionVariable.class.getMethod("step2", String.class); + final Method l_myMethod3 = TestShuffled_eventPassedAsExecutionVariable.class.getMethod("step3", String.class); + + Object[] l_providerStep1 = PhasedTestManager.fetchProvidersSingle(l_myMethod1); + var l_expectedGroup = PhasedTestManager.STD_PHASED_GROUP_NIE_PREFIX + "NI_Event2"; + + assertThat("We should have a parameter for this method",l_providerStep1.length, equalTo(1)); + + assertThat("We should have the correct parameter for this method",l_providerStep1[0], equalTo(l_expectedGroup)); + + Object[] l_providerStep2 = PhasedTestManager.fetchProvidersSingle(l_myMethod2); + + assertThat("We should have a parameter for this method",l_providerStep2.length, equalTo(1)); + assertThat("We should have the correct parameter for this method",l_providerStep2[0], equalTo(l_expectedGroup)); + - Object[] l_providerStep3 = PhasedTestManager.fetchProvidersSingle(l_myMethod1); + Object[] l_providerStep3 = PhasedTestManager.fetchProvidersSingle(l_myMethod3); assertThat("We should have a parameter for this method",l_providerStep3.length, equalTo(1)); + assertThat("We should have the correct parameter for this method",l_providerStep3[0], equalTo(l_expectedGroup)); } diff --git a/src/test/java/com/adobe/campaign/tests/integro/phased/TestPhasedNonInterruptive.java b/src/test/java/com/adobe/campaign/tests/integro/phased/TestPhasedNonInterruptive.java index 4e8fe90e..62a2de46 100644 --- a/src/test/java/com/adobe/campaign/tests/integro/phased/TestPhasedNonInterruptive.java +++ b/src/test/java/com/adobe/campaign/tests/integro/phased/TestPhasedNonInterruptive.java @@ -11,6 +11,7 @@ */ package com.adobe.campaign.tests.integro.phased; +import com.adobe.campaign.tests.integro.phased.data.NormalSeries_A; import com.adobe.campaign.tests.integro.phased.data.PhasedSeries_F_Shuffle; import com.adobe.campaign.tests.integro.phased.data.PhasedTestShuffledWithoutCanShuffleNested; import com.adobe.campaign.tests.integro.phased.data.events.*; @@ -21,6 +22,7 @@ import com.adobe.campaign.tests.integro.phased.utils.MockTestTools; import com.adobe.campaign.tests.integro.phased.utils.TestTools; import org.hamcrest.Matchers; +import org.testng.Assert; import org.testng.ITestResult; import org.testng.TestListenerAdapter; import org.testng.TestNG; @@ -465,9 +467,12 @@ public void testNonInterruptive_ParellelHardCoded_SINGLE() { assertThat("The first element should be an event defined in step2", l_eventStartLog.getEventName(), Matchers.equalTo(MyNonInterruptiveEvent.class.getTypeName())); + + var l_shaffleGroup = PhasedTestManager.STD_PHASED_GROUP_NIE_PREFIX+MyNonInterruptiveEvent.class.getSimpleName(); + assertThat("The first element should be an event started by step2", l_eventStartLog.getPhasedStepName(), Matchers.equalTo( - "com.adobe.campaign.tests.integro.phased.data.events.TestSINGLEWithEvent_eventAsAnnotation.step2(phased-singleRun)")); + "com.adobe.campaign.tests.integro.phased.data.events.TestSINGLEWithEvent_eventAsAnnotation.step2("+l_shaffleGroup+")")); ITestResult l_testSubjectedToEvent = tla.getPassedTests().stream().filter(t -> t.getName().equals("step2")) .collect(Collectors.toList()).get(0); @@ -482,7 +487,7 @@ public void testNonInterruptive_ParellelHardCoded_SINGLE() { Matchers.equalTo(MyNonInterruptiveEvent.class.getTypeName())); assertThat("The second element should be an event started by step2", l_eventEndLog.getPhasedStepName(), Matchers.equalTo( - "com.adobe.campaign.tests.integro.phased.data.events.TestSINGLEWithEvent_eventAsAnnotation.step2(phased-singleRun)")); + "com.adobe.campaign.tests.integro.phased.data.events.TestSINGLEWithEvent_eventAsAnnotation.step2("+l_shaffleGroup+")")); assertThat("Our test should have started before the end of the event", l_testSubjectedToEvent.getStartMillis(), lessThan(l_eventEndLog.getEventDate().getTime())); @@ -535,9 +540,12 @@ public void testNonInterruptive_ParellelConfigured_SINGLELegacy() { assertThat("The first element should be an event defined in step2", l_eventStartLog.getEventName(), Matchers.equalTo(MyNonInterruptiveEvent.class.getTypeName())); + + var l_shaffleGroup = PhasedTestManager.STD_PHASED_GROUP_NIE_PREFIX+MyNonInterruptiveEvent.class.getSimpleName(); + assertThat("The first element should be an event started by step2", l_eventStartLog.getPhasedStepName(), Matchers.equalTo( - l_testClass.getTypeName() + ".step2(phased-singleRun)")); + l_testClass.getTypeName() + ".step2("+l_shaffleGroup+")")); ITestResult l_testSubjectedToEvent = tla.getPassedTests().stream().filter(t -> t.getName().equals("step2")) .collect(Collectors.toList()).get(0); @@ -552,7 +560,7 @@ public void testNonInterruptive_ParellelConfigured_SINGLELegacy() { Matchers.equalTo(MyNonInterruptiveEvent.class.getTypeName())); assertThat("The second element should be an event started by step2", l_eventEndLog.getPhasedStepName(), Matchers.equalTo( - l_testClass.getTypeName() + ".step2(phased-singleRun)")); + l_testClass.getTypeName() + ".step2("+l_shaffleGroup+")")); assertThat("Our test should have started before the end of the event", l_testSubjectedToEvent.getStartMillis(), lessThan(l_eventEndLog.getEventDate().getTime())); @@ -605,9 +613,10 @@ public void testNonInterruptive_ParellelConfigured_SINGLE() { assertThat("The first element should be an event defined in step2", l_eventStartLog.getEventName(), Matchers.equalTo(MyNonInterruptiveEvent.class.getTypeName())); + var l_shaffleGroup = PhasedTestManager.STD_PHASED_GROUP_NIE_PREFIX+MyNonInterruptiveEvent.class.getSimpleName(); assertThat("The first element should be an event started by step2", l_eventStartLog.getPhasedStepName(), Matchers.equalTo( - l_testClass.getTypeName() + ".step2(phased-singleRun)")); + l_testClass.getTypeName() + ".step2(" + l_shaffleGroup + ")")); ITestResult l_testSubjectedToEvent = tla.getPassedTests().stream().filter(t -> t.getName().equals("step2")) .collect(Collectors.toList()).get(0); @@ -622,7 +631,7 @@ public void testNonInterruptive_ParellelConfigured_SINGLE() { Matchers.equalTo(MyNonInterruptiveEvent.class.getTypeName())); assertThat("The second element should be an event started by step2", l_eventEndLog.getPhasedStepName(), Matchers.equalTo( - l_testClass.getTypeName() + ".step2(phased-singleRun)")); + l_testClass.getTypeName() + ".step2(" + l_shaffleGroup + ")")); assertThat("Our test should have started before the end of the event", l_testSubjectedToEvent.getStartMillis(), lessThan(l_eventEndLog.getEventDate().getTime())); @@ -1054,6 +1063,7 @@ public void testNonInterruptive_23_Targeted() { Matchers.equalTo(2)); } + /** * This is a test for non-intyerruptive events in shuffled classes */ @@ -1184,4 +1194,76 @@ public void testNonInterruptive_ParellelConfiguredOnPhaseTestAnnotation_Shuffled PhasedEventManager.getEventLogs().size(), Matchers.equalTo(6)); } + + @Test + public void testEventAppliesTo_specificEventAnnotationOnMethod() throws NoSuchMethodException { + //Case 1 Event Declared on PhasedEvent annotation + Method l_methodWithEvent = TestSINGLEWithEvent_eventAsAnnotation.class.getMethod("step2", String.class); + + assertThat("We should correctly extract the event from the method", + PhasedEventManager.fetchApplicableEvent(l_methodWithEvent), + equalTo(MyNonInterruptiveEvent.class.getTypeName())); + + assertThat("We should correctly identify that this class could be subject to a non-interruptive event", + PhasedEventManager.fetchApplicableEvent(TestSINGLEWithEvent_eventAsAnnotation.class), + Matchers.equalTo(MyNonInterruptiveEvent.class)); + } + + @Test + public void testEventAppliesTo_eventAnnotationOnMethod() throws NoSuchMethodException { + //Case 2A Event Declared as Exec property with none set + assertThat("We should correctly extract the event from the method", + PhasedEventManager.fetchApplicableEvent(TestSINGLEWithEvent_eventAsExecProperty.class), nullValue()); + + ConfigValueHandlerPhased.EVENTS_NONINTERRUPTIVE.activate(MyNonInterruptiveEvent.class.getTypeName()); + + //Case 2B Event Declared as Exec property with none set + assertThat("We should correctly extract the event from the method", + PhasedEventManager.fetchApplicableEvent(TestSINGLEWithEvent_eventAsExecProperty.class), + Matchers.equalTo(MyNonInterruptiveEvent.class)); + } + + @Test + public void testEventAppliesTo_eventInjected() throws NoSuchMethodException { + + assertThat("By default this scenario is not subject to an event", + PhasedEventManager.fetchApplicableEvent(PhasedSeries_F_Shuffle.class), + Matchers.nullValue()); + + //Case 3 Normal test where the event is injected into the + Method l_targettedMethodWithoutEvent = PhasedSeries_F_Shuffle.class.getMethod("step2", String.class); + ConfigValueHandlerPhased.EVENTS_NONINTERRUPTIVE.activate(MyNonInterruptiveEvent.class.getTypeName()); + ConfigValueHandlerPhased.EVENT_TARGET.activate(ClassPathParser.fetchFullName(l_targettedMethodWithoutEvent)); + + assertThat("Now that one of the methods is targetted, we should correctly extract the event from the method", + PhasedEventManager.fetchApplicableEvent(TestSINGLEWithEvent_eventAsExecProperty.class), + Matchers.equalTo(MyNonInterruptiveEvent.class)); + + } + + + @Test + public void testEventAppliesTo_noEventAnnotationClass() throws NoSuchMethodException { + + //Case 2B Event Declared as Exec property with none set + assertThat("We should correctly extract the event from the method", + PhasedEventManager.fetchApplicableEvent(TestShuffled_eventDefinedOnPhasedTestAnnotation.class), + Matchers.equalTo(MyNonInterruptiveEvent.class)); + } + + @Test + public void testEventAppliesTo_NEGATIVE_nonExitingEvent() throws NoSuchMethodException { + ConfigValueHandlerPhased.EVENTS_NONINTERRUPTIVE.activate("i.really.do.not.Exist"); + Assert.assertThrows(PhasedTestConfigurationException.class, () -> + PhasedEventManager.fetchApplicableEvent(PhasedSeries_F_Shuffle.class)); + } + + @Test + public void testEventAppliesTo_NEGATIVE_eventNotAnEvent() throws NoSuchMethodException { + + assertThat("", NonInterruptiveEvent.class.isAssignableFrom(MyNonInterruptiveEvent.class)); + ConfigValueHandlerPhased.EVENTS_NONINTERRUPTIVE.activate(NormalSeries_A.class.getTypeName()); + Assert.assertThrows(PhasedTestConfigurationException.class, () -> + PhasedEventManager.fetchApplicableEvent(PhasedSeries_F_Shuffle.class)); + } }