View Javadoc

1   package org.ogf.saga.file;
2   
3   import java.io.BufferedReader;
4   import java.io.ByteArrayOutputStream;
5   import java.io.InputStreamReader;
6   
7   import org.junit.Ignore;
8   import org.junit.Test;
9   import org.ogf.saga.buffer.Buffer;
10  import org.ogf.saga.buffer.BufferFactory;
11  import org.ogf.saga.namespace.Flags;
12  import org.ogf.saga.namespace.NSFactory;
13  import org.ogf.saga.namespace.base.ReadBaseTest;
14  
15  /* ***************************************************
16  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
17  * ***             http://cc.in2p3.fr/             ***
18  * ***************************************************
19  * File:   FileReadTest
20  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
21  * Author: lionel.schwarz@in2p3.fr
22  * Date:   5 NOV 2013
23  * ***************************************************
24  * Description:                                      */
25  /**
26   *
27   */
28  public abstract class ReadTest extends ReadBaseTest {
29      protected ReadTest(String protocol) throws Exception {
30          super(protocol);
31      }
32  
33      @Test
34      public void test_getSize() throws Exception {
35          if (m_file instanceof File) {
36              assertEquals(
37                      DEFAULT_CONTENT.length(),
38                      ((File)m_file).getSize());
39          } else {
40          	fail("Not an instance of class: File");
41          }
42      }
43  
44      @Test
45      public void test_read_applicationManagedBuffer() throws Exception {
46          if (m_file instanceof File) {
47              Buffer buffer = BufferFactory.createBuffer(new byte[1024]);
48              File reader = (File) NSFactory.createNSEntry(m_session, m_fileUrl, Flags.READ.getValue());
49              reader.read(buffer);
50              byte[] bytes = new byte[DEFAULT_CONTENT.length()];
51              System.arraycopy(buffer.getData(), 0, bytes, 0, DEFAULT_CONTENT.length());
52              assertEquals(
53                      DEFAULT_CONTENT,
54                      new String(bytes));
55              reader.close();
56          } else {
57          	fail("Not an instance of class: File");
58          }
59      }
60  
61      @Test
62      public void test_read() throws Exception {
63          if (m_file instanceof File) {
64              checkWrited(m_fileUrl, DEFAULT_CONTENT);
65          } else {
66          	fail("Not an instance of class: File");
67          }
68      }
69  
70      @Test
71      public void test_inputStream() throws Exception {
72      	FileInputStream fis = FileFactory.createFileInputStream(m_session, m_fileUrl);
73      	ByteArrayOutputStream baos = new ByteArrayOutputStream();
74      	int read;
75      	while ((read=fis.read()) != -1) {
76      		baos.write(read);
77      	}
78      	fis.close();
79      	assertEquals(
80      			DEFAULT_CONTENT,
81      			baos.toString());
82      	baos.close();
83      }
84      
85      @Test
86      @Ignore
87      public void test_seek() throws Exception {
88          // not implemented
89      }
90  }