/**
 * Copyright 2008-2010 Digital Enterprise Research Institute (DERI)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.deri.any23.extractor;

import junit.framework.Assert;
import org.deri.any23.RDFHelper;
import org.deri.any23.extractor.html.TitleExtractor;
import org.deri.any23.writer.TripleHandler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openrdf.model.URI;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

/**
 * Test case for {@link org.deri.any23.extractor.ExtractionResultImpl} class.
 *
 * @author Michele Mostarda ( michele.mostarda@gmail.com )
 * @version $Id$
 */
public class ExtractionResultImplTest {

    private static final URI TEST_URI = RDFHelper.uri("http://host/test/service");

    private ExtractionResultImpl extractionResult;
    private Extractor extractor;
    private TripleHandler mockTripleHandler;

    private ByteArrayOutputStream baos;
    private PrintStream ps;

    @Before
    public void setUp() {
        if(baos != null) { baos.reset(); }
        extractor = new TitleExtractor();
        mockTripleHandler = new FakeTripleHandler();
        extractionResult  = new ExtractionResultImpl(TEST_URI, extractor, mockTripleHandler );
    }

    @After
    public void tearDown() {
        extractionResult.close();
        mockTripleHandler.close();
        extractor         = null;
        mockTripleHandler = null;
        extractionResult  = null;
    }

    @Test
    public void testNotifyErrors() throws IOException {
        notifyErrors(extractionResult);
        ExtractionResult subExtractionResult = extractionResult.openSubResult("sub-id");
        notifyErrors(subExtractionResult);

        Assert.assertEquals("Unespected number of errors.", 6, extractionResult.getErrorsCount()   );
        Assert.assertEquals("Unspected errors list size." , 6, extractionResult.getErrors().size() );

        extractionResult.printErrorsReport( getPrintStream() );
        assertOutputString("ERROR");
        assertOutputString("WARN");
        assertOutputString("FATAL");
        assertOutputString("errors: 6");
    }

    private void notifyErrors(ExtractionResult er) {
        extractionResult.notifyError(ExtractionResult.ErrorLevel.ERROR, "Error message"  , 1, 2);
        extractionResult.notifyError(ExtractionResult.ErrorLevel.WARN,  "Warning message", 3, 4);
        extractionResult.notifyError(ExtractionResult.ErrorLevel.FATAL, "Fatal message"  , 5, 6);
    }

    private PrintStream getPrintStream() {
        if(baos == null) {
            baos = new ByteArrayOutputStream();
            ps   = new PrintStream(baos);
        }
        return ps;
    }

    private void assertOutputString(String s) {
        ps.flush();
        Assert.assertTrue( String.format("Cannot find string '%s' in output stream.", s), baos.toString().contains(s) );
    }

}

@@ -1458,153 +1458,41 @@
     
-private ByteArrayOutputStream baos;%0A    private PrintStream ps;%0A%0A    @Before%0A    public void setUp() %7B%0A        if(baos != null) %7B baos.reset(); %7D
+@Before%0A    public void setUp() %7B
 %0A   
@@ -1981,47 +1981,13 @@
     
-notifyErrors(extractionResult);%0A       
+final
  Ext
@@ -2085,20 +2085,17 @@
 yErrors(
-subE
+e
 xtractio
@@ -2104,25 +2104,24 @@
 esult);%0A
-%0A
         
 Assert.a
@@ -2116,110 +2116,52 @@
     
-Assert.assertEquals(%22Unespected number of errors.%22, 6, extractionResult.getErrorsCount()   
+notifyErrors(subExtractionResult
 );%0A
+%0A
         
 Asse
@@ -2160,111 +2160,49 @@
     
-Assert.assertEquals(%22Unspected errors list size.%22 , 6, extractionResult.getErrors().size() 
+assertContent(extractionResult
 );%0A
-%0A
         
 extr
@@ -2201,212 +2201,41 @@
     
-extractionResult.printErrorsReport( getPrintStream() );%0A        assertOutputString(%22ERROR%22);%0A        assertOutputString(%22WARN%22);%0A        assertOutputString(%22FATAL%22);%0A        assertOutputString(%22errors: 6%22
+assertContent(subExtractionResult
 );%0A 
@@ -2298,39 +2298,25 @@
  %7B%0A        e
-xtractionResult
+r
 .notifyError
@@ -2382,39 +2382,25 @@
 );%0A        e
-xtractionResult
+r
 .notifyError
@@ -2470,31 +2470,17 @@
        e
-xtractionResult
+r
 .notifyE
@@ -2568,239 +2568,529 @@
 ate 
-PrintStream getPrintStream() %7B%0A        if(baos == null) %7B%0A            baos = new ByteArrayOutputStream();%0A            ps   = new PrintStream(baos);%0A        %7D%0A        return ps;%0A    %7D%0A%0A    private void assertOutputString(String s) %7B
+void assertContent(ExtractionResult er) %7B%0A        Assert.assertEquals(%22Unexpected errors list size.%22 , 3, er.getErrors().size() );%0A        assertOutputString(er, %22ERROR%22);%0A        assertOutputString(er, %22WARN%22);%0A        assertOutputString(er, %22FATAL%22);%0A        assertOutputString(er, %22errors: 3%22);%0A    %7D%0A%0A    private void assertOutputString(ExtractionResult er, String s) %7B%0A        ByteArrayOutputStream baos = new ByteArrayOutputStream();%0A        PrintStream ps = new PrintStream(baos);%0A        er.printErrorsReport(ps);
 %0A   

Test case has been improved.