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.InputStream;
22  import java.io.OutputStream;
23  import java.io.PrintStream;
24  import java.io.PrintWriter;
25  import java.io.Reader;
26  import java.io.StringReader;
27  import java.io.Writer;
28  
29  import org.jdom2.Element;
30  import org.jdom2.input.SAXBuilder;
31  import org.junit.Test;
32  
33  import static org.junit.Assert.assertEquals;
34  import static org.junit.Assert.assertNull;
35  
36  public class JDomPropertiesTest {
37      private SAXBuilder builder = new SAXBuilder();
38  
39      @Test
40      public void testSetProperty() throws Exception {
41          String content = "<properties></properties>";
42          Element propertiesElm = builder.build(new StringReader(content)).getRootElement();
43          assertNull(getProperty(propertiesElm, "KEY"));
44  
45          // Adding not allowed, prepare properties
46          content = "<properties><KEY>OLD_VALUE</KEY></properties>";
47          propertiesElm = builder.build(new StringReader(content)).getRootElement();
48          assertEquals("OLD_VALUE", getProperty(propertiesElm, "KEY"));
49          new JDomProperties(propertiesElm).setProperty("KEY", "NEW_VALUE");
50          assertEquals("NEW_VALUE", getProperty(propertiesElm, "KEY"));
51      }
52  
53      @Test(expected = UnsupportedOperationException.class)
54      public void testLoadReader() throws Exception {
55          new JDomProperties(null).load((Reader) null);
56      }
57  
58      @Test(expected = UnsupportedOperationException.class)
59      public void testLoadInputStream() throws Exception {
60          new JDomProperties(null).load((InputStream) null);
61      }
62  
63      @Test(expected = UnsupportedOperationException.class)
64      public void testSave() {
65          new JDomProperties(null).save(null, null);
66      }
67  
68      @Test(expected = UnsupportedOperationException.class)
69      public void testStoreWriter() throws Exception {
70          new JDomProperties(null).store((Writer) null, null);
71      }
72  
73      @Test(expected = UnsupportedOperationException.class)
74      public void testStoreOutputStream() throws Exception {
75          new JDomProperties(null).store((OutputStream) null, null);
76      }
77  
78      @Test(expected = UnsupportedOperationException.class)
79      public void testLoadFromXML() throws Exception {
80          new JDomProperties(null).loadFromXML(null);
81      }
82  
83      @Test(expected = UnsupportedOperationException.class)
84      public void testStoreToXML() throws Exception {
85          new JDomProperties(null).storeToXML(null, null);
86      }
87  
88      @Test(expected = UnsupportedOperationException.class)
89      public void testStoreToXMLEncoded() throws Exception {
90          new JDomProperties(null).storeToXML((OutputStream) null, null, (String) null);
91      }
92  
93      @Test
94      public void testGetProperty() throws Exception {
95          String content = "<properties></properties>";
96          Element propertiesElm = builder.build(new StringReader(content)).getRootElement();
97          assertNull(new JDomProperties(propertiesElm).getProperty("KEY"));
98  
99          content = "<properties><KEY>VALUE</KEY></properties>";
100         propertiesElm = builder.build(new StringReader(content)).getRootElement();
101         assertEquals("VALUE", new JDomProperties(propertiesElm).getProperty("KEY"));
102     }
103 
104     @Test(expected = UnsupportedOperationException.class)
105     public void testGetPropertyDefault() {
106         new JDomProperties(null).getProperty(null, null);
107     }
108 
109     @Test(expected = UnsupportedOperationException.class)
110     public void testPropertyNames() {
111         new JDomProperties(null).propertyNames();
112     }
113 
114     @Test(expected = UnsupportedOperationException.class)
115     public void testStringPropertyNames() {
116         new JDomProperties(null).stringPropertyNames();
117     }
118 
119     @Test(expected = UnsupportedOperationException.class)
120     public void testListPrintStream() {
121         new JDomProperties(null).list((PrintStream) null);
122     }
123 
124     @Test(expected = UnsupportedOperationException.class)
125     public void testListPrintWriter() {
126         new JDomProperties(null).list((PrintWriter) null);
127     }
128 
129     private String getProperty(Element propertiesElm, String key) {
130         return propertiesElm.getChildText(key, propertiesElm.getNamespace());
131     }
132 }