/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.test.misc;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.commons.io.IOUtils;

import junit.framework.TestCase;

public class HTMLExportTest extends TestCase
{
    /**
     * Verify that the HTML export feature works on a single simple page by downloading the generated Zip.
     */
    public void testHTMLExportCurrentPage() throws Exception
    {
        assertHTMLExportURL("http://localhost:8080/xwiki/bin/export/Main/WebHome?format=html");
    }

    /**
     * Verify that the HTML export feature works on a passed patern instead of an explicit page name or the current page.
     */
    public void testHTMLExportPattern() throws Exception
    {
        assertHTMLExportURL(
            "http://localhost:8080/xwiki/bin/export/UnexistingSpace/UnexistingPage?format=html&pages=Main.WebH%25");
    }

    private void assertHTMLExportURL(String htmlExportURL) throws Exception
    {
        URL url = new URL(htmlExportURL);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        InputStream is = connection.getInputStream();
        ZipInputStream zis = new ZipInputStream(is);

        boolean foundWebHome = false;
        boolean foundResourcesDirectory = false;
        boolean foundAttachmentDirectory = false;
        boolean foundSkinsDirectory = false;
        boolean foundColibriCSS = false;

        // We must read the full stream as otherwise if we close it before we've fully read it
        // then the server side will get a broken pipe since it's still trying to send data on it.
        for (ZipEntry entry; (entry = zis.getNextEntry()) != null; zis.closeEntry()) {
            if (entry.getName().equals("xwiki.Main.WebHome.html")) {
                String content = IOUtils.toString(zis);

                // Verify that the content was rendered properly
                assertTrue("Should have contained 'Welcome to your wiki'", content.contains("Welcome to your wiki"));

                // Ensure that the translations have been rendered properly
                assertFalse("$msg should have been expanded", content.contains("$msg"));

                foundWebHome = true;
            } else if (entry.getName().startsWith("resources/")) {
                foundResourcesDirectory = true;
                IOUtils.readLines(zis);
            } else if (entry.getName().startsWith("skins/")) {
                foundSkinsDirectory = true;
                // Verify that the skin is correctly going to be applied by verifyin the colibri.css file is found
                // and is correctly referenced. This fixes http://jira.xwiki.org/browse/XWIKI-9145
                if (entry.getName().equals("skins/flamingo/style.css")) {
                    assertSkinIsActive(IOUtils.readLines(zis));
                    foundColibriCSS = true;
                } else {
                    IOUtils.readLines(zis);
                }
            } else if (entry.getName().startsWith("attachment/")) {
                foundAttachmentDirectory = true;
                IOUtils.readLines(zis);
            } else {
                IOUtils.readLines(zis);
            }
        }

        assertTrue("Failed to find the xwiki.Main.WebHome.html entry", foundWebHome);
        assertTrue("Failed to find the resources/ directory entry", foundResourcesDirectory);
        assertTrue("Failed to find the skins/ directory entry", foundSkinsDirectory);
        assertTrue("Failed to find the attachment/ directory entry", foundAttachmentDirectory);
        assertTrue("Failed to find the link to colibri.css in style.css", foundColibriCSS);

        zis.close();
    }

    private void assertSkinIsActive(List<String> content) throws Exception
    {
        // Verify that the link to the Skin CSS (i.e. colibri.css) is relative and leads to
        // skins/colibri/colibri.css
        // Note that the reason we have "../.." is because at runtime the path is going to be resolved relative to
        // the style.css file (from where it's referenced) and since style.css is in skins/colibri and colibri.css is
        // also in that directory, we need the "../..." to point to the right location...
        assertTrue("style.css doesn't contain a link to the colibri.css file",
            content.contains("@import \"../../skins/colibri/colibri.css\";"));
    }
}

@@ -1093,16 +1093,50 @@
 tream;%0A%0A
+import junit.framework.TestCase;%0A%0A
 import o
@@ -1169,40 +1169,51 @@
 ls;%0A
-%0A
 import 
-junit.framework.TestCase
+org.apache.commons.lang3.StringUtils
 ;%0A%0Ap
@@ -1623,16 +1623,17 @@
 ssed pat
+t
 ern inst
@@ -1675,16 +1675,23 @@
  current
+%0A     *
  page.%0A 
@@ -1788,29 +1788,16 @@
 portURL(
-%0A            
 %22http://
@@ -3140,62 +3140,132 @@
 e(%22$
-msg should have been expanded%22, content.contains(%22$msg
+services.localization.render should have been expanded%22,%0A                    content.contains(%22$services.localization.render
 %22));
@@ -3653,32 +3653,59 @@
 fyin
+g
  the 
-colibri.css file is
+flamingo/style.css file is%0A                //
  fou
@@ -4894,530 +4894,112 @@
     
-// Verify that the link to the Skin CSS (i.e. colibri.css) is relative and leads to%0A        // skins/colibri/colibri.css%0A        // Note that the reason we have %22../..%22 is because at runtime the path is going to be resolved relative to%0A        // the style.css file (from where it's referenced) and
+assertTrue(%22style.css is not the one outputed by the flamingo
  s
+k
 in
-ce style.css is in skins/colibri and colibri.css is%0A        // also in that directory, we need the %22../...%22 to point to the right location...%0A        assertTrue(%22style.css doesn't contain a link to the colibri.css file%22,
+%22, StringUtils.join(content.toArray())
 %0A   
@@ -4999,39 +4999,32 @@
 ())%0A            
-content
 .contains(%22@impo
@@ -5022,52 +5022,21 @@
 ns(%22
-@import %5C%22../../skins/colibri/colibri.css%5C%22;
+skin-flamingo
 %22));

Switch to Flamingo Skin by default. - Fixed some tests.