package com.adobe.epubcheck.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import junit.framework.Assert;

import org.junit.Test;

import com.adobe.epubcheck.messages.MessageId;

/**
 * Test the coverage of reported message ids by all the tests.  This test should run after all the others have completed.
 */
public class message_coverage
{
  @Test
  public void MessageId_Coverage_Test() throws Exception
  {
    File dumpFile = new File("ReportedMessages.txt");
    Assert.assertTrue("MessageId output file is missing: " + dumpFile.getAbsolutePath(), dumpFile.exists());
    HashSet<MessageId> reportedMessageIds = new HashSet<MessageId>();
    BufferedReader reader = new BufferedReader(new FileReader(dumpFile));
    Assert.assertNotNull("Could not read the reported message file.", reader);
    String line;
    while ((line = reader.readLine()) != null)
    {
      line = line.trim();
      if (!line.equals(""))
      {
        reportedMessageIds.add(MessageId.fromString(line));
      }
    }
    reader.close();
    List<MessageId> allMessageList = Arrays.asList(MessageId.values());
    Assert.assertNotNull("Could not read the reported message file.", allMessageList);
    Set<MessageId> allMessages = new HashSet<MessageId>(allMessageList);
    allMessages.removeAll(reportedMessageIds);
    Set<MessageId> expectedMissedCoverage = new HashSet<MessageId>();
    expectedMissedCoverage.add(MessageId.HTM_002); //This message is in a code path that shouldn't be hit
    expectedMissedCoverage.add(MessageId.HTM_011); //This message may never be reported.  Undeclared entities result in a Sax Parser Error and message RSC_005.
    expectedMissedCoverage.add(MessageId.CHK_007); //This message is in a code path that shouldn't be hit, but is here in case there is some other error accessing the file.
    expectedMissedCoverage.add(MessageId.CHK_006); //This message is in a code path that shouldn't ever be hit, but its here in case there is an error parsing the regex result.
    expectedMissedCoverage.add(MessageId.OPF_010); //This is only reported in an exception that is difficult to generate in a test
    expectedMissedCoverage.add(MessageId.OPF_011); //This is currently reported as RSC_005 in Schematron, but would be safer in prefix-checked code
    expectedMissedCoverage.add(MessageId.PKG_015); //This is only reported in an exception that is difficult to generate in a test
    expectedMissedCoverage.add(MessageId.PKG_005); //This is only reported in an exception that is difficult to generate in a test
    expectedMissedCoverage.add(MessageId.RSC_017); //This message may never be reported.   Sax Parser Error and message RSC_005, but this covers SAX warnings, which may never happen.

    Assert.assertEquals("Messages not covered by tests", expectedMissedCoverage, allMessages);
  }
}

@@ -158,16 +158,48 @@
 ashSet;%0A
+import java.util.LinkedHashSet;%0A
 import j
@@ -774,32 +774,38 @@
 essageIds = new 
+Linked
 HashSet%3CMessageI
@@ -1381,24 +1381,30 @@
 sages = new 
+Linked
 HashSet%3CMess
@@ -1522,16 +1522,22 @@
 e = new 
+Linked
 HashSet%3C
@@ -1595,15 +1595,15 @@
 eId.
-HTM_002
+CHK_006
 ); /
@@ -1653,173 +1653,84 @@
 n't 
+ever 
 be hit
-%0A    expectedMissedCoverage.add(MessageId.HTM_011); //This message may never be reported.  Undeclared entities result in a Sax Parser Error and message RSC_005
+, but its here in case there is an error parsing the regex result
 .%0A  
@@ -1933,39 +1933,39 @@
 e.add(MessageId.
-CHK_006
+HTM_002
 ); //This messag
@@ -2003,85 +2003,330 @@
 n't 
-ever be hit, but its here in case there is an error parsing the regex result.
+be hit%0A    expectedMissedCoverage.add(MessageId.HTM_011); //This message may never be reported.  Undeclared entities result in a Sax Parser Error and message RSC_005.%0A    expectedMissedCoverage.add(MessageId.NAV_001); //This message is only reported if the user explicitly calls validation of a Nav Doc with EPUB 2 version
 %0A   
@@ -2647,17 +2647,17 @@
 Id.PKG_0
-1
+0
 5); //Th
@@ -2770,33 +2770,33 @@
 (MessageId.PKG_0
-0
+1
 5); //This is on
@@ -2908,149 +2908,34 @@
 eId.
-RSC_017); //This message may never be reported.   Sax Parser Error and m
+PKG_023); //TODO add t
 es
+t
 s
-age RSC_005, but this covers SAX warnings, which may never happen.
 %0A%0A  

Fix message coverage test