/*
 * ################################################################
 *
 * ProActive: The Java(TM) library for Parallel, Distributed,
 *            Concurrent computing with Security and Mobility
 *
 * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
 * Contact: proactive@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://proactive.inria.fr/team_members.htm
 *  Contributor(s):
 *
 * ################################################################
 */
package unitTests.doc;

/*
 * ################################################################
 *
 * ProActive: The Java(TM) library for Parallel, Distributed,
 *            Concurrent computing with Security and Mobility
 *
 * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
 * Contact: proactive@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://proactive.inria.fr/team_members.htm
 *  Contributor(s):
 *
 * ################################################################
 */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

import junit.framework.Assert;

import doc.DocBookize;


/**
 * Contains tests for {@link DocBookize}.
 * 
 * @author vjuresch
 * @since 3.9
 * @version $Id$
 *
 */
public class TestDocBookize {
    /**
     * Regular expresion matching the  & lt ; and & gt ; 
     * inside programlisting tags.  
     */
    private static final String REGEXP = "(<programlisting.*>)(.*(&lt;).*|(&gt;).*?)(</programlisting>)";
    /** File containing the xml that triggers the bug  */
    private static final String XML_FILE = "singleLine.xml";
    /** Temporary file that will contain the modified xml   */
    private static final String TEST_FILE = "singleLineTest.xml";

    /**
     * Tests for bug PROACTIVE-346: 
     * Characters < and > do not get transformed into & lt; & gt; when code 
     * is included from programlisting tags from a file and it is a single line of code.
     * 
     * @throws Exception
     */
    @Test
    public void testTransform() throws Exception {
        final String path[] = { "./" };
        final String javaSrc = "";
        //copy file to temporary file
        final InputStream input = new FileInputStream(this.getClass().getResource(XML_FILE).getFile());

        File outputFile = File.createTempFile(this.getClass().getName(), null);
        final OutputStream output = new FileOutputStream(outputFile);
        outputFile.deleteOnExit();

        //copy singleLine.xml to singleLineTest.xml
        final byte[] buf = new byte[1024];
        int len;
        while ((len = input.read(buf)) > 0) {
            output.write(buf, 0, len);
        }
        input.close();
        output.close();

        //run DocBookize on singleLineTest file
        final String mainArgs[] = { outputFile.toString(), javaSrc, path[0] };
        DocBookize.main(mainArgs);

        //load singleLineText.xml in  String (it's a small file)
        final File tmp = new File(outputFile.toString());
        final BufferedReader buff = new BufferedReader(new FileReader(tmp));
        String fileText = "";
        String tmpLine = "";
        while (tmpLine != null) {
            fileText += tmpLine;
            tmpLine = buff.readLine();
        }
        buff.close();
        System.out.println(fileText);
        //scan for programlisting containing < >
        //String regex = "(<programlisting.*>)(.*[><].*?)(</programlisting>)";
        //it assumes the test file is changed to contain &lt and &gt 
        final Matcher matcher = Pattern.compile(REGEXP).matcher(fileText);
        final boolean good = matcher.find();
        //delete temporary file
        if (!tmp.delete()) {
            throw new IOException("Cannot delete temporary file : " + tmp.getAbsolutePath());
        }
        Assert.assertTrue(good);
    }
}
@@ -1,1237 +1,4 @@
-/*%0A * ################################################################%0A *%0A * ProActive: The Java(TM) library for Parallel, Distributed,%0A *            Concurrent computing with Security and Mobility%0A *%0A * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis%0A * Contact: proactive@objectweb.org%0A *%0A * This library is free software; you can redistribute it and/or%0A * modify it under the terms of the GNU General Public License%0A * as published by the Free Software Foundation; either version%0A * 2 of the License, or any later version.%0A *%0A * This library is distributed in the hope that it will be useful,%0A * but WITHOUT ANY WARRANTY; without even the implied warranty of%0A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU%0A * General Public License for more details.%0A *%0A * You should have received a copy of the GNU General Public License%0A * along with this library; if not, write to the Free Software%0A * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307%0A * USA%0A *%0A *  Initial developer(s):               The ProActive Team%0A *                        http://proactive.inria.fr/team_members.htm%0A *  Contributor(s):%0A *%0A * ################################################################%0A */%0A
 pack
@@ -1977,24 +1977,157 @@
 listing%3E)%22;%0A
+    /** Path to the files cotaining xml that triggers the bug  */%0A    private static final String PATH = %22src/Tests/unitTests/doc/%22;%0A
     /** File
@@ -2741,242 +2741,217 @@
 ing 
-javaSrc = %22%22;%0A        //copy file to temporary file%0A        final InputStream input = new FileInputStream(this.getClass().getResource(XML_FILE).getFile());%0A%0A        File outputFile = File.createTempFile(this.getClass().getName(), null
+inPath = PATH + XML_FILE;%0A        final String javaSrc = %22%22;%0A        final String testPath = PATH + TEST_FILE;%0A        //copy file to temporary file%0A        final InputStream input = new FileInputStream(inPath
 );%0A 
@@ -3010,53 +3010,16 @@
 eam(
-outputFile);%0A        outputFile.deleteOnExit(
+testPath
 );%0A%0A
@@ -3357,37 +3357,24 @@
 s%5B%5D = %7B 
-outputFile.toString()
+testPath
 , javaSr
@@ -3526,29 +3526,16 @@
 ile(
-outputFile.toString()
+testPath
 );%0A 

revert to working version of the test