/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.lang3.builder;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Iterator;
import java.util.List;

import org.junit.jupiter.api.Test;

/**
 * Unit tests {@link DiffResult}.
 */
public class DiffResultTest {

    private static final SimpleClass SIMPLE_FALSE = new SimpleClass(false);
    private static final SimpleClass SIMPLE_TRUE = new SimpleClass(true);
    private static final ToStringStyle SHORT_STYLE = ToStringStyle.SHORT_PREFIX_STYLE;

    private static class SimpleClass implements Diffable<SimpleClass> {
        private final boolean booleanField;

        SimpleClass(final boolean booleanField) {
            this.booleanField = booleanField;
        }

        static String getFieldName() {
            return "booleanField";
        }

        @Override
        public DiffResult diff(final SimpleClass obj) {
            return new DiffBuilder(this, obj, ToStringStyle.SHORT_PREFIX_STYLE)
                    .append(getFieldName(), booleanField, obj.booleanField)
                    .build();
        }
    }

    private static class EmptyClass {
    }

    @Test
    public void testListIsNonModifiable() {
        final SimpleClass lhs = new SimpleClass(true);
        final SimpleClass rhs = new SimpleClass(false);

        final List<Diff<?>> diffs = lhs.diff(rhs).getDiffs();

        final DiffResult list = new DiffResult(lhs, rhs, diffs, SHORT_STYLE);
        assertEquals(diffs, list.getDiffs());
        assertEquals(1, list.getNumberOfDiffs());
        assertThrows(UnsupportedOperationException.class, () -> list.getDiffs().remove(0));
    }

    @Test
    public void testIterator() {
        final SimpleClass lhs = new SimpleClass(true);
        final SimpleClass rhs = new SimpleClass(false);

        final List<Diff<?>> diffs = lhs.diff(rhs).getDiffs();
        final Iterator<Diff<?>> expectedIterator = diffs.iterator();

        final DiffResult list = new DiffResult(lhs, rhs, diffs, SHORT_STYLE);
        final Iterator<Diff<?>> iterator = list.iterator();

        while (iterator.hasNext()) {
            assertTrue(expectedIterator.hasNext());
            assertEquals(expectedIterator.next(), iterator.next());
        }
    }

    @Test
    public void testToStringOutput() {
        final DiffResult list = new DiffBuilder(new EmptyClass(), new EmptyClass(),
                ToStringStyle.SHORT_PREFIX_STYLE).append("test", false, true)
                .build();
        assertEquals(
                "DiffResultTest.EmptyClass[test=false] differs from DiffResultTest.EmptyClass[test=true]",
                list.toString());
    }

    @Test
    public void testToStringSpecifyStyleOutput() {
        final DiffResult list = SIMPLE_FALSE.diff(SIMPLE_TRUE);
        assertEquals(list.getToStringStyle(), SHORT_STYLE);

        final String lhsString = new ToStringBuilder(SIMPLE_FALSE,
                ToStringStyle.MULTI_LINE_STYLE).append(
                SimpleClass.getFieldName(), SIMPLE_FALSE.booleanField).build();

        final String rhsString = new ToStringBuilder(SIMPLE_TRUE,
                ToStringStyle.MULTI_LINE_STYLE).append(
                SimpleClass.getFieldName(), SIMPLE_TRUE.booleanField).build();

        final String expectedOutput = String.format("%s differs from %s", lhsString,
                rhsString);
        assertEquals(expectedOutput,
                list.toString(ToStringStyle.MULTI_LINE_STYLE));
    }

    @Test
    public void testNullLhs() {
        assertThrows(NullPointerException.class,
            () -> new DiffResult(null, SIMPLE_FALSE, SIMPLE_TRUE.diff(SIMPLE_FALSE).getDiffs(), SHORT_STYLE));
    }

    @Test
    public void testNullRhs() {
        assertThrows(NullPointerException.class,
            () -> new DiffResult(SIMPLE_TRUE, null, SIMPLE_TRUE.diff(SIMPLE_FALSE).getDiffs(), SHORT_STYLE));
    }

    @Test
    public void testNullList() {
        assertThrows(NullPointerException.class,
            () -> new DiffResult(SIMPLE_TRUE, SIMPLE_FALSE, null, SHORT_STYLE));
    }

    @Test
    public void testNullStyle() {
        final DiffResult diffResult = new DiffResult(SIMPLE_TRUE, SIMPLE_FALSE, SIMPLE_TRUE
                .diff(SIMPLE_FALSE).getDiffs(), null);
        assertEquals(ToStringStyle.DEFAULT_STYLE, diffResult.getToStringStyle());
    }

    @Test
    public void testNoDifferencesString() {
        final DiffResult diffResult = new DiffBuilder(SIMPLE_TRUE, SIMPLE_TRUE,
                SHORT_STYLE).build();
        assertEquals(DiffResult.OBJECTS_SAME_STRING, diffResult.toString());
    }

    @Test
    public void testLeftAndRightGetters() {
        final SimpleClass left = new SimpleClass(true);
        final SimpleClass right = new SimpleClass(false);

        final List<Diff<?>> diffs = left.diff(right).getDiffs();
        final DiffResult diffResult = new DiffResult(left, right, diffs, SHORT_STYLE);

        assertEquals(left, diffResult.getLeft());
        assertEquals(right, diffResult.getRight());
    }
}

@@ -1767,24 +1767,37 @@
 c DiffResult
+%3CSimpleClass%3E
  diff(final 
@@ -1849,16 +1849,18 @@
 fBuilder
+%3C%3E
 (this, o
@@ -2058,16 +2058,33 @@
 Class %7B%0A
+        // empty%0A
     %7D%0A%0A 
@@ -2327,32 +2327,45 @@
 final DiffResult
+%3CSimpleClass%3E
  list = new Diff
@@ -2362,32 +2362,34 @@
 = new DiffResult
+%3C%3E
 (lhs, rhs, diffs
@@ -2902,32 +2902,45 @@
 final DiffResult
+%3CSimpleClass%3E
  list = new Diff
@@ -2945,16 +2945,18 @@
 ffResult
+%3C%3E
 (lhs, rh
@@ -3279,32 +3279,44 @@
 final DiffResult
+%3CEmptyClass%3E
  list = new Diff
@@ -3322,16 +3322,18 @@
 fBuilder
+%3C%3E
 (new Emp
@@ -3720,16 +3720,29 @@
 ffResult
+%3CSimpleClass%3E
  list = 
@@ -4584,16 +4584,18 @@
 ffResult
+%3C%3E
 (null, S
@@ -4787,32 +4787,34 @@
 %3E new DiffResult
+%3C%3E
 (SIMPLE_TRUE, nu
@@ -4998,32 +4998,34 @@
 %3E new DiffResult
+%3C%3E
 (SIMPLE_TRUE, SI
@@ -5124,32 +5124,45 @@
 final DiffResult
+%3CSimpleClass%3E
  diffResult = ne
@@ -5173,16 +5173,18 @@
 ffResult
+%3C%3E
 (SIMPLE_
@@ -5429,32 +5429,45 @@
 final DiffResult
+%3CSimpleClass%3E
  diffResult = ne
@@ -5475,24 +5475,26 @@
  DiffBuilder
+%3C%3E
 (SIMPLE_TRUE

Fix generics compiler warnings