<commit_before>/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2002-2016, 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.gce.image;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.util.logging.Logger;

import junit.textui.TestRunner;

import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.test.TestData;
import org.opengis.coverage.grid.Format;
import org.opengis.parameter.GeneralParameterValue;
import org.opengis.parameter.ParameterValueGroup;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.operation.TransformException;

/**
 * Test class for WorldImageWriter. This test tries to read, writer and re-read
 * successive images checking for errors.
 * 
 * @author <NAME>
 * @author rgould
 *
 *
 * @source $URL$
 */
public class WorldImageWriterTest extends WorldImageBaseTestCase {
	private final static String[] supportedFormat = new String[] { "tiff",
			"gif", "png", "bmp", "jpeg" };

	private Logger logger = org.geotools.util.logging.Logging.getLogger(WorldImageWriterTest.class
			.toString());

	/** The format for the image e will write. */
	//private String format;


	public WorldImageWriterTest(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		super.setUp();
		File testData = TestData.file(this, ".");
		new File(testData,"write").mkdir();
	}
	/**
	 * This method simply read all the respecting a predefined pattern inside
	 * the testData directory and then it tries to read, write and re-read them
	 * back. All the possible errors are caught.
	 * 
	 * @throws MalformedURLException
	 * @throws IOException
	 * @throws IllegalArgumentException
	 * @throws FactoryException
	 * @throws TransformException
	 * @throws ParseException
	 * @throws ParseException
	 */
	public void testWrite() throws MalformedURLException, IOException,
			IllegalArgumentException, FactoryException, TransformException,
			ParseException {

		// checking test data directory for all kind of inputs
		final File test_data_dir = TestData.file(this, null);
		File output;
		final String[] fileList = test_data_dir.list(new MyFileFilter());
		for (String format : supportedFormat) {
			final StringBuffer buff = new StringBuffer("Format is ").append(
					format).append("\n");
			for( String filePath : fileList ){
				buff.append(" Testing ability to write ").append(filePath);
				// url
				final URL url = TestData.getResource(this, filePath);
				assertTrue(url != null);
				output = this.write(url, format);
				buff.append(" as url ").append(filePath).append(output.getName());

				// getting file
				final File file = TestData.file(this, filePath);
				assertTrue(file != null);
				// starting write test
				output = this.write(file, format);
				buff.append(" and file ").append(filePath).append(output.getName()+"\n");
			}
			logger.info(buff.toString());
		}
	}

	/**
	 * This method is responsible for loading the provided source object as a
	 * cverage then for writing it on the temp directoy and finally for
	 * rereading the coverage back into memory in order to display it.
	 * 
	 * @param source
	 *            Object The object on disk representing the coverage to test.
	 * 
	 * @throws IOException
	 * @throws IllegalArgumentException
	 * @throws FactoryException
	 * @throws TransformException
	 * @throws ParseException
	 */
	private File write(Object source, String format) throws IOException,
			IllegalArgumentException, FactoryException, TransformException,
			ParseException {
		// instantiating a reader
		WorldImageReader wiReader = new WorldImageReader(source);

		// reading the original coverage
		GridCoverage2D coverage = (GridCoverage2D) wiReader.read(null);

		assertNotNull(coverage);
		assertNotNull(coverage.getRenderedImage());
		assertNotNull(coverage.getEnvelope());

		// remember to provide a valid name, it wil be mde unique by the helper
		// function temp
		final StringBuffer buff = new StringBuffer("./write/temp").append(".").append(format);
		final File tempFile =TestData.temp(this, buff.toString());


		// getting a writer
		final WorldImageWriter wiWriter = new WorldImageWriter(tempFile);

		// writing parameters for png
		final Format writerFormat = wiWriter.getFormat();
		

		// setting write parameters
		final ParameterValueGroup params = writerFormat.getWriteParameters();
		params.parameter(WorldImageFormat.FORMAT.getName().toString())
				.setValue(format);
		final GeneralParameterValue[] gpv = { params
				.parameter(WorldImageFormat.FORMAT.getName().toString()) };
		// writing
		wiWriter.write(coverage, gpv);
        wiWriter.dispose();

		// reading again
		assertTrue(tempFile.exists());
		wiReader = new WorldImageReader(tempFile);
		coverage = (GridCoverage2D) wiReader.read(null);

		// displaying the coverage
		if (TestData.isInteractiveTest())
			coverage.show();
		else
			coverage.getRenderedImage().getData();
		wiReader.dispose();
        coverage.dispose(true);
        
            return tempFile;
	}

    

