1 package com.atlassian.maven.plugins.amps.product;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import javax.xml.parsers.DocumentBuilderFactory;
7 import javax.xml.xpath.XPathExpression;
8 import javax.xml.xpath.XPathFactory;
9
10 import org.apache.commons.io.FileUtils;
11 import org.apache.maven.plugin.MojoExecutionException;
12 import org.junit.After;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.w3c.dom.Document;
16
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertTrue;
19
20 public class TestJiraProductHandler
21 {
22 static File tempHome;
23
24 @Before
25 public void createTemporaryHomeDirectory() throws IOException
26 {
27 File f = File.createTempFile("temp-jira-", "-home");
28 if (!f.delete())
29 {
30 throw new IOException();
31 }
32
33 if (!f.mkdir())
34 {
35 throw new IOException();
36 }
37
38 tempHome = f;
39 }
40
41 @After
42 public void deleteFileAndTemporaryHomeDirectory() throws Exception
43 {
44 if (tempHome != null)
45 {
46 new File(tempHome, "dbconfig.xml").delete();
47 tempHome.delete();
48 }
49 }
50
51 @Test
52 public void dbconfigXmlCreatedWithCorrectPath() throws Exception
53 {
54 JiraProductHandler.createDbConfigXmlIfNecessary(tempHome);
55
56 File f = new File(tempHome, "dbconfig.xml");
57 assertTrue("The dbconfig.xml is created", f.exists());
58 assertTrue("And it's a regular file", f.isFile());
59
60 File dbFile = new File(tempHome, "database");
61
62 Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(f);
63
64 XPathExpression xpe = XPathFactory.newInstance().newXPath().compile("/jira-database-config/jdbc-datasource/url");
65
66 String x = xpe.evaluate(d);
67 assertEquals("The JDBC URI for the embedded database is as expected",
68 "jdbc:hsqldb:file:" + dbFile.toURI().getPath(), x);
69 }
70
71 @Test
72 public void dbconfigXmlNotCreatedWhenAlreadyExists() throws MojoExecutionException, IOException
73 {
74 File f = new File(tempHome, "dbconfig.xml");
75 FileUtils.writeStringToFile(f, "Original contents");
76 JiraProductHandler.createDbConfigXmlIfNecessary(tempHome);
77
78 String after = FileUtils.readFileToString(f);
79 assertEquals("Original contents", after);
80 }
81 }