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;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import org.junit.jupiter.api.*;
22  
23  class ContextSession_Test extends TestBase {
24  
25  	//====================================================================================================
26  	// ContextSession.Builder.property() - Lines 142-149
27  	//====================================================================================================
28  
29  	@Test void a01_property_addProperty() {
30  		// Test line 147: adding a property with non-null value
31  		var session = BeanContext.DEFAULT.createSession()
32  			.property("key1", "value1")
33  			.property("key2", 123)
34  			.build();
35  		var props = session.getSessionProperties();
36  		assertEquals("value1", props.get("key1"));
37  		assertEquals(123, props.get("key2"));
38  	}
39  
40  	@Test void a02_property_removeProperty() {
41  		// Test line 145: removing a property by setting value to null
42  		var session = BeanContext.DEFAULT.createSession()
43  			.property("key1", "value1")
44  			.property("key2", "value2")
45  			.property("key1", null)  // Remove key1
46  			.build();
47  		var props = session.getSessionProperties();
48  		assertFalse(props.containsKey("key1"));
49  		assertEquals("value2", props.get("key2"));
50  	}
51  
52  	@Test void a03_property_nullKey() {
53  		// Test line 142: assertArgNotNull on key
54  		assertThrows(IllegalArgumentException.class, () -> {
55  			BeanContext.DEFAULT.createSession()
56  				.property(null, "value");
57  		});
58  	}
59  
60  	//====================================================================================================
61  	// ContextSession constructor - Line 185
62  	//====================================================================================================
63  
64  	@Test void b01_unmodifiableSession_emptyProperties() {
65  		// Test line 185: unmodifiable session with empty properties should use Collections.emptyMap()
66  		// Note: The actual implementation may wrap empty maps differently, so we test the behavior
67  		var session = BeanContext.DEFAULT.createSession()
68  			.unmodifiable()
69  			.build();
70  		var props = session.getSessionProperties();
71  		// Test line 185: if properties are empty, should use Collections.emptyMap() or equivalent
72  		// The key test is that it's unmodifiable and empty
73  		assertTrue(props.isEmpty());
74  		assertThrows(UnsupportedOperationException.class, () -> {
75  			props.put("key", "value");
76  		});
77  	}
78  
79  	@Test void b02_unmodifiableSession_withProperties() {
80  		// Test line 185: unmodifiable session with properties should use unmodifiable map
81  		var session = BeanContext.DEFAULT.createSession()
82  			.property("key1", "value1")
83  			.property("key2", "value2")
84  			.unmodifiable()
85  			.build();
86  		var props = session.getSessionProperties();
87  		assertEquals("value1", props.get("key1"));
88  		assertEquals("value2", props.get("key2"));
89  		// Verify it's unmodifiable
90  		assertThrows(UnsupportedOperationException.class, () -> {
91  			props.put("key3", "value3");
92  		});
93  	}
94  
95  	//====================================================================================================
96  	// ContextSession.addWarning() - Line 201
97  	//====================================================================================================
98  
99  	@Test void c01_addWarning_unmodifiableSession() {
100 		// Test line 201: addWarning should return early if session is unmodifiable
101 		var session = BeanContext.DEFAULT.createSession()
102 			.unmodifiable()
103 			.build();
104 		// Should not throw exception, just return early
105 		session.addWarning("Test warning");
106 		assertTrue(session.getWarnings().isEmpty());
107 	}
108 
109 	@Test void c02_addWarning_modifiableSession() {
110 		// Test that addWarning works on modifiable sessions
111 		var session = BeanContext.DEFAULT.createSession()
112 			.build();
113 		session.addWarning("Test warning");
114 		var warnings = session.getWarnings();
115 		assertFalse(warnings.isEmpty());
116 		assertTrue(warnings.get(0).contains("Test warning"));
117 	}
118 
119 	//====================================================================================================
120 	// ContextSession.getContext() - Line 220
121 	//====================================================================================================
122 
123 	@Test void d01_getContext() {
124 		// Test line 220: getContext() returns the context that created the session
125 		var context = BeanContext.DEFAULT;
126 		var session = context.createSession().build();
127 		assertSame(context, session.getContext());
128 	}
129 }
130