Exercise 2 Task 2.1

Moderatoren: pmueller, SE - Design and Construction

Jan
Erstie
Erstie
Beiträge: 13
Registriert: 24. Apr 2015 13:12

Exercise 2 Task 2.1

Beitrag von Jan »

Hi,
I was wondering, is there really any difference between the two tests cachedObjectIsReturned and noUselessEvaluationOfSupplier? If the cached object is returned, the supplier was not evaluated, and vice versa if the supplier is not evaluated, the cached object was returned. So do these tests specify the same thing?

Furthermore, what is meant by "illegal values" for the illegalValuesHaveNoSideEffect test? Using anything but (String, Supplier) as the arguments for the get method will result in an error by the compiler before tests can be run. Are you talking about null values? If so, using null as the key is supported by Java HashMaps, and using null as the supplier will just result in a NullPointerException.

Thanks,
Jan

eichberg
Moderator
Moderator
Beiträge: 448
Registriert: 25. Sep 2007 12:12
Kontaktdaten:

Re: Exercise 2 Task 2.1

Beitrag von eichberg »

You should write black-box and not white-box tests :-). In that case you simple don't know how a method such as get is implemented and it would be very well possible (due to an implementation error) that <supplier>get is always evaluated, but we still always get the right (cached!) object.

Illegal values => null values

Jan
Erstie
Erstie
Beiträge: 13
Registriert: 24. Apr 2015 13:12

Re: Exercise 2 Task 2.1

Beitrag von Jan »

thanks for your answer, that clears things up :)

nebsterboy
Neuling
Neuling
Beiträge: 4
Registriert: 8. Nov 2017 13:02

Re: Exercise 2 Task 2.1

Beitrag von nebsterboy »

Hi,
I have a question regarding the test "getSpecificId". This test should test that the order in which objects are created is reflected by the id. Moreover, in the exercise sheet it is mentioned that we should only test the class Context. Now I am wondering how a tester of the Context class should know that objects are related to ids in any way? Especially if we write black-box tests. Since then I only see a method getNextId and a method get of the Context class.
Should we use the class ReferenceType for this test? And if so, wouldn't this contradict to the single responsibility principle?

Thanks,
Benni

marcel h
Erstie
Erstie
Beiträge: 13
Registriert: 15. Mai 2013 12:28

Re: Exercise 2 Task 2.1

Beitrag von marcel h »

I am with you on this one @Jan.
There seems to be nothing which connects an ID of getNextID to the get method. The integer IDs are never used to query the HashMap. If the user decides to use the ID of getNextID, convert it to a String and use this as the key parameter to call get, then all of this happens outside of the code to test. Ordering related test would have to be performed on this new code, or in our case, on the ReferenceType implementation, which has been explicitly excluded from the tests in this test suit.

Any hints?

eichberg
Moderator
Moderator
Beiträge: 448
Registriert: 25. Sep 2007 12:12
Kontaktdaten:

Re: Exercise 2 Task 2.1

Beitrag von eichberg »

Clearly a case of lost documentation on our side. The interdependencies between Context and ReferenceType should have been documented more clearly. In the following you'll see an updated variant of ReferenceType.

Code: Alles auswählen

/**
 * Representation of a ReferenceType; for example: "java.lang.String".
 */
public final class ReferenceType {

    /**
     * The global unique ID of this Type. All types are numbered sequentially
     * to make it possible to use an array to attach information to an array.
     * The ids are generated using Context's <code>getNextID</code> method.
     */
    final public int id;

    /** The name of a type in Java notation. */
    final public String name;

    private ReferenceType(String name, int id) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "ReferenceType("+name+","+id+")";
    }

    /**
     * Returns the type object which represents the specified type.
     * Each type – identified by its String – is associated a unique
     * `ReferenceType` object. Those objects are cached using
     * Context's cache.
     */
    // In the Scala version, the factory method should be name apply and get the context via an implicit parameter.
    public static ReferenceType create(String name) {
        return Context.get(name, () -> {
            return new ReferenceType(name, Context.getNextId());
        });
    }
}

marcel h
Erstie
Erstie
Beiträge: 13
Registriert: 15. Mai 2013 12:28

Re: Exercise 2 Task 2.1

Beitrag von marcel h »

Thanks, but its still not clear to me if we should use the ReferenceType implementation to test this, or should we reproduce this behavior?
This additional documentation is helpful, but it confuses me even more as the tasks still states that the test suit should be concerned with the Context class only. Reproducing the behavior seems fishy too, as there is an obvious implementation present.

Antworten

Zurück zu „Software Engineering - Design and Construction“