/*
 * The MIT License
 *
 * Copyright 2015 James Nord.
 *
 * 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 hudson.model;

import hudson.model.FreeStyleProject;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.IsNull.nullValue;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

public class AbstractItemTest {

    @Rule
    public JenkinsRule jenkinsRule = new JenkinsRule();

    @Test()
    // SECURITY-167
    public void testUpdateByXmlIDoesNotProcessForeignResources() throws Exception {
        final String xml = "<?xml version='1.0' encoding='UTF-8'?>\n" +
                "<!DOCTYPE project[\n" +
                "  <!ENTITY foo SYSTEM \"file:///\">\n" +
                "]>\n" +
                "<project>\n" +
                "  <actions/>\n" +
                "  <description>&foo;</description>\n" +
                "  <keepDependencies>false</keepDependencies>\n" +
                "  <properties/>\n" +
                "  <scm class=\"hudson.scm.NullSCM\"/>\n" +
                "  <canRoam>true</canRoam>\n" +
                "  <triggers/>\n" +
                "  <builders/>\n" +
                "  <publishers/>\n" +
                "  <buildWrappers/>\n" +
                "</project>";

        FreeStyleProject project = jenkinsRule.createFreeStyleProject("security-167");

        try {
            project.updateByXml((StreamSource) new StreamSource(new StringReader(xml)));
            fail("Exception should have been thrown");
        } catch (IOException ex) {
            assertThat(ex.getCause(), not(nullValue()));
            assertThat(ex.getCause().getMessage(), containsString("Refusing to resolve entity"));
        }

    }


    @Test()
    // SECURITY-167
    public void testUpdateByXmlIDoesNotFail() throws Exception {
        final String xml = "<?xml version='1.0' encoding='UTF-8'?>\n" +
                "<project>\n" +
                "  <actions/>\n" +
                "  <description>&amp;</description>\n" +
                "  <keepDependencies>false</keepDependencies>\n" +
                "  <properties/>\n" +
                "  <scm class=\"hudson.scm.NullSCM\"/>\n" +
                "  <canRoam>true</canRoam>\n" +
                "  <triggers/>\n" +
                "  <builders/>\n" +
                "  <publishers/>\n" +
                "  <buildWrappers/>\n" +
                "</project>";

        FreeStyleProject project = jenkinsRule.createFreeStyleProject("security-167");
        project.updateByXml((StreamSource) new StreamSource(new StringReader(xml)));
        assertThat(project.getDescription(), is("&")); // the entity is transformed
    }

}

@@ -1857,16 +1857,83 @@
 String;%0A
+import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString;%0A
 import s
@@ -3023,16 +3023,57 @@
 -167%22);%0A
+        project.setDescription(%22Wibble%22);
 %0A       
@@ -3107,39 +3107,24 @@
 updateByXml(
-(StreamSource) 
 new StreamSo
@@ -3169,48 +3169,132 @@
     
-fail(%22Exception should have been thrown%22
+// if we didn't fail JAXP has thrown away the entity.%0A            assertThat(project.getDescription(), isEmptyOrNullString()
 );%0A 
@@ -3545,39 +3545,35 @@
 public void test
-UpdateB
+ham
 yXmlIDoesNotFail

fix failing unit test.