1
1
/*
2
- * Copyright 2006-2023 the original author or authors.
2
+ * Copyright 2006-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .retry .policy ;
18
18
19
+ import java .io .ByteArrayInputStream ;
20
+ import java .io .IOException ;
21
+ import java .io .ObjectInputStream ;
19
22
import java .util .ArrayList ;
20
23
import java .util .List ;
21
24
import java .util .Set ;
42
45
/**
43
46
* @author Dave Syer
44
47
* @author Gary Russell
48
+ * @author Artem Bilan
45
49
*
46
50
*/
47
51
public class RetryContextSerializationTests {
48
52
49
53
private static final Log logger = LogFactory .getLog (RetryContextSerializationTests .class );
50
54
51
- @ SuppressWarnings ("deprecation" )
52
55
public static List <Object []> policies () {
53
56
List <Object []> result = new ArrayList <>();
54
57
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider (true );
@@ -58,11 +61,11 @@ public static List<Object[]> policies() {
58
61
Set <BeanDefinition > candidates = scanner .findCandidateComponents ("org.springframework.retry.policy" );
59
62
for (BeanDefinition beanDefinition : candidates ) {
60
63
try {
61
- result .add (new Object [] {
62
- BeanUtils . instantiate (ClassUtils .resolveClassName (beanDefinition .getBeanClassName (), null )) });
64
+ result .add (new Object [] { BeanUtils
65
+ . instantiateClass (ClassUtils .resolveClassName (beanDefinition .getBeanClassName (), null )) });
63
66
}
64
67
catch (Exception e ) {
65
- logger .warn ("Cannot create instance of " + beanDefinition .getBeanClassName (), e );
68
+ logger .info ("Cannot create instance of " + beanDefinition .getBeanClassName (), e );
66
69
}
67
70
}
68
71
ExceptionClassifierRetryPolicy extra = new ExceptionClassifierRetryPolicy ();
@@ -71,25 +74,35 @@ public static List<Object[]> policies() {
71
74
return result ;
72
75
}
73
76
74
- @ SuppressWarnings ("deprecation" )
75
77
@ ParameterizedTest
76
78
@ MethodSource ("policies" )
77
79
public void testSerializationCycleForContext (RetryPolicy policy ) {
78
80
RetryContext context = policy .open (null );
79
81
assertThat (context .getRetryCount ()).isEqualTo (0 );
80
82
policy .registerThrowable (context , new RuntimeException ());
81
83
assertThat (context .getRetryCount ()).isEqualTo (1 );
82
- assertThat (
83
- ((RetryContext ) SerializationUtils .deserialize (SerializationUtils .serialize (context ))).getRetryCount ())
84
- .isEqualTo (1 );
84
+ assertThat (deserialize (SerializationUtils .serialize (context ))).extracting ("retryCount" ).isEqualTo (1 );
85
85
}
86
86
87
87
@ ParameterizedTest
88
88
@ MethodSource ("policies" )
89
- @ SuppressWarnings ("deprecation" )
90
89
public void testSerializationCycleForPolicy (RetryPolicy policy ) {
91
- assertThat (SerializationUtils .deserialize (SerializationUtils .serialize (policy )) instanceof RetryPolicy )
92
- .isTrue ();
90
+ assertThat (deserialize (SerializationUtils .serialize (policy )) instanceof RetryPolicy ).isTrue ();
91
+ }
92
+
93
+ private static Object deserialize (byte [] bytes ) {
94
+ if (bytes == null ) {
95
+ return null ;
96
+ }
97
+ try (ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (bytes ))) {
98
+ return ois .readObject ();
99
+ }
100
+ catch (IOException ex ) {
101
+ throw new IllegalArgumentException ("Failed to deserialize object" , ex );
102
+ }
103
+ catch (ClassNotFoundException ex ) {
104
+ throw new IllegalStateException ("Failed to deserialize object type" , ex );
105
+ }
93
106
}
94
107
95
108
}
0 commit comments