-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Guarantee initial order for decorators to sort
Plus, add some test coverage with basic scenarios.
- Loading branch information
Showing
3 changed files
with
140 additions
and
10 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
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
121 changes: 121 additions & 0 deletions
121
core/src/test/java/io/dekorate/utils/TopologicalSortTest.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,121 @@ | ||
package io.dekorate.utils; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.dekorate.kubernetes.decorator.Decorator; | ||
|
||
public class TopologicalSortTest { | ||
|
||
private List<Decorator> sortedDecorators; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
sortedDecorators = null; | ||
} | ||
|
||
@Test | ||
public void shouldGroupDecoratorsWithSameTypeAndShouldPreserveOrder() { | ||
FooDecorator a1 = new FooDecorator(); | ||
DummyDecorator b = new DummyDecorator().withAfter(FooDecorator.class); | ||
FooDecorator a2 = new FooDecorator(); | ||
|
||
whenSort(b, a1, a2); | ||
thenDecoratorsAre(a1, a2, b); | ||
} | ||
|
||
@Test | ||
public void shouldThrowCycleException() { | ||
FooDecorator foo = new FooDecorator(); | ||
DummyDecorator bar = new BarDecorator().withAfter(FooDecorator.class); | ||
DummyDecorator special = new SpecialFooDecorator().withAfter(FooDecorator.class, BarDecorator.class); | ||
|
||
// it should fail because: | ||
// bar -> foo | ||
// special (it's also a foo) -> bar | ||
assertThrows(RuntimeException.class, () -> whenSort(foo, bar, special)); | ||
} | ||
|
||
@Test | ||
public void shouldAvoidCycleErrorWhenDecoratorHasSameParent() { | ||
FooDecorator foo = new FooDecorator(); | ||
DummyDecorator bar = new BarDecorator().withBefore(SpecialFooDecorator.class).withAfter(FooDecorator.class); | ||
DummyDecorator special = new SpecialFooDecorator().withAfter(FooDecorator.class, BarDecorator.class); | ||
|
||
whenSort(foo, bar, special); | ||
thenDecoratorsAre(foo, bar, special); | ||
} | ||
|
||
private void whenSort(Decorator decorator, Decorator... decorators) { | ||
List<Decorator> unsortedDecorators = new ArrayList<>(); | ||
unsortedDecorators.add(decorator); | ||
if (decorators != null) { | ||
for (Decorator rest : decorators) { | ||
unsortedDecorators.add(rest); | ||
} | ||
} | ||
|
||
sortedDecorators = TopologicalSort.sortDecorators(unsortedDecorators); | ||
} | ||
|
||
private void thenDecoratorsAre(Decorator... expectedDecorators) { | ||
for (int position = 0; position < expectedDecorators.length; position++) { | ||
Decorator expected = expectedDecorators[position]; | ||
assertEquals(sortedDecorators.get(position), expected, "Unexpected order in decorators: " + sortedDecorators); | ||
} | ||
} | ||
|
||
static class FooDecorator extends DummyDecorator { | ||
|
||
} | ||
|
||
static class SpecialFooDecorator extends FooDecorator { | ||
|
||
} | ||
|
||
static class BarDecorator extends DummyDecorator { | ||
|
||
} | ||
|
||
static class DummyDecorator extends Decorator { | ||
|
||
private Class[] before; | ||
private Class[] after; | ||
|
||
public DummyDecorator withAfter(Class... classes) { | ||
after = classes; | ||
return this; | ||
} | ||
|
||
public DummyDecorator withBefore(Class... classes) { | ||
before = classes; | ||
return this; | ||
} | ||
|
||
@Override | ||
public void visit(Object o) { | ||
|
||
} | ||
|
||
@Override | ||
public int compareTo(Object o) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public Class<? extends Decorator>[] before() { | ||
return before; | ||
} | ||
|
||
@Override | ||
public Class<? extends Decorator>[] after() { | ||
return after; | ||
} | ||
} | ||
} |