package com.github.rnewson.couchdb.lucene;

/**
 * Copyright 2009 Robert Newson
 * 
 * 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.
 */

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.lucene.document.Document;
import org.junit.Before;
import org.junit.Test;

import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;

public class TikaTest {

	private Tika tika;
	private Document doc;

	@Before
	public void setup() {
		tika = new Tika();
		doc = new Document();
	}

	@Test
	public void testPDF() throws IOException {
		parse("paxos-simple.pdf", "application/pdf");
		assertThat(doc.getField(Config.BODY), not(nullValue()));
	}

	@Test
	public void testXML() throws IOException {
		parse("example.xml", "text/xml");
		assertThat(doc.getField(Config.BODY), not(nullValue()));
	}

	@Test
	public void testEnglish() throws IOException {
		assertThat(detectLanguage("english"), is(nullValue()));
		assertThat(detectLanguage("english text here"), is("en"));
	}

	@Test
	public void testGerman() throws IOException {
		assertThat(detectLanguage("Alle Menschen sind frei und gleich"), is("de"));
	}

	@Test
	public void testFrench() throws IOException {
		assertThat(detectLanguage("Me permettez-vous, dans ma gratitude"), is("fr"));
	}

	private String detectLanguage(final String in) throws IOException {
		CharsetDetector detector = new CharsetDetector();
		detector.setText(new ByteArrayInputStream(in.getBytes()));
		CharsetMatch match = detector.detect();
		if (match.getConfidence() < 50) {
			return null;
		}
		return match.getLanguage();
	}

	private void parse(final String resource, final String type) throws IOException {
		final InputStream in = getClass().getClassLoader().getResourceAsStream(resource);
		try {
			tika.parse(in, type, doc);
		} finally {
			in.close();
		}
	}

}

@@ -640,48 +640,8 @@
 */%0A%0A
-import static org.hamcrest.Matchers.is;%0A
 impo
@@ -677,16 +677,16 @@
 rs.not;%0A
+
 import s
@@ -772,45 +772,8 @@
 t;%0A%0A
-import java.io.ByteArrayInputStream;%0A
 impo
@@ -922,88 +922,8 @@
 t;%0A%0A
-import com.ibm.icu.text.CharsetDetector;%0Aimport com.ibm.icu.text.CharsetMatch;%0A%0A
 publ
@@ -1313,24 +1313,24 @@
 text/xml%22);%0A
+
 %09%09assertThat
@@ -1384,774 +1384,8 @@
 %09%7D%0A%0A
-%09@Test%0A%09public void testEnglish() throws IOException %7B%0A%09%09assertThat(detectLanguage(%22english%22), is(nullValue()));%0A%09%09assertThat(detectLanguage(%22english text here%22), is(%22en%22));%0A%09%7D%0A%0A%09@Test%0A%09public void testGerman() throws IOException %7B%0A%09%09assertThat(detectLanguage(%22Alle Menschen sind frei und gleich%22), is(%22de%22));%0A%09%7D%0A%0A%09@Test%0A%09public void testFrench() throws IOException %7B%0A%09%09assertThat(detectLanguage(%22Me permettez-vous, dans ma gratitude%22), is(%22fr%22));%0A%09%7D%0A%0A%09private String detectLanguage(final String in) throws IOException %7B%0A%09%09CharsetDetector detector = new CharsetDetector();%0A%09%09detector.setText(new ByteArrayInputStream(in.getBytes()));%0A%09%09CharsetMatch match = detector.detect();%0A%09%09if (match.getConfidence() %3C 50) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09return match.getLanguage();%0A%09%7D%0A%0A
 %09pri

remove useless TikaTests