コード例 #1
0
 public void testValidValuesInList() throws Exception {
   int quantity = getListRowCount();
   assertTrue("For this test is needed at least one created delivery", quantity > 0);
   Collection values = new ArrayList();
   values.add("Lokal");
   values.add("Nachional");
   values.add("Internachional");
   boolean thereIsOne = false;
   for (int i = 0; i < quantity; i++) {
     String value = getValueInList(i, "distance");
     if (Is.emptyString(value)) continue;
     if (values.contains(value)) {
       thereIsOne = true;
       continue;
     }
     fail("Only the next values are valid: " + values);
   }
   assertTrue(
       "For this test is need at least one delivery with value in 'distance' property",
       thereIsOne);
 }
コード例 #2
0
ファイル: Audit.java プロジェクト: bgarrels/xpert-framework
  public List<AbstractMetadata> getMetadata(
      Object object, Object persisted, AbstractAuditing auditing) throws Exception {
    List<Method> methodsGet = getMethods(object);
    List<AbstractMetadata> metadatas = new ArrayList<AbstractMetadata>();
    boolean isDelete =
        auditing.getAuditingType() != null
            && auditing.getAuditingType().equals(AuditingType.DELETE);
    for (Method method : methodsGet) {
      try {
        Object fieldValue = method.invoke(object);
        Object fieldOld = null;
        if (persisted != null) {
          fieldOld = method.invoke(persisted);
        }
        AbstractMetadata metadata = Configuration.getAbstractMetadata();
        if (fieldValue != null && fieldValue.getClass().isAnnotationPresent(Embeddable.class)) {
          List<AbstractMetadata> embedableMetadata = getMetadata(fieldValue, persisted, auditing);
          if (embedableMetadata != null && !embedableMetadata.isEmpty()) {
            metadatas.addAll(embedableMetadata);
          }
        } else {
          boolean addMetadata = persisted == null;
          if (fieldValue instanceof Collection) { // para as coleções
            Collection collectionNew = (Collection<Object>) fieldValue;
            Collection collectionOld = (Collection<Object>) fieldOld;
            StringBuilder newValue = new StringBuilder();
            if (fieldOld == null) {
              for (Object item : collectionNew) {
                newValue.append(item.toString()).append("; ");
              }
              addMetadata = true;
            } else {
              StringBuilder oldValue = new StringBuilder();
              if ((!(collectionNew instanceof PersistentBag)
                      && !(collectionNew instanceof PersistentCollection))
                  || isDelete == true) {
                if ((collectionNew == null && collectionOld != null)
                    || (collectionNew != null && collectionOld == null)
                    || (collectionNew.size() != collectionOld.size())) {
                  addMetadata = true;
                } else {
                  for (Object current : collectionNew) {
                    if (collectionOld != null && !collectionOld.contains(current)) {
                      for (Object currentOld : collectionOld) {
                        if (!currentOld.equals(current)) {
                          addMetadata = true;
                          break;
                        }
                      }
                    }
                  }
                }
                for (Object old : collectionOld) {
                  oldValue.append(old).append("; ");
                }
                for (Object item : collectionNew) {
                  newValue.append(item.toString()).append("; ");
                }
                metadata.setOldValue(oldValue.toString());
              }
            }
            metadata.setNewValue(newValue.toString());
          } else if (isEntity(method.getReturnType())) {
            Object newId = getId(fieldValue);
            // a proxy doesnt has value changed
            if (!(fieldValue instanceof HibernateProxy) || isDelete == true) {
              /** One to One cascade ALL */
              if (isOneToOneCascadeAll(method)) {
                List<AbstractMetadata> embedableMetadata = null;
                // add metadata for oneToOne cascade all based on new object
                if (persisted == null) {
                  embedableMetadata = getMetadata(fieldValue, null, auditing);
                } else {
                  embedableMetadata = getMetadata(fieldValue, getPersisted(fieldValue), auditing);
                }

                if (embedableMetadata != null && !embedableMetadata.isEmpty()) {
                  metadatas.addAll(embedableMetadata);
                }
              }

              Object oldId = null;
              if (fieldOld instanceof HibernateProxy) {
                oldId = ((HibernateProxy) fieldOld).getHibernateLazyInitializer().getIdentifier();
              } else {
                oldId = getId(fieldOld);
              }
              metadata.setOldIdentifier(oldId == null ? null : Long.valueOf(oldId.toString()));
              metadata.setOldValue(fieldOld == null ? "" : fieldOld.toString());
              if ((oldId == null && newId != null)
                  || (oldId != null && newId == null)
                  || (oldId != null && !oldId.equals(newId))) {
                addMetadata = true;
              }
              metadata.setEntity(method.getDeclaringClass().getName());
              metadata.setNewIdentifier(newId == null ? null : Long.valueOf(newId.toString()));
              metadata.setNewValue(fieldValue == null ? "" : fieldValue.toString());
            }
          } else {
            if (fieldOld != null) {
              metadata.setOldValue(getToString(fieldOld));
            }
            if (fieldValue != null) {
              metadata.setNewValue(getToString(fieldValue));
            }
            // verify empty String
            if (fieldValue instanceof String) {
              if ((fieldOld == null && fieldValue != null && !fieldValue.toString().isEmpty())
                  || (fieldOld != null && !fieldOld.toString().isEmpty() && fieldValue == null)
                  || (fieldOld != null && !fieldOld.equals(fieldValue))) {
                addMetadata = true;
              }
            } else {
              if ((fieldOld == null && fieldValue != null)
                  || (fieldOld != null && fieldValue == null)
                  || (fieldOld != null && !fieldOld.equals(fieldValue))) {
                addMetadata = true;
              }
            }
          }
          metadata.setField(getMethodName(method));
          metadata.setAuditing(auditing);
          if (addMetadata) {
            metadatas.add(metadata);
          }
        }
      } catch (Throwable t) {
        logger.log(Level.SEVERE, t.getMessage(), t);
      }
    }
    return metadatas;
  }