/**
   * Test method for {@link org.smartsnip.persistence.BlackholePersistence#BlackholePersistence()} .
   *
   * <p>This is an example test to the safety of the used singleton pattern. Due to the equivalence
   * to the constructors of all other classes which implement {@link IPersistence}, further tests on
   * their constructors may be omitted.
   *
   * @throws Throwable
   */
  @Test
  public void testBlackholePersistence() throws Throwable {

    // get an instance with the proposed getter method
    IPersistence instance = PersistenceFactory.getInstance(PersistenceFactory.PERSIST_BLACKHOLE);
    assertNotNull("instance = null", instance);

    // redo the get and assert equality
    IPersistence secondInstance =
        PersistenceFactory.getInstance(PersistenceFactory.PERSIST_BLACKHOLE);
    assertEquals(instance, secondInstance); // same content?
    assertTrue(instance == secondInstance); // same reference?

    // try to get a instance of different type - must fail
    try {
      secondInstance = PersistenceFactory.getInstance(PersistenceFactory.PERSIST_MEMORY_VOLATILE);
      fail("IllegalAccessException expected.");
    } catch (IllegalAccessException ie) {
      assertEquals(
          "Mismatch between requested and initialized persistence object.", ie.getMessage());
    }
    assertEquals(instance, secondInstance); // same content?
    assertTrue(instance == secondInstance); // same reference?

    // try to get an instance by direct call of the constructor - must fail
    try {
      secondInstance = new BlackholePersistence();
      fail("IllegalAccessException expected.");
    } catch (IllegalAccessException iae) {
      assertEquals("Singleton pattern: caller must be PersistenceFactory class.", iae.getMessage());
    }
    assertEquals(instance, secondInstance); // same content?
    assertTrue(instance == secondInstance); // same reference?

    // try to get an instance by using the reflections mechanism - must fail
    try {
      @SuppressWarnings("rawtypes")
      Constructor[] c = BlackholePersistence.class.getDeclaredConstructors();
      c[0].setAccessible(true);

      secondInstance = (BlackholePersistence) c[0].newInstance();
      fail("Exception expected");
    } catch (InvocationTargetException e) {
      assertTrue(e.getCause() instanceof IllegalAccessException);
      assertEquals(
          "Singleton pattern: caller must be PersistenceFactory class.", e.getCause().getMessage());
    }
    assertEquals(instance, secondInstance); // same content?
    assertTrue(instance == secondInstance); // same reference?
  }
示例#2
0
  private Object getValueOfField(String fieldname, Class<?> sourceClass, Object source) {
    String sourceClassName = sourceClass.getName();

    Field field = null;
    try {
      field = sourceClass.getDeclaredField(fieldname);
      field.setAccessible(true);
    } catch (SecurityException e) {
      fail(
          "Unable to retrieve "
              + fieldname
              + " field from "
              + sourceClassName
              + ": "
              + e.getCause());
    } catch (NoSuchFieldException e) {
      fail(
          "Unable to retrieve "
              + fieldname
              + " field from "
              + sourceClassName
              + ": "
              + e.getCause());
    }

    assertNotNull("." + fieldname + " field is null!?!", field);
    Object fieldValue = null;
    try {
      fieldValue = field.get(source);
    } catch (IllegalArgumentException e) {
      fail(
          "Unable to retrieve value of "
              + fieldname
              + " from "
              + sourceClassName
              + ": "
              + e.getCause());
    } catch (IllegalAccessException e) {
      fail(
          "Unable to retrieve value of "
              + fieldname
              + " from "
              + sourceClassName
              + ": "
              + e.getCause());
    }
    return fieldValue;
  }
 // helper helper
 private static boolean handleLinkedList(Field f1, Object o1, Field f2, Object o2) {
   try {
     LinkedList<?> l1 = (LinkedList<?>) f1.get(o1);
     LinkedList<?> l2 = (LinkedList<?>) f2.get(o2);
     int length = l1.size();
     for (int i = 0; i < length; i++) {
       if (!(structurallyEqual(l1.get(i), l2.get(i)))) {
         return false;
       }
     }
   } catch (IllegalArgumentException e) {
     e.printStackTrace();
   } catch (IllegalAccessException e) {
     e.printStackTrace();
   }
   return true;
 }
