|
|
@@ -6,14 +6,21 @@
|
|
|
*/
|
|
|
package net.ranides.assira.reflection.impl;
|
|
|
|
|
|
-import java.lang.invoke.WrongMethodTypeException;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
+import org.hamcrest.CoreMatchers;
|
|
|
+import org.junit.Assume;
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
import net.ranides.assira.generic.TypeToken;
|
|
|
import net.ranides.assira.generic.Wrapper;
|
|
|
import net.ranides.assira.reflection.*;
|
|
|
import net.ranides.assira.reflection.mockup.ForFields;
|
|
|
-import org.junit.Test;
|
|
|
+
|
|
|
+import java.lang.invoke.WrongMethodTypeException;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.ExecutionException;
|
|
|
+import java.util.concurrent.Executors;
|
|
|
+
|
|
|
import static net.ranides.assira.junit.NewAssert.*;
|
|
|
|
|
|
/**
|
|
|
@@ -132,7 +139,43 @@ public class RFieldTest {
|
|
|
});
|
|
|
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testRewritable() {
|
|
|
+ IField field = cr.field("frozen").get().rewritable();
|
|
|
+
|
|
|
+ ForFields.Record record = new ForFields.Record();
|
|
|
+ Wrapper<Object> wrapper = field.bind(record);
|
|
|
+
|
|
|
+ assertEquals(11, field.get(record));
|
|
|
+ assertEquals(11, wrapper.get());
|
|
|
+ assertEquals(11, record.frozen);
|
|
|
+
|
|
|
+ field.set(record, 99);
|
|
|
+
|
|
|
+ // reflective call will see changes
|
|
|
+ assertEquals(99, field.get(record));
|
|
|
+ assertEquals(99, wrapper.get());
|
|
|
+
|
|
|
+ // direct access can return updated value but it is not guaranteed
|
|
|
+ Assume.assumeThat(record.frozen, CoreMatchers.equalTo(99));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testRewritableThread() throws InterruptedException, ExecutionException {
|
|
|
+ IField field = cr.field("frozen").get().rewritable();
|
|
|
+
|
|
|
+ ForFields.Record record = Executors.newSingleThreadExecutor().submit(() -> {
|
|
|
+ ForFields.Record temp = new ForFields.Record();
|
|
|
+ field.set(temp, 77);
|
|
|
+ return temp;
|
|
|
+ }).get();
|
|
|
+
|
|
|
+ // it is more or less guaranteed that change is visible because
|
|
|
+ // object was created in another thread and field was modified before any read operation
|
|
|
+ assertEquals(77, record.frozen);
|
|
|
+ }
|
|
|
+
|
|
|
private static void assertEquiList(List<String> expected, List<String> values) {
|
|
|
while(values.remove("$jacocoData")) { }
|
|
|
while(values.remove("Record $jacocoData")) { }
|