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.apache.maven.model.Model;
24  import org.apache.maven.model.Scm;
25  import org.jdom2.Document;
26  import org.jdom2.Element;
27  import org.jdom2.input.SAXBuilder;
28  import org.junit.Test;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertNotNull;
32  import static org.junit.Assert.assertNull;
33  
34  public class JDomModelTest {
35      private SAXBuilder builder = new SAXBuilder();
36  
37      @Test
38      public void testGetScm() throws Exception {
39          String content = "<project></project>";
40          Document document = builder.build(new StringReader(content));
41          assertNull(new JDomModel(document).getScm());
42      }
43  
44      @Test
45      public void testSetScm() throws Exception {
46          String content = "<project></project>";
47          Document document = builder.build(new StringReader(content));
48          Model model = new JDomModel(document);
49          assertNull(model.getScm());
50  
51          model.setScm(new Scm());
52          assertNotNull(model.getScm());
53  
54          model.setScm(null);
55          assertNull(model.getScm());
56      }
57  
58      @Test
59      public void testSetVersion() throws Exception {
60          String content = "<project></project>";
61          Element projectElm = builder.build(new StringReader(content)).getRootElement();
62          Model model = new JDomModel(projectElm);
63          assertNull(model.getVersion());
64  
65          model.setVersion("VERSION");
66          assertEquals("VERSION", getVersion(projectElm));
67  
68          model.setVersion(null);
69          assertNull(model.getVersion());
70  
71          // inherit from parent
72          // this business logic might need to moved.
73          content = "<project><parent><version>PARENT_VERSION</version></parent></project>";
74          projectElm = builder.build(new StringReader(content)).getRootElement();
75          model = new JDomModel(projectElm);
76          assertNull(model.getVersion());
77  
78          model.setVersion("PARENT_VERSION");
79          assertNull(getVersion(projectElm));
80  
81          model.setVersion("VERSION");
82          assertEquals("VERSION", getVersion(projectElm));
83  
84          model.setVersion(null);
85          assertNull(model.getVersion());
86      }
87  
88      private String getVersion(Element projectElm) {
89          return projectElm.getChildText("version", projectElm.getNamespace());
90      }
91  }