/amps-maven-plugin/src/test/java/com/atlassian/maven/plugins/amps/product/TestJiraProductHandler.java
Java | 488 lines | 377 code | 85 blank | 26 comment | 10 complexity | 5ba739af739c67e5401651f09e1aeb68 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
- package com.atlassian.maven.plugins.amps.product;
- import com.atlassian.maven.plugins.amps.MavenContext;
- import com.atlassian.maven.plugins.amps.MavenGoals;
- import com.atlassian.maven.plugins.amps.Node;
- import com.atlassian.maven.plugins.amps.Product;
- import com.atlassian.maven.plugins.amps.XmlOverride;
- import com.atlassian.maven.plugins.amps.database.DatabaseType;
- import com.atlassian.maven.plugins.amps.database.MssqlJtds;
- import com.atlassian.maven.plugins.amps.database.MssqlMicrosoft;
- import com.atlassian.maven.plugins.amps.database.MySQL;
- import com.atlassian.maven.plugins.amps.database.Oracle10g;
- import com.atlassian.maven.plugins.amps.database.Oracle12c;
- import com.atlassian.maven.plugins.amps.database.Postgres;
- import com.atlassian.maven.plugins.amps.product.manager.WebAppManager;
- import com.atlassian.maven.plugins.amps.util.ConfigFileUtils.Replacement;
- import org.apache.commons.io.FileUtils;
- import org.apache.maven.artifact.resolver.ArtifactResolver;
- import org.apache.maven.model.Build;
- import org.apache.maven.plugin.logging.Log;
- import org.apache.maven.project.MavenProject;
- import org.apache.maven.repository.RepositorySystem;
- import org.dom4j.io.SAXReader;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Rule;
- import org.junit.Test;
- import org.junit.rules.MethodRule;
- import org.junit.rules.TemporaryFolder;
- import org.mockito.ArgumentCaptor;
- import org.mockito.Captor;
- import org.mockito.Mock;
- import org.mockito.junit.MockitoJUnit;
- import org.w3c.dom.Document;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.xpath.XPathExpression;
- import javax.xml.xpath.XPathFactory;
- import java.io.File;
- import java.io.IOException;
- import java.nio.charset.StandardCharsets;
- import java.util.Collection;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Optional;
- import static com.atlassian.maven.plugins.amps.product.JiraProductHandler.BUNDLED_PLUGINS_FROM_4_1;
- import static com.atlassian.maven.plugins.amps.product.JiraProductHandler.BUNDLED_PLUGINS_UNZIPPED;
- import static com.atlassian.maven.plugins.amps.product.JiraProductHandler.BUNDLED_PLUGINS_UPTO_4_0;
- import static com.atlassian.maven.plugins.amps.product.JiraProductHandler.FILENAME_DBCONFIG;
- import static com.atlassian.maven.plugins.amps.product.JiraProductHandler.INSTALLED_PLUGINS_DIR;
- import static com.atlassian.maven.plugins.amps.product.JiraProductHandler.PLUGINS_DIR;
- import static com.google.common.collect.Lists.newArrayList;
- import static java.lang.String.format;
- import static java.util.Collections.singletonList;
- import static org.apache.commons.lang3.JavaVersion.JAVA_9;
- import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtLeast;
- import static org.hamcrest.CoreMatchers.containsString;
- import static org.hamcrest.CoreMatchers.is;
- import static org.hamcrest.MatcherAssert.assertThat;
- import static org.hamcrest.Matchers.contains;
- import static org.hamcrest.Matchers.hasEntry;
- import static org.hamcrest.Matchers.hasKey;
- import static org.hamcrest.Matchers.not;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertNotNull;
- import static org.junit.Assert.assertTrue;
- import static org.mockito.ArgumentMatchers.any;
- import static org.mockito.Mockito.mock;
- import static org.mockito.Mockito.spy;
- import static org.mockito.Mockito.times;
- import static org.mockito.Mockito.verify;
- import static org.mockito.Mockito.when;
- public class TestJiraProductHandler {
- private static File TEMP_HOME;
- @Rule
- public final MethodRule mockitoRule = MockitoJUnit.rule();
- @Rule
- public final TemporaryFolder temporaryFolder = new TemporaryFolder();
- @Mock
- private RepositorySystem repositorySystem;
- @Mock
- private ArtifactResolver artifactResolver;
- @Mock
- private Build build;
- @Mock
- private Log log;
- @Mock
- private MavenContext mavenContext;
- @Mock
- private MavenGoals mavenGoals;
- @Mock
- private MavenProject mavenProject;
- @Mock
- private WebAppManager webAppManager;
- @Captor
- private ArgumentCaptor<Collection<XmlOverride>> expectedOverridesCaptor;
- // Can't use @InjectMocks because of method calls on constructor args
- private JiraProductHandler productHandler;
- private static File createTempDir(final String subPath) {
- return new File(System.getProperty("java.io.tmpdir"), subPath);
- }
- @Before
- public void setUp() throws Exception {
- when(build.getDirectory()).thenReturn(temporaryFolder.newFolder("jira").getAbsolutePath());
- when(mavenProject.getBuild()).thenReturn(build);
- when(mavenContext.getLog()).thenReturn(log);
- when(mavenContext.getProject()).thenReturn(mavenProject);
- productHandler = new JiraProductHandler(mavenContext, mavenGoals, repositorySystem, artifactResolver, webAppManager);
- createTemporaryHomeDirectory();
- }
- public void createTemporaryHomeDirectory() throws IOException {
- final File f = File.createTempFile("temp-jira-", "-home");
- if (!f.delete()) {
- throw new IOException();
- }
- if (!f.mkdir()) {
- throw new IOException();
- }
- TEMP_HOME = f;
- }
- @After
- public void deleteTemporaryHomeDirectoryAndContents() throws Exception {
- if (TEMP_HOME != null) {
- FileUtils.deleteDirectory(TEMP_HOME);
- TEMP_HOME = null;
- }
- }
- @Test
- public void itShouldSetNewJvmArgsForJira8_0_0AndHigher() {
- newArrayList(
- newProduct("8.0.0-ALPHA"), newProduct("8.0.0-SNAPSHOT"), newProduct("8.0-EAP01"),
- newProduct("8.0.0"), newProduct("8.0.1-SNAPSHOT"), newProduct("8.0.2"),
- newProduct("8.0.0-m0030"), newProduct("8.1-rc1"), newProduct("8.1"))
- .forEach(product -> {
- productHandler.fixJvmArgs(product);
- assertThat(format("Jira version %s does not have the correct Xmx", product.getVersion()), product.getJvmArgs(), containsString("-Xmx2g"));
- assertThat(format("Jira version %s does not have the correct Xms", product.getVersion()), product.getJvmArgs(), containsString("-Xms1g"));
- if (isJavaVersionAtLeast(JAVA_9)) {
- assertThat(product.getJvmArgs(), containsString("--add-opens"));
- }
- });
- }
- @Test
- public void itShouldSetNewJvmArgsForJira7_7_0AndHigher() {
- newArrayList(
- newProduct("7.7.0-ALPHA"), newProduct("7.7.0-SNAPSHOT"), newProduct("7.7-EAP01"), newProduct("7.7.0"),
- newProduct("7.7.1-SNAPSHOT"), newProduct("7.7.2"), newProduct("7.8-rc1"), newProduct("7.8"))
- .forEach(product -> {
- productHandler.fixJvmArgs(product);
- assertThat(format("Jira version %s does not have the correct Xmx", product.getVersion()), product.getJvmArgs(), containsString("-Xmx768m"));
- assertThat(format("Jira version %s does not have the correct Xms", product.getVersion()), product.getJvmArgs(), containsString("-Xms384m"));
- });
- }
- @Test
- public void itShouldUseDefaultJvmArgsForLowerThanJira7_7_0() {
- newArrayList(
- newProduct("7.6.0"), newProduct("7.6.19-SNAPSHOT"), newProduct("7.1"))
- .forEach(product -> {
- productHandler.fixJvmArgs(product);
- assertThat(format("Jira version %s does not have the correct Xmx", product.getVersion()), product.getJvmArgs(), containsString("-Xmx512m"));
- assertThat(format("Jira version %s has Xms set and should not", product.getVersion()), product.getJvmArgs(), not(containsString("-Xms384m")));
- });
- }
- @Test
- public void dbconfigXmlCreatedWithCorrectPath() throws Exception {
- JiraProductHandler.createDbConfigXmlIfNone(TEMP_HOME);
- File f = new File(TEMP_HOME, FILENAME_DBCONFIG);
- assertTrue("The config file is created: " + FILENAME_DBCONFIG, f.exists());
- assertTrue("And it's a regular file", f.isFile());
- File dbFile = new File(TEMP_HOME, "database");
- Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(f);
- XPathExpression xpe = XPathFactory.newInstance().newXPath().compile("/jira-database-config/jdbc-datasource/url");
- String x = xpe.evaluate(d);
- assertEquals("The JDBC URI for the embedded database is as expected",
- "jdbc:hsqldb:file:" + dbFile.toURI().getPath(), x);
- }
- @Test
- public void updateDBConfigXmlForOracle10g() throws Exception {
- testUpdateDbConfigXml(new Oracle10g(log));
- }
- @Test
- public void updateDBConfigXmlForOracle12c() throws Exception {
- testUpdateDbConfigXml(new Oracle12c(log));
- }
- @Test
- public void updateDBConfigXmlForMysql() throws Exception {
- testUpdateDbConfigXml(new MySQL(log));
- }
- @Test
- public void updateDBConfigXmlForPostgres() throws Exception {
- testUpdateDbConfigXml(new Postgres(log));
- }
- @Test
- public void updateDBConfigXmlForMicrosoftMssql() throws Exception {
- testUpdateDbConfigXml(new MssqlMicrosoft(log));
- }
- @Test
- public void updateDBConfigXmlForJtdsMssql() throws Exception {
- testUpdateDbConfigXml(new MssqlJtds(log));
- }
- @Test
- public void shouldByDefaultForceJiraSynchronousStartup() {
- final Product product = newProduct();
- final Map<String, String> systemProperties = productHandler.getSystemProperties(product, 0);
- // then
- assertThat(
- systemProperties, hasEntry(
- "com.atlassian.jira.startup.LauncherContextListener.SYNCHRONOUS", "true"
- ));
- }
- @Test
- public void shouldPassAwaitInitializationFlagFromProduct() {
- final Product product = newProduct();
- final Map<String, String> systemPropertiesWithAwait = productHandler.getSystemProperties(product, 0);
- product.setAwaitFullInitialization(false);
- final Map<String, String> systemPropertiesNoWait = productHandler.getSystemProperties(product, 0);
- // then
- assertThat(
- systemPropertiesWithAwait, hasEntry(
- "com.atlassian.jira.startup.LauncherContextListener.SYNCHRONOUS", "true"
- ));
- assertThat(
- systemPropertiesNoWait, not(hasKey(
- "com.atlassian.jira.startup.LauncherContextListener.SYNCHRONOUS"
- )));
- }
- private void testUpdateDbConfigXml(final DatabaseType databaseType) throws Exception {
- // Create default dbconfig.xml
- JiraProductHandler.createDbConfigXmlIfNone(TEMP_HOME);
- // Setup
- final File f = new File(TEMP_HOME, FILENAME_DBCONFIG);
- final String schema = "test-schema";
- final SAXReader reader = new SAXReader();
- org.dom4j.Document dbConfigXml = reader.read(f);
- // Check default db type
- assertEquals("hsql", getDbType(dbConfigXml));
- assertEquals("PUBLIC", getDbSchema(dbConfigXml));
- // Invoke: update dbconfig.xml
- JiraProductHandler.updateDbConfigXml(TEMP_HOME, databaseType, schema);
- dbConfigXml = reader.read(f);
- // Check
- assertEquals(databaseType.getOfBizName(), getDbType(dbConfigXml));
- if (databaseType.hasSchema()) {
- assertThat("Schema has to update", schema.equals(getDbSchema(dbConfigXml)), is(true));
- } else {
- assertThat("Schema has not to update", schema.equals(getDbSchema(dbConfigXml)), is(false));
- }
- }
- private String getDbType(org.dom4j.Document dbConfigXml) {
- final org.dom4j.Node dbTypeNode = dbConfigXml.selectSingleNode("//jira-database-config/database-type");
- return dbTypeNode == null ? "" : dbTypeNode.getStringValue();
- }
- private String getDbSchema(org.dom4j.Document dbConfigXml) {
- final org.dom4j.Node schemaNode = dbConfigXml.selectSingleNode("//jira-database-config/schema-name");
- return schemaNode == null ? "" : schemaNode.getStringValue();
- }
- @Test
- public void dbconfigXmlNotCreatedWhenAlreadyExists() throws Exception {
- final File f = new File(TEMP_HOME, FILENAME_DBCONFIG);
- FileUtils.writeStringToFile(f, "Original contents", StandardCharsets.UTF_8);
- JiraProductHandler.createDbConfigXmlIfNone(TEMP_HOME);
- final String after = FileUtils.readFileToString(f, StandardCharsets.UTF_8);
- assertEquals("Original contents", after);
- }
- @Test
- public void pluginsShouldGoIntoLocalHomeIfNoSharedHomeIsSpecified() {
- final File localHome = createTempDir("jira-local");
- assertUserInstalledPluginsDirectory(localHome, null, localHome);
- }
- @Test
- public void pluginsShouldGoIntoSharedHomeIfOneIsSpecified() {
- final File sharedHome = createTempDir("jira-shared");
- assertUserInstalledPluginsDirectory(null, sharedHome.getPath(), sharedHome);
- }
- private void assertUserInstalledPluginsDirectory(
- final File localHome, final String sharedHomePath, final File expectedParentDir) {
- // Set up
- final Product mockProduct = mock(Product.class);
- when(mockProduct.getSharedHome()).thenReturn(sharedHomePath);
- // Invoke
- final Optional<File> userInstalledPluginsDirectory =
- productHandler.getUserInstalledPluginsDirectory(mockProduct, null, localHome);
- // Check
- assertNotNull(userInstalledPluginsDirectory);
- assertThat(userInstalledPluginsDirectory,
- is(Optional.of(new File(new File(expectedParentDir, PLUGINS_DIR), INSTALLED_PLUGINS_DIR))));
- }
- @Test
- public void bundledPluginsShouldBeUnzippedIfPresent() {
- final File bundledPluginsDir = new File(TEMP_HOME, BUNDLED_PLUGINS_UNZIPPED);
- //noinspection ResultOfMethodCallIgnored
- bundledPluginsDir.mkdirs();
- assertTrue(bundledPluginsDir.exists());
- assertBundledPluginPath("6.3", TEMP_HOME, bundledPluginsDir);
- }
- @Test
- public void bundledPluginsLocationCorrectFor41() {
- final File bundledPluginsZip = new File(TEMP_HOME, BUNDLED_PLUGINS_FROM_4_1);
- assertBundledPluginPath("4.1", TEMP_HOME, bundledPluginsZip);
- }
- @Test
- public void bundledPluginsLocationCorrectFor40() {
- final File bundledPluginsZip = new File(TEMP_HOME, BUNDLED_PLUGINS_UPTO_4_0);
- assertBundledPluginPath("4.0", TEMP_HOME, bundledPluginsZip);
- }
- @Test
- public void bundledPluginsLocationCorrectForFallback() {
- final File bundledPluginsZip = new File(TEMP_HOME, BUNDLED_PLUGINS_FROM_4_1);
- assertBundledPluginPath("not.a.version", TEMP_HOME, bundledPluginsZip);
- }
- @Test
- public void testCustomiseInstanceForServerXmlOverridesForNewJiras() {
- final Product ctx = mock(Product.class);
- when(ctx.getVersion()).thenReturn("7.12.0");
- final JiraProductHandler spied = spy(productHandler);
- // Execute
- spied.customiseInstance(ctx, new File("./"), new File("./"));
- // Validate
- verify(ctx, times(1)).setCargoXmlOverrides(expectedOverridesCaptor.capture());
- assertThat(expectedOverridesCaptor.getValue().size(), is(2));
- final Iterator<XmlOverride> valueIterator = expectedOverridesCaptor.getValue().iterator();
- final XmlOverride first = valueIterator.next();
- assertThat(first.getAttributeName(), is("relaxedPathChars"));
- assertThat(first.getValue(), is("[]|"));
- final XmlOverride second = valueIterator.next();
- assertThat(second.getAttributeName(), is("relaxedQueryChars"));
- assertThat(second.getValue(), is("[]|{}^\\`\"<>"));
- }
- @Test
- public void testCustomiseInstanceForServerXmlOverridesForOlderJiras() {
- final Product ctx = mock(Product.class);
- when(ctx.getVersion()).thenReturn("7.11.0");
- final JiraProductHandler spied = spy(productHandler);
- // Execute
- spied.customiseInstance(ctx, new File("./"), new File("./"));
- // Validate we did not overide configuration
- // reason - older Jiras run older tomcat version that are less resticted
- verify(ctx, times(0)).setCargoXmlOverrides(any());
- }
- private Product newProduct(String version) {
- final Product product = newProduct();
- product.setVersion(version);
- return product;
- }
- private Product newProduct() {
- final Product product = new Product();
- product.setInstanceId("jira");
- product.setDataSources(newArrayList());
- product.setAwaitFullInitialization(null);
- product.initialiseNodes();
- return product;
- }
- private void assertBundledPluginPath(final String version, final File appDir, final File expectedPath) {
- // Set up
- final Log mockLog = mock(Log.class);
- when(mavenContext.getLog()).thenReturn(mockLog);
- final Product mockProduct = mock(Product.class);
- when(mockProduct.getVersion()).thenReturn(version);
- // Invoke
- final File bundledPluginPath = productHandler.getBundledPluginPath(mockProduct, appDir);
- // Check
- assertNotNull(bundledPluginPath);
- assertEquals(expectedPath, bundledPluginPath);
- }
- @Test
- public void getExtraJarsToSkipWhenScanningForTldsAndWebFragments_whenCalled_shouldSkipJotmAndXapool() {
- // Set up
- // Invoke
- final Collection<String> extraJarsToSkip = productHandler.getExtraJarsToSkipWhenScanningForTldsAndWebFragments();
- // Check
- assertThat(extraJarsToSkip, contains("jotm*.jar", "xapool*.jar"));
- }
- @Test
- public void getReplacements_whenBaseUrlReplaced_shouldUseCorrectPortNumber() {
- assertReplacedWithBaseUrl("@base-url@");
- }
- @Test
- public void getReplacements_whenLocalhost8080Replaced_shouldUseCorrectPortNumber() {
- assertReplacedWithBaseUrl("http://localhost:8080");
- }
- private void assertReplacedWithBaseUrl(final String original) {
- // Set up
- final Product product = mock(Product.class);
- when(product.getContextPath()).thenReturn("theContextPath");
- when(product.getDataHome()).thenReturn("theParent/theDataHome");
- when(product.getInstanceId()).thenReturn("theInstanceId");
- when(product.getProtocol()).thenReturn("theProtocol");
- when(product.getServer()).thenReturn("theServer");
- when(product.getWebPortForNode(0)).thenReturn(42);
- final Node node = mock(Node.class);
- when(product.getNodes()).thenReturn(singletonList(node));
- // Invoke
- final List<Replacement> replacements = productHandler.getReplacements(product, 0);
- // Check
- final String result = apply(replacements, original);
- assertThat(result, is("theProtocol://theServer:42/theContextPath"));
- }
- private static String apply(final List<Replacement> replacements, String result) {
- for (final Replacement replacement : replacements) {
- result = replacement.replace(result);
- }
- return result;
- }
- }