Skip to content

Commit

Permalink
Fix observe values (STAMP-project#422)
Browse files Browse the repository at this point in the history
* fix: add checks on serializable

* test: update tests for check serializable

* update script: remove old .class
  • Loading branch information
danglotb authored May 10, 2018
1 parent 4eb1c13 commit a04014f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
3 changes: 0 additions & 3 deletions dspot/src/main/bash/update-classes-in-resources.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@
# this is done to have matched version with sources and compiled resources

rm -rf src/main/resources/compare/
rm -rf src/main/resources/listener/
mkdir --parent src/main/resources/listener/
cp -r target/classes/fr/inria/diversify/compare/ src/main/resources/compare/
cp target/classes/fr/inria/stamp/test/listener/TestListener.class src/main/resources/listener/TestListener.class
24 changes: 18 additions & 6 deletions dspot/src/main/java/fr/inria/diversify/compare/ObjectLog.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package fr.inria.diversify.compare;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -65,10 +67,11 @@ private void _log(Object startingObject,
int deep,
List<Method> methodsToReachCurrentObject) {
if (deep < maxDeep) {
if (objectToObserve == null ||
Utils.isPrimitive(objectToObserve) ||
Utils.isPrimitiveArray(objectToObserve) ||
Utils.isPrimitiveCollectionOrMap(objectToObserve)) {
if (isSerializable(objectToObserve) &&
(objectToObserve == null ||
Utils.isPrimitive(objectToObserve) ||
Utils.isPrimitiveArray(objectToObserve) ||
Utils.isPrimitiveCollectionOrMap(objectToObserve))) {
addObservation(id, observedObjectAsString, objectToObserve);
} else if (!objectToObserve.getClass().getName().toLowerCase().contains("mock")) {
observeNotNullObject(
Expand All @@ -83,6 +86,15 @@ private void _log(Object startingObject,
}
}

private boolean isSerializable(Object candidate) {
try {
new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(candidate);
return true;
} catch (IOException e) {
return false;
}
}

private void addObservation(String id, String observedObjectAsString, Object actualValue) {
if (!observations.containsKey(id)) {
observations.put(id, new Observation());
Expand Down Expand Up @@ -192,8 +204,8 @@ public static Map<String, Observation> load() {
} catch (Exception e) {
throw new RuntimeException(e);
}
} catch (Exception e){
throw new RuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

Expand Down
Binary file modified dspot/src/main/resources/compare/ObjectLog.class
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

Expand All @@ -23,6 +25,14 @@ public void setUp() throws Exception {
}

private static class MyInternalClass {
private static final ArrayList<Integer> list = new ArrayList<>();
static {
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
}
private int a;
private int b;
private static Random random = new Random();
Expand All @@ -32,6 +42,10 @@ public MyInternalClass(int a, int b) {
this.b = b;
}

public List<Integer> getObject() {
return list.subList(0, 2);
}

public int getA() {
return a;
}
Expand Down Expand Up @@ -66,7 +80,7 @@ public void test() throws Exception {
assertNotNull(add__0);
assertEquals(1, add__0.getNotDeterministValues().size());
final Map<String, Object> observationValues = add__0.getObservationValues();
assertEquals(4, observationValues.size());
assertEquals(7, observationValues.size());
assertEquals(25, observationValues.get("((fr.inria.diversify.compare.ObjectLogTest.MyInternalClass)myInternalClass ).compute()"));
assertEquals(3, observationValues.get("((fr.inria.diversify.compare.ObjectLogTest.MyInternalClass)myInternalClass ).getA()"));
assertEquals(20, observationValues.get("((fr.inria.diversify.compare.ObjectLogTest.MyInternalClass)myInternalClass ).getB()"));
Expand Down

0 comments on commit a04014f

Please sign in to comment.