View Javadoc
1   package com.atlassian.plugin.manager;
2   
3   import com.atlassian.plugin.Plugin;
4   import com.atlassian.plugin.PluginRegistry;
5   import com.google.common.collect.ImmutableList;
6   import com.google.common.collect.Maps;
7   import org.junit.Test;
8   
9   import java.util.Collection;
10  import java.util.List;
11  import java.util.Map;
12  
13  import static com.atlassian.plugin.manager.PluginWithDeps.GET_KEY;
14  import static org.hamcrest.MatcherAssert.assertThat;
15  import static org.hamcrest.Matchers.anyOf;
16  import static org.hamcrest.Matchers.contains;
17  import static org.hamcrest.Matchers.containsInAnyOrder;
18  import static org.hamcrest.Matchers.is;
19  
20  public class TestPluginsInEnableOrder {
21      private static class PluginSystemStateAccessor implements PluginRegistry.ReadOnly {
22  
23          private final Map<String, Plugin> plugins;
24  
25          private PluginSystemStateAccessor(List<Plugin> pluginList) {
26              this.plugins = Maps.uniqueIndex(pluginList, GET_KEY);
27          }
28  
29          @Override
30          public Collection<Plugin> getAll() {
31              return plugins.values();
32          }
33  
34          @Override
35          public Plugin get(final String key) {
36              return plugins.get(key);
37          }
38      }
39  
40      @Test(timeout = 1000)
41      public void sortPluginsForEnableSorts() throws Exception {
42          Plugin a = new PluginWithDeps("a");
43          Plugin b = new PluginWithDeps("b", "a");
44          Plugin c = new PluginWithDeps("c", "b");
45  
46          final ImmutableList<Plugin> plugins = ImmutableList.of(b, c, a);
47  
48          List<Plugin> sortedList = new PluginsInEnableOrder(plugins, new PluginSystemStateAccessor(plugins)).get();
49  
50          assertThat(sortedList, contains(a, b, c));
51      }
52  
53      @Test(timeout = 1000)
54      public void sortPluginsForEnableSortsCycle() throws Exception {
55          Plugin a = new PluginWithDeps("a");
56          Plugin b = new PluginWithDeps("b", "a", "d");
57          Plugin c = new PluginWithDeps("c", "b");
58          Plugin d = new PluginWithDeps("d", "c");
59  
60          final ImmutableList<Plugin> plugins = ImmutableList.of(b, c, a, d);
61  
62          List<Plugin> sortedList = new PluginsInEnableOrder(plugins, new PluginSystemStateAccessor(plugins)).get();
63  
64          assertThat(sortedList, containsInAnyOrder(a, b, c, d));
65  
66          // depending on the set key order the first could be either a or c
67          assertThat(sortedList.get(0), anyOf(is(a), is(c)));
68      }
69  
70      @Test(timeout = 1000)
71      public void sortPluginsForEnableIgnoresNonPluginRequirements() {
72          Plugin a = new PluginWithDeps("a");
73          Plugin b = new PluginWithDeps("b", "a", "framework");
74          Plugin c = new PluginWithDeps("c", "b", "host");
75  
76          final ImmutableList<Plugin> plugins = ImmutableList.of(b, c, a);
77  
78          List<Plugin> sortedList = new PluginsInEnableOrder(plugins, new PluginSystemStateAccessor(plugins)).get();
79  
80          assertThat(sortedList, contains(a, b, c));
81      }
82  
83      @Test(timeout = 1000)
84      public void sortPluginsForEnableTraversesButHidesUnrequestedPlugins() {
85          Plugin a = new PluginWithDeps("a");
86          Plugin b = new PluginWithDeps("b", "a");
87          Plugin c = new PluginWithDeps("c", "b");
88  
89          final ImmutableList<Plugin> pluginsToEnable = ImmutableList.of(c, a);
90          final ImmutableList<Plugin> allPlugins = ImmutableList.of(a, b, c);
91  
92          List<Plugin> sortedList = new PluginsInEnableOrder(pluginsToEnable, new PluginSystemStateAccessor(allPlugins)).get();
93  
94          assertThat(sortedList, contains(a, c));
95  
96      }
97  }