Skip to content

Commit

Permalink
Merge pull request #75 from MontiCore/fix/statistic-listener-nullpointer
Browse files Browse the repository at this point in the history
Fix nullpointer in case of missing start time
  • Loading branch information
luepges authored Jan 13, 2025
2 parents b1abc51 + 34c8bda commit 1b94beb
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ private static synchronized StatisticListener getSingleton() {

public synchronized static void registerOnce(Project project) {
if (alreadyRegistered.compareAndSet(false, true)
&& (!project.hasProperty(StatisticListener.enable_tracking)
|| "true".equals(project.getProperties().get(StatisticListener.enable_tracking)))) {
&& (!project.hasProperty(StatisticListener.enable_tracking)
|| "true".equals(project.getProperties().get(StatisticListener.enable_tracking)))) {
project.getGradle().addListener(getSingleton());
}
}
Expand All @@ -47,7 +47,7 @@ public void buildStarted(Gradle gradle) {

}

public void beforeSettings(Settings settings){
public void beforeSettings(Settings settings) {

}

Expand Down Expand Up @@ -76,29 +76,68 @@ public void buildFinished(BuildResult buildResult) {
Log.debug("buildFinished", this.getClass().getName());
alreadyRegistered.set(false); // Reset is necessary, otherwise Listener is not used in next build

if(projectStartTime != null) {
if (projectStartTime != null) {
data.setExecutionTime(Duration.between(projectStartTime, Instant.now()));


if ("true".equals(buildResult.getGradle().getRootProject().getProperties().get(show_report))) {
System.out.println(data.toString());
}
StatisticsHandler.storeReport(data.toString(), "MC_GRADLE_JSON");
} else{
} else {
Log.info("<projectStartTime> was null. ", this.getClass().getName());
}
}

@Override
public void beforeExecute(Task task) {
Log.trace(
"Start before task execution for Task `"
+ task.getName()
+ "`",
this.getClass().getName()
);

startTime.put(task, Instant.now());

Log.trace(
"Finish before task execution for Task `"
+ task.getName()
+ "`",
this.getClass().getName()
);
}

@Override
public void afterExecute(Task task, TaskState taskState) {
Duration duration = Duration.between(startTime.remove(task), Instant.now());
StatisticData.TaskData taskData = new StatisticData.TaskData(task, taskState, duration);
data.addTask(taskData);
Log.trace(
"Start after task execution for Task `"
+ task.getName()
+ "`",
this.getClass().getName()
);

Instant taskStartTime = startTime.remove(task);

if (taskStartTime != null) {
Duration duration = Duration.between(taskStartTime, Instant.now());
StatisticData.TaskData taskData = new StatisticData.TaskData(task, taskState, duration);
data.addTask(taskData);
} else {
Log.debug(
"The <taskStartTime> of Task `"
+ task.getName()
+ "` was null.",
this.getClass().getName()
);
}

Log.trace(
"Finish after task execution for Task `"
+ task.getName()
+ "`",
this.getClass().getName()
);
}

}
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);
}
}

0 comments on commit 1b94beb

Please sign in to comment.