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 JDomExtensionTest {
31      private SAXBuilder builder = new SAXBuilder();
32  
33      @Test
34      public void testGetArtifactId() throws Exception {
35          String content = "<extension></extension>";
36          Element extensionElm = builder.build(new StringReader(content)).getRootElement();
37          assertNull(new JDomExtension(extensionElm).getArtifactId());
38  
39          content = "<extension><artifactId>ARTIFACTID</artifactId></extension>";
40          extensionElm = builder.build(new StringReader(content)).getRootElement();
41          assertEquals("ARTIFACTID", new JDomExtension(extensionElm).getArtifactId());
42      }
43  
44      @Test
45      public void testGetGroupId() throws Exception {
46          String content = "<extension></extension>";
47          Element extensionElm = builder.build(new StringReader(content)).getRootElement();
48          assertNull(new JDomExtension(extensionElm).getGroupId());
49  
50          content = "<extension><groupId>GROUPID</groupId></extension>";
51          extensionElm = builder.build(new StringReader(content)).getRootElement();
52          assertEquals("GROUPID", new JDomExtension(extensionElm).getGroupId());
53      }
54  
55      @Test
56      public void testGetVersion() throws Exception {
57          String content = "<extension></extension>";
58          Element extensionElm = builder.build(new StringReader(content)).getRootElement();
59          assertNull(new JDomExtension(extensionElm).getVersion());
60  
61          content = "<extension><version>VERSION</version></extension>";
62          extensionElm = builder.build(new StringReader(content)).getRootElement();
63          assertEquals("VERSION", new JDomExtension(extensionElm).getVersion());
64      }
65  
66      @Test(expected = UnsupportedOperationException.class)
67      public void testSetArtifactId() {
68          new JDomExtension(null).setArtifactId(null);
69      }
70  
71      @Test(expected = UnsupportedOperationException.class)
72      public void testSetGroupId() {
73          new JDomExtension(null).setGroupId(null);
74      }
75  
76      @Test
77      public void testSetVersion() throws Exception {
78          String content = "<extension><version>OLD_VERSION</version></extension>";
79          Element extensionElm = builder.build(new StringReader(content)).getRootElement();
80          new JDomExtension(extensionElm).setVersion("NEW_VERSION");
81          assertEquals("NEW_VERSION", getVersion(extensionElm));
82      }
83  
84      @Test
85      public void testGetName() {
86          assertEquals("extension", new JDomExtension(null).getName());
87      }
88  
89      private String getVersion(Element extensionElm) {
90          return extensionElm.getChildTextTrim("version", extensionElm.getNamespace());
91      }
92  }