/*
 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library 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.
 *
 */
package org.nuxeo.ecm.platform.preview.tests.service;

import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.nuxeo.ecm.core.api.Blob;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
import org.nuxeo.ecm.core.api.impl.blob.FileBlob;
import org.nuxeo.ecm.core.test.CoreFeature;
import org.nuxeo.ecm.platform.mimetype.MimetypeDetectionException;
import org.nuxeo.ecm.platform.mimetype.MimetypeNotFoundException;
import org.nuxeo.ecm.platform.preview.adapter.MimeTypePreviewer;
import org.nuxeo.ecm.platform.preview.adapter.PreviewAdapterManager;
import org.nuxeo.ecm.platform.preview.api.HtmlPreviewAdapter;
import org.nuxeo.runtime.api.Framework;
import org.nuxeo.runtime.test.runner.Deploy;
import org.nuxeo.runtime.test.runner.Features;
import org.nuxeo.runtime.test.runner.FeaturesRunner;

import com.google.inject.Inject;

/**
 * Test Preview adapter management service registration
 *
 * @author tiry
 *
 */
@RunWith(FeaturesRunner.class)
@Features({ CoreFeature.class })
@Deploy({ "org.nuxeo.ecm.platform.commandline.executor",
        "org.nuxeo.ecm.platform.convert",
        "org.nuxeo.ecm.platform.mimetype.api",
        "org.nuxeo.ecm.platform.mimetype.core",
        "org.nuxeo.ecm.platform.preview", "org.nuxeo.ecm.platform.dublincore" })
public class TestService {

    @Test
    public void testService() {
        PreviewAdapterManager pam = Framework.getLocalService(PreviewAdapterManager.class);
        assertNotNull(pam);
        MimeTypePreviewer previewer = pam.getPreviewer("text/html");
        assertNotNull(previewer);
    }

    @Test
    // disabled because failing under windows, see NXP-12666
    public void testLatin1() throws IOException, ClientException,
            MimetypeNotFoundException, MimetypeDetectionException {
        DocumentModel doc = repo.createDocumentModel("/", "fake", "File");
        doc = repo.createDocument(doc);
        checkLatin1(doc, "latin1.txt", "text/plain");
        checkLatin1(doc, "latin1.xhtml", "text/html");
        checkLatin1(doc, "latin1.csv", "text/csv");
    }

    @Inject
    protected CoreSession repo;

    protected final Pattern charsetPattern = Pattern.compile(String.format(
            ".*content=\"text/html;\\s*charset=(%s)\".*", charset()));

    protected static String charset() {
        return System.getProperty("os.name").toLowerCase().indexOf("win") >= 0 ? "windows-1252" : "UTF-8";
    }

    public void checkLatin1(DocumentModel doc, String name, String mtype) throws IOException, ClientException, MimetypeNotFoundException, MimetypeDetectionException {
        File file = new File(getClass().getResource("/" + name).getPath());
        Blob blob = new FileBlob(file);
        blob.setMimeType(mtype);
        doc.getAdapter(BlobHolder.class).setBlob(blob);
        HtmlPreviewAdapter adapter = doc.getAdapter(HtmlPreviewAdapter.class);
        Blob htmlBlob = adapter.getFilePreviewBlobs().get(0);
        String htmlContent = htmlBlob.getString();
        Matcher matcher = charsetPattern.matcher(htmlContent);
        Assert.assertThat(matcher.matches(), Matchers.is(true));
        String charset = matcher.group();
        if ("windows-1252".equals(charset)) {
            Assert.assertThat(htmlContent, Matchers.containsString("Test de pr&Atilde;&copy;visualisation avant rattachement"));
        } else {
            Assert.assertThat(htmlContent, Matchers.containsString("Test de prévisualisation avant rattachement"));
        }
     }
}

@@ -757,72 +757,8 @@
 ion;
-%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0Aim
@@ -810,16 +810,41 @@
 Assert;%0A
+import org.junit.Ignore;%0A
 import o
@@ -1964,24 +1964,16 @@
 ecutor%22,
-%0A       
  %22org.nu
@@ -2135,16 +2135,24 @@
 review%22,
+%0A       
  %22org.nu
@@ -2179,17 +2179,16 @@
 lincore%22
- 
 %7D)%0Apubli
@@ -2553,16 +2553,28 @@
 P-12666%0A
+    @Ignore%0A
     publ
@@ -2630,28 +2630,16 @@
 ception,
-%0A           
  Mimetyp
@@ -2981,20 +2981,16 @@
  @Inject
-%0A   
  protect
@@ -3015,310 +3015,8 @@
 o;%0A%0A
-    protected final Pattern charsetPattern = Pattern.compile(String.format(%0A            %22.*content=%5C%22text/html;%5C%5Cs*charset=(%25s)%5C%22.*%22, charset()));%0A%0A    protected static String charset() %7B%0A        return System.getProperty(%22os.name%22).toLowerCase().indexOf(%22win%22) %3E= 0 ? %22windows-1252%22 : %22UTF-8%22;%0A    %7D%0A%0A
     
@@ -3536,454 +3536,46 @@
     
-String htmlContent = htmlBlob.getString();%0A        Matcher matcher = charsetPattern.matcher(htmlContent);%0A        Assert.assertThat(matcher.matches(), Matchers.is(true));%0A        String charset = matcher.group();%0A        if (%22windows-1252%22.equals(charset)) %7B%0A            Assert.assertThat(htmlContent, Matchers.containsString(%22Test de pr&Atilde;&copy;visualisation avant rattachement%22));%0A        %7D else %7B%0A            Assert.assertThat(htmlContent
+Assert.assertThat(htmlBlob.getString()
 , Ma
@@ -3649,19 +3649,8 @@
 ));%0A
-        %7D%0A 
     

Revert "NXP-12666 adapt latin1 check for windows behavior"