	/**
	 * TestRunner for testing inside a java application. It gives us the ability
	 * to keep windows open to inspect what happened.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		TestRunner.run(WorldImageWriterTest.class);
	}
}<commit_msg>Whitespace cleanup<commit_after>/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2002-2016, 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.gce.image;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.util.logging.Logger;

import junit.textui.TestRunner;

import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.test.TestData;
import org.geotools.util.logging.Logging;
import org.opengis.coverage.grid.Format;
import org.opengis.parameter.GeneralParameterValue;
import org.opengis.parameter.ParameterValueGroup;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.operation.TransformException;

/**
 * Test class for WorldImageWriter. This test tries to read, writer and re-read
 * successive images checking for errors.
 * 
 * @author <NAME>
 * @author rgould
 *
 * @source $URL$
 */
public class WorldImageWriterTest extends WorldImageBaseTestCase {
    private final static String[] supportedFormat = new String[] { "tiff",
            "gif", "png", "bmp", "jpeg" };

    private Logger logger = Logging.getLogger(WorldImageWriterTest.class);

    public WorldImageWriterTest(String name) {
        super(name);
    }

    protected void setUp() throws Exception {
        super.setUp();
        File testData = TestData.file(this, ".");
        new File(testData,"write").mkdir();
    }
    /**
     * This method simply read all the respecting a predefined pattern inside
     * the testData directory and then it tries to read, write and re-read them
     * back. All the possible errors are caught.
     * 
     * @throws MalformedURLException
     * @throws IOException
     * @throws IllegalArgumentException
     * @throws FactoryException
     * @throws TransformException
     * @throws ParseException
     * @throws ParseException
     */
    public void testWrite() throws MalformedURLException, IOException,
            IllegalArgumentException, FactoryException, TransformException,
            ParseException {

        // checking test data directory for all kind of inputs
        final File test_data_dir = TestData.file(this, null);
        File output;
        final String[] fileList = test_data_dir.list(new MyFileFilter());
        for (String format : supportedFormat) {
            final StringBuffer buff = new StringBuffer("Format is ").append(
                    format).append("\n");
            for( String filePath : fileList ){
                buff.append(" Testing ability to write ").append(filePath);
                // url
                final URL url = TestData.getResource(this, filePath);
                assertTrue(url != null);
                output = this.write(url, format);
                buff.append(" as url ").append(filePath).append(output.getName());

                // getting file
                final File file = TestData.file(this, filePath);
                assertTrue(file != null);
                // starting write test
                output = this.write(file, format);
                buff.append(" and file ").append(filePath).append(output.getName()+"\n");
            }
            logger.info(buff.toString());
        }
    }

    /**
     * This method is responsible for loading the provided source object as a
     * cverage then for writing it on the temp directoy and finally for
     * rereading the coverage back into memory in order to display it.
     * 
     * @param source
     *            Object The object on disk representing the coverage to test.
     * 
     * @throws IOException
     * @throws IllegalArgumentException
     * @throws FactoryException
     * @throws TransformException
     * @throws ParseException
     */
    private File write(Object source, String format) throws IOException,
            IllegalArgumentException, FactoryException, TransformException,
            ParseException {
        // instantiating a reader
        WorldImageReader wiReader = new WorldImageReader(source);

        // reading the original coverage
        GridCoverage2D coverage = (GridCoverage2D) wiReader.read(null);

        assertNotNull(coverage);
        assertNotNull(coverage.getRenderedImage());
        assertNotNull(coverage.getEnvelope());

        // remember to provide a valid name, it wil be mde unique by the helper
        // function temp
        final StringBuffer buff = new StringBuffer("./write/temp").append(".").append(format);
        final File tempFile =TestData.temp(this, buff.toString());


        // getting a writer
        final WorldImageWriter wiWriter = new WorldImageWriter(tempFile);

        // writing parameters for png
        final Format writerFormat = wiWriter.getFormat();
        

        // setting write parameters
        final ParameterValueGroup params = writerFormat.getWriteParameters();
        params.parameter(WorldImageFormat.FORMAT.getName().toString())
                .setValue(format);
        final GeneralParameterValue[] gpv = { params
                .parameter(WorldImageFormat.FORMAT.getName().toString()) };
        // writing
        wiWriter.write(coverage, gpv);
        wiWriter.dispose();

        // reading again
        assertTrue(tempFile.exists());
        wiReader = new WorldImageReader(tempFile);
        coverage = (GridCoverage2D) wiReader.read(null);

        // displaying the coverage
        if (TestData.isInteractiveTest())
            coverage.show();
        else
            coverage.getRenderedImage().getData();
        wiReader.dispose();
        coverage.dispose(true);
        
        return tempFile;
    }

    

    /**
     * TestRunner for testing inside a java application. It gives us the ability
     * to keep windows open to inspect what happened.
     * 
     * @param args
     */
    public static void main(String[] args) {
        TestRunner.run(WorldImageWriterTest.class);
    }
}
{'fraction_non_alphanumeric': 0.09526879987104055, 'fraction_numerical': 0.0022567905214798097, 'mean_word_length': 3.0708661417322833, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7663982', 'n_tokens_mistral': 3483, 'n_tokens_neox': 3225, 'n_words': 1029}