-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from MontiCore/fix/statistic-listener-nullpointer
Fix nullpointer in case of missing start time
- Loading branch information
Showing
2 changed files
with
121 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
monticore-generator/src/test/java/de/monticore/gradle/StatisticListenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* (c) https://github.com/MontiCore/monticore */ | ||
package de.monticore.gradle; | ||
|
||
import org.gradle.api.Project; | ||
import org.gradle.api.Task; | ||
import org.gradle.api.internal.tasks.TaskStateInternal; | ||
import org.gradle.api.invocation.Gradle; | ||
import org.gradle.api.tasks.TaskState; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class StatisticListenerTest { | ||
|
||
@Test | ||
void shouldStoreTaskExecutionTime() { | ||
// Given | ||
String taskName = "Task1"; | ||
|
||
Task task = mock(Task.class); | ||
when(task.getName()).thenReturn(taskName); | ||
when(task.getProject()).thenReturn(mock(Project.class)); | ||
|
||
TaskState state = new TaskStateInternal(); | ||
|
||
StatisticListener listener = new StatisticListener(); | ||
listener.projectsEvaluated(mock(Gradle.class)); | ||
|
||
// Call beforeExecute before afterExecute | ||
listener.beforeExecute(task); | ||
|
||
int sizePre = listener.data.tasks.size(); | ||
|
||
// When | ||
listener.afterExecute(task, state); | ||
|
||
// Then | ||
int size = listener.data.tasks.size(); | ||
|
||
Assertions.assertEquals(size, sizePre + 1); | ||
Assertions.assertEquals(listener.data.tasks.get(size - 1) | ||
.data.getMember("Name").getAsJsonString().getValue(), taskName); | ||
} | ||
|
||
@Test | ||
void shouldNotStoreTaskExecutionTime() { | ||
// Given | ||
String taskName = "Task1"; | ||
|
||
Task task = mock(Task.class); | ||
when(task.getName()).thenReturn(taskName); | ||
when(task.getProject()).thenReturn(mock(Project.class)); | ||
|
||
TaskState state = new TaskStateInternal(); | ||
|
||
StatisticListener listener = new StatisticListener(); | ||
listener.projectsEvaluated(mock(Gradle.class)); | ||
|
||
// Do not call beforeExecute | ||
// listener.beforeExecute(task); | ||
|
||
int sizePre = listener.data.tasks.size(); | ||
|
||
// When | ||
listener.afterExecute(task, state); | ||
|
||
// Then | ||
int size = listener.data.tasks.size(); | ||
|
||
Assertions.assertEquals(size, sizePre); | ||
} | ||
} |