Skip to content

Avoid generating many reflection statements for LinkedHashMap #2227

Closed
@EgorkaKulikov

Description

@EgorkaKulikov

Description

Try to generate Spring integration tests for spring-boot-testing project OrderController.getAllOrders method.

Generated test looks like not very good, it contains too much reflection.
The problem is that we do not have specific wrappers for LinkedHashMap

 @Test(description = "getAllOrders: ")
    public void testGetAllOrders() throws Exception {
        // begin repository fill up
        Order order = new Order();
        order.setPrice(Double.NEGATIVE_INFINITY);
        order.setQty(Integer.MIN_VALUE);

        // end repository fill up

        ResponseEntity actual = orderController.getAllOrders();

        ResponseEntity expected = ((ResponseEntity) createInstance("org.springframework.http.ResponseEntity"));
        HttpStatus httpStatus = HttpStatus.OK;
        setField(expected, "org.springframework.http.ResponseEntity", "status", httpStatus);
        Object readOnlyHttpHeaders = createInstance("org.springframework.http.ReadOnlyHttpHeaders");
        setField(readOnlyHttpHeaders, "org.springframework.http.ReadOnlyHttpHeaders", "cachedContentType", null);
        setField(readOnlyHttpHeaders, "org.springframework.http.ReadOnlyHttpHeaders", "cachedAccept", null);
        MultiValueMapAdapter multiValueMapAdapter = ((MultiValueMapAdapter) createInstance("org.springframework.util.MultiValueMapAdapter"));
        LinkedCaseInsensitiveMap linkedCaseInsensitiveMap = ((LinkedCaseInsensitiveMap) createInstance("org.springframework.util.LinkedCaseInsensitiveMap"));
        LinkedHashMap anonymousLinkedHashMap = ((LinkedHashMap) createInstance("org.springframework.util.LinkedCaseInsensitiveMap$1"));
        setField(anonymousLinkedHashMap, "org.springframework.util.LinkedCaseInsensitiveMap$1", "this$0", linkedCaseInsensitiveMap);
        setField(anonymousLinkedHashMap, "java.util.LinkedHashMap", "head", null);
        setField(anonymousLinkedHashMap, "java.util.LinkedHashMap", "tail", null);
        setField(anonymousLinkedHashMap, "java.util.LinkedHashMap", "accessOrder", false);
        setField(anonymousLinkedHashMap, "java.util.HashMap", "table", null);
        setField(anonymousLinkedHashMap, "java.util.HashMap", "entrySet", null);
        setField(anonymousLinkedHashMap, "java.util.HashMap", "size", 0);
        setField(anonymousLinkedHashMap, "java.util.HashMap", "modCount", 0);
        setField(anonymousLinkedHashMap, "java.util.HashMap", "threshold", 16);
        setField(anonymousLinkedHashMap, "java.util.HashMap", "loadFactor", 0.75f);
        setField(anonymousLinkedHashMap, "java.util.AbstractMap", "keySet", null);
        setField(anonymousLinkedHashMap, "java.util.AbstractMap", "values", null);
        setField(linkedCaseInsensitiveMap, "org.springframework.util.LinkedCaseInsensitiveMap", "targetMap", anonymousLinkedHashMap);
        HashMap hashMap = new HashMap();
        setField(linkedCaseInsensitiveMap, "org.springframework.util.LinkedCaseInsensitiveMap", "caseInsensitiveKeys", hashMap);
        Locale locale = ((Locale) createInstance("java.util.Locale"));
        setField(linkedCaseInsensitiveMap, "org.springframework.util.LinkedCaseInsensitiveMap", "locale", locale);
        setField(linkedCaseInsensitiveMap, "org.springframework.util.LinkedCaseInsensitiveMap", "keySet", null);
        setField(linkedCaseInsensitiveMap, "org.springframework.util.LinkedCaseInsensitiveMap", "values", null);
        setField(linkedCaseInsensitiveMap, "org.springframework.util.LinkedCaseInsensitiveMap", "entrySet", null);
        setField(multiValueMapAdapter, "org.springframework.util.MultiValueMapAdapter", "targetMap", linkedCaseInsensitiveMap);
        setField(readOnlyHttpHeaders, "org.springframework.http.HttpHeaders", "headers", multiValueMapAdapter);
        setField(expected, "org.springframework.http.HttpEntity", "headers", readOnlyHttpHeaders);
        ArrayList arrayList = new ArrayList();
        Order order1 = ((Order) createInstance("com.rest.order.models.Order"));
        Long long1 = 2L;
        setField(order1, "com.rest.order.models.Order", "id", long1);
        setField(order1, "com.rest.order.models.Order", "buyer", null);
        Double double1 = Double.NEGATIVE_INFINITY;
        setField(order1, "com.rest.order.models.Order", "price", double1);
        setField(order1, "com.rest.order.models.Order", "qty", Integer.MIN_VALUE);
        arrayList.add(order1);
        setField(expected, "org.springframework.http.HttpEntity", "body", arrayList);
        Object expectedStatus = getFieldValue(expected, "org.springframework.http.ResponseEntity", "status");
        Object actualStatus = getFieldValue(actual, "org.springframework.http.ResponseEntity", "status");
        assertEquals(expectedStatus, actualStatus);

        HttpHeaders expectedHeaders = ((HttpHeaders) getFieldValue(expected, "org.springframework.http.HttpEntity", "headers"));
        HttpHeaders actualHeaders = ((HttpHeaders) getFieldValue(actual, "org.springframework.http.HttpEntity", "headers"));
        // org.springframework.http.HttpHeaders is iterable or Map, use outer deep equals to iterate over
        assertTrue(deepEquals(actualHeaders, expectedHeaders));

        Object expectedBody = getFieldValue(expected, "org.springframework.http.HttpEntity", "body");
        Object actualBody = getFieldValue(actual, "org.springframework.http.HttpEntity", "body");
        assertTrue(deepEquals(actualBody, expectedBody));

    }

Metadata

Metadata

Assignees

Labels

comp-fuzzingIssue is related to the fuzzingcomp-springIssue is related to Spring projects supportctg-enhancementNew feature, improvement or change request

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions