@Test
 public void testSizeConstraint_not_there() throws NoSuchFieldException, SecurityException {
   Field f1 = TestClass.class.getDeclaredField("notNullString");
   ObjectContext<Object> s1 = uut.forField(f1);
   assertFalse(s1.getSizeConstraints().getMin().isPresent());
   assertFalse(s1.getSizeConstraints().getMax().isPresent());
 }
 @Test
 public void testSizeConstraint() throws NoSuchFieldException, SecurityException {
   Field f1 = TestClass.class.getDeclaredField("sizeString");
   ObjectContext<Object> s1 = uut.forField(f1);
   assertEquals(1, s1.getSizeConstraints().getMin().get().intValue());
   assertEquals(10, s1.getSizeConstraints().getMax().get().intValue());
 }
Beispiel #3
0
 public final ObjectContext object() throws RecognitionException {
   ObjectContext _localctx = new ObjectContext(_ctx, getState());
   enterRule(_localctx, 2, RULE_object);
   int _la;
   try {
     setState(27);
     switch (getInterpreter().adaptivePredict(_input, 2, _ctx)) {
       case 1:
         _localctx = new AnObjectContext(_localctx);
         enterOuterAlt(_localctx, 1);
         {
           setState(14);
           match(LCURLY);
           setState(15);
           pair();
           setState(20);
           _errHandler.sync(this);
           _la = _input.LA(1);
           while (_la == T__0) {
             {
               {
                 setState(16);
                 match(T__0);
                 setState(17);
                 pair();
               }
             }
             setState(22);
             _errHandler.sync(this);
             _la = _input.LA(1);
           }
           setState(23);
           match(T__1);
         }
         break;
       case 2:
         _localctx = new EmptyObjectContext(_localctx);
         enterOuterAlt(_localctx, 2);
         {
           setState(25);
           match(LCURLY);
           setState(26);
           match(T__1);
         }
         break;
     }
   } catch (RecognitionException re) {
     _localctx.exception = re;
     _errHandler.reportError(this, re);
     _errHandler.recover(this, re);
   } finally {
     exitRule();
   }
   return _localctx;
 }
  @Test
  public void testCustomSizeConstraints() throws NoSuchFieldException, SecurityException {
    Field f1 = TestClass.class.getDeclaredField("notEmptyString");
    ObjectContext<Object> s1 = uut.forField(f1);
    assertEquals(1, s1.getSizeConstraints().getMin().get().intValue());

    Field f2 = TestClass.class.getDeclaredField("notEmptyStringWithSize");
    ObjectContext<Object> s2 = uut.forField(f2);
    assertEquals(10, s2.getSizeConstraints().getMin().get().intValue());
  }
  /**
   * Returns a map key for a given to-many map relationship and a target object.
   *
   * @since 3.0
   */
  protected Object getMapKey(String relationshipName, Object value) {

    EntityResolver resolver = objectContext.getEntityResolver();
    ClassDescriptor descriptor = resolver.getClassDescriptor(objectId.getEntityName());

    if (descriptor == null) {
      throw new IllegalStateException("DataObject's entity is unmapped, objectId: " + objectId);
    }

    PropertyDescriptor property = descriptor.getProperty(relationshipName);
    if (property instanceof ToManyMapProperty) {
      return ((ToManyMapProperty) property).getMapKey(value);
    }

    throw new IllegalArgumentException(
        "Relationship '" + relationshipName + "' is not a to-many Map");
  }
 public Class<?> getParentObjectClass() {
   return parentContext.getValue().getClass();
 }
