PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/opup/src/test/java/com/atlassian/labs/opup/VersionChangerTest.java

https://bitbucket.org/jwalton/opup
Java | 261 lines | 203 code | 58 blank | 0 comment | 3 complexity | b877bf5e427dbd312cea2912241200f4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package com.atlassian.labs.opup;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.InputStream;
  5. import java.util.Iterator;
  6. import javax.xml.namespace.NamespaceContext;
  7. import javax.xml.parsers.DocumentBuilderFactory;
  8. import javax.xml.xpath.XPath;
  9. import javax.xml.xpath.XPathConstants;
  10. import javax.xml.xpath.XPathExpressionException;
  11. import javax.xml.xpath.XPathFactory;
  12. import org.apache.commons.io.IOUtils;
  13. import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
  14. import org.hamcrest.CoreMatchers;
  15. import org.hamcrest.Matchers;
  16. import org.junit.Test;
  17. import org.w3c.dom.Document;
  18. import static org.hamcrest.Matchers.startsWith;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.assertFalse;
  21. import static org.junit.Assert.assertThat;
  22. import static org.junit.Assert.assertTrue;
  23. public class VersionChangerTest
  24. {
  25. @Test
  26. public void returnsFalseWhenNoVersionsAreChanged() throws Exception
  27. {
  28. VersionChanger vc = new VersionChanger();
  29. InputStream in = getClass().getResourceAsStream("sample-pom.xml");
  30. assertFalse(vc.process(in, new ByteArrayOutputStream()));
  31. }
  32. private Document processSampleDocument(VersionChanger vc) throws Exception
  33. {
  34. InputStream in = getClass().getResourceAsStream("sample-pom.xml");
  35. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  36. assertTrue(vc.process(in, baos));
  37. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  38. dbf.setNamespaceAware(true);
  39. Document after = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(baos.toByteArray()));
  40. assertEquals("project", after.getDocumentElement().getTagName());
  41. assertEquals("http://maven.apache.org/POM/4.0.0", after.getDocumentElement().getNamespaceURI());
  42. return after;
  43. }
  44. private static String xpathEvaluateString(Document doc, String expr) throws XPathExpressionException
  45. {
  46. XPath xpath = XPathFactory.newInstance().newXPath();
  47. xpath.setNamespaceContext(new TestNamespaceContext());
  48. return (String) xpath.evaluate(expr, doc, XPathConstants.STRING);
  49. }
  50. @Test
  51. public void simpleVersionsAreChanged() throws Exception
  52. {
  53. VersionChanger vc = new VersionChanger();
  54. vc.setNewVersion("a:simple", new DefaultArtifactVersion("1.1"));
  55. Document after = processSampleDocument(vc);
  56. assertEquals("1.1", xpathEvaluateString(after, "/m:project/m:dependencies/m:dependency[m:artifactId='simple']/m:version"));
  57. }
  58. @Test
  59. public void warVersionsAreChanged() throws Exception
  60. {
  61. VersionChanger vc = new VersionChanger();
  62. vc.setNewVersion("b:war-artifact:war", new DefaultArtifactVersion("2.1"));
  63. Document after = processSampleDocument(vc);
  64. assertEquals("2.1", xpathEvaluateString(after, "/m:project/m:dependencies/m:dependency[m:artifactId='war-artifact']/m:version"));
  65. }
  66. @Test
  67. public void dependencyManagementVersionsAreChanged() throws Exception
  68. {
  69. VersionChanger vc = new VersionChanger();
  70. vc.setNewVersion("c:specified-in-dependency-management", new DefaultArtifactVersion("3.1"));
  71. Document after = processSampleDocument(vc);
  72. assertEquals("3.1", xpathEvaluateString(after, "/m:project/m:dependencyManagement/m:dependencies/m:dependency[m:artifactId='specified-in-dependency-management']/m:version"));
  73. }
  74. @Test
  75. public void propertyVersionsAreChanged() throws Exception
  76. {
  77. VersionChanger vc = new VersionChanger();
  78. vc.setNewPropertyValue("property.version", new DefaultArtifactVersion("4.1"));
  79. Document after = processSampleDocument(vc);
  80. assertEquals("4.1", xpathEvaluateString(after, "/m:project/m:properties/m:property.version"));
  81. }
  82. @Test
  83. public void allVersionsAreChanged() throws Exception
  84. {
  85. VersionChanger vc = new VersionChanger();
  86. vc.setNewVersion("a:simple", new DefaultArtifactVersion("1.1"));
  87. vc.setNewVersion("b:war-artifact:war", new DefaultArtifactVersion("2.1"));
  88. vc.setNewVersion("c:specified-in-dependency-management", new DefaultArtifactVersion("3.1"));
  89. vc.setNewPropertyValue("property.version", new DefaultArtifactVersion("4.1"));
  90. Document after = processSampleDocument(vc);
  91. assertEquals("1.1", xpathEvaluateString(after, "/m:project/m:dependencies/m:dependency[m:artifactId='simple']/m:version"));
  92. assertEquals("2.1", xpathEvaluateString(after, "/m:project/m:dependencies/m:dependency[m:artifactId='war-artifact']/m:version"));
  93. assertEquals("3.1", xpathEvaluateString(after, "/m:project/m:dependencyManagement/m:dependencies/m:dependency[m:artifactId='specified-in-dependency-management']/m:version"));
  94. assertEquals("4.1", xpathEvaluateString(after, "/m:project/m:properties/m:property.version"));
  95. }
  96. static class TestNamespaceContext implements NamespaceContext
  97. {
  98. @Override
  99. public String getNamespaceURI(String prefix)
  100. {
  101. if (prefix.equals("m"))
  102. {
  103. return "http://maven.apache.org/POM/4.0.0";
  104. }
  105. else
  106. {
  107. return null;
  108. }
  109. }
  110. @Override
  111. public String getPrefix(String namespaceURI)
  112. {
  113. throw new UnsupportedOperationException();
  114. }
  115. @Override
  116. public Iterator<String> getPrefixes(String namespaceURI)
  117. {
  118. throw new UnsupportedOperationException();
  119. }
  120. }
  121. @Test
  122. public void reasonableStringRepresentationWhenEmpty() throws Exception
  123. {
  124. VersionChanger vc = new VersionChanger();
  125. assertTrue(vc.isEmpty());
  126. assertEquals("No changes.", vc.toString());
  127. }
  128. @Test
  129. public void reasonableStringRepresentationWithVersions() throws Exception
  130. {
  131. VersionChanger vc = new VersionChanger();
  132. vc.setNewVersion("group:artifact", new DefaultArtifactVersion("1"));
  133. assertFalse(vc.isEmpty());
  134. assertEquals("group:artifact 1", vc.toString());
  135. }
  136. @Test
  137. public void reasonableStringRepresentationWithProperties() throws Exception
  138. {
  139. VersionChanger vc = new VersionChanger();
  140. vc.setNewPropertyValue("version.property", new DefaultArtifactVersion("1"));
  141. assertFalse(vc.isEmpty());
  142. assertEquals("version.property 1", vc.toString());
  143. }
  144. @Test
  145. public void reasonableStringRepresentationWithMultipleVersions() throws Exception
  146. {
  147. VersionChanger vc;
  148. vc = new VersionChanger();
  149. vc.setNewVersion("c", new DefaultArtifactVersion("3"));
  150. vc.setNewVersion("d", new DefaultArtifactVersion("4"));
  151. assertTrue(vc.toString().equals("c 3, d 4") || vc.toString().equals("d 4, c 3"));
  152. }
  153. @Test
  154. public void reasonableStringRepresentationWithMultipleProperties() throws Exception
  155. {
  156. VersionChanger vc;
  157. vc = new VersionChanger();
  158. vc.setNewPropertyValue("a", new DefaultArtifactVersion("1"));
  159. vc.setNewPropertyValue("b", new DefaultArtifactVersion("2"));
  160. assertTrue(vc.toString().equals("a 1, b 2") || vc.toString().equals("b 2, a 1"));
  161. }
  162. @Test
  163. public void processedDocumentHasNewlineBeforeProject() throws Exception
  164. {
  165. InputStream in = getClass().getResourceAsStream("sample-pom.xml");
  166. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  167. VersionChanger vc = new VersionChanger();
  168. vc.process(in, baos);
  169. String result = new String(baos.toByteArray(), "utf-8");
  170. assertThat(result, CoreMatchers.<String>either(startsWith("<project")).or(Matchers.containsString("\n<project")));
  171. }
  172. @Test
  173. public void xmlFormattingSurvivesRoundTrip() throws Exception
  174. {
  175. InputStream origIn = getClass().getResourceAsStream("sample-pom.xml");
  176. String original = IOUtils.toString(origIn);
  177. VersionChanger vc = new VersionChanger();
  178. InputStream in;
  179. ByteArrayOutputStream out;
  180. vc.setNewPropertyValue("property.version", new DefaultArtifactVersion("1"));
  181. in = getClass().getResourceAsStream("sample-pom.xml");
  182. out = new ByteArrayOutputStream();
  183. vc.process(in, out);
  184. vc.setNewPropertyValue("property.version", new DefaultArtifactVersion("4"));
  185. in = new ByteArrayInputStream(out.toByteArray());
  186. out = new ByteArrayOutputStream();
  187. vc.process(in, out);
  188. String after = new String(out.toByteArray(), "us-ascii");
  189. assertEquals(original, after);
  190. }
  191. @Test
  192. public void addAllCombinesChangers()
  193. {
  194. VersionChanger vc1 = new VersionChanger();
  195. vc1.setNewPropertyValue("prop", new DefaultArtifactVersion("1"));
  196. VersionChanger vc2 = new VersionChanger();
  197. vc2.setNewVersion("group:artifact", new DefaultArtifactVersion("2"));
  198. VersionChanger combined = new VersionChanger();
  199. combined.addAll(vc1);
  200. combined.addAll(vc2);
  201. assertEquals("group:artifact 2, prop 1", combined.toString());
  202. }
  203. }