/*
 * (C) Copyright 2006-2009 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.
 *
 * Contributors:
 *     Nuxeo - initial API and implementation
 *
 * $Id$
 */

package org.nuxeo.connect.download.tests;

import java.util.ArrayList;
import java.util.List;


import org.junit.runner.RunWith;
import org.junit.Test;
import static org.junit.Assert.*;

import org.nuxeo.connect.NuxeoConnectClient;
import org.nuxeo.connect.data.DownloadingPackage;
import org.nuxeo.connect.data.PackageDescriptor;
import org.nuxeo.connect.downloads.ConnectDownloadManager;
import org.nuxeo.connect.update.Version;
import org.nuxeo.runtime.test.runner.Features;
import org.nuxeo.runtime.test.runner.FeaturesRunner;
import org.nuxeo.runtime.test.runner.Jetty;


@RunWith(FeaturesRunner.class)
@Features(DownloadFeature.class)
@Jetty(port = 8082)
public class TestDownloader {

    @Test
    public void testSimpleDownload() throws Exception {
        ConnectDownloadManager cdm = NuxeoConnectClient.getDownloadManager();
        assertNotNull(cdm);

        int nbDownloads = 5;
        int maxLoop = 40;

        List<PackageDescriptor> pkgToDownload = new ArrayList<PackageDescriptor>();
        for (int i = 0; i < nbDownloads; i++) {
            PackageDescriptor pkg = new PackageDescriptor();
            pkg.setSourceUrl("test" + i);
            pkg.setName("FakePackage-" + i);
            pkg.setVersion(new Version(1));
            pkgToDownload.add(pkg);
        }

        List<DownloadingPackage> downloads = new ArrayList<DownloadingPackage>();

        for (PackageDescriptor pkg : pkgToDownload) {
            DownloadingPackage lpkg = cdm.storeDownloadedBundle(pkg);
            assertNotNull(lpkg);
            downloads.add(lpkg);
            Thread.sleep(100);
        }

        boolean downloadInProgress = true;

        int nbLoop = 0;
        while (downloadInProgress) {
            downloadInProgress = false;
            for (DownloadingPackage pkg : downloads) {
                if (pkg.isCompleted()) {
                    System.out.print(pkg.getId() + ":complete  - ");
                } else {
                    downloadInProgress = true;
                    System.out.print(pkg.getId() + ":in progress - ");
                }
            }
            System.out.println();
            nbLoop++;
            if (nbLoop > maxLoop) {
                throw new RuntimeException("Download is stuck");
            }
            Thread.sleep(500);
        }
    }

}

@@ -24,10 +24,10 @@
 6-20
-09
+12
  Nux
@@ -31,17 +31,16 @@
 Nuxeo SA
-S
  (http:/
@@ -731,29 +731,48 @@
 ort 
-java.util.ArrayList
+static org.junit.Assert.assertNotNull
 ;%0A
+%0A
 impo
@@ -788,48 +788,42 @@
 til.
+Array
 List;%0A
-%0A%0A
 import 
-org.junit.runner.RunWith;
+java.util.List;%0A
 %0Aimp
@@ -841,39 +841,32 @@
 it.Test;%0Aimport 
-static 
 org.junit.Assert
@@ -863,18 +863,23 @@
 nit.
-Assert.*;%0A
+runner.RunWith;
 %0Aimp
@@ -2181,16 +2181,24 @@
 dBundle(
+%22fake%22, 
 pkg);%0A  

Align test on interface change