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.junit.bct;
18  
19  import static org.apache.juneau.junit.bct.BctUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import org.apache.juneau.*;
23  import org.junit.jupiter.api.*;
24  
25  /**
26   * Unit tests for {@link PropertyNotFoundException}.
27   */
28  class PropertyNotFoundException_Test extends TestBase {
29  
30  	@Nested
31  	class A_construction extends TestBase {
32  
33  		@Test
34  		void a01_messageConstructor() {
35  			String message = "Custom error message";
36  			var ex = new PropertyNotFoundException(message);
37  
38  			assertEquals(message, ex.getMessage());
39  			assertNull(ex.getCause());
40  		}
41  
42  		@Test
43  		void a02_messageAndCauseConstructor() {
44  			String message = "Custom error message";
45  			var cause = new RuntimeException("Root cause");
46  			var ex = new PropertyNotFoundException(message, cause);
47  
48  			assertEquals(message, ex.getMessage());
49  			assertEquals(cause, ex.getCause());
50  		}
51  
52  		@Test
53  		void a03_propertyAndTypeConstructor() {
54  			String propertyName = "invalidProperty";
55  			Class<?> objectType = String.class;
56  			var ex = new PropertyNotFoundException(propertyName, objectType);
57  
58  			assertEquals("Property 'invalidProperty' not found on object of type String", ex.getMessage());
59  			assertNull(ex.getCause());
60  		}
61  
62  		@Test
63  		void a04_propertyTypeAndCauseConstructor() {
64  			String propertyName = "missingField";
65  			Class<?> objectType = Integer.class;
66  			var cause = new RuntimeException("Field not found");
67  			var ex = new PropertyNotFoundException(propertyName, objectType, cause);
68  
69  			assertEquals("Property 'missingField' not found on object of type Integer", ex.getMessage());
70  			assertEquals(cause, ex.getCause());
71  		}
72  	}
73  
74  	@Nested
75  	class B_integration extends TestBase {
76  
77  		@Test
78  		void b01_thrownByPropertyExtractor() {
79  			BeanConverter converter = BasicBeanConverter.DEFAULT;
80  			var bean = new TestBean("test", 42);
81  
82  			// This should throw PropertyNotFoundException
83  			PropertyNotFoundException ex = assertThrows(PropertyNotFoundException.class, () -> {
84  				converter.getNested(bean, tokenize("nonExistentProperty").get(0));
85  			});
86  
87  			assertTrue(ex.getMessage().contains("nonExistentProperty"));
88  			assertTrue(ex.getMessage().contains("TestBean"));
89  		}
90  
91  		@SuppressWarnings("cast")
92  		@Test
93  		void b02_exceptionHierarchy() {
94  			var ex = new PropertyNotFoundException("test");
95  
96  			// Should be a RuntimeException
97  			assertTrue(ex instanceof RuntimeException);
98  			assertTrue(ex instanceof Exception);
99  			assertTrue(ex instanceof Throwable);
100 		}
101 	}
102 
103 	// Helper class for testing
104 	static class TestBean {
105 		final String name;
106 		final int value;
107 
108 		TestBean(String name, int value) {
109 			this.name = name;
110 			this.value = value;
111 		}
112 
113 		public String getName() { return name; }
114 		public int getValue() { return value; }
115 	}
116 }