View Javadoc

1   package org.ogf.saga.file;
2   
3   import org.junit.Test;
4   import org.ogf.saga.buffer.Buffer;
5   import org.ogf.saga.buffer.BufferFactory;
6   import org.ogf.saga.error.AlreadyExistsException;
7   import org.ogf.saga.error.DoesNotExistException;
8   import org.ogf.saga.namespace.Flags;
9   import org.ogf.saga.namespace.NSEntry;
10  import org.ogf.saga.namespace.NSFactory;
11  import org.ogf.saga.namespace.base.WriteBaseTest;
12  import org.ogf.saga.url.URL;
13  
14  /* ***************************************************
15  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
16  * ***             http://cc.in2p3.fr/             ***
17  * ***************************************************
18  * File:   FileWriteTest
19  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
20  * Author: lionel.schwarz@in2p3.fr
21  * Date:   5 NOV 2013
22  * ***************************************************
23  * Description:                                      */
24  /**
25   *
26   */
27  public abstract class WriteTest extends WriteBaseTest {
28      // test data
29      private static final String DEFAULT_CONTENT2 = "Another text !";
30      private static final String DEFAULT_ENCODED_FILENAME = "fileéàê.txt";
31  
32      protected WriteTest(String protocol) throws Exception {
33          super(protocol);
34      }
35  
36      @Test(expected = DoesNotExistException.class)
37      public void test_write_nocreate()throws Exception {
38          if (m_file instanceof File) {
39              NSFactory.createNSEntry(m_session, createURL(m_dirUrl, "ThisFileDoesNotExist"), Flags.WRITE.getValue()).close();
40          } else {
41          	fail("Not an instance of class: File");
42          }
43      }
44  
45      @Test
46      public void test_write_nooverwrite() throws Exception {
47          if (m_file instanceof File) {
48              try {
49                  NSFactory.createNSEntry(m_session, m_fileUrl, Flags.WRITE.or(Flags.EXCL)).close();
50                  fail("Expected AlreadyExist exception");
51              } catch(AlreadyExistsException e) {
52                  checkWrited(m_fileUrl, DEFAULT_CONTENT);
53              }
54          } else {
55          	fail("Not an instance of class: File");
56          }
57      }
58  
59      @Test
60      public void test_write_encoded_filename() throws Exception {
61          URL fileUrl = createURL(m_subDirUrl, DEFAULT_ENCODED_FILENAME);
62          NSEntry file = m_dir.open(fileUrl, FLAGS_FILE);
63          Buffer buffer = BufferFactory.createBuffer(DEFAULT_CONTENT2.getBytes());
64          ((File)file).write(buffer);
65          file.close();
66          try {
67              NSFactory.createNSEntry(m_session, createURL(m_subDirUrl, DEFAULT_ENCODED_FILENAME), Flags.WRITE.or(Flags.EXCL)).close();
68              fail("Expected AlreadyExist exception");
69          } catch(AlreadyExistsException e) {
70          }
71          checkWrited(fileUrl, DEFAULT_CONTENT2);
72      }
73      
74      @Test
75      public void test_write_overwrite() throws Exception {
76          if (m_file instanceof File) {
77              Buffer buffer = BufferFactory.createBuffer(DEFAULT_CONTENT2.getBytes());
78              File writer = (File) NSFactory.createNSEntry(m_session, m_fileUrl, Flags.WRITE.getValue());
79              writer.write(buffer);
80              writer.close();
81              checkWrited(m_fileUrl, DEFAULT_CONTENT2);
82          } else {
83          	fail("Not an instance of class: File");
84          }
85      }
86  
87      @Test
88      public void test_write_append() throws Exception {
89          if (m_file instanceof File) {
90              Buffer buffer = BufferFactory.createBuffer(DEFAULT_CONTENT2.getBytes());
91              File writer = (File) NSFactory.createNSEntry(m_session, m_fileUrl, Flags.WRITE.or(Flags.APPEND));
92              writer.write(buffer);
93              writer.close();
94              checkWrited(m_fileUrl, DEFAULT_CONTENT+DEFAULT_CONTENT2);
95          } else {
96          	fail("Not an instance of class: File");
97          }
98      }
99      
100     @Test
101     public void test_read_and_write() throws Exception {
102         if (m_file instanceof File) {
103             Buffer buffer;
104             buffer = BufferFactory.createBuffer(DEFAULT_CONTENT.getBytes());
105             File reader = (File) NSFactory.createNSEntry(m_session, m_fileUrl, Flags.READ.or(Flags.WRITE.or(Flags.CREATE.getValue())));
106             // read
107             reader.read(buffer);
108             // check size
109             //assertEquals(0, reader.getSize());
110             // write
111             buffer = BufferFactory.createBuffer(DEFAULT_CONTENT2.getBytes());
112             reader.write(buffer);
113             // check new size
114             assertEquals(DEFAULT_CONTENT2.length(), reader.getSize());
115             // check new content
116             reader.close();
117             checkWrited(m_fileUrl, DEFAULT_CONTENT2);
118         } else {
119         	fail("Not an instance of class: File");
120         }
121     }
122 
123     @Test
124     public void test_outputStream_overwrite() throws Exception {
125         if (m_file instanceof File) {
126         	FileOutputStream fos = FileFactory.createFileOutputStream(m_session, m_fileUrl, false);
127         	fos.write(DEFAULT_CONTENT2.getBytes());
128         	fos.flush();
129         	fos.close();
130             checkWrited(m_fileUrl, DEFAULT_CONTENT2);
131         } else {
132         	fail("Not an instance of class: File");
133         }
134     }
135 
136     @Test
137     public void test_outputStream_append() throws Exception {
138         if (m_file instanceof File) {
139         	FileOutputStream fos = FileFactory.createFileOutputStream(m_session, m_fileUrl, true);
140         	fos.write(DEFAULT_CONTENT2.getBytes());
141         	fos.flush();
142         	fos.close();
143             checkWrited(m_fileUrl, DEFAULT_CONTENT+DEFAULT_CONTENT2);
144         } else {
145         	fail("Not an instance of class: File");
146         }
147     }
148     
149 }