Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cadc-util/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ sourceCompatibility = 1.8

group = 'org.opencadc'

version = '1.12.15'
version = '1.12.16'

description = 'OpenCADC core utility library'
def git_url = 'https://github.com/opencadc/core'
Expand Down
164 changes: 99 additions & 65 deletions cadc-util/src/main/java/org/opencadc/persist/Entity.java

Large diffs are not rendered by default.

26 changes: 25 additions & 1 deletion cadc-util/src/main/java/org/opencadc/persist/EntityVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,32 @@ public interface EntityVisitor {
public void visitNode(String vodmlID, Object val);

/**
* Visit a null value. This could be a node or a primitive.
* Visit a null value. This could be a node or a leaf.
* @param vodmlID the {declaringClass}.{fieldName} aka vodml-id
*/
public void visitNull(String vodmlID);

/**
* Visit a collection of child entity(s).
*
* @param vodmlID the {declaringClass}.{fieldName} aka vodml-id
* @param val the collection
*/
public void visitChildCollection(String vodmlID, Collection val);

/**
* Visit a null child entity value.
*
* @param vodmlID the {declaringClass}.{fieldName} aka vodml-id
*/
public void visitChildNull(String vodmlID);

/**
* Visit a direct child entity. There is no recursion into the structure of
* the child entity.
*
* @param vodmlID the {declaringClass}.{fieldName} aka vodml-id
* @param val the entity
*/
public void visitChildEntity(String vodmlID, Entity val);
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,12 @@
* @author pdowler
*/
public interface PrimitiveWrapper {
public Object getValue();
/**
* Unwrap the inner state and return it. The return value can be a primitive,
* immutable class (String, URI, Double, Long, etc), a primitive array (e.g. double[]),
* or a java.util.Collection (possibly empty).
*
* @return the wrapped value
*/
public Object getWrappedValue();
}
10 changes: 10 additions & 0 deletions cadc-util/src/test/java/ca/nrc/cadc/date/DateUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,16 @@ public void testFromModifiedJulianDateToISO8601Date()
Assert.fail("unexpected exception: " + unexpected);
throw unexpected;
}

DateFormat df = DateUtil.getDateFormat(DateUtil.IVOA_DATE_FORMAT, DateUtil.UTC);
String str = "2026-03-08T02:02:41.056";
Date orig = df.parse(str);
double mjd = DateUtil.toModifiedJulianDate(orig);
Date actual = DateUtil.fromModifiedJulianDate(mjd);
log.info(str + " -> " + df.format(orig) + " -> " + mjd + " -> " + df.format(actual));
Assert.assertEquals(orig, actual);


}

@Test
Expand Down
55 changes: 33 additions & 22 deletions cadc-util/src/test/java/org/opencadc/persist/EntityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class EntityTest {

static {
Log4jInit.setLevel("org.opencadc.persist", Level.DEBUG);
// this actually controls the large amoutn of debug output from checksum
// this actually controls the large amount of debug output from checksum
// algorithm, but it effects the whole jvm so only enable when running
// these tests specificially and looking at output
//Entity.MCS_DEBUG = true;
Expand All @@ -109,41 +109,41 @@ public void testTemplate() {
@Test
public void testEntity() {
// base: the cadc-inventory-0.x configuration
doEntityTest(false, false, false);
doNewVersionTest(false, false, false);
doEntityTest(false, false, false, false);
doNewVersionTest(false, false, false, false);
}

@Test
public void testEntityTruncateDates() {
// the caom2-2.4 configuration
doEntityTest(true, false, false);
doNewVersionTest(true, false, false);
doEntityTest(true, false, false, false);
doNewVersionTest(true, false, false, false);
}

@Test
public void testEntityDigestFieldNames() {
// the cadc-vos-2.x configuration
doEntityTest(false, true, false);
doNewVersionTest(false, true, false);
doEntityTest(false, true, false, false);
doNewVersionTest(false, true, false, false);
}

@Test
public void testEntityDigestFieldNamesLower() {
// the cadc-vos-2.x configuration
doEntityTest(false, true, true);
doNewVersionTest(false, true, true);
// the caom-2.5 configuration
doEntityTest(false, true, true, false);
doNewVersionTest(false, true, true, false);
}

@Test
public void testEntitySafeMode() {
// no known use, but truncateDates and digestFieldNames is the safest mode
doEntityTest(true, true, true);
doNewVersionTest(true, true, true);
doEntityTest(true, true, true, true);
doNewVersionTest(true, true, true, false);
}

private void doEntityTest(boolean trunc, boolean dig, boolean digL) {
private void doEntityTest(boolean trunc, boolean dig, boolean digL, boolean digZB) {
try {
SampleEntity sample = new SampleEntity("name-of-this-entity", trunc, dig, digL);
SampleEntity sample = new SampleEntity("name-of-this-entity", trunc, dig, digL, digZB);
log.info("created: " + sample);

URI mcs1 = sample.computeMetaChecksum(MessageDigest.getInstance("MD5"));
Expand Down Expand Up @@ -173,12 +173,23 @@ private void doEntityTest(boolean trunc, boolean dig, boolean digL) {
Assert.assertNotEquals(mcs6, mcs7);

// set of string
sample.strList.add("foo");
sample.strList.add("abc");
URI mcs8 = sample.computeMetaChecksum(MessageDigest.getInstance("MD5"));
Assert.assertNotEquals(mcs7, mcs8);
sample.strList.add("bar");
sample.strList.add("def");
URI mcs9 = sample.computeMetaChecksum(MessageDigest.getInstance("MD5"));
Assert.assertNotEquals(mcs8, mcs9);
// list-item subtleness
sample.strList.clear();
sample.strList.add("abcd");
sample.strList.add("ef");
log.warn("checking digZB...");
URI mcs9b = sample.computeMetaChecksum(MessageDigest.getInstance("MD5"));
if (digZB) {
Assert.assertNotEquals(mcs9, mcs9b);
} else {
Assert.assertEquals(mcs9, mcs9b);
}
// revert to 7
sample.strList.clear();
URI mcs10 = sample.computeMetaChecksum(MessageDigest.getInstance("MD5"));
Expand All @@ -193,11 +204,11 @@ private void doEntityTest(boolean trunc, boolean dig, boolean digL) {
Assert.assertNotEquals(mcs7, mcs12);

// entities do not get included in metaChecksum
sample.children.add(new SampleEntity("flibble", trunc, dig, digL));
sample.children.add(new SampleEntity("flibble", trunc, dig, digL, digZB));
URI tcs1 = sample.computeMetaChecksum(MessageDigest.getInstance("MD5"));
Assert.assertEquals(mcs12, tcs1);

sample.relation = new SampleEntity("flibble", trunc, dig, digL);
sample.child1 = new SampleEntity("flibble", trunc, dig, digL, digZB);
mcs11 = sample.computeMetaChecksum(MessageDigest.getInstance("MD5"));
Assert.assertEquals(mcs12, mcs11);

Expand All @@ -219,13 +230,13 @@ private void doEntityTest(boolean trunc, boolean dig, boolean digL) {
}

// also doubles as a sub-class/extension test
private void doNewVersionTest(boolean trunc, boolean dig, boolean digL) {
private void doNewVersionTest(boolean trunc, boolean dig, boolean digL, boolean digZB) {
try {
SampleEntity v1 = new SampleEntity("name-of-this-entity", trunc, dig, digL);
SampleEntity v1 = new SampleEntity("name-of-this-entity", trunc, dig, digL, digZB);
log.info("created: " + v1);
URI mcs1 = v1.computeMetaChecksum(MessageDigest.getInstance("MD5"));

SampleEntityV2 v2 = new SampleEntityV2(v1.getID(), v1.getName(), trunc, dig, digL);
SampleEntityV2 v2 = new SampleEntityV2(v1.getID(), v1.getName(), trunc, dig, digL, digZB);
log.info("created: " + v1);
URI mcs2 = v2.computeMetaChecksum(MessageDigest.getInstance("MD5"));

Expand All @@ -239,7 +250,7 @@ private void doNewVersionTest(boolean trunc, boolean dig, boolean digL) {
@Test
public void testNonState() {
try {
SampleEntity sample = new SampleEntity("name-of-this-entity", false, false, false);
SampleEntity sample = new SampleEntity("name-of-this-entity", false, false, false, false);
log.info("created: " + sample);

URI mcs1 = sample.computeMetaChecksum(MessageDigest.getInstance("MD5"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,15 @@ public EntityVisitorTest() {
@Test
public void testLogVisitor() {

SampleEntity e = new SampleEntity("foo", false, true, true);
SampleEntity e = new SampleEntity("foo", false, true, true, true);
e.dateVal = null;
e.doubleVal = 2.0;
e.longVal = 123L;
e.nested = new SampleEntity.Nested("flibble");
e.nestedSet.add(new SampleEntity.Nested("abc"));
e.nestedSet.add(new SampleEntity.Nested("def"));
e.strList.add("foo");
e.child1 = new SampleEntity("foo.child1", false, true, true, true);

log.info("testLogVisitor: START");
LogVisitor v = new LogVisitor();
Expand All @@ -108,16 +109,25 @@ public void testLogVisitor() {
Assert.assertEquals("numLeaf", 9, v.numLeaf);
Assert.assertEquals("numNode", 3, v.numNode); // nested, nestedSet, emptySet
Assert.assertEquals("numNull", 7, v.numNull);
Assert.assertEquals("numCol", 3, v.numCol);
Assert.assertEquals("numChildCol", 1, v.numChildCol);
Assert.assertEquals("numChildEntity", 1, v.numChildEntity);
Assert.assertEquals("numChildNull", 2, v.numChildNull);
}

private class LogVisitor implements EntityVisitor {
int numLeaf = 0;
int numNode = 0;
int numNull = 0;
int numCol = 0;
int numChildCol = 0;
int numChildNull = 0;
int numChildEntity = 0;

@Override
public void visitCollection(String vodmlID, Collection val) {
System.out.println("visit collection: " + vodmlID + " " + val.size());
numCol++;
}

@Override
Expand All @@ -137,7 +147,23 @@ public void visitNull(String vodmlID) {
System.out.println("visit null: " + vodmlID);
numNull++;
}



@Override
public void visitChildCollection(String vodmlID, Collection val) {
System.out.println("visit collection<entity>: " + vodmlID);
numChildCol++;
}

@Override
public void visitChildNull(String vodmlID) {
System.out.println("visit child null: " + vodmlID);
numChildNull++;
}

@Override
public void visitChildEntity(String vodmlID, Entity val) {
System.out.println("visit child entity: " + vodmlID);
numChildEntity++;
}
}
}
14 changes: 9 additions & 5 deletions cadc-util/src/test/java/org/opencadc/persist/SampleEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,22 @@ public class SampleEntity extends Entity implements Comparable<SampleEntity> {

// not included
public Set<SampleEntity> children = new TreeSet<>();
public SampleEntity relation;
public SampleEntity child1;
public SampleEntity child2;
public SampleEntity child3;
public static String staticVal;
public transient String transientVal;


public SampleEntity(String name, boolean truncateDateToSec, boolean digestFieldNames, boolean digestFieldNamesLowerCase) {
super(truncateDateToSec, digestFieldNames, digestFieldNamesLowerCase);
public SampleEntity(String name, boolean truncateDateToSec, boolean digestFieldNames, boolean digestFieldNamesLowerCase,
boolean digestZeroByteAfterListItem) {
super(truncateDateToSec, digestFieldNames, digestFieldNamesLowerCase, digestZeroByteAfterListItem);
this.name = name;
}

public SampleEntity(UUID id, String name, boolean truncateDateToSec, boolean digestFieldNames, boolean digestFieldNamesLowerCase) {
super(id, truncateDateToSec, digestFieldNames, digestFieldNamesLowerCase);
public SampleEntity(UUID id, String name, boolean truncateDateToSec, boolean digestFieldNames, boolean digestFieldNamesLowerCase,
boolean digestZeroByteAfterListItem) {
super(id, truncateDateToSec, digestFieldNames, digestFieldNamesLowerCase, digestZeroByteAfterListItem);
this.name = name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public class SampleEntityV2 extends SampleEntity {
public Integer optionalInt;
public String optionalString;

public SampleEntityV2(UUID id, String name, boolean trunc, boolean dig, boolean digL) {
super(id, name, trunc, dig, digL);
public SampleEntityV2(UUID id, String name, boolean trunc, boolean dig, boolean digL, boolean digZB) {
super(id, name, trunc, dig, digL, digZB);
}

public String toString() {
Expand Down
Loading