Committed by
Gerrit Code Review
Refactor event dispatch injector to use TestUtils field setter
Change-Id: I793183de883c7b20784957eaa213d79a42951639
Showing
2 changed files
with
17 additions
and
6 deletions
... | @@ -15,6 +15,7 @@ | ... | @@ -15,6 +15,7 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.net; | 16 | package org.onosproject.net; |
17 | 17 | ||
18 | +import org.onlab.junit.TestUtils; | ||
18 | import org.onlab.packet.ChassisId; | 19 | import org.onlab.packet.ChassisId; |
19 | import org.onlab.packet.IpAddress; | 20 | import org.onlab.packet.IpAddress; |
20 | import org.onosproject.TestApplicationId; | 21 | import org.onosproject.TestApplicationId; |
... | @@ -126,11 +127,11 @@ public final class NetTestTools { | ... | @@ -126,11 +127,11 @@ public final class NetTestTools { |
126 | for (Field f : mc.getSuperclass().getDeclaredFields()) { | 127 | for (Field f : mc.getSuperclass().getDeclaredFields()) { |
127 | if (f.getType().equals(EventDeliveryService.class)) { | 128 | if (f.getType().equals(EventDeliveryService.class)) { |
128 | try { | 129 | try { |
129 | - f.setAccessible(true); | 130 | + TestUtils.setField(manager, f.getName(), svc); |
130 | - f.set(manager, svc); | 131 | + } catch (TestUtils.TestUtilsException e) { |
131 | - } catch (IllegalAccessException e) { | ||
132 | throw new IllegalArgumentException("Unable to inject reference", e); | 132 | throw new IllegalArgumentException("Unable to inject reference", e); |
133 | } | 133 | } |
134 | + break; | ||
134 | } | 135 | } |
135 | } | 136 | } |
136 | } | 137 | } | ... | ... |
... | @@ -40,13 +40,23 @@ public final class TestUtils { | ... | @@ -40,13 +40,23 @@ public final class TestUtils { |
40 | public static <T, U> void setField(T subject, String fieldName, U value) | 40 | public static <T, U> void setField(T subject, String fieldName, U value) |
41 | throws TestUtilsException { | 41 | throws TestUtilsException { |
42 | @SuppressWarnings("unchecked") | 42 | @SuppressWarnings("unchecked") |
43 | - Class<T> clazz = (Class<T>) subject.getClass(); | 43 | + Class clazz = subject.getClass(); |
44 | + try { | ||
45 | + while (clazz != null) { | ||
44 | try { | 46 | try { |
45 | Field field = clazz.getDeclaredField(fieldName); | 47 | Field field = clazz.getDeclaredField(fieldName); |
46 | field.setAccessible(true); | 48 | field.setAccessible(true); |
47 | field.set(subject, value); | 49 | field.set(subject, value); |
48 | - } catch (NoSuchFieldException | SecurityException | | 50 | + break; |
49 | - IllegalArgumentException | IllegalAccessException e) { | 51 | + } catch (NoSuchFieldException ex) { |
52 | + if (clazz == clazz.getSuperclass()) { | ||
53 | + break; | ||
54 | + } | ||
55 | + clazz = clazz.getSuperclass(); | ||
56 | + } | ||
57 | + } | ||
58 | + } catch (SecurityException | IllegalArgumentException | | ||
59 | + IllegalAccessException e) { | ||
50 | throw new TestUtilsException("setField failed", e); | 60 | throw new TestUtilsException("setField failed", e); |
51 | } | 61 | } |
52 | } | 62 | } | ... | ... |
-
Please register or login to post a comment