Skip to content

Commit 991b857

Browse files
committed
HV-1591 Test for deprecated use of Value annotation
A user must apply `@Value` to the argument(s) of a container type. If they don't, there should be message in the logs warning them.
1 parent e563467 commit 991b857

File tree

1 file changed

+207
-0
lines changed

1 file changed

+207
-0
lines changed
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.validator.test.constraints.annotations.hv;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Set;
12+
13+
import jakarta.validation.Valid;
14+
15+
import org.hibernate.validator.internal.metadata.aggregated.CascadingMetaDataBuilder;
16+
import org.hibernate.validator.test.constraints.annotations.AbstractConstrainedTest;
17+
import org.hibernate.validator.test.internal.engine.serialization.Email;
18+
import org.hibernate.validator.testutils.ListAppender;
19+
20+
import org.apache.logging.log4j.Level;
21+
import org.apache.logging.log4j.core.Logger;
22+
import org.apache.logging.log4j.core.LoggerContext;
23+
import org.apache.logging.log4j.core.config.Configurator;
24+
import org.testng.annotations.AfterTest;
25+
import org.testng.annotations.BeforeMethod;
26+
import org.testng.annotations.BeforeTest;
27+
import org.testng.annotations.Test;
28+
29+
public class ValidAnnotationTest extends AbstractConstrainedTest {
30+
31+
private ListAppender logAppender;
32+
private Level originalLogLevel;
33+
private Logger targetLogger;
34+
35+
/**
36+
* @return true if the string matches the expected log message for deprecated use of @Value.
37+
*/
38+
private static boolean deprecatedUsedOfValueCode(String s) {
39+
return s.startsWith( "HV000270" );
40+
}
41+
42+
@BeforeTest
43+
public void setUpLogger() {
44+
logAppender = new ListAppender( ValidAnnotationTest.class.getSimpleName() );
45+
logAppender.start();
46+
47+
LoggerContext context = LoggerContext.getContext( false );
48+
targetLogger = context.getLogger( CascadingMetaDataBuilder.class.getName() );
49+
targetLogger.addAppender( logAppender );
50+
51+
// Set level of log messages to WARN (if they are not already enabled)
52+
if ( targetLogger.getLevel().isMoreSpecificThan( Level.WARN ) ) {
53+
// Store the original log level to restore it later
54+
originalLogLevel = targetLogger.getLevel();
55+
56+
// Override the log level for this test class only
57+
// Default tests will only print the error messages, we need to override it
58+
// so that we can capture the deprecated warning message
59+
Configurator.setLevel( CascadingMetaDataBuilder.class.getName(), Level.WARN );
60+
context.updateLoggers();
61+
}
62+
}
63+
64+
@BeforeMethod
65+
public void cleanLogger() {
66+
logAppender.clear();
67+
}
68+
69+
@AfterTest
70+
public void tearDownLogger() {
71+
targetLogger.removeAppender( logAppender );
72+
logAppender.stop();
73+
74+
// Restore the original log level
75+
if ( originalLogLevel != null ) {
76+
Configurator.setLevel( CascadingMetaDataBuilder.class.getName(), originalLogLevel );
77+
// Update the logger context to apply changes
78+
LoggerContext context = LoggerContext.getContext( false );
79+
context.updateLoggers();
80+
}
81+
}
82+
83+
@Test
84+
public void twiceWithList() {
85+
class Foo {
86+
87+
@Valid
88+
private List<@Valid String> prop;
89+
90+
public Foo(List<String> prop) {
91+
this.prop = prop;
92+
}
93+
}
94+
95+
Foo foo = new Foo( List.of( "K1" ) );
96+
validator.validate( foo );
97+
98+
assertThat( logAppender.getMessages() ).hasSize( 1 ).allMatch( ValidAnnotationTest::deprecatedUsedOfValueCode );
99+
}
100+
101+
@Test
102+
public void onTheContainerWithList() {
103+
class Foo {
104+
105+
@Valid
106+
private List<MyBean> prop;
107+
108+
public Foo(List<MyBean> prop) {
109+
this.prop = prop;
110+
}
111+
}
112+
113+
Foo foo = new Foo( List.of( new MyBean( "[email protected]" ) ) );
114+
validator.validate( foo );
115+
116+
assertThat( logAppender.getMessages() ).hasSize( 1 ).allMatch( ValidAnnotationTest::deprecatedUsedOfValueCode );
117+
}
118+
119+
@Test
120+
public void twiceWithSet() {
121+
class Foo {
122+
123+
@Valid
124+
private Set<@Valid String> prop;
125+
126+
public Foo(Set<String> prop) {
127+
this.prop = prop;
128+
}
129+
}
130+
131+
Foo foo = new Foo( Set.of( "K1" ) );
132+
validator.validate( foo );
133+
134+
assertThat( logAppender.getMessages() ).hasSize( 1 ).allMatch( ValidAnnotationTest::deprecatedUsedOfValueCode );
135+
}
136+
137+
@Test
138+
public void onTheContainerWithSet() {
139+
class Foo {
140+
141+
@Valid
142+
private Set<MyBean> prop;
143+
144+
public Foo(Set<MyBean> prop) {
145+
this.prop = prop;
146+
}
147+
}
148+
149+
Foo foo = new Foo( Set.of( new MyBean( "[email protected]" ) ) );
150+
validator.validate( foo );
151+
152+
assertThat( logAppender.getMessages() ).hasSize( 1 ).allMatch( ValidAnnotationTest::deprecatedUsedOfValueCode );
153+
}
154+
155+
@Test
156+
public void twiceWithMap() {
157+
class Foo {
158+
159+
@Valid
160+
private Map<String, @Valid String> prop;
161+
162+
public Foo(Map<String, String> prop) {
163+
this.prop = prop;
164+
}
165+
}
166+
167+
Foo foo = new Foo( Map.of( "K1", "V1" ) );
168+
validator.validate( foo );
169+
170+
assertThat( logAppender.getMessages() ).hasSize( 1 ).allMatch( ValidAnnotationTest::deprecatedUsedOfValueCode );
171+
}
172+
173+
@Test
174+
public void onContainerWithMap() {
175+
class Foo {
176+
177+
@Valid
178+
private Map<String, MyBean> prop;
179+
180+
public Foo(Map<String, MyBean> prop) {
181+
this.prop = prop;
182+
}
183+
}
184+
185+
Foo foo = new Foo( Map.of( "K1", new MyBean( "[email protected]" ) ) );
186+
validator.validate( foo );
187+
188+
assertThat( logAppender.getMessages() ).hasSize( 1 ).allMatch( ValidAnnotationTest::deprecatedUsedOfValueCode );
189+
}
190+
191+
private static class MyBean {
192+
@Email
193+
private String email;
194+
195+
public MyBean(String email) {
196+
this.email = email;
197+
}
198+
199+
public String getEmail() {
200+
return email;
201+
}
202+
203+
public void setEmail(String email) {
204+
this.email = email;
205+
}
206+
}
207+
}

0 commit comments

Comments
 (0)