/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.webdav.simple;

import java.io.ByteArrayOutputStream;
import java.io.File;

import javax.jcr.Repository;
import javax.jcr.Session;

import junit.framework.TestCase;

import org.apache.commons.io.IOUtils;
import org.apache.jackrabbit.commons.JcrUtils;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LitmusTest extends TestCase {

    /**
     * Logger instance.
     */
    private static final Logger log = LoggerFactory.getLogger(LitmusTest.class);

    private static String litmus = System.getProperty("litmus", "litmus");

    private static boolean litmusIsAvailable = isLitmusAvailable(litmus);

    public void testBasic() throws Exception {
        assertLitmus("basic", 0);
    }

    public void testHttp() throws Exception {
        assertLitmus("http", 0);
    }

    public void testProps() throws Exception {
        assertLitmus("props", 0);
    }

    public void testCopyMove() throws Exception {
        // FIXME: JCR-2637: WebDAV shallow copy test failure
        assertLitmus("copymove", 1);
    }

    public void testLocks() throws Exception {
        // FIXME: JCR-2638: Litmus locks test failures
        assertLitmus("locks", 1);
    }

    private void assertLitmus(String tests, int exit) throws Exception {
        File dir = new File("target", "litmus");

        if (Boolean.getBoolean("jackrabbit.test.integration")
                && litmusIsAvailable) {
            final Repository repository = JcrUtils.getRepository(
                    "jcr-jackrabbit://" + dir.getCanonicalPath());
            Session session = repository.login(); // for the TransientRepository
            try {
                SocketConnector connector = new SocketConnector();
                connector.setHost("localhost");
                connector.setPort(Integer.getInteger("litmus.port", 0));

                Server server = new Server();
                server.addConnector(connector);

                ServletHolder holder = new ServletHolder(
                        new SimpleWebdavServlet() {
                            @Override
                            public Repository getRepository() {
                                return repository;
                            }
                        });
                holder.setInitParameter("resource-config", "/config.xml");

                Context context = new Context(server, "/");
                context.setResourceBase("src/test/resources");
                context.addServlet(holder, "/*");
                server.addHandler(context);

                server.start();
                try {
                    int port = connector.getLocalPort();
                    String url = "http://localhost:" + port + "/default";

                    ProcessBuilder builder =
                        new ProcessBuilder(litmus, url, "admin", "admin");
                    builder.environment().put("TESTS", tests);
                    builder.directory(dir);
                    builder.redirectErrorStream();

                    Process process = builder.start();
                    IOUtils.copy(process.getInputStream(), System.out);
                    assertEquals(exit, process.waitFor());
                } finally {
                    server.stop();
                }
            } finally {
                session.logout();
            }
        }
    }

    private static boolean isLitmusAvailable(String litmus) {
        try {
            ProcessBuilder builder = new ProcessBuilder(litmus, "--version");
            builder.redirectErrorStream();
            Process process = builder.start();

            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            IOUtils.copy(process.getInputStream(), buffer);
            int rv = process.waitFor();
            log.info("litmus version: {}", buffer.toString("US-ASCII").trim());

            return rv == 0;
        } catch (Exception e) {
            log.warn("litmus is not available: " + litmus, e);
            return false;
        }
    }

}

@@ -1479,822 +1479,150 @@
    p
-rivate static String litmus = System.getProperty(%22litmus%22, %22litmus%22);%0A%0A    private static boolean litmusIsAvailable = isLitmusAvailable(litmus);%0A%0A    public void testBasic() throws Exception %7B%0A        assertLitmus(%22basic%22, 0);%0A    %7D%0A%0A    public void testHttp() throws Exception %7B%0A        assertLitmus(%22http%22, 0);%0A    %7D%0A%0A    public void testProps() throws Exception %7B%0A        assertLitmus(%22props%22, 0);%0A    %7D%0A%0A    public void testCopyMove() throws Exception %7B%0A        // FIXME: JCR-2637: WebDAV shallow copy test failure%0A        assertLitmus(%22copymove%22, 1
+ublic void testLitmus() throws Exception %7B%0A        File dir = new File(%22target%22, %22litmus%22
 );%0A    
-%7D%0A%0A
     
-public void testLocks() throws Exception %7B%0A        // FIXME: JCR-2638: Litmus locks test failures%0A        assertLitmus(%22locks%22, 1);%0A    %7D%0A%0A    private void assertLitmus(String tests, int exit) throws Exception %7B%0A        File dir = new File(%22target
+String litmus = System.getProperty(%22litmus
 %22, %22
@@ -1717,16 +1717,16 @@
  && 
-l
+isL
 itmus
-Is
 Avai
@@ -1730,16 +1730,24 @@
 vailable
+(litmus)
 ) %7B%0A    
@@ -3211,40 +3211,21 @@
 der.
-environment().put(%22TESTS%22, tests
+directory(dir
 );%0A 
@@ -3251,32 +3251,40 @@
 builder.
+re
 direct
-ory(dir
+ErrorStream(
 );%0A
+%0A
         
@@ -3299,95 +3299,155 @@
     
-builder.redirectErrorStream();%0A%0A                    Process process = builder.start(
+assertLitmus(builder, %22basic%22, 0);%0A%0A                    assertLitmus(builder, %22http%22, 0);%0A%0A                    assertLitmus(builder, %22props%22, 0
 );%0A
+%0A
     
@@ -3466,60 +3466,120 @@
     
-IOUtils.copy(process.getInputStream(), System.out
+// FIXME: JCR-2637: WebDAV shallow copy test failure%0A                    assertLitmus(builder, %22copymove%22, 1
 );%0A
+%0A
     
@@ -3598,44 +3598,107 @@
     
-assertEquals(exit, process.waitFor()
+// FIXME: JCR-2638: Litmus locks test failures%0A                    assertLitmus(builder, %22locks%22, 1
 );%0A 
@@ -3866,16 +3866,334 @@
 %0A    %7D%0A%0A
+    private void assertLitmus(%0A            ProcessBuilder builder, String tests, int exit) throws Exception %7B%0A        builder.environment().put(%22TESTS%22, tests);%0A        Process process = builder.start();%0A        IOUtils.copy(process.getInputStream(), System.out);%0A        assertEquals(exit, process.waitFor());%0A    %7D%0A%0A
     priv

Include the WebDAV litmus tests in the Jackrabbit integration tests