Package geodress.tests

Source Code of geodress.tests.SanselanWriterTest

/**
* GeoDress - A program for reverse geocoding
* Copyright (C) 2010  Stefan T.
*
* See COPYING for Details.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package geodress.tests;

import geodress.main.FileOperations;
import geodress.main.InfoConstants;
import geodress.model.reader.SanselanReader;
import geodress.model.writer.SanselanWriter;

import java.io.File;
import java.io.IOException;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
* Tests the {@link SanselanWriter} class.<br>
* Attention: This class uses the {@link SanselanReader} class to assert the
* results.
*
* @author Stefan T.
*/
public class SanselanWriterTest {

  /** test picture with EXIF data */
  private File testFileBeach;
  /** test picture with EXIF data and user comment */
  private File testFileGate;
  /** test picture with EXIF data and image description */
  private File testFileSky;
  /** a test writer */
  private SanselanWriter writer;
  /** a reader to validate test writings */
  private SanselanReader reader;

  /**
   * Initializes the objects and makes copies of the test files that could be
   * modified.
   *
   * @throws java.lang.Exception
   */
  @Before
  public void setUp() throws Exception {
    /* beach.jpg */
    File testPictureWithExifBeach = new File("testfiles" + File.separator
        + "beach.jpg");
    testFileBeach = File.createTempFile("geodress.tests.ExifWriterTest_"
        + System.currentTimeMillis(), ".jpg");
    FileOperations.copy(testPictureWithExifBeach, testFileBeach);

    /* gate.jpg */
    File testPictureWithExifGate = new File("testfiles" + File.separator
        + "gate.jpg");
    testFileGate = File.createTempFile("geodress.tests.ExifWriterTest_"
        + System.currentTimeMillis(), ".jpg");
    FileOperations.copy(testPictureWithExifGate, testFileGate);

    /* sky.jpg */
    File testPictureWithExifSky = new File("testfiles" + File.separator
        + "sky.jpg");
    testFileSky = File.createTempFile("geodress.tests.ExifWriterTest_"
        + System.currentTimeMillis(), ".jpg");
    FileOperations.copy(testPictureWithExifSky, testFileSky);

    writer = new SanselanWriter();
    reader = new SanselanReader();
  }

  /**
   * Makes a writing test for comments.
   *
   * @throws Exception
   *             may cause an exception
   */
  @Test
  public void testCommentWriting() throws Exception {
    writer.setData(InfoConstants.USER_COMMENT, "foo");
    writer.write(testFileBeach);

    reader.setFile(testFileBeach);
    Assert.assertEquals("foo", reader.getData(InfoConstants.USER_COMMENT));
  }

  /**
   * Makes a writing test for comments when there's already a comment saved.
   *
   * @throws Exception
   *             may cause an exception
   */
  @Test
  public void testExistingCommentWriting() throws Exception {
    reader.setFile(testFileGate);
    Assert.assertEquals("A gate on Mallorca.", reader
        .getData(InfoConstants.USER_COMMENT));

    writer.setData(InfoConstants.USER_COMMENT, "Hey ho");
    writer.write(testFileGate);

    reader.setFile(testFileGate);
    Assert.assertEquals("Hey ho", reader
        .getData(InfoConstants.USER_COMMENT));
  }

  /**
   * Makes a writing test for description.
   *
   * @throws Exception
   *             may cause an exception
   */
  @Test
  public void testDescriptionWriting() throws Exception {
    writer.setData(InfoConstants.IMAGE_DESCRIPTION, "text");
    writer.write(testFileBeach);

    reader.setFile(testFileBeach);
    Assert.assertEquals("text", reader
        .getData(InfoConstants.IMAGE_DESCRIPTION));
  }

  /**
   * Makea a writing test for description with special characters.
   *
   * @throws Exception
   *             may cause an exception
   */
  @Test
  public void testDescriptionSpecialWriting() throws Exception {
    String[] texts = new String[] { "-", ",", ".", "_", "?", "!", "'",
        "*", "^", "(", ")", "/" };
    for (String testString : texts) {
      writer.setData(InfoConstants.IMAGE_DESCRIPTION, testString);
      writer.write(testFileBeach);

      reader.setFile(testFileBeach);
      Assert.assertEquals(testString, reader
          .getData(InfoConstants.IMAGE_DESCRIPTION));
    }
  }

  /**
   * Makes a writing test for description when there's already a description
   * saved.
   *
   * @throws Exception
   *             may cause an exception
   */
  @Test
  public void testExistingDescriptionWriting() throws Exception {
    reader.setFile(testFileSky);
    Assert.assertEquals("Simply blue sky", reader
        .getData(InfoConstants.IMAGE_DESCRIPTION));

    writer.setData(InfoConstants.IMAGE_DESCRIPTION,
        "qc3b4.q.35cb..45 z83. v23 8Ztoe5-z.iz");
    writer.write(testFileSky);

    reader.setFile(testFileSky);
    Assert.assertEquals("qc3b4.q.35cb..45 z83. v23 8Ztoe5-z.iz", reader
        .getData(InfoConstants.IMAGE_DESCRIPTION));
  }

  /** Deletes the temporary files after testing. */
  @After
  public void deleteFiles() {
    if (!(testFileBeach.delete() && testFileGate.delete() && testFileSky
        .delete())) {
      System.err.println("temp files could not be deleted");
    }
  }

  /**
   * Writes to a picture that doesn't exist.
   *
   * @throws Exception
   *             may cause an exception
   */
  @Test(expected = IOException.class)
  public void testPictureWriteFail() throws Exception {
    writer.write(new File("testfiles" + File.separator + "foo.jpg"));
  }
}
TOP

Related Classes of geodress.tests.SanselanWriterTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.