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.rest.arg;
18  
19  import static org.apache.juneau.commons.utils.CollectionUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.util.*;
23  
24  import org.apache.http.*;
25  import org.apache.juneau.*;
26  import org.apache.juneau.commons.reflect.*;
27  import org.apache.juneau.http.*;
28  import org.apache.juneau.http.header.*;
29  import org.junit.jupiter.api.*;
30  
31  class ArgException_Test extends TestBase {
32  
33  	private static ParameterInfo testParameterInfo;
34  
35  	@BeforeAll
36  	public static void setup() throws Exception {
37  		// Create a test ParameterInfo for a sample method parameter
38  		var mi = MethodInfo.of(ArgException_Test.class.getMethod("sampleMethod", String.class));
39  		testParameterInfo = mi.getParameter(0);
40  	}
41  
42  	public static void sampleMethod(String param) {
43  		// Sample method for creating ParameterInfo
44  	}
45  
46  	@Test void a01_basic() {
47  		var x = new ArgException(testParameterInfo, "Test message");
48  		assertNotNull(x);
49  		assertTrue(x.getMessage().contains("Test message"));
50  		assertTrue(x.getMessage().contains("parameter 0"));
51  	}
52  
53  	@Test void a02_withArgs() {
54  		var x = new ArgException(testParameterInfo, "Test {0} {1}", "foo", "bar");
55  		assertTrue(x.getMessage().contains("Test foo bar"));
56  		assertTrue(x.getMessage().contains("parameter 0"));
57  	}
58  
59  	@Test void a03_fluentSetters() {
60  		var x = new ArgException(testParameterInfo, "Test");
61  
62  		// Test setMessage returns same instance for fluent chaining
63  		assertSame(x, x.setMessage("New message"));
64  		assertTrue(x.getMessage().contains("New message"));
65  
66  		// Test setHeader2 returns same instance
67  		assertSame(x, x.setHeader2("X-Test", "test-value"));
68  
69  		// Test setHeaders(HeaderList) returns same instance
70  		var headers = HeaderList.of(BasicHeader.of("X-Header1", "value1"));
71  		assertSame(x, x.setHeaders(headers));
72  
73  		// Test setHeaders2(Header...) returns same instance
74  		assertSame(x, x.setHeaders2(BasicHeader.of("X-Header2", "value2")));
75  
76  		// Test setLocale2 returns same instance
77  		assertSame(x, x.setLocale2(Locale.US));
78  
79  		// Test setProtocolVersion returns same instance
80  		assertSame(x, x.setProtocolVersion(new ProtocolVersion("HTTP", 1, 1)));
81  
82  		// Test setReasonPhrase2 returns same instance
83  		assertSame(x, x.setReasonPhrase2("Custom Reason"));
84  
85  		// Test setReasonPhraseCatalog returns same instance
86  		assertSame(x, x.setReasonPhraseCatalog(null));
87  
88  		// Test setStatusLine returns same instance
89  		assertSame(x, x.setStatusLine(BasicStatusLine.create(500, "Test")));
90  
91  		// Test setHeaders(List<Header>) returns same instance
92  		List<Header> headerList = l(BasicHeader.of("X-Header3", "value3"));
93  		assertSame(x, x.setHeaders(headerList));
94  		assertEquals("value3", x.getFirstHeader("X-Header3").getValue());
95  
96  		// Test setContent(String) returns same instance
97  		assertSame(x, x.setContent("test content"));
98  
99  		// Test setContent(HttpEntity) returns same instance
100 		HttpEntity entity = x.getEntity();
101 		assertSame(x, x.setContent(entity));
102 
103 		// Test setUnmodifiable returns same instance (must be last - makes bean read-only)
104 		assertSame(x, x.setUnmodifiable());
105 	}
106 
107 	@Test void a04_fluentChaining() {
108 		// Test multiple fluent calls can be chained
109 		var x = new ArgException(testParameterInfo, "Initial")
110 			.setHeaders(l(BasicHeader.of("X-Chain", "chained")))
111 			.setContent("Chained content");
112 
113 		assertEquals("chained", x.getFirstHeader("X-Chain").getValue());
114 	}
115 
116 	@Test void a05_copy() {
117 		// Test that copy() returns correct type
118 		var x = new ArgException(testParameterInfo, "Original message");
119 
120 		ArgException copy = x.copy();
121 
122 		// Verify it's a different instance
123 		assertNotSame(x, copy);
124 
125 		// Verify it returns the correct type (not InternalServerError)
126 		assertInstanceOf(ArgException.class, copy);
127 
128 		// Verify message is copied
129 		assertTrue(copy.getMessage().contains("Original message"));
130 	}
131 }