11/*
2- * Copyright 2006-2023 the original author or authors.
2+ * Copyright 2006-2024 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616
1717package org .springframework .retry .policy ;
1818
19+ import java .io .ByteArrayInputStream ;
20+ import java .io .IOException ;
21+ import java .io .ObjectInputStream ;
1922import java .util .ArrayList ;
2023import java .util .List ;
2124import java .util .Set ;
4245/**
4346 * @author Dave Syer
4447 * @author Gary Russell
48+ * @author Artem Bilan
4549 *
4650 */
4751public class RetryContextSerializationTests {
4852
4953 private static final Log logger = LogFactory .getLog (RetryContextSerializationTests .class );
5054
51- @ SuppressWarnings ("deprecation" )
5255 public static List <Object []> policies () {
5356 List <Object []> result = new ArrayList <>();
5457 ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider (true );
@@ -58,11 +61,11 @@ public static List<Object[]> policies() {
5861 Set <BeanDefinition > candidates = scanner .findCandidateComponents ("org.springframework.retry.policy" );
5962 for (BeanDefinition beanDefinition : candidates ) {
6063 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 )) });
6366 }
6467 catch (Exception e ) {
65- logger .warn ("Cannot create instance of " + beanDefinition .getBeanClassName (), e );
68+ logger .info ("Cannot create instance of " + beanDefinition .getBeanClassName (), e );
6669 }
6770 }
6871 ExceptionClassifierRetryPolicy extra = new ExceptionClassifierRetryPolicy ();
@@ -71,25 +74,35 @@ public static List<Object[]> policies() {
7174 return result ;
7275 }
7376
74- @ SuppressWarnings ("deprecation" )
7577 @ ParameterizedTest
7678 @ MethodSource ("policies" )
7779 public void testSerializationCycleForContext (RetryPolicy policy ) {
7880 RetryContext context = policy .open (null );
7981 assertThat (context .getRetryCount ()).isEqualTo (0 );
8082 policy .registerThrowable (context , new RuntimeException ());
8183 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 );
8585 }
8686
8787 @ ParameterizedTest
8888 @ MethodSource ("policies" )
89- @ SuppressWarnings ("deprecation" )
9089 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+ }
93106 }
94107
95108}
0 commit comments