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