Skip to content

Commit 70f7daf

Browse files
committed
Remove unused Main class and add comprehensive test coverage
- Delete obsolete Main.java - Add unit tests for utility classes, resource loading, logging, and property builder
1 parent b5bffa5 commit 70f7daf

12 files changed

Lines changed: 982 additions & 55 deletions

propify/src/main/java/com/vgerbot/propify/Main.java

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.vgerbot.propify.common;
2+
3+
import org.junit.Test;
4+
import java.lang.reflect.Constructor;
5+
import java.lang.reflect.InvocationTargetException;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertTrue;
9+
import static org.junit.Assert.fail;
10+
11+
/**
12+
* Test for the ReflectionUtils constructor to ensure it cannot be instantiated.
13+
*/
14+
public class ReflectionUtils_ConstructorTest {
15+
16+
/**
17+
* Test that the ReflectionUtils constructor throws a PropifyException when attempting to instantiate it.
18+
* This verifies that the utility class is properly designed to prevent instantiation.
19+
*/
20+
@Test
21+
public void testConstructor_ThrowsPropifyException() {
22+
try {
23+
// Get the private constructor
24+
Constructor<ReflectionUtils> constructor = ReflectionUtils.class.getDeclaredConstructor();
25+
// Make it accessible
26+
constructor.setAccessible(true);
27+
// Try to create a new instance
28+
constructor.newInstance();
29+
// If we get here, the test should fail
30+
fail("Expected PropifyException was not thrown");
31+
} catch (InvocationTargetException e) {
32+
// The constructor should throw a PropifyException
33+
Throwable cause = e.getCause();
34+
assertTrue("Expected PropifyException but got: " + cause.getClass().getName(),
35+
cause instanceof PropifyException);
36+
assertEquals("Cannot instantiate ReflectionUtils class", cause.getMessage());
37+
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException e) {
38+
fail("Unexpected exception: " + e.getMessage());
39+
}
40+
}
41+
}
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
package com.vgerbot.propify.common;
2+
3+
import org.junit.Test;
4+
5+
import java.lang.reflect.AccessibleObject;
6+
import java.lang.reflect.Constructor;
7+
import java.lang.reflect.Field;
8+
import java.lang.reflect.Method;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Tests for the utility methods in ReflectionUtils class.
14+
*/
15+
public class ReflectionUtils_MethodsTest {
16+
17+
// Test class with various members for testing reflection
18+
private static class TestClass {
19+
private String privateField;
20+
public String publicField;
21+
22+
private TestClass() {}
23+
24+
public TestClass(String value) {
25+
this.privateField = value;
26+
}
27+
28+
private void privateMethod() {}
29+
30+
public void publicMethod() {}
31+
32+
private String privateMethodWithParams(String param1, int param2) {
33+
return param1 + param2;
34+
}
35+
}
36+
37+
/**
38+
* Test for makeAccessible method with an already accessible object.
39+
*/
40+
@Test
41+
public void testMakeAccessible_AlreadyAccessible() {
42+
// Create an accessible object
43+
AccessibleObject object = TestClass.class.getFields()[0]; // publicField
44+
object.setAccessible(true);
45+
46+
// Call makeAccessible
47+
ReflectionUtils.makeAccessible(object);
48+
49+
// Verify it's still accessible
50+
assertTrue(object.isAccessible());
51+
}
52+
53+
/**
54+
* Test for makeAccessible method with an inaccessible object.
55+
*/
56+
@Test
57+
public void testMakeAccessible_Inaccessible() {
58+
// Create an inaccessible object
59+
AccessibleObject object = null;
60+
try {
61+
object = TestClass.class.getDeclaredField("privateField");
62+
object.setAccessible(false);
63+
} catch (NoSuchFieldException e) {
64+
fail("Test setup failed: " + e.getMessage());
65+
}
66+
67+
// Call makeAccessible
68+
ReflectionUtils.makeAccessible(object);
69+
70+
// Verify it's now accessible
71+
assertTrue(object.isAccessible());
72+
}
73+
74+
/**
75+
* Test for getDeclaredConstructor method with a public constructor.
76+
*/
77+
@Test
78+
public void testGetDeclaredConstructor_PublicConstructor() {
79+
// Get public constructor
80+
Constructor<TestClass> constructor = ReflectionUtils.getDeclaredConstructor(TestClass.class, String.class);
81+
82+
// Verify it's the correct constructor
83+
assertNotNull(constructor);
84+
assertEquals(1, constructor.getParameterCount());
85+
assertEquals(String.class, constructor.getParameterTypes()[0]);
86+
assertTrue(constructor.isAccessible());
87+
}
88+
89+
/**
90+
* Test for getDeclaredConstructor method with a private constructor.
91+
*/
92+
@Test
93+
public void testGetDeclaredConstructor_PrivateConstructor() {
94+
// Get private constructor
95+
Constructor<TestClass> constructor = ReflectionUtils.getDeclaredConstructor(TestClass.class);
96+
97+
// Verify it's the correct constructor
98+
assertNotNull(constructor);
99+
assertEquals(0, constructor.getParameterCount());
100+
assertTrue(constructor.isAccessible());
101+
}
102+
103+
/**
104+
* Test for getDeclaredConstructor method when constructor doesn't exist.
105+
*/
106+
@Test
107+
public void testGetDeclaredConstructor_NonExistentConstructor() {
108+
try {
109+
// Try to get a constructor that doesn't exist
110+
ReflectionUtils.getDeclaredConstructor(TestClass.class, Integer.class, Boolean.class);
111+
fail("Expected PropifyException was not thrown");
112+
} catch (PropifyException e) {
113+
// Verify the exception message
114+
assertTrue(e.getMessage().contains("Failed to get constructor"));
115+
assertTrue(e.getCause() instanceof NoSuchMethodException);
116+
}
117+
}
118+
119+
/**
120+
* Test for getDeclaredField method with a public field.
121+
*/
122+
@Test
123+
public void testGetDeclaredField_PublicField() {
124+
// Get public field
125+
Field field = ReflectionUtils.getDeclaredField(TestClass.class, "publicField");
126+
127+
// Verify it's the correct field
128+
assertNotNull(field);
129+
assertEquals("publicField", field.getName());
130+
assertTrue(field.isAccessible());
131+
}
132+
133+
/**
134+
* Test for getDeclaredField method with a private field.
135+
*/
136+
@Test
137+
public void testGetDeclaredField_PrivateField() {
138+
// Get private field
139+
Field field = ReflectionUtils.getDeclaredField(TestClass.class, "privateField");
140+
141+
// Verify it's the correct field
142+
assertNotNull(field);
143+
assertEquals("privateField", field.getName());
144+
assertTrue(field.isAccessible());
145+
}
146+
147+
/**
148+
* Test for getDeclaredField method when field doesn't exist.
149+
*/
150+
@Test
151+
public void testGetDeclaredField_NonExistentField() {
152+
try {
153+
// Try to get a field that doesn't exist
154+
ReflectionUtils.getDeclaredField(TestClass.class, "nonExistentField");
155+
fail("Expected PropifyException was not thrown");
156+
} catch (PropifyException e) {
157+
// Verify the exception message
158+
assertTrue(e.getMessage().contains("Failed to get field"));
159+
assertTrue(e.getCause() instanceof NoSuchFieldException);
160+
}
161+
}
162+
163+
/**
164+
* Test for getDeclaredMethod method with a public method.
165+
*/
166+
@Test
167+
public void testGetDeclaredMethod_PublicMethod() {
168+
// Get public method
169+
Method method = ReflectionUtils.getDeclaredMethod(TestClass.class, "publicMethod");
170+
171+
// Verify it's the correct method
172+
assertNotNull(method);
173+
assertEquals("publicMethod", method.getName());
174+
assertEquals(0, method.getParameterCount());
175+
assertTrue(method.isAccessible());
176+
}
177+
178+
/**
179+
* Test for getDeclaredMethod method with a private method.
180+
*/
181+
@Test
182+
public void testGetDeclaredMethod_PrivateMethod() {
183+
// Get private method
184+
Method method = ReflectionUtils.getDeclaredMethod(TestClass.class, "privateMethod");
185+
186+
// Verify it's the correct method
187+
assertNotNull(method);
188+
assertEquals("privateMethod", method.getName());
189+
assertEquals(0, method.getParameterCount());
190+
assertTrue(method.isAccessible());
191+
}
192+
193+
/**
194+
* Test for getDeclaredMethod method with a method that has parameters.
195+
*/
196+
@Test
197+
public void testGetDeclaredMethod_MethodWithParameters() {
198+
// Get method with parameters
199+
Method method = ReflectionUtils.getDeclaredMethod(TestClass.class, "privateMethodWithParams",
200+
String.class, int.class);
201+
202+
// Verify it's the correct method
203+
assertNotNull(method);
204+
assertEquals("privateMethodWithParams", method.getName());
205+
assertEquals(2, method.getParameterCount());
206+
assertEquals(String.class, method.getParameterTypes()[0]);
207+
assertEquals(int.class, method.getParameterTypes()[1]);
208+
assertTrue(method.isAccessible());
209+
}
210+
211+
/**
212+
* Test for getDeclaredMethod method when method doesn't exist.
213+
*/
214+
@Test
215+
public void testGetDeclaredMethod_NonExistentMethod() {
216+
try {
217+
// Try to get a method that doesn't exist
218+
ReflectionUtils.getDeclaredMethod(TestClass.class, "nonExistentMethod");
219+
fail("Expected PropifyException was not thrown");
220+
} catch (PropifyException e) {
221+
// Verify the exception message
222+
assertTrue(e.getMessage().contains("Failed to get method"));
223+
assertTrue(e.getCause() instanceof NoSuchMethodException);
224+
}
225+
}
226+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.vgerbot.propify.common;
2+
3+
import org.junit.Test;
4+
import java.lang.reflect.Constructor;
5+
import java.lang.reflect.InvocationTargetException;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertTrue;
9+
import static org.junit.Assert.fail;
10+
11+
/**
12+
* Test for the Utils constructor to ensure it cannot be instantiated.
13+
*/
14+
public class Utils_ConstructorTest {
15+
16+
/**
17+
* Test that the Utils constructor throws a PropifyException when attempting to instantiate it.
18+
* This verifies that the utility class is properly designed to prevent instantiation.
19+
*/
20+
@Test
21+
public void testConstructor_ThrowsPropifyException() {
22+
try {
23+
// Get the private constructor
24+
Constructor<Utils> constructor = Utils.class.getDeclaredConstructor();
25+
// Make it accessible
26+
constructor.setAccessible(true);
27+
// Try to create a new instance
28+
constructor.newInstance();
29+
// If we get here, the test should fail
30+
fail("Expected PropifyException was not thrown");
31+
} catch (InvocationTargetException e) {
32+
// The constructor should throw a PropifyException
33+
Throwable cause = e.getCause();
34+
assertTrue("Expected PropifyException but got: " + cause.getClass().getName(),
35+
cause instanceof PropifyException);
36+
assertEquals("Cannot instantiate Utils class", cause.getMessage());
37+
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException e) {
38+
fail("Unexpected exception: " + e.getMessage());
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)