示例#4
0
 @Test
 public void testSetProperty_4args_deniedByFilter() throws Exception {
   C1 object = new C1();
   object.setString1(null);
   String propertyName = "string1";
   Object value = "myStringValue";
   Bean instance = new Bean(C1.class);
   ClassFilter filter = new SimpleClassFilter(C3.class, TypeAnn.class);
   try {
     instance.setProperty(object, propertyName, value, filter);
   } catch (IllegalAccessException ex) {
     if (ex.getMessage().equals("Property no found")) {
       // IS OK - it is correct
       return;
     }
   }
   // Error
   throw new Exception("No correct - Inncorect result");
 }
示例#5
0
 public static void authenticateEndpoint(Endpoint endpoint, String user, String password) {
   try {
     Field userField = endpoint.getClass().getSuperclass().getDeclaredField("user");
     userField.setAccessible(true);
     userField.set(endpoint, user);
     Field passwordField = endpoint.getClass().getSuperclass().getDeclaredField("password");
     passwordField.setAccessible(true);
     passwordField.set(endpoint, password);
   } catch (SecurityException e) {
     fail(e.getMessage());
     e.printStackTrace();
   } catch (NoSuchFieldException e) {
     fail(e.getMessage());
     e.printStackTrace();
   } catch (IllegalArgumentException e) {
     fail(e.getMessage());
     e.printStackTrace();
   } catch (IllegalAccessException e) {
     fail(e.getMessage());
     e.printStackTrace();
   }
 }
  /*
   * A helper for sanity checking de-/serialization.
   * We test two objects to be structurally equal by means of reflection.
   */
  public static boolean structurallyEqual(Object o1, Object o2) {
    try {
      if (o1.getClass().getName().equals(o2.getClass().getName())) {

        for (Class<?> obj = o1.getClass(); !obj.equals(Object.class); obj = obj.getSuperclass()) {
          Field[] f1 = obj.getDeclaredFields();
          Field[] f2 = obj.getDeclaredFields();

          for (int i = 0; i < f1.length; i++) {
            f1[i].setAccessible(true);
            f2[i].setAccessible(true);
            // check if fields are primitive types and compare
            if ((f1[i].getType().isPrimitive() && f2[i].getType().isPrimitive())) {
              if (!(f1[i].getName().equals(f2[i].getName()))) return false;
              else {
                if (!(f1[i].get(o1).equals(f2[i].get(o2)))) {
                  return false;
                }
              }
              // otherwise, they must be objects
            } else {
              // to be equal, both can not be null
              if (f1[i].get(o1) != null && f2[i].get(o2) != null) {
                // check, if they are of the same class
                if (f1[i].get(o1).getClass().getName().equals(f2[i].get(o2).getClass().getName())) {
                  // check if the class is Double,
                  // Integer or String
                  if (check(f1[i].get(o1).getClass().getName())) {
                    // compare values
                    if (!(f1[i].get(o1).equals(f2[i].get(o2)))) {
                      return false;
                    }
                  } else {
                    // special case, if the object is an
                    // linked list
                    if (f1[i].get(o1).getClass().getName().equals("java.util.LinkedList")) {
                      if (!(handleLinkedList(f1[i], o1, f2[i], o2))) {
                        return false;
                      }
                    } else {
                      // otherwise, compare the objects
                      structurallyEqual(f1[i].get(o1), f2[i].get(o2));
                    }
                  }
                } else {
                  return false;
                }
              } else {
                // if one of them is null, the objects can not
                // be equal
                if (f1[i].get(o1) == null && f2[i].get(o2) != null) {
                  return false;
                }
                if (f1[i].get(o1) != null && f2[i].get(o2) == null) {
                  return false;
                }
              }
            }
          }
        }
      } else {
        return false;
      }
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }
    return true;
  }