Skip to content

Commit

Permalink
Updated Main handler run start ups to execute in priority order
Browse files Browse the repository at this point in the history
  • Loading branch information
daiLlew committed Jan 10, 2022
1 parent 831c241 commit 60c3b99
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -235,20 +234,25 @@ public void setupPostFilters(Reflections reflections) {
}

public void runStartups(Reflections reflections) {
this.startups = getStartUpsOrdered(reflections);
this.startups.stream().forEach(startup -> startup.init());
}

Set<Startup> startups = new HashSet<>();
static List<Startup> getStartUpsOrdered(Reflections reflections) {
Set<Class<? extends Startup>> startupClasses = reflections.getSubTypesOf(Startup.class);

List<Startup> startUpInstances = new ArrayList<>();
for (Class<? extends Startup> startupClass : startupClasses) {
try {
startups.add(startupClass.newInstance());
startUpInstances.add(startupClass.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
log.info("Error instantiating startup class {}", startupClass.getName());
e.printStackTrace();
}
}
for (Startup startup : startups) {
startup.init();
}
this.startups = startups;

Collections.sort(startUpInstances, new PriorityComparator(startupClasses.size()));
return startUpInstances;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.github.davidcarboni.restolino.jetty;

import com.github.davidcarboni.restolino.framework.Priority;
import com.github.davidcarboni.restolino.framework.Startup;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.reflections.Reflections;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;

public class MainHandlerTest {

@Mock
private Reflections reflections;

private Set<Class<? extends Startup>> startUpInstances;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);

this.startUpInstances = new HashSet<Class<? extends Startup>>() {{
add(StartUp2.class);
add(StartUp1.class);
add(StartUp4.class);
add(StartUp3.class);
}};
}

/**
* Test verifies that the {@link StartUp2} instances are loaded in the order specified by a {@link Priority}
* annotation.
*/
@Test
public void testGetStartUpsOrdered() throws Exception {
when(reflections.getSubTypesOf(Startup.class))
.thenReturn(startUpInstances);

List<Startup> result = MainHandler.getStartUpsOrdered(reflections);

assertThat(result, is(notNullValue()));
assertThat(result.size(), equalTo(startUpInstances.size()));
assertTrue(result.get(0) instanceof StartUp1);
assertTrue(result.get(1) instanceof StartUp2);
assertTrue(result.get(2) instanceof StartUp3);
assertTrue(result.get(3) instanceof StartUp4);
}

@Priority(1)
public static class StartUp1 implements Startup {

@Override
public void init() {}
}

@Priority(2)
public static class StartUp2 implements Startup {

@Override
public void init() {}
}

@Priority(3)
public static class StartUp3 implements Startup {

@Override
public void init() {}
}

// No priority - should be last.
public static class StartUp4 implements Startup {

@Override
public void init() {}
}
}

0 comments on commit 60c3b99

Please sign in to comment.