package net.sf.jabref.importer.fileformat;

import net.sf.jabref.*;

import net.sf.jabref.importer.OutputPrinterToNull;
import net.sf.jabref.model.entry.BibtexEntry;
import net.sf.jabref.model.entry.BibtexEntryTypes;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

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

/**
 * Test cases for the RISImporter
 *
 * @author $Author: coezbek $
 */
public class RISImporterTest {

    @Before
    public void setUp() throws Exception {
        if (Globals.prefs == null) {
            Globals.prefs = JabRefPreferences.getInstance();
        }
    }

    @Test
    public void testIsRecognizedFormat() throws IOException {

        RisImporter importer = new RisImporter();
        Assert.assertTrue(importer.isRecognizedFormat(RISImporterTest.class
                .getResourceAsStream("RisImporterTest1.ris")));
    }

    @Test
    public void testProcessSubSup() {

        HashMap<String, String> hm = new HashMap<>();
        hm.put("title", "/sub 3/");
        IsiImporter.processSubSup(hm);
        Assert.assertEquals("$_3$", hm.get("title"));

        hm.put("title", "/sub   3   /");
        IsiImporter.processSubSup(hm);
        Assert.assertEquals("$_3$", hm.get("title"));

        hm.put("title", "/sub 31/");
        IsiImporter.processSubSup(hm);
        Assert.assertEquals("$_{31}$", hm.get("title"));

        hm.put("abstract", "/sub 3/");
        IsiImporter.processSubSup(hm);
        Assert.assertEquals("$_3$", hm.get("abstract"));

        hm.put("review", "/sub 31/");
        IsiImporter.processSubSup(hm);
        Assert.assertEquals("$_{31}$", hm.get("review"));

        hm.put("title", "/sup 3/");
        IsiImporter.processSubSup(hm);
        Assert.assertEquals("$^3$", hm.get("title"));

        hm.put("title", "/sup 31/");
        IsiImporter.processSubSup(hm);
        Assert.assertEquals("$^{31}$", hm.get("title"));

        hm.put("abstract", "/sup 3/");
        IsiImporter.processSubSup(hm);
        Assert.assertEquals("$^3$", hm.get("abstract"));

        hm.put("review", "/sup 31/");
        IsiImporter.processSubSup(hm);
        Assert.assertEquals("$^{31}$", hm.get("review"));

        hm.put("title", "/sub $Hello/");
        IsiImporter.processSubSup(hm);
        Assert.assertEquals("$_{\\$Hello}$", hm.get("title"));
    }

    @Test
    public void testImportEntries() throws IOException {
        RisImporter importer = new RisImporter();

        List<BibtexEntry> entries = importer.importEntries(RISImporterTest.class
                .getResourceAsStream("RisImporterTest1.ris"), new OutputPrinterToNull());
        Assert.assertEquals(1, entries.size());
        BibtexEntry entry = entries.get(0);
        Assert.assertEquals("Editorial: Open Source and Empirical Software Engineering", entry
                .getField("title"));
        Assert.assertEquals(
                "Harrison, Warren",
                entry.getField("author"));

        Assert.assertEquals(BibtexEntryTypes.ARTICLE, entry.getType());
        Assert.assertEquals("Empirical Software Engineering", entry.getField("journal"));
        Assert.assertEquals("2001", entry.getField("year"));
        Assert.assertEquals("6", entry.getField("volume"));
        Assert.assertEquals("3", entry.getField("number"));
        Assert.assertEquals("193--194", entry.getField("pages"));
        Assert.assertEquals("#sep#", entry.getField("month"));
    }
}

@@ -312,16 +312,44 @@
 eption;%0A
+import java.io.InputStream;%0A
 import j
@@ -802,54 +802,34 @@
     
-Assert.assertTrue(importer.isRecognizedFormat(
+try (InputStream stream = 
 RISI
@@ -907,18 +907,96 @@
 1.ris%22))
-);
+ %7B%0A            Assert.assertTrue(importer.isRecognizedFormat(stream));%0A        %7D
 %0A    %7D%0A%0A
@@ -2585,59 +2585,34 @@
     
-List%3CBibtexEntry%3E entries = importer.importEntries(
+try (InputStream stream = 
 RISI
@@ -2689,16 +2689,89 @@
 t1.ris%22)
+) %7B%0A            List%3CBibtexEntry%3E entries = importer.importEntries(stream
 , new Ou
@@ -2788,24 +2788,28 @@
 rToNull());%0A
+    
         Asse
@@ -2852,16 +2852,20 @@
         
+  
+  
 BibtexEn
@@ -2892,16 +2892,20 @@
 get(0);%0A
+    
         
@@ -3003,24 +3003,28 @@
             
+    
 .getField(%22t
@@ -3032,32 +3032,36 @@
 tle%22));%0A        
+  
+  
 Assert.assertEqu
@@ -3065,16 +3065,20 @@
 Equals(%0A
+    
         
@@ -3121,16 +3121,20 @@
         
+    
 entry.ge
@@ -3153,32 +3153,36 @@
 or%22));%0A%0A        
+  
+  
 Assert.assertEqu
@@ -3225,24 +3225,28 @@
 getType());%0A
+    
         Asse
@@ -3323,32 +3323,36 @@
 nal%22));%0A        
+    
 Assert.assertEqu
@@ -3388,32 +3388,36 @@
 ear%22));%0A        
+  
+  
 Assert.assertEqu
@@ -3448,24 +3448,28 @@
 %22volume%22));%0A
+    
         Asse
@@ -3516,32 +3516,36 @@
 ber%22));%0A        
+    
 Assert.assertEqu
@@ -3586,32 +3586,36 @@
 ges%22));%0A        
+  
+  
 Assert.assertEqu
@@ -3649,20 +3649,30 @@
 (%22month%22));%0A
+        %7D%0A
     %7D%0A%7D%0A

Fix two resource leaks in RISImporterTest