package hudson.plugins.emailext.plugins;

import hudson.model.AbstractBuild;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.plugins.emailext.ExtendedEmailPublisher;
import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor;
import hudson.util.StreamTaskListener;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.recipes.WithPlugin;

import java.io.IOException;
import java.lang.reflect.Field;


import static org.mockito.Mockito.*;

public class ContentBuilderTest
    extends HudsonTestCase
{
    private ExtendedEmailPublisher publisher;
    private StreamTaskListener listener;

    @Override
    public void setUp()
        throws Exception
    {
        super.setUp();
        
        listener = new StreamTaskListener(System.out);
        
        publisher = mock(ExtendedEmailPublisher.class);
        publisher.defaultContent = "For only 10 easy payment of $69.99 , AWESOME-O 4000 can be yours!";
        publisher.defaultSubject = "How would you like your very own AWESOME-O 4000?";
        publisher.recipientList = "ashlux@gmail.com";

        Field f = ExtendedEmailPublisherDescriptor.class.getDeclaredField( "defaultBody" );
        f.setAccessible( true );
        f.set( ExtendedEmailPublisher.DESCRIPTOR, "Give me $4000 and I'll mail you a check for $40,000!" );
        f = ExtendedEmailPublisherDescriptor.class.getDeclaredField( "defaultSubject" );
        f.setAccessible( true );
        f.set( ExtendedEmailPublisher.DESCRIPTOR, "Nigerian needs your help!" );

        f = ExtendedEmailPublisherDescriptor.class.getDeclaredField( "recipientList" );
        f.setAccessible( true );
        f.set( ExtendedEmailPublisher.DESCRIPTOR, "ashlux@gmail.com" );
    }

    public void testTransformText_shouldExpand_$PROJECT_DEFAULT_CONTENT()
        throws IOException, InterruptedException
    {
        assertEquals(publisher.defaultContent, new ContentBuilder().transformText( "$PROJECT_DEFAULT_CONTENT", publisher, null,
                                                         mock( AbstractBuild.class ), listener ));
        assertEquals(publisher.defaultContent, new ContentBuilder().transformText( "${PROJECT_DEFAULT_CONTENT}", publisher, null,
                                                         mock( AbstractBuild.class ), listener ));
    }

    public void testTransformText_shouldExpand_$PROJECT_DEFAULT_SUBJECT()
        throws IOException, InterruptedException
    {
        assertEquals(publisher.defaultSubject, new ContentBuilder().transformText( "$PROJECT_DEFAULT_SUBJECT", publisher, null,
                                                         mock( AbstractBuild.class ), listener ));
        assertEquals(publisher.defaultSubject, new ContentBuilder().transformText( "${PROJECT_DEFAULT_SUBJECT}", publisher, null,
                                                         mock( AbstractBuild.class ), listener ));
    }
    
    
    public void testTransformText_shouldExpand_$DEFAULT_CONTENT()
        throws IOException, InterruptedException
    {
        assertEquals( ExtendedEmailPublisher.DESCRIPTOR.getDefaultBody(),
                        new ContentBuilder().transformText( "$DEFAULT_CONTENT", publisher, null,
                                                            mock( AbstractBuild.class ), listener ) );
        assertEquals( ExtendedEmailPublisher.DESCRIPTOR.getDefaultBody(),
                        new ContentBuilder().transformText( "${DEFAULT_CONTENT}", publisher, null,
                                                            mock( AbstractBuild.class ), listener ) );
    }

    public void testTransformText_shouldExpand_$DEFAULT_SUBJECT()
        throws IOException, InterruptedException
    {
        assertEquals( ExtendedEmailPublisher.DESCRIPTOR.getDefaultSubject(),
                        new ContentBuilder().transformText( "$DEFAULT_SUBJECT", publisher, null,
                                                            mock( AbstractBuild.class ), listener ) );
        assertEquals( ExtendedEmailPublisher.DESCRIPTOR.getDefaultSubject(),
                        new ContentBuilder().transformText( "${DEFAULT_SUBJECT}", publisher, null,
                                                            mock( AbstractBuild.class ), listener ) );
    }
    
    public void testTransformText_shouldExpand_$DEFAULT_RECIPIENT_LIST()
        throws IOException, InterruptedException
    {
        assertEquals( ExtendedEmailPublisher.DESCRIPTOR.getDefaultRecipients(),
                        new ContentBuilder().transformText( "$DEFAULT_RECIPIENTS", publisher, null,
                                                        mock( AbstractBuild.class ), listener ) );
        assertEquals( ExtendedEmailPublisher.DESCRIPTOR.getDefaultRecipients(),
                        new ContentBuilder().transformText( "${DEFAULT_RECIPIENTS}", publisher, null,
                                                        mock( AbstractBuild.class ), listener ) );
    }
    
    public void testTransformText_noNPEWithNullDefaultSubjectBody() throws NoSuchFieldException, IllegalAccessException
    {
        Field f = ExtendedEmailPublisherDescriptor.class.getDeclaredField( "defaultBody" );
        f.setAccessible( true );
        f.set( ExtendedEmailPublisher.DESCRIPTOR, null );
        f = ExtendedEmailPublisherDescriptor.class.getDeclaredField( "defaultSubject" );
        f.setAccessible( true );
        f.set( ExtendedEmailPublisher.DESCRIPTOR, null );
        assertEquals( "", new ContentBuilder().transformText( "$DEFAULT_SUBJECT", publisher, null, mock (AbstractBuild.class ), listener));
        assertEquals( "", new ContentBuilder().transformText( "$DEFAULT_CONTENT", publisher, null, mock (AbstractBuild.class ), listener));
    }
}

@@ -55,27 +55,15 @@
 son.
-model.AbstractBuild
+EnvVars
 ;%0D%0Ai
@@ -85,61 +85,21 @@
 del.
-FreeStyleBuild;%0D%0Aimport hudson.model.FreeStyleProject
+AbstractBuild
 ;%0D%0Ai
@@ -308,58 +308,8 @@
 se;%0D
-%0Aimport org.jvnet.hudson.test.recipes.WithPlugin;%0D
 %0A%0D%0Ai
@@ -565,16 +565,56 @@
 stener;%0D
+%0A    private AbstractBuild%3C?, ?%3E build;%0D
 %0A%0D%0A    @
@@ -1724,24 +1724,151 @@
 il.com%22 );%0D%0A
+        %0D%0A        build = mock(AbstractBuild.class);%0D%0A        when(build.getEnvironment(listener)).thenReturn(new EnvVars());%0D%0A
     %7D%0D%0A%0D%0A   
@@ -2186,35 +2186,13 @@
     
-mock( AbstractBuild.class )
+build
 , li
@@ -2395,35 +2395,13 @@
     
-mock( AbstractBuild.class )
+build
 , li
@@ -2743,35 +2743,13 @@
     
-mock( AbstractBuild.class )
+build
 , li
@@ -2952,35 +2952,13 @@
     
-mock( AbstractBuild.class )
+build
 , li
@@ -3349,35 +3349,13 @@
     
-mock( AbstractBuild.class )
+build
 , li
@@ -3606,35 +3606,13 @@
     
-mock( AbstractBuild.class )
+build
 , li
@@ -3997,35 +3997,13 @@
     
-mock( AbstractBuild.class )
+build
 , li
@@ -4257,35 +4257,13 @@
     
-mock( AbstractBuild.class )
+build
 , li
@@ -4661,35 +4661,13 @@
     
-mock( AbstractBuild.class )
+build
 , li
@@ -4923,35 +4923,13 @@
     
-mock( AbstractBuild.class )
+build
 , li
@@ -5554,35 +5554,13 @@
 ll, 
-mock (AbstractBuild.class )
+build
 , li
@@ -5673,35 +5673,13 @@
 ll, 
-mock (AbstractBuild.class )
+build
 , li

Fix mocking of AbstractBuild