/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2004-2010, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library 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;
 *    version 2.1 of the License.
 *
 *    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.geotools.xml;

import java.io.File;
import java.net.URI;

import org.geotools.data.DataUtilities;
import org.geotools.test.OnlineTestSupport;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * Online test for {@link AppSchemaCatalog}.
 * 
 * @author Ben Caradoc-Davies, CSIRO Earth Science and Resource Engineering
 */
public class AppSchemaCacheOnlineTest extends OnlineTestSupport {

    /**
     * Test cache built here.
     */
    private static final File CACHE_DIRECTORY = new File("target/app-schema-cache");

    /**
     * Schema that is downloaded.
     */
    private static final String SCHEMA_LOCATION = "http://www.geosciml.org/geosciml/2.0/xsd/geosciml.xsd";

    /**
     * Filename of the schema.
     */
    private static final String SCHEMA_FILENAME;

    static {
        String[] parts = SCHEMA_LOCATION.split("/");
        SCHEMA_FILENAME = parts[parts.length - 1];
    }

    /**
     * @see org.geotools.test.OnlineTestSupport#getFixtureId()
     */
    @Override
    protected String getFixtureId() {
        return "app-schema-resolver";
    }

    /**
     * @see org.geotools.test.OnlineTestSupport#before()
     */
    @Before
    @Override
    public void before() throws Exception {
        super.before();
        AppSchemaCache.delete(CACHE_DIRECTORY);
    }

    /**
     * @see org.geotools.test.OnlineTestSupport#after()
     */
    @After
    @Override
    public void after() throws Exception {
        super.after();
        AppSchemaCache.delete(CACHE_DIRECTORY);
    }

    /**
     * Test download of schema via http.
     */
    @Test
    public void downloadHttp() throws Exception {
        check(AppSchemaCache.download(new URI(SCHEMA_LOCATION)));
    }

    /**
     * Test download of schema via http with smaller block size.
     */
    @Test
    public void downloadHttpWithSmallBlockSize() throws Exception {
        check(AppSchemaCache.download(new URI(SCHEMA_LOCATION), 32));

    }

    /**
     * Test download of schema via http with larger block size.
     */
    @Test
    public void downloadHttpWithLargeBlockSize() throws Exception {
        check(AppSchemaCache.download(new URI(
                "http://www.geosciml.org/geosciml/2.0/xsd/geosciml.xsd"), 65536));
    }

    /**
     * Test download of schema via https.
     */
    @Test
    public void downloadHttps() throws Exception {
        check(AppSchemaCache.download(new URI("https://www.seegrid.csiro.au/"
                + "subversion/GeoSciML/trunk/schema/GeoSciML/geosciml.xsd")));
    }
    /**
     * Basic sanity checks of schema and test store to disk.
     */
    private void check(byte[] bytes) {
        Assert.assertTrue(bytes.length > 0);
        String text = new String(bytes);
        Assert.assertTrue(text.contains("GeoSciML"));
        Assert.assertTrue(text.contains("<schema"));
        Assert.assertTrue(text.contains("</schema>"));
        File cachedFile = new File("target/test/test.xsd");
        AppSchemaCache.delete(cachedFile);
        Assert.assertFalse(cachedFile.exists());
        AppSchemaCache.store(cachedFile, bytes);
        Assert.assertTrue(cachedFile.exists());
        Assert.assertEquals(bytes.length, cachedFile.length());
    }

    @Test
    public void cache() throws Exception {
        // expect failure when downloading disabled
        {
            AppSchemaCache cache = new AppSchemaCache(CACHE_DIRECTORY, false);
            String location = cache.resolveLocation(SCHEMA_LOCATION);
            Assert.assertNull(location);
        }
        // should succeed if able to download
        {
            AppSchemaCache cache = new AppSchemaCache(CACHE_DIRECTORY, true);
            String location = cache.resolveLocation(SCHEMA_LOCATION);
            Assert.assertNotNull(location);
            Assert.assertTrue(location.startsWith("file:"));
            Assert.assertTrue(location.endsWith(SCHEMA_FILENAME));
            Assert.assertTrue(DataUtilities.urlToFile((new URI(location)).toURL()).exists());
        }
        // now that schema is is in the cache, should succeed even if downloading is disabled
        {
            AppSchemaCache cache = new AppSchemaCache(CACHE_DIRECTORY, false);
            String location = cache.resolveLocation(SCHEMA_LOCATION);
            Assert.assertNotNull(location);
            Assert.assertTrue(location.startsWith("file:"));
            Assert.assertTrue(location.endsWith(SCHEMA_FILENAME));
            Assert.assertTrue(DataUtilities.urlToFile((new URI(location)).toURL()).exists());
        }
    }

}

@@ -3136,17 +3136,16 @@
 csiro.au
-/
 %22%0A      
@@ -3157,16 +3157,17 @@
      + %22
+/
 subversi
@@ -3183,12 +3183,17 @@
 ML/t
-runk
+ags/2.0.0
 /sch
@@ -3225,24 +3225,25 @@
 %22)));%0A    %7D%0A
+%0A
     /**%0A    

Fix online https schema download test broken by server path change