package com.drew.imaging.png;

import com.drew.lang.KeyValuePair;
import com.drew.lang.annotations.NotNull;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.png.PngDirectory;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import static org.junit.Assert.*;

/**
 * @author Drew Noakes https://drewnoakes.com
 */
public class PngMetadataReaderTest
{
    @NotNull
    public static <T extends Directory> T processFile(@NotNull String filePath, @NotNull Class<T> directoryClass) throws IOException, PngProcessingException
    {
        T directory = processFile(filePath).getDirectory(directoryClass);
        assertNotNull(directory);
        return directory;
    }

    @NotNull
    private static Metadata processFile(@NotNull String filePath) throws PngProcessingException, IOException
    {
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(filePath);
            return PngMetadataReader.readMetadata(inputStream);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }

    @Test
    public void testGimpGreyscaleWithManyChunks() throws Exception
    {
        PngDirectory directory = processFile("Tests/Data/gimp-8x12-greyscale-alpha-time-background.png", PngDirectory.class);

        assertEquals(8, directory.getInt(PngDirectory.TAG_IMAGE_WIDTH));
        assertEquals(12, directory.getInt(PngDirectory.TAG_IMAGE_HEIGHT));
        assertEquals(8, directory.getInt(PngDirectory.TAG_BITS_PER_SAMPLE));
        assertEquals(4, directory.getInt(PngDirectory.TAG_COLOR_TYPE));
        assertEquals(0, directory.getInt(PngDirectory.TAG_COMPRESSION_TYPE));
        assertEquals(0, directory.getInt(PngDirectory.TAG_FILTER_METHOD));
        assertEquals(0, directory.getInt(PngDirectory.TAG_INTERLACE_METHOD));
        assertEquals(0.45455, directory.getDouble(PngDirectory.TAG_GAMMA), 0.00001);
        assertArrayEquals(new byte[]{0, 52}, directory.getByteArray(PngDirectory.TAG_BACKGROUND_COLOR));
        //noinspection ConstantConditions
        assertEquals("Tue Jan 01 04:08:30 GMT 2013", directory.getDate(PngDirectory.TAG_LAST_MODIFICATION_TIME).toString());
        @SuppressWarnings("unchecked")
        List<KeyValuePair> pairs = (List<KeyValuePair>)directory.getObject(PngDirectory.TAG_TEXTUAL_DATA);
        assertNotNull(pairs);
        assertEquals(1, pairs.size());
        assertEquals("Comment", pairs.get(0).getKey());
        assertEquals("Created with GIMP", pairs.get(0).getValue());
    }
}

@@ -321,16 +321,43 @@
 il.List;
+%0Aimport java.util.TimeZone;
 %0A%0Aimport
@@ -1302,32 +1302,165 @@
 Exception%0A    %7B%0A
+        TimeZone timeZone = TimeZone.getDefault();%0A%0A        try %7B%0A            TimeZone.setDefault(TimeZone.getTimeZone(%22GMT%22));%0A%0A    
         PngDirec
@@ -1566,24 +1566,28 @@
 ry.class);%0A%0A
+    
         asse
@@ -1647,32 +1647,36 @@
 IDTH));%0A        
+    
 assertEquals(12,
@@ -1722,24 +1722,28 @@
 E_HEIGHT));%0A
+    
         asse
@@ -1807,32 +1807,36 @@
 MPLE));%0A        
+ 
+   
 assertEquals(4, 
@@ -1875,32 +1875,36 @@
 G_COLOR_TYPE));%0A
+    
         assertEq
@@ -1965,32 +1965,36 @@
 TYPE));%0A        
+  
+  
 assertEquals(0, 
@@ -2036,32 +2036,36 @@
 ILTER_METHOD));%0A
+    
         assertEq
@@ -2126,32 +2126,36 @@
 THOD));%0A        
+   
+ 
 assertEquals(0.4
@@ -2215,16 +2215,20 @@
 00001);%0A
+    
         
@@ -2332,16 +2332,20 @@
         
+   
+ 
 //noinsp
@@ -2370,16 +2370,20 @@
 ditions%0A
+    
         
@@ -2495,24 +2495,28 @@
 oString());%0A
+    
         @Sup
@@ -2550,16 +2550,20 @@
         
+    
 List%3CKey
@@ -2653,16 +2653,20 @@
 _DATA);%0A
+    
         
@@ -2687,32 +2687,36 @@
 pairs);%0A        
+    
 assertEquals(1, 
@@ -2726,24 +2726,28 @@
 rs.size());%0A
+    
         asse
@@ -2790,32 +2790,36 @@
 Key());%0A        
+ 
+   
 assertEquals(%22Cr
@@ -2858,20 +2858,93 @@
 etValue());%0A
+        %7D finally %7B%0A            TimeZone.setDefault(timeZone);%0A        %7D%0A
     %7D%0A%7D%0A

Set time zone during test execution. Relates to #6.