Beispiel #7
0
  @Override
  public void run() {
    String sourceId = objectContext.getSourceSummary().getIdentifier();

    if (!syncControl.isRunning()) {
      log.debug("aborting sync task because terminate() was called: " + sourceId);
      return;
    }

    boolean processed = false, recordExists = false;
    SyncRecord record;
    try {

      // this should lazy-load all but metadata from the storage; this is so we see
      // ObjectNotFoundException here
      objectContext.setObject(source.loadObject(sourceId));

      ObjectMetadata metadata = objectContext.getObject().getMetadata();

      // truncate milliseconds (the DB only stores to the second)
      Date mtime = null;
      if (metadata.getModificationTime() != null)
        mtime = new Date(metadata.getModificationTime().getTime() / 1000 * 1000);

      record = dbService.getSyncRecord(objectContext);
      recordExists = record != null;
      if (record != null && record.getTargetId() != null)
        objectContext.setTargetId(record.getTargetId());

      if (!objectContext.getOptions().isVerifyOnly()) {
        if (record == null
            || !record.getStatus().isSuccess()
            || (mtime != null && record.getMtime() != null && mtime.after(record.getMtime()))) {

          log.debug(
              "O--+ syncing {} {}", metadata.isDirectory() ? "directory" : "object", sourceId);

          objectContext.setStatus(ObjectStatus.InTransfer);
          recordExists = dbService.setStatus(objectContext, null, !recordExists);

          try {
            filterChain.filter(objectContext);
          } catch (Throwable t) {
            if (t instanceof NonRetriableException) throw t;
            retryHandler.submitForRetry(source, objectContext, t);
            return;
          }

          if (metadata.isDirectory()) log.info("O--O finished syncing directory {}", sourceId);
          else
            log.info(
                "O--O finished syncing object {} ({} bytes transferred)",
                sourceId,
                objectContext.getObject().getBytesRead());

          objectContext.setStatus(ObjectStatus.Transferred);
          dbService.setStatus(objectContext, null, false);
          processed = true;
        } else {
          log.info("O--* skipping {} because it is up-to-date in the target", sourceId);
        }
      }

      if (objectContext.getOptions().isVerify() || objectContext.getOptions().isVerifyOnly()) {
        if (record == null
            || record.getStatus() != ObjectStatus.Verified
            || (mtime != null && record.getMtime() != null && mtime.after(record.getMtime()))) {

          log.debug(
              "O==? verifying {} {}", sourceId, metadata.isDirectory() ? "directory" : "object");

          objectContext.setStatus(ObjectStatus.InVerification);
          recordExists = dbService.setStatus(objectContext, null, !recordExists);

          try {
            SyncObject targetObject = filterChain.reverseFilter(objectContext);

            try {
              verifier.verify(objectContext.getObject(), targetObject);
            } finally {
              try {
                // be sure to close all object resources
                targetObject.close();
              } catch (Throwable t) {
                log.warn("could not close target object resources", t);
              }
            }

          } catch (Throwable t) {
            if (!objectContext
                .getOptions()
                .isVerifyOnly()) { // if we just copied the data and verification failed, we should
                                   // retry
              retryHandler.submitForRetry(source, objectContext, t);
              return;
            } else throw t;
          }

          log.info("O==O verification successful for {}", sourceId);
          objectContext.setStatus(ObjectStatus.Verified);
          dbService.setStatus(objectContext, null, false);
          processed = true;
        } else {
          log.info("O==* skipping {} because it has already been verified", sourceId);
        }
      }

      if (processed) {
        syncStats.incObjectsComplete();
        syncStats.incBytesComplete(objectContext.getObject().getBytesRead());
      } else {
        syncStats.incObjectsSkipped();
        syncStats.incBytesSkipped(objectContext.getSourceSummary().getSize());
      }

      try { // delete object if the source supports deletion (implements the delete() method)
        if (objectContext.getOptions().isDeleteSource()) {
          source.delete(sourceId);
          log.info("X--O deleted {} from source", sourceId);
          dbService.setDeleted(objectContext, !recordExists);
        }
      } catch (Throwable t) {
        log.warn("!--O could not delete {} from source: {}", sourceId, t);
      }

    } catch (Throwable t) {
      try {
        objectContext.setStatus(ObjectStatus.Error);
        dbService.setStatus(objectContext, SyncUtil.summarize(t), !recordExists);
      } catch (Throwable t2) {
        log.warn("error setting DB status", t2);
      }

      log.warn("O--! object " + sourceId + " failed", SyncUtil.getCause(t));

      syncStats.incObjectsFailed();
      if (objectContext.getOptions().isRememberFailed()) syncStats.addFailedObject(sourceId);

    } finally {
      try {
        // be sure to close all object resources
        if (objectContext.getObject() != null) objectContext.getObject().close();
      } catch (Throwable t) {
        log.warn("could not close object resources", t);
      }
    }
  }
 @Test
 public void testNotNullConstraint_not_there() throws NoSuchFieldException, SecurityException {
   Field f1 = TestClass.class.getDeclaredField("sizeString");
   ObjectContext<Object> s1 = uut.forField(f1);
   assertFalse(s1.isRequired());
 }
 @Test
 public void testCustomNotNullConstraint() throws NoSuchFieldException, SecurityException {
   Field f1 = TestClass.class.getDeclaredField("notEmptyString");
   ObjectContext<Object> s1 = uut.forField(f1);
   assertTrue(s1.isRequired());
 }
 @Before
 public void setuUp() {
   uut = ObjectContext.buildFor(TestClass.class).build();
 }