/*--------------------------------------------------------------------------
 *  Copyright 2007 utgenome.org
 *
 *  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.
 *--------------------------------------------------------------------------*/
//--------------------------------------
// UTGB Common Project
//
// FASTAPullParserTest.java
// Since: Jun 4, 2007
//
// $URL$ 
// $Author$
//--------------------------------------
package org.utgenome.format.fasta;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.utgenome.format.InvalidFormatException;
import org.xerial.util.FileResource;

public class FASTAPullParserTest {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void pullParsing() throws IOException, InvalidFormatException {
		FASTAPullParser parser = new FASTAPullParser(new StringReader(">1|mySeq\nACGCTT\nACCC\n>2\nCCGGA"));
		FASTASequence s1 = parser.nextSequence();
		FASTASequence s2 = parser.nextSequence();

		assertNotNull(s1);
		assertNotNull(s2);
		assertEquals("ACGCTTACCC", s1.getSequence());
		assertEquals("CCGGA", s2.getSequence());

		assertEquals(2, s1.getDescriptionSize());
		assertEquals("1", s1.getDescription(0));
		assertEquals("mySeq", s1.getDescription(1));
		assertEquals(1, s2.getDescriptionSize());
		assertEquals("2", s2.getDescription(0));

		FASTASequence s3 = parser.nextSequence();
		assertNull(s3);
	}

	@Test
	public void pullParsing2() throws IOException, InvalidFormatException {
		FASTAPullParser parser = new FASTAPullParser(new StringReader(">1|mySeq\nACGCTT\nACCC\n>2\nCCGGA"));

		assertEquals("1|mySeq", parser.nextDescriptionLine());
		assertEquals("ACGCTT", parser.nextSequenceLine());
		assertEquals("ACCC", parser.nextSequenceLine());
		assertNull(parser.nextSequenceLine());

		assertEquals("2", parser.nextDescriptionLine());
		assertEquals("CCGGA", parser.nextSequenceLine());
		assertNull(parser.nextSequenceLine());

	}

	@Test
	public void readTarFile() throws Exception {
		FASTAPullParser parser = FASTAPullParser.newTARGZFileReader(FileResource.openByteStream(FASTAPullParserTest.class, "sample-archive.fa.tar.gz"));
		ArrayList<String> chrList = new ArrayList<String>();
		for (FASTASequence seq; (seq = parser.nextSequence()) != null;) {
			String name = seq.getSequenceName();
			chrList.add(name);
		}

		for (String e : new String[] { "chr1", "chr2", "chr3" }) {
			assertTrue(chrList.contains(e));
		}

	}

	/*
	 * @Test public void largeFileLoadTest() throws IOException {
	 * FASTAPullParser parser = new FASTAPullParser(new FileReader(
	 * "J:/utgb/dev-work/Basecolor/input/human/hg18/allhuman.hg18.fa"));
	 * 
	 * while (parser.nextDescriptionLine() != null) { while
	 * (parser.nextSequenceLine() != null) {} }
	 *  }
	 */

}

@@ -1142,16 +1142,53 @@
 ertTrue;
+%0Aimport static org.junit.Assert.fail;
 %0A%0Aimport
@@ -3192,72 +3192,37 @@
 ;%0A%09%09
-%7D%0A%0A%09%09for (String e : new String%5B%5D %7B %22chr1%22, %22chr2%22, %22chr3%22 %7D
+%09if (name.equals(%22chr1%22)
 ) %7B%0A
+%09
 %09%09%09a
@@ -3230,364 +3230,528 @@
 sert
-True(chrList.contains(e
+Equals(%22NNNNNNACGGATTCTTGCTATATANTTACTTACCCGTAGTCTAGAGATCTTTCCAATATCGTCT%22, seq.getSequence(
 ));%0A%09%09
-%7D%0A%0A
 %09%7D%0A
-%0A%09/*%0A%09 * @Test public void largeFileLoadTest() throws IOException %7B%0A%09 * FASTAPullParser parser = new FASTAPullParser(new FileReader(%0A%09 * %22J:/utgb/dev-work/Basecolor/input/human/hg18/allhuman.hg18.fa%22));%0A%09 * %0A%09 * while (parser.nextDescriptionLine() != null) %7B while%0A%09 * (parser.nextSequenceLine() != null) %7B%7D %7D%0A%09 *  %7D%0A%09 */
+%09%09%09else if (name.equals(%22chr2%22)) %7B%0A%09%09%09%09assertEquals(%22ACCGGTATCTCTAAAAAAAAAAAGGG%22, seq.getSequence());%0A%09%09%09%7D%0A%09%09%09else if (name.equals(%22chr3%22)) %7B%0A%09%09%09%09assertEquals(%22CGGTCTGTCGTCGTCAACGTCGGCCTTTCGCGCGCGGGGCCTAAATTAATTATAATTAAAAATCCTCT%22, seq.getSequence());%0A%09%09%09%7D%0A%09%09%09else %7B%0A%09%09%09%09fail(%22unknown sequence: %22 + name);%0A%09%09%09%7D%0A%0A%09%09%7D%0A%0A%09%09for (String e : new String%5B%5D %7B %22chr1%22, %22chr2%22, %22chr3%22 %7D) %7B%0A%09%09%09assertTrue(chrList.contains(e));%0A%09%09%7D%0A%0A%09%7D
 %0A%0A%7D%0A

add assertions to the Tar file read test