View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.release.transform.jdom2;
20  
21  import java.io.StringReader;
22  
23  import org.jdom2.Element;
24  import org.jdom2.input.SAXBuilder;
25  import org.junit.Test;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertNull;
29  
30  public class JDomScmTest {
31      private SAXBuilder builder = new SAXBuilder();
32  
33      @Test(expected = UnsupportedOperationException.class)
34      public void testGetConnection() {
35          new JDomScm(null).getConnection();
36      }
37  
38      @Test(expected = UnsupportedOperationException.class)
39      public void testGetDeveloperConnection() {
40          new JDomScm(null).getDeveloperConnection();
41      }
42  
43      @Test(expected = UnsupportedOperationException.class)
44      public void testGetTag() {
45          new JDomScm(null).getTag();
46      }
47  
48      @Test(expected = UnsupportedOperationException.class)
49      public void testGetUrl() {
50          new JDomScm(null).getUrl();
51      }
52  
53      @Test
54      public void testSetConnectionString() throws Exception {
55          String content = "<scm></scm>";
56          Element scmElm = builder.build(new StringReader(content)).getRootElement();
57  
58          assertNull(getConnection(scmElm));
59  
60          new JDomScm(scmElm).setConnection("CONNECTION");
61          assertEquals("CONNECTION", getConnection(scmElm));
62  
63          new JDomScm(scmElm).setConnection(null);
64          assertNull(getConnection(scmElm));
65      }
66  
67      @Test
68      public void testSetDeveloperConnectionString() throws Exception {
69          String content = "<scm></scm>";
70          Element scmElm = builder.build(new StringReader(content)).getRootElement();
71  
72          assertNull(getDeveloperConnection(scmElm));
73  
74          new JDomScm(scmElm).setDeveloperConnection("DEVELOPERCONNECTION");
75          assertEquals("DEVELOPERCONNECTION", getDeveloperConnection(scmElm));
76  
77          new JDomScm(scmElm).setDeveloperConnection(null);
78          assertNull(getDeveloperConnection(scmElm));
79      }
80  
81      @Test
82      public void testSetTagString() throws Exception {
83          String content = "<scm></scm>";
84          Element scmElm = builder.build(new StringReader(content)).getRootElement();
85  
86          assertNull(getUrl(scmElm));
87  
88          new JDomScm(scmElm).setUrl("URL");
89          assertEquals("URL", getUrl(scmElm));
90  
91          new JDomScm(scmElm).setUrl(null);
92          assertNull(getUrl(scmElm));
93      }
94  
95      @Test
96      public void testSetUrlString() throws Exception {
97          String content = "<scm></scm>";
98          Element scmElm = builder.build(new StringReader(content)).getRootElement();
99  
100         assertNull(getTag(scmElm));
101 
102         new JDomScm(scmElm).setTag("TAG");
103         assertEquals("TAG", getTag(scmElm));
104 
105         new JDomScm(scmElm).setTag(null);
106         assertNull(getTag(scmElm));
107     }
108 
109     private String getConnection(Element scmElm) {
110         return scmElm.getChildText("connection", scmElm.getNamespace());
111     }
112 
113     private String getDeveloperConnection(Element scmElm) {
114         return scmElm.getChildText("developerConnection", scmElm.getNamespace());
115     }
116 
117     private String getTag(Element scmElm) {
118         return scmElm.getChildText("tag", scmElm.getNamespace());
119     }
120 
121     private String getUrl(Element scmElm) {
122         return scmElm.getChildText("url", scmElm.getNamespace());
123     }
124 }