package edu.tamu.tcat.hathitrust.basic.tests;

import java.io.BufferedReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import edu.tamu.tcat.hathitrust.htrc.features.simple.ExtractedFeatures;
import edu.tamu.tcat.hathitrust.htrc.features.simple.ExtractedFeaturesProvider;
import edu.tamu.tcat.hathitrust.htrc.features.simple.impl.DefaultExtractedFeaturesProvider;

public class ExtractedFeaturesTests
{
   private static final String FILES_PATH_ROOT = "\\\\citd.tamu.edu\\citdfs\\archive\\HTRC_Dataset\\";
   private static final String FILE_VOL_IDS = "res/volume_ids.txt";
   
   @BeforeClass
   public static void log()
   {
      ConsoleHandler ch = new ConsoleHandler();
      ch.setFormatter(new SimpleFormatter());
      ch.setLevel(Level.ALL);
      // Use empty string to properly get the "global" logger
      Logger.getLogger("").addHandler(ch);
      
      // Enable all loggers in the package for these test cases
      Logger.getLogger(DefaultExtractedFeaturesProvider.class.getPackage().getName()).setLevel(Level.ALL);
   }
   
   @Test
   @Ignore
   public void testGrab() throws Exception
   {
      List<String> ids = new ArrayList<>();
      try (BufferedReader reader = Files.newBufferedReader(Paths.get(FILE_VOL_IDS)))
      {
         String s;
         while ((s = reader.readLine()) != null)
         {
            s = s.trim();
            if (s.isEmpty())
               continue;
            ids.add(s);
         }
      }
      
      try (DefaultExtractedFeaturesProvider mp = createProvider())
      {
         ExtractedFeaturesProvider p = mp;
         for (String id : ids)
         {
            doTokens(id, p, (pkg) -> {
               try {
                  System.out.println(pkg.token + ": " + pkg.bodyData.getCount(pkg.token));
               } catch (Exception e) {
                  e.printStackTrace();
               }
            });
         }
      }
   }
   
   /**
    * An encapsulation to use in test cases
    */
   static class Pkg
   {
      String token;
      ExtractedFeatures.ExtractedPagePartOfSpeechData bodyData;
      
      Pkg(String t,
          ExtractedFeatures.ExtractedPagePartOfSpeechData p)
      {
         token = t;
         bodyData = p;
      }
   }
   
   private void doTokens(String id, ExtractedFeaturesProvider p, Consumer<Pkg> f) throws Exception
   {
      try (ExtractedFeatures feat = p.getExtractedFeatures(id))
      {
         String vid = feat.getVolumeId();
         System.out.println("Loaded: " + vid);
         
         System.out.println("Title: " + feat.getMetadata().title());
         
         ExtractedFeatures.ExtractedPageFeatures page = feat.getPage(48);
         ExtractedFeatures.ExtractedPagePartOfSpeechData bodyData = page.getBodyData();
         Set<String> toks = bodyData.tokens();
         toks.stream().sorted().forEach(tok ->
         {
            f.accept(new Pkg(tok, bodyData));
         });
      }
   }
   
   @Test
   @Ignore
   public void testFactory() throws Exception
   {
      String TEST_FILE = "hvd.ah3d1a";
      try (DefaultExtractedFeaturesProvider mp = createProvider())
      {
         doTokens(TEST_FILE, mp, pkg -> {
            try {
               System.out.println(pkg.token + ": " + pkg.bodyData.getCount(pkg.token));
            } catch (Exception e) {
               e.printStackTrace();
            }
         });
      }
   }
   
   // Return the actual type because the interface type is not AutoCloseable
   private static DefaultExtractedFeaturesProvider createProvider()
   {
      return new DefaultExtractedFeaturesProvider(Paths.get(FILES_PATH_ROOT));
   }
}

@@ -385,16 +385,45 @@
 atter;%0A%0A
+import org.junit.AfterClass;%0A
 import o
@@ -776,16 +776,117 @@
 Tests%0A%7B%0A
+   private static final Logger debug = Logger.getLogger(ExtractedFeaturesTests.class.getName());%0A   %0A
    priva
@@ -1048,24 +1048,88 @@
 s.txt%22;%0A   %0A
+   private static ConsoleHandler ch = new ConsoleHandler();%0A   %0A
    @BeforeCl
@@ -1169,56 +1169,8 @@
   %7B%0A
-      ConsoleHandler ch = new ConsoleHandler();%0A
     
@@ -1349,24 +1349,151 @@
 ch);%0A      %0A
+      // Enable loggers for this test case%0A      Logger.getLogger(ExtractedFeaturesTests.class.getName()).setLevel(Level.ALL);%0A
       // Ena
@@ -1515,16 +1515,21 @@
  in the 
+impl 
 package 
@@ -1657,32 +1657,329 @@
 .ALL);%0A   %7D%0A   %0A
+   @AfterClass%0A   public static void endLog()%0A   %7B%0A      Logger.getLogger(%22%22).removeHandler(ch);%0A      Logger.getLogger(ExtractedFeaturesTests.class.getName()).setLevel(Level.OFF);%0A      Logger.getLogger(DefaultExtractedFeaturesProvider.class.getPackage().getName()).setLevel(Level.OFF);%0A   %7D%0A   %0A
    @Test%0A   @Ign
@@ -2614,34 +2614,26 @@
         
-System.out.println
+debug.info
 (pkg.tok
@@ -2736,34 +2736,50 @@
         
-e.printStackTrace(
+debug.log(Level.SEVERE, %22Error%22, e
 );%0A     
@@ -3192,16 +3192,36 @@
 ring id,
+ Integer pageNumber,
  Extract
@@ -3408,34 +3408,40 @@
     
-System.out.println(%22Loaded
+%0A         debug.info(%22Processing
 : %22 
@@ -3467,34 +3467,26 @@
         
-System.out.println
+debug.info
 (%22Title:
@@ -4136,26 +4136,18 @@
     
-System.out.println
+debug.info
 (pkg
@@ -4252,26 +4252,42 @@
     
-e.printStackTrace(
+debug.log(Level.SEVERE, %22Error%22, e
 );%0A 

Clean up logging