// The MIT License (MIT)
//
// Copyright (c) 2016 Timothy D. Jones
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package io.github.jonestimd.swing.table;

import java.awt.Color;
import java.awt.Font;
import java.awt.font.TextAttribute;

import javax.swing.JComponent;

import io.github.jonestimd.swing.table.model.ChangeBufferTableModel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class UnsavedChangeDecoratorTest {
    @Mock
    private DecoratedTable<Object, ChangeBufferTableModel<Object>> table;
    @Mock
    private ChangeBufferTableModel<Object> model;
    @Mock
    private JComponent renderer;
    private UnsavedChangeDecorator decorator = new UnsavedChangeDecorator();

    @Test
    public void setsStrikeoutFontForPendingDelete() throws Exception {
        final Font font = Font.decode(Font.MONOSPACED);
        when(table.getModel()).thenReturn(model);
        when(model.isPendingDelete(0)).thenReturn(true);
        when(renderer.getBackground()).thenReturn(Color.lightGray);
        when(renderer.getFont()).thenReturn(font);

        decorator.prepareRenderer(table, renderer, 0, 0);

        verify(renderer).setBackground(new Color(190, 129, 129));
        ArgumentCaptor<Font> captor = ArgumentCaptor.forClass(Font.class);
        verify(renderer).setFont(captor.capture());
        assertThat(captor.getValue().getAttributes().get(TextAttribute.STRIKETHROUGH)).isEqualTo(TextAttribute.STRIKETHROUGH_ON);
    }

    @Test
    public void setsBackgroundForPendingChange() throws Exception {
        when(table.getModel()).thenReturn(model);
        when(model.isPendingDelete(0)).thenReturn(false);
        when(model.isChangedAt(0, 0)).thenReturn(true);
        when(renderer.getBackground()).thenReturn(Color.lightGray);

        decorator.prepareRenderer(table, renderer, 0, 0);

        verify(renderer).setBackground(new Color(10, 190, 190));
        verify(renderer, never()).setFont(any(Font.class));
    }
}
@@ -1279,25 +1279,21 @@
 .swing.J
-Component
+Label
 ;%0A%0Aimpor
@@ -1416,43 +1416,8 @@
 th;%0A
-import org.mockito.ArgumentCaptor;%0A
 impo
@@ -1793,25 +1793,60 @@
  model;%0A
+%0A
     
-@Mock
+private JLabel renderer = new JLabel();
 %0A    pri
@@ -1854,18 +1854,19 @@
 ate 
-JCompone
+Font fo
 nt
+ =
  ren
@@ -1870,16 +1870,26 @@
 renderer
+.getFont()
 ;%0A    pr
@@ -2045,64 +2045,8 @@
 n %7B%0A
-        final Font font = Font.decode(Font.MONOSPACED);%0A
     
@@ -2148,37 +2148,32 @@
 (true);%0A        
-when(
 renderer.getBack
@@ -2157,33 +2157,33 @@
        renderer.
-g
+s
 etBackground()).
@@ -2175,38 +2175,24 @@
 tBackground(
-)).thenReturn(
 Color.lightG
@@ -2200,59 +2200,8 @@
 ay);
-%0A        when(renderer.getFont()).thenReturn(font);
 %0A%0A  
@@ -2257,38 +2257,42 @@
 0, 0);%0A%0A        
-verify
+assertThat
 (renderer).setBa
@@ -2276,35 +2276,34 @@
 ertThat(renderer
-).s
+.g
 etBackground(new
@@ -2291,32 +2291,45 @@
 r.getBackground(
+)).isEqualTo(
 new Color(190, 1
@@ -2351,89 +2351,18 @@
     
-ArgumentCaptor%3CFont%3E captor = ArgumentCaptor.forClass(Font.class);%0A        verify
+assertThat
 (ren
@@ -2370,72 +2370,17 @@
 erer
-).s
+.g
 etFont(
-captor.capture());%0A        assertThat(captor.getValue(
 ).ge
@@ -2727,21 +2727,16 @@
         
-when(
 renderer
@@ -2728,33 +2728,33 @@
        renderer.
-g
+s
 etBackground()).
@@ -2750,30 +2750,16 @@
 kground(
-)).thenReturn(
 Color.li
@@ -2828,38 +2828,42 @@
 0, 0);%0A%0A        
-verify
+assertThat
 (renderer).setBa
@@ -2855,19 +2855,18 @@
 renderer
-).s
+.g
 etBackgr
@@ -2870,16 +2870,29 @@
 kground(
+)).isEqualTo(
 new Colo
@@ -2921,57 +2921,52 @@
     
-verify(renderer, never()).setFont(any(Font.class)
+assertThat(renderer.getFont()).isSameAs(font
 );%0A 

Fix intermittent test failure