New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tesst test same objects, which cause assertion exception #85

Closed
sta-szek opened this Issue Sep 13, 2016 · 1 comment

Comments

Projects
None yet
1 participant
@sta-szek
Owner

sta-szek commented Sep 13, 2016

Tests test same objects, which makes e.g. NotEqualEqualsAssertionError and the message is:

pl.pojo.tester.internal.assertion.equals.NotEqualEqualsAssertionError: 


Class org.pojo.playground.GoodPojo has bad 'equals' method implementation.
The equals method should return false if objects should not be equal.
Current implementation returns true.
Object:
GoodPojo{byteField=0, shortType=0, intType=0, longType=0, doubleType=0.0, booleanType=false, floatType=0.0, charType= }
should not be equal to:
GoodPojo{byteField=0, shortType=0, intType=0, longType=0, doubleType=0.0, booleanType=false, floatType=0.0, charType= }

@sta-szek sta-szek added this to the 0.2.0 milestone Sep 13, 2016

@sta-szek sta-szek self-assigned this Sep 13, 2016

@sta-szek

This comment has been minimized.

Owner

sta-szek commented Sep 13, 2016

Getting pl.pojo.tester.internal.assertion.equals.NotEqualEqualsAssertionError is caused by ObjectGenerator's method generateDifferentObjects
It creates new object of field type class and sets to baseObjectCopy without checking if values are equal so solution for this problem is to put tihs code int 72 line:

Object newFieldTypeInstance = createNewInstance(permutationFieldType);
    try {
        if (newFieldTypeInstance.equals(FieldUtils.getValue(baseObject, permutationField))) {
            newFieldTypeInstance = abstractFieldValueChanger.increaseValue(newFieldTypeInstance);
        }
    } catch (final IllegalAccessException e) {
        // TODO
        throw new RuntimeException("Make FieldUtils.getValue(...,...) throw runtime exception");
}

but this makes another problem.
When creating proxy object (using java proxy or javaasist proxy) we need to pass InvocationHandler or MethodInvocationHandler.

This is implemented to return 0:

return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, (proxy, method, args) -> 0);

So when equals method is invoked, we get CastClassException. Naturally, we cant cast 0 to boolean so at first glance we can replace return statement with

return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, (proxy, method, args) -> method.invoke(proxy,args));

but this makes sweet Java throw java.lang.reflect.UndeclaredThrowableException
.
.
.
Can you imagine life without exceptions?
Just imagine...

And finally great sollution for this is:

 private Object proxyByJava() {
        return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, this::createInvocationHandler);
    }

    private Object createInvocationHandler(final Object proxy, final Method method, final Object[] args) {
        try {
            return method.invoke(proxy, args);
        } catch (IllegalAccessException | InvocationTargetException e) {
            final Class<?> returnType = method.getReturnType();
            if (returnType.equals(boolean.class) || returnType.equals(Boolean.class)) {
                return true;
            } else {
                return 0;
            }
        }
    }

😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆 😆

sta-szek pushed a commit that referenced this issue Sep 13, 2016

sta-szek pushed a commit that referenced this issue Sep 15, 2016

Piotr Joñski

sta-szek pushed a commit that referenced this issue Sep 15, 2016

Piotr Joñski

sta-szek pushed a commit that referenced this issue Sep 15, 2016

Piotr Joński
Merge pull request #91 from sta-szek/#85-Tesst-test-same-objects,-whi…
…ch-cause-assertion-exception

#85 tests test same objects, which cause assertion exception

@sta-szek sta-szek closed this Sep 15, 2016

@sta-szek sta-szek removed the in progress label Sep 15, 2016

sta-szek pushed a commit that referenced this issue Sep 16, 2016

sta-szek pushed a commit that referenced this issue Sep 16, 2016

sta-szek pushed a commit that referenced this issue Sep 17, 2016

Piotr Joñski
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment