View Javadoc

1   package org.ogf.saga.logicalfile;
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.DoesNotExistException;
7   import org.ogf.saga.file.File;
8   import org.ogf.saga.namespace.Flags;
9   import org.ogf.saga.namespace.base.WriteBaseTest;
10  
11  /* ***************************************************
12  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
13  * ***             http://cc.in2p3.fr/             ***
14  * ***************************************************
15  * File:   LogicalFileWriteTest
16  * Author: Lionel.schwarz@in2p3.fr
17  * Date:   5 NOV 2013
18  * ***************************************************
19  * Description:                                      */
20  /**
21   *
22   */
23  public abstract class LogicalWriteTest extends WriteBaseTest {
24      protected LogicalWriteTest(String protocol) throws Exception {
25          super(protocol);
26      }
27  
28      @Test
29      public void test_addLocation() throws Exception {
30          if (m_file instanceof LogicalFile) {
31              // should be ignored (already exist)
32              ((LogicalFile)m_file).addLocation(m_physicalFileUrl);
33              assertEquals(
34                      1,
35                      ((LogicalFile)m_file).listLocations().size());
36  
37              
38              //Create m_physicalFileUrl2... will be removed by tearDown()
39              if(m_physicalFileUrl2 != null && !new java.io.File(m_physicalFileUrl2.getString()).exists()){
40  	            File physicalFile = (File) m_physicalDir.open(m_physicalFileUrl2, FLAGS_FILE);
41  	            Buffer buffer = BufferFactory.createBuffer(DEFAULT_CONTENT2.getBytes());
42  	            physicalFile.write(buffer);
43  	            physicalFile.close();
44              }
45  
46              // add
47              ((LogicalFile)m_file).addLocation(m_physicalFileUrl2);
48              assertEquals(
49                      2,
50                      ((LogicalFile)m_file).listLocations().size());
51          } else {
52              fail("Not an instance of class: LogicalFile");
53          }
54      }
55  
56      @Test
57      public void test_removeLocation() throws Exception {
58          if (m_file instanceof LogicalFile) {
59              // should throw an exception (does not exist)
60              try {
61                  ((LogicalFile)m_file).removeLocation(m_physicalFileUrl2);
62                  fail("Expected exception: "+ DoesNotExistException.class);
63              } catch(DoesNotExistException e) {
64                  assertEquals(
65                          1,
66                          ((LogicalFile)m_file).listLocations().size());
67              }
68  
69              // remove
70              ((LogicalFile)m_file).removeLocation(m_physicalFileUrl);
71              assertEquals(
72                      0,
73                      ((LogicalFile)m_file).listLocations().size());
74          } else {
75              fail("Not an instance of class: LogicalFile");
76          }
77      }
78  
79      @Test
80      public void test_updateLocation() throws Exception {
81          if (m_file instanceof LogicalFile) {
82          	//Create m_physicalFileUrl2... will be removed by tearDown()
83              if(m_physicalFileUrl2 != null && !new java.io.File(m_physicalFileUrl2.getString()).exists()){
84  	            File physicalFile = (File) m_physicalDir.open(m_physicalFileUrl2, FLAGS_FILE);
85  	            Buffer buffer = BufferFactory.createBuffer(DEFAULT_CONTENT2.getBytes());
86  	            physicalFile.write(buffer);
87  	            physicalFile.close();
88              }
89              
90              ((LogicalFile)m_file).updateLocation(m_physicalFileUrl, m_physicalFileUrl2);
91              assertEquals(
92                      1,
93                      ((LogicalFile)m_file).listLocations().size());
94              assertEquals(
95                      m_physicalFileUrl2.toString(),
96                      ((LogicalFile)m_file).listLocations().get(0).toString());
97          } else {
98              fail("Not an instance of class: LogicalFile");
99          }
100     }
101 
102     @Test
103     public void test_replicate() throws Exception {
104         if (m_file instanceof LogicalFile) {
105             // replicate
106             ((LogicalFile)m_file).replicate(m_physicalFileUrl2, Flags.NONE.getValue());
107             assertEquals(
108                     2,
109                     ((LogicalFile)m_file).listLocations().size());
110 
111             // read new replica
112             checkWrited(m_physicalFileUrl2, DEFAULT_CONTENT);
113         } else {
114             fail("Not an instance of class: LogicalFile");
115         }
116     }
117 }