View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.utils;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import java.util.*;
22  import java.util.jar.*;
23  
24  import org.apache.juneau.*;
25  import org.junit.jupiter.api.*;
26  
27  class ManifestFile_Test extends TestBase {
28  
29  	//-----------------------------------------------------------------------------------------------------------------
30  	// test - Basic tests
31  	//-----------------------------------------------------------------------------------------------------------------
32  	@Test void a01_basic() throws Exception {
33  		var manifest = new Manifest();
34  		manifest.getMainAttributes().put(java.util.jar.Attributes.Name.MANIFEST_VERSION, "1.0");
35  		manifest.getMainAttributes().put(new java.util.jar.Attributes.Name("Bundle-Name"), "Test Bundle");
36  		manifest.getMainAttributes().put(new java.util.jar.Attributes.Name("Bundle-Version"), "1.0.0");
37  
38  		var mf = new ManifestFile(manifest);
39  
40  		assertEquals("1.0", mf.get("Manifest-Version"));
41  		assertEquals("Test Bundle", mf.get("Bundle-Name"));
42  		assertEquals("1.0.0", mf.get("Bundle-Version"));
43  	}
44  
45  	//-----------------------------------------------------------------------------------------------------------------
46  	// test - Fluent setters
47  	//-----------------------------------------------------------------------------------------------------------------
48  	@Test void a02_fluentSetters() throws Exception {
49  		var manifest = new Manifest();
50  		manifest.getMainAttributes().put(java.util.jar.Attributes.Name.MANIFEST_VERSION, "1.0");
51  
52  		var mf = new ManifestFile(manifest);
53  
54  		// Test inner() returns same instance for fluent chaining
55  		var innerMap = new HashMap<String,Object>();
56  		innerMap.put("test", "value");
57  		assertSame(mf, mf.inner(innerMap));
58  
59  		// Test session() returns same instance
60  		BeanSession session = BeanContext.DEFAULT.getSession();
61  		assertSame(mf, mf.session(session));
62  
63  		// Test append(String, Object) returns same instance
64  		assertSame(mf, mf.append("Custom-Key", "custom-value"));
65  		assertEquals("custom-value", mf.get("Custom-Key"));
66  
67  		// Test append(Map) returns same instance
68  		var appendMap = new HashMap<String,Object>();
69  		appendMap.put("Another-Key", "another-value");
70  		assertSame(mf, mf.append(appendMap));
71  		assertEquals("another-value", mf.get("Another-Key"));
72  
73  		// Test appendIf() returns same instance
74  		assertSame(mf, mf.appendIf(true, "Conditional-Key", "conditional-value"));
75  		assertEquals("conditional-value", mf.get("Conditional-Key"));
76  		assertSame(mf, mf.appendIf(false, "Skipped-Key", "skipped-value"));
77  		assertNull(mf.get("Skipped-Key"));
78  
79  		// Test filtered() returns same instance
80  		assertSame(mf, mf.filtered(x -> x != null));
81  
82  		// Test keepAll() returns same instance
83  		assertSame(mf, mf.keepAll("Custom-Key", "Another-Key"));
84  
85  		// Test setBeanSession() returns same instance
86  		assertSame(mf, mf.setBeanSession(session));
87  
88  		// Test modifiable() returns same instance
89  		assertSame(mf, mf.modifiable());
90  
91  		// Test unmodifiable() returns same instance
92  		assertSame(mf, mf.unmodifiable());
93  	}
94  
95  	@Test void a03_fluentChaining() throws Exception {
96  		var manifest = new Manifest();
97  		manifest.getMainAttributes().put(java.util.jar.Attributes.Name.MANIFEST_VERSION, "1.0");
98  
99  		// Test multiple fluent calls can be chained
100 		var mf = new ManifestFile(manifest)
101 			.append("Key1", "value1")
102 			.append("Key2", "value2")
103 			.appendIf(true, "Key3", "value3");
104 
105 		assertEquals("value1", mf.get("Key1"));
106 		assertEquals("value2", mf.get("Key2"));
107 		assertEquals("value3", mf.get("Key3"));
108 	}
109 }