-
Notifications
You must be signed in to change notification settings - Fork 340
Notify listeners when days off are removed, and add the missing removal methods #2830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Natalie-the-technician
wants to merge
3
commits into
bardsoftware:master
Choose a base branch
from
Natalie-the-technician:daysoff-removal-notifies
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
86 changes: 86 additions & 0 deletions
86
ganttproject-tester/test/net/sourceforge/ganttproject/resource/HumanResourceDaysOffTest.kt
This file contains hidden or 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,86 @@ | ||
| /* | ||
| Copyright 2026 BarD Software s.r.o | ||
|
|
||
| This file is part of GanttProject, an open-source project management tool. | ||
|
|
||
| GanttProject is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| GanttProject is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with GanttProject. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package net.sourceforge.ganttproject.resource | ||
|
|
||
| import biz.ganttproject.core.time.CalendarFactory | ||
| import biz.ganttproject.core.calendar.GanttDaysOff | ||
| import biz.ganttproject.customproperty.CustomColumnsManager | ||
| import net.sourceforge.ganttproject.roles.Role | ||
| import org.junit.jupiter.api.Assertions.assertEquals | ||
| import org.junit.jupiter.api.Test | ||
| import java.text.DateFormat | ||
| import java.util.Date | ||
| import java.util.Locale | ||
|
|
||
| /** | ||
| * Adding and removing days off must be symmetric with respect to notification: whoever listens to | ||
| * a resource has to learn about both, because both change the resource's load distribution. | ||
| * | ||
| * Adding goes through [HumanResource.addDaysOff], which resets the loads and fires. Removing the | ||
| * last one used to notify nobody: there was no entry point for it, and the only way was to mutate | ||
| * the list handed out by [HumanResource.getDaysOff], which nothing was watching. Removing now goes | ||
| * through [HumanResource.clearDaysOff], which fires just as adding does. | ||
| */ | ||
| class HumanResourceDaysOffTest { | ||
| init { | ||
| // GanttDaysOff builds GanttCalendars, and those need a locale. | ||
| object : CalendarFactory() { | ||
| init { | ||
| setLocaleApi(object : CalendarFactory.LocaleApi { | ||
| override fun getLocale(): Locale = Locale.GERMANY | ||
| override fun getShortDateFormat(): DateFormat = | ||
| DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMANY) | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private class CountingView : ResourceView { | ||
| var changed = 0 | ||
| override fun resourceAdded(event: ResourceEvent) {} | ||
| override fun resourcesRemoved(event: ResourceEvent) {} | ||
| override fun resourceChanged(e: ResourceEvent) { changed++ } | ||
| override fun resourceAssignmentsChanged(e: ResourceEvent) {} | ||
| override fun resourceStructureChanged() {} | ||
| override fun resourceModelReset() {} | ||
| } | ||
|
|
||
| private fun september(day: Int): Date = CalendarFactory.createGanttCalendar(2026, 8, day).time | ||
|
|
||
| @Test | ||
| fun `removing the last day off notifies the listeners just as adding it does`() { | ||
| val manager = HumanResourceManager(null as Role?, CustomColumnsManager()) | ||
| val person = manager.newHumanResource() | ||
| person.name = "Tester" | ||
| manager.add(person) | ||
|
|
||
| val view = CountingView() | ||
| manager.addView(view) | ||
|
|
||
| person.addDaysOff(GanttDaysOff(september(9), september(10))) | ||
| assertEquals(1, view.changed, "adding a day off must notify the listeners") | ||
|
|
||
| view.changed = 0 | ||
| person.clearDaysOff() | ||
| assertEquals( | ||
| 1, view.changed, | ||
| "removing the LAST day off must notify the listeners too -- otherwise nobody recalculates" | ||
| ) | ||
| } | ||
| } |
107 changes: 107 additions & 0 deletions
107
...project-tester/test/net/sourceforge/ganttproject/resource/HumanResourceDaysOffViewTest.kt
This file contains hidden or 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,107 @@ | ||
| /* | ||
| Copyright 2026 BarD Software s.r.o | ||
|
|
||
| This file is part of GanttProject, an open-source project management tool. | ||
|
|
||
| GanttProject is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| GanttProject is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with GanttProject. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package net.sourceforge.ganttproject.resource | ||
|
|
||
| import biz.ganttproject.core.calendar.GanttDaysOff | ||
| import biz.ganttproject.core.time.CalendarFactory | ||
| import biz.ganttproject.customproperty.CustomColumnsManager | ||
| import net.sourceforge.ganttproject.roles.Role | ||
| import org.junit.jupiter.api.Assertions.assertEquals | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.assertThrows | ||
| import java.text.DateFormat | ||
| import java.util.Date | ||
| import java.util.Locale | ||
|
|
||
| /** | ||
| * [HumanResource.getDaysOff] hands out an unmodifiable view of the resource's days off. These two | ||
| * tests pin both halves of that sentence. | ||
| * | ||
| * Unmodifiable is what makes the notification correct: every change now goes through addDaysOff, | ||
| * removeDaysOff or clearDaysOff, and each of them resets the load distribution and fires. A caller | ||
| * able to mutate the handed-out list could change the resource behind the back of both. | ||
| * | ||
| * A view rather than a copy is what keeps the list cheap and current: a caller holding on to it | ||
| * keeps seeing the resource, and no caller in the tree is handed a snapshot that silently goes | ||
| * stale. | ||
| */ | ||
| class HumanResourceDaysOffViewTest { | ||
| init { | ||
| // GanttDaysOff builds GanttCalendars, and those need a locale. | ||
| object : CalendarFactory() { | ||
| init { | ||
| setLocaleApi(object : CalendarFactory.LocaleApi { | ||
| override fun getLocale(): Locale = Locale.GERMANY | ||
| override fun getShortDateFormat(): DateFormat = | ||
| DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMANY) | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun september(day: Int): Date = CalendarFactory.createGanttCalendar(2026, 8, day).time | ||
|
|
||
| private fun daysOff(from: Int, to: Int) = GanttDaysOff(september(from), september(to)) | ||
|
|
||
| private fun newPerson(): HumanResource { | ||
| val manager = HumanResourceManager(null as Role?, CustomColumnsManager()) | ||
| val person = manager.newHumanResource() | ||
| person.name = "Tester" | ||
| manager.add(person) | ||
| return person | ||
| } | ||
|
|
||
| @Test | ||
| fun `the list handed out by getDaysOff cannot be modified`() { | ||
| val person = newPerson() | ||
| person.addDaysOff(daysOff(9, 10)) | ||
| val handedOut = person.daysOff | ||
|
|
||
| assertThrows<UnsupportedOperationException>("clearing the handed-out list must not be possible") { | ||
| handedOut.clear() | ||
| } | ||
| assertThrows<UnsupportedOperationException>("adding to the handed-out list must not be possible") { | ||
| handedOut.add(0, daysOff(20, 21)) | ||
| } | ||
| assertThrows<UnsupportedOperationException>("removing from the handed-out list must not be possible") { | ||
| handedOut.removeAt(0) | ||
| } | ||
| assertThrows<UnsupportedOperationException>("overwriting in the handed-out list must not be possible") { | ||
| handedOut.set(0, daysOff(20, 21)) | ||
| } | ||
|
|
||
| assertEquals(1, person.daysOff.size, "none of the rejected calls may have changed the resource") | ||
| } | ||
|
|
||
| @Test | ||
| fun `the list handed out by getDaysOff is a view and not a copy`() { | ||
| val person = newPerson() | ||
| person.addDaysOff(daysOff(9, 10)) | ||
|
|
||
| val handedOut = person.daysOff | ||
| assertEquals(1, handedOut.size, "the day off given before the call must be in the handed-out list") | ||
|
|
||
| person.addDaysOff(daysOff(20, 21)) | ||
| assertEquals( | ||
| 2, handedOut.size, | ||
| "a day off given AFTER the call must show up too -- a copy would still show one" | ||
| ) | ||
| assertEquals(daysOff(20, 21).start, handedOut.get(1).start, "and it must be the new interval") | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any sense to join these three test classes into a single one?