|
| 1 | + |
| 2 | +package org.scijava.search.module; |
| 3 | + |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | +import org.scijava.Context; |
| 10 | +import org.scijava.Priority; |
| 11 | +import org.scijava.plugin.Plugin; |
| 12 | +import org.scijava.search.DefaultSearchAction; |
| 13 | +import org.scijava.search.SearchAction; |
| 14 | +import org.scijava.search.SearchActionFactory; |
| 15 | +import org.scijava.search.SearchResult; |
| 16 | +import org.scijava.search.SearchService; |
| 17 | +import org.scijava.search.classes.ClassSearchResult; |
| 18 | + |
| 19 | +/** |
| 20 | + * Tests that duplicate labels are removed with the {@link SearchService} |
| 21 | + * |
| 22 | + * @author Gabriel Selzer |
| 23 | + */ |
| 24 | +public class DuplicateLabelsTest { |
| 25 | + |
| 26 | + /** |
| 27 | + * Used to allow the search actions to change state. Run actions can't return |
| 28 | + * anything, so we need another way to track that they run. |
| 29 | + */ |
| 30 | + public static Integer state = 0; |
| 31 | + |
| 32 | + private SearchService searchService; |
| 33 | + |
| 34 | + @Before |
| 35 | + public void init() { |
| 36 | + Context context = new Context(); |
| 37 | + searchService = context.getService(SearchService.class); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testDuplicateLabelRemoval() { |
| 42 | + SearchResult dummyResult = new ClassSearchResult(this.getClass(), ""); |
| 43 | + List<SearchAction> actions = searchService.actions(dummyResult); |
| 44 | + actions.removeIf(a -> !a.toString().equals("test")); |
| 45 | + Assert.assertEquals(1, actions.size()); |
| 46 | + actions.get(0).run(); |
| 47 | + assert DuplicateLabelsTest.state == 1; |
| 48 | + } |
| 49 | + |
| 50 | + @Plugin(type = SearchActionFactory.class, priority = Priority.HIGH) |
| 51 | + public static class TestSearchActionFactoryHigh implements |
| 52 | + SearchActionFactory |
| 53 | + { |
| 54 | + |
| 55 | + @Override |
| 56 | + public SearchAction create(SearchResult data) { |
| 57 | + return new DefaultSearchAction("test", // |
| 58 | + () -> DuplicateLabelsTest.state = 1 // |
| 59 | + ); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Plugin(type = SearchActionFactory.class, priority = Priority.LOW) |
| 64 | + public static class TestSearchActionFactoryLow implements |
| 65 | + SearchActionFactory |
| 66 | + { |
| 67 | + |
| 68 | + @Override |
| 69 | + public SearchAction create(SearchResult data) { |
| 70 | + return new DefaultSearchAction("test", // |
| 71 | + () -> DuplicateLabelsTest.state = 2 // |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments