View Javadoc
1   package it.allproducts;
2   
3   import com.atlassian.plugin.spring.scanner.test.InternalComponent;
4   import it.allproducts.ComponentExpectations.AbstractExpectedComponent;
5   import com.google.common.io.CharStreams;
6   import it.perproduct.AbstractComponentsInProductTest;
7   import org.hamcrest.Matcher;
8   import org.junit.Test;
9   
10  import java.io.IOException;
11  import java.io.InputStream;
12  import java.util.Collection;
13  import java.io.InputStreamReader;
14  import java.util.List;
15  import java.util.stream.Collectors;
16  
17  import static org.hamcrest.CoreMatchers.notNullValue;
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.containsInAnyOrder;
20  
21  /**
22   * Check that the index files produced by atlassian-spring-scanner-maven-plugin are what we expect given the annotations in this plugin.
23   * <p>
24   * Note this test is <b>build-time only</b> - it just validates the packaging part of the scanner.
25   * It's in this integration-test phase due to Maven classpath issues.
26   * It is not asserting anything about the plugin running in the product - it's actually just looking at
27   * either your {@code /target/classes} directory (if run from IDEA) or maybe the artifact jar on the classpath (from Maven).
28   *
29   * @see AbstractComponentsInProductTest subclasses for runtime tests
30   */
31  public class TestPackaging {
32      @Test
33      public void testExpectedComponents() throws Exception {
34          assertIndexContents("component",
35                  ComponentExpectations.COMMON.getExpectedScannerCreatedComponents());
36      }
37  
38      @Test
39      public void testExpectedImports() throws Exception {
40          assertIndexContents("imports",
41                  ComponentExpectations.COMMON.getExpectedScannerCreatedComponentImports());
42      }
43  
44      @Test
45      public void testExpectedExports() throws Exception {
46          assertIndexContents("exports",
47                  ComponentExpectations.COMMON.getExpectedScannerCreatedExports());
48      }
49  
50      @Test
51      public void testExpectedDevExports() throws Exception {
52          assertIndexContents("dev-exports",
53                  ComponentExpectations.COMMON.getExpectedScannerCreatedDevExports());
54      }
55  
56      @Test
57      public void testExpectedDynamicComponents() throws Exception {
58          assertIndexContents("profile-dynamic/component",
59                  ComponentExpectations.COMMON.getExpectedScannerCreatedDynamicComponents());
60      }
61  
62      @Test
63      public void testExpectedDynamicImports() throws Exception {
64          assertIndexContents("profile-dynamic/imports",
65                  ComponentExpectations.COMMON.getExpectedScannerCreatedDynamicComponentImports());
66      }
67  
68      @Test
69      public void testExpectedPackageInfoComponents() throws Exception {
70          //
71          // these two use package info level annotations
72          assertIndexContents("profile-unused/component",
73                  "com.atlassian.plugin.spring.scanner.test.unused.UnusedComponent1",
74                  "com.atlassian.plugin.spring.scanner.test.unused.UnusedComponent2");
75  
76          //
77          // this specific class has another profile override and hence does NOT take the package level ones
78          //
79          // It cumulative inheritance, its override specifics
80          //
81          assertIndexContents("profile-specific-override/component",
82                  "com.atlassian.plugin.spring.scanner.test.unused.UnusedSpecificOverride");
83      }
84  
85      @Test
86      public void testBambooComponents() throws Exception {
87          testProductSpecificComponents("bamboo", ComponentExpectations.BAMBOO);
88      }
89  
90      @Test
91      public void testBitbucketComponents() throws Exception {
92          testProductSpecificComponents("bitbucket", ComponentExpectations.BITBUCKET);
93      }
94  
95      @Test
96      public void testConfluenceComponents() throws Exception {
97          testProductSpecificComponents("confluence", ComponentExpectations.CONFLUENCE);
98      }
99  
100     @Test
101     public void testFecruComponents() throws Exception {
102         testProductSpecificComponents("fecru", ComponentExpectations.FECRU);
103     }
104 
105     @Test
106     public void testJiraComponents() throws Exception {
107         testProductSpecificComponents("jira", ComponentExpectations.JIRA_CLOUD);
108     }
109 
110     @Test
111     public void testRefappComponents() throws Exception {
112         testProductSpecificComponents("refapp", ComponentExpectations.REFAPP);
113     }
114 
115     @Test
116     public void testStashComponents() throws Exception {
117         testProductSpecificComponents("stash", ComponentExpectations.STASH);
118     }
119 
120     private void testProductSpecificComponents(String indexFileQualifier, ComponentExpectations productSpecificExpectations) throws IOException {
121         assertIndexContents("component-" + indexFileQualifier,
122                 productSpecificExpectations.getProductSpecificScannerCreatedComponents());
123         assertIndexContents("imports-" + indexFileQualifier,
124                 productSpecificExpectations.getProductSpecificScannerCreatedComponentImports());
125     }
126 
127     private void assertIndexContents(final String indexFilename, final List<? extends AbstractExpectedComponent> expectedItems)
128             throws IOException {
129         final List<String> actual = readIndexFile(indexFilename);
130         Collection<Matcher<? super String>> lineMatchers =
131                 expectedItems.stream()
132                         .map(AbstractExpectedComponent::getIndexLineMatcher)
133                         .collect(Collectors.toList());
134         assertThat(actual, containsInAnyOrder(lineMatchers));
135     }
136 
137     private void assertIndexContents(final String indexFilename, final String... expected)
138             throws IOException {
139         final List<String> actual = readIndexFile(indexFilename);
140         assertThat(actual, containsInAnyOrder(expected));
141     }
142 
143     private List<String> readIndexFile(final String indexFileName) throws IOException {
144         final String resourcePath = "META-INF/plugin-components/" + indexFileName;
145 
146         // A class inside the plugin
147         final InputStream inputStream = InternalComponent.class.getClassLoader().getResourceAsStream(resourcePath);
148 
149         assertThat("Unable to read expected index file '" + resourcePath + "'", inputStream, notNullValue());
150 
151         try (InputStreamReader reader = new InputStreamReader(inputStream)) {
152             return CharStreams.readLines(reader);
153         }
154     }
155 }