/*

BSD license

Copyright (c) 2006, Jean Lazarou
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list 
of conditions and the following disclaimer. 
Redistributions in binary form must reproduce the above copyright notice, this 
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution. 
Neither the name of the null nor the names of its contributors may be 
used to endorse or promote products derived from this software without specific 
prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
OF THE POSSIBILITY OF SUCH DAMAGE.

*/

/*
 * @author: Jean Lazarou
 * @date: 1 mars 04
 */
package com.ap.jdbcunit;

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

import java.util.Iterator;

import com.ap.store.Store;
import com.ap.store.Content;
import com.ap.store.MemoryStore;
import com.ap.jdbcunit.csv.CSVMedia;
import com.ap.jdbcunit.util.JDBCUnitTestCase;

public class TestMemoryUsage extends JDBCUnitTestCase {

    public TestMemoryUsage(String name) {
        super(name);
    }

    protected void setUp() throws Exception {
        
        repository = new MemoryStore("repository");
        toc = repository.add("toc.csv"new TOCContent());
        result = repository.add("toc_1.csv", content = new ResultContent());
        
        media = new CSVMedia(toc);
    }

    public void testLoadOnDemand() throws Exception {
        
        media.open();
        
        Iterator it = media.getTrack("my:db:Database""SELECT * FROM HelloWorld");
        
        assertEquals(1, content.currentLine());

        it.next();
        assertEquals(2, content.currentLine());
        
        it.next();
        assertEquals(3, content.currentLine());
        
        it.next();
        assertEquals(4, content.currentLine());
        
        //media.close(); don't need to do it
    }
        
    ResultContent content;
        
    Store toc;
    Store result;
    MemoryStore repository;
        
    Media media;
}

class TOCContent implements Content {

    public long size() {
        return content.length();
    }

    public void create() {
    }

    public boolean exists() {
        return true;
    }

    public OutputStream output() {
        return null;
    }

    public InputStream input() {
        return new ByteArrayInputStream(content.getBytes());
    }
    
    String content = "dbURL, SQL, Name\n" 
                     "my:db:Database, SELECT * FROM HelloWorld, toc_1.csv";
}

class ResultContent implements Content {

    public long size() {
        return content.length();
    }

    public void create() {
    }

    public boolean exists() {
        return true;
    }

    public OutputStream output() {
        return null;
    }

    public InputStream input() {
        return new LocalReader(content);
    }
    
    public int currentLine() {
        return line;
    }
                         
    int line;
    
    String content = "Field, Field2\n" 
                     "1, Hello\n" 
                     "2, Hello\n" 
                     "3, Hello\n" ;
                     
    class LocalReader extends InputStream {

        int pos = 0;
        byte[] content;
                                         
        public LocalReader(String s) {
            content = s.getBytes();
        }

        public int read() {
            
            if (pos >= content.length) {
                return -1;
            }
            
            int c = content[pos++];
            
            if (c == '\n'line++;
            
            return c;
        }

        public int read(byte[] b, int off, int lenthrows IOException {
        
            // force len - 1 to prevent io objects to fill a buffer (cache)
                  
            int n = super.read(b, off, 1);
            
            if (n == -1return -1;
            
            return 1;
        
        }

    }
}