package org.opensolaris.opengrok.analysis.php;
import static org.junit.Assert.assertEquals;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import org.junit.Test;
import org.opensolaris.opengrok.analysis.XrefWriter;
/**
* Tests the {@link PhpXref} class.
* @author Gustavo Lopes
*/
public class PhpXrefTest {
static InputStream getTestFile(String name) throws FileNotFoundException {
File f = new File("test/"
+ PhpXrefTest.class.getPackage().getName().replace('.', '/')
+ "/" + name);
return new FileInputStream(f);
}
/**
* Xref output test for a sample string.
* @throws IOException
*/
@SuppressWarnings("static-method")
@Test
public void basicTest() throws IOException {
String s = "<?php foo bar";
StringWriter w = new StringWriter();
XrefWriter out = new XrefWriter(w);
PhpAnalyzer.writeXref(new StringReader(s), out, null, null, null);
assertEquals("<div id='lines'\n><div><strong>&lt;"
+ "?php</strong> <a href=\"/source/s?defs=foo\">foo</a> "
+ "<a href=\"/source/s?defs=bar\">bar</a></div\n></div\n>",
w.toString());
out.close();
}
/**
* Xref output test for {@code sample.php}.
* @throws IOException
*/
@SuppressWarnings("static-method")
@Test
public void sampleTest() throws IOException {
// produce
InputStreamReader in =
new InputStreamReader(getTestFile("sample.php"), "UTF-8");
Writer w = new StringWriter();
XrefWriter out = new XrefWriter(w);
PhpAnalyzer.writeXref(in, out, null, null, null);
out.close();
in.close();
// get expected
in = new InputStreamReader(getTestFile("sampleXrefRes.html"), "UTF-8");
ArrayList<String> expected = new ArrayList<String>(64);
BufferedReader bir = new BufferedReader(in);
String s;
while ((s = bir.readLine()) != null) {
expected.add(s);
}
bir.close();
// compare
String gotten[] = w.toString().split("\n");
assertEquals(expected.size(), gotten.length);
for (int i = 0; i < gotten.length; i++) {
assertEquals("line "+(i+1), expected.get(i).trim(), gotten[i].trim());
}
}
}