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 JDomDependencyTest {
31      private SAXBuilder builder = new SAXBuilder();
32  
33      @Test(expected = UnsupportedOperationException.class)
34      public void testIsOptional() {
35          new JDomDependency(null).isOptional();
36      }
37  
38      @Test(expected = UnsupportedOperationException.class)
39      public void testSetOptional() {
40          new JDomDependency(null).setOptional(true);
41      }
42  
43      @Test(expected = UnsupportedOperationException.class)
44      public void testAddExclusion() {
45          new JDomDependency(null).addExclusion(null);
46      }
47  
48      @Test
49      public void testGetArtifactId() throws Exception {
50          String content = "<dependency></dependency>";
51          Element dependencyElm = builder.build(new StringReader(content)).getRootElement();
52          assertNull(new JDomDependency(dependencyElm).getArtifactId());
53  
54          content = "<dependency><artifactId>ARTIFACTID</artifactId></dependency>";
55          dependencyElm = builder.build(new StringReader(content)).getRootElement();
56          assertEquals("ARTIFACTID", new JDomDependency(dependencyElm).getArtifactId());
57      }
58  
59      @Test(expected = UnsupportedOperationException.class)
60      public void testGetClassifier() {
61          new JDomDependency(null).getClassifier();
62      }
63  
64      @Test(expected = UnsupportedOperationException.class)
65      public void testGetExclusions() {
66          new JDomDependency(null).getExclusions();
67      }
68  
69      @Test
70      public void testGetGroupId() throws Exception {
71          String content = "<dependency></dependency>";
72          Element dependencyElm = builder.build(new StringReader(content)).getRootElement();
73          assertNull(new JDomDependency(dependencyElm).getGroupId());
74  
75          content = "<dependency><groupId>GROUPID</groupId></dependency>";
76          dependencyElm = builder.build(new StringReader(content)).getRootElement();
77          assertEquals("GROUPID", new JDomDependency(dependencyElm).getGroupId());
78      }
79  
80      @Test(expected = UnsupportedOperationException.class)
81      public void testGetScope() {
82          new JDomDependency(null).getScope();
83      }
84  
85      @Test(expected = UnsupportedOperationException.class)
86      public void testGetSystemPath() {
87          new JDomDependency(null).getSystemPath();
88      }
89  
90      @Test(expected = UnsupportedOperationException.class)
91      public void testGetType() {
92          new JDomDependency(null).getType();
93      }
94  
95      @Test
96      public void testGetVersion() throws Exception {
97          String content = "<dependency></dependency>";
98          Element dependencyElm = builder.build(new StringReader(content)).getRootElement();
99          assertNull(new JDomDependency(dependencyElm).getVersion());
100 
101         content = "<dependency><version>VERSION</version></dependency>";
102         dependencyElm = builder.build(new StringReader(content)).getRootElement();
103         assertEquals("VERSION", new JDomDependency(dependencyElm).getVersion());
104     }
105 
106     @Test(expected = UnsupportedOperationException.class)
107     public void testRemoveExclusion() {
108         new JDomDependency(null).removeExclusion(null);
109         ;
110     }
111 
112     @Test(expected = UnsupportedOperationException.class)
113     public void testSetArtifactIdString() {
114         new JDomDependency(null).setArtifactId(null);
115         ;
116     }
117 
118     @Test(expected = UnsupportedOperationException.class)
119     public void testSetClassifierString() {
120         new JDomDependency(null).setClassifier(null);
121         ;
122     }
123 
124     @Test(expected = UnsupportedOperationException.class)
125     public void testSetExclusions() {
126         new JDomDependency(null).setExclusions(null);
127         ;
128     }
129 
130     @Test(expected = UnsupportedOperationException.class)
131     public void testSetGroupIdString() {
132         new JDomDependency(null).setGroupId(null);
133     }
134 
135     @Test(expected = UnsupportedOperationException.class)
136     public void testSetScopeString() {
137         new JDomDependency(null).setScope(null);
138     }
139 
140     @Test(expected = UnsupportedOperationException.class)
141     public void testSetSystemPathString() {
142         new JDomDependency(null).setSystemPath(null);
143     }
144 
145     @Test(expected = UnsupportedOperationException.class)
146     public void testSetTypeString() {
147         new JDomDependency(null).setType(null);
148     }
149 
150     @Test
151     public void testSetVersionString() throws Exception {
152         String content = "<dependency><version>OLD_VERSION</version></dependency>";
153         Element dependencyElm = builder.build(new StringReader(content)).getRootElement();
154         new JDomDependency(dependencyElm).setVersion("NEW_VERSION");
155         assertEquals("NEW_VERSION", getVersion(dependencyElm));
156     }
157 
158     @Test
159     public void testGetName() {
160         assertEquals("dependency", new JDomDependency(null).getName());
161     }
162 
163     private String getVersion(Element dependencyElm) {
164         return dependencyElm.getChildTextTrim("version", dependencyElm.getNamespace());
165     }
166 }