package hudson.plugins.promoted_builds;

import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import hudson.model.Cause.UserCause;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.util.IOException2;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.IOException;
import java.util.List;

import static org.junit.Assert.assertTrue;

/**
 * @author Kohsuke Kawaguchi
 */
public class PromotedBuildActionTest {

    @Rule
    public JenkinsRule j = new JenkinsRule();

    @Test
    public void testDeletedPromotionProcess() throws Exception {
        FreeStyleProject p = j.createFreeStyleProject();
        JobPropertyImpl base = new JobPropertyImpl(p);
        p.addProperty(base);
        PromotionProcess foo = base.addProcess("foo");

        // promote a build
        FreeStyleBuild b1 = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
        foo.promote(b1,new UserCause(),new ManualPromotionBadge());

        // now delete the promotion process
        p.removeProperty(base);
        p.addProperty(base=new JobPropertyImpl(p));
        assertTrue(base.getActiveItems().isEmpty());

        // make sure that the page renders OK without any error
        HtmlPage page = j.createWebClient().getPage(p);
        List<?> candidates = page.getByXPath("//IMG");
        for (Object candidate : candidates) {
            if (!(candidate instanceof HtmlImage)) {
                continue;
            } 
            HtmlImage img = (HtmlImage)candidate;
            try {
                img.getHeight();
            } catch (IOException e) {
                throw new IOException2("Failed to load "+img.getSrcAttribute(),e);
            }
        }
    }
}

@@ -424,16 +424,61 @@
 .List;%0A%0A
+import static org.junit.Assert.assertEquals;%0A
 import s
@@ -1677,22 +1677,157 @@
     
-img.getHeight(
+assertEquals(%22Failed to load %22 + img.getSrcAttribute(),%0A                        200,%0A                        img.getWebResponse(true).getStatusCode()
 );%0A 
@@ -1889,28 +1889,30 @@
 row new 
-IOException2
+AssertionError
 (%22Failed
@@ -1921,17 +1921,19 @@
 o load %22
-+
+ + 
 img.getS
@@ -1949,10 +1949,8 @@
 te()
-,e
 );%0A 

fix download of svg images