View Javadoc

1   package org.ogf.saga.namespace.abstracts;
2   
3   import org.junit.After;
4   import org.junit.Before;
5   import org.ogf.saga.buffer.Buffer;
6   import org.ogf.saga.buffer.BufferFactory;
7   import org.ogf.saga.file.File;
8   import org.ogf.saga.logicalfile.LogicalFile;
9   import org.ogf.saga.namespace.*;
10  import org.ogf.saga.session.SessionFactory;
11  import org.ogf.saga.url.URL;
12  import org.ogf.saga.url.URLFactory;
13  
14  import java.util.List;
15  
16  /* ***************************************************
17  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
18  * ***             http://cc.in2p3.fr/             ***
19  * ***************************************************
20  * File:   AbstractNSCopyTest
21  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
22  * Date:   2 juil. 2007
23  * ***************************************************
24  * Description:                                      */
25  /**
26   *
27   */
28  public abstract class AbstractDataMovement extends AbstractDirectory {
29      protected static final int FLAGS_BYPASSEXIST = 4096;
30  
31      // test data
32      protected static final String DEFAULT_DIRNAME_2 = "dir2/";
33      protected static final String DEFAULT_FILENAME_2 = "file2.txt";
34      protected static final String DEFAULT_CONTENT_2 = "Content of file 2 on base2.url...\n";
35  
36      // configuration
37      protected URL m_dirUrl2;
38      protected URL m_subDirUrl2;
39      protected URL m_fileUrl2;
40  
41      // setup
42      protected NSDirectory m_dir2;
43  
44      public AbstractDataMovement(String protocol, String targetProtocol) throws Exception {
45          super(protocol);
46          URL baseUrl2;
47          if (protocol.equals(targetProtocol)) {
48              if (getOptionalProperty(protocol, CONFIG_BASE2_URL) != null) {
49                  baseUrl2 = URLFactory.createURL(getOptionalProperty(protocol, CONFIG_BASE2_URL));
50              } else {
51                  baseUrl2 = URLFactory.createURL(getRequiredProperty(protocol, CONFIG_BASE_URL));
52              }
53          } else {
54              baseUrl2 = URLFactory.createURL(getRequiredProperty(targetProtocol, CONFIG_BASE_URL));
55          }
56          m_dirUrl2 = createURL(baseUrl2, DEFAULT_DIRNAME_2);
57          m_subDirUrl2 = createURL(m_dirUrl2, DEFAULT_SUBDIRNAME);
58          m_fileUrl2 = createURL(m_subDirUrl2, DEFAULT_FILENAME_2);
59          if (m_session==null && baseUrl2.getFragment()!=null) {
60              m_session = SessionFactory.createSession(true);
61          }
62          if (m_physicalDirUrl ==null && getOptionalProperty(targetProtocol, CONFIG_PHYSICAL_PROTOCOL) != null) {
63              String physicalProtocol = getOptionalProperty(targetProtocol, CONFIG_PHYSICAL_PROTOCOL);
64              URL basePhysicalUrl = URLFactory.createURL(getRequiredProperty(physicalProtocol, CONFIG_BASE_URL));
65              m_physicalDirUrl = createURL(basePhysicalUrl, DEFAULT_DIRNAME);
66              m_physicalFileUrl = createURL(m_physicalDirUrl, DEFAULT_PHYSICAL);
67              m_physicalFileUrl2 = createURL(m_physicalDirUrl, DEFAULT_PHYSICAL2);
68          }
69      }
70  
71      @Before
72  	public void setUp() throws Exception {
73          super.setUp();
74          try {
75          	if (m_physicalFileUrl2 != null) {
76                  // will be removed by super.tearDown()
77                  File physicalFile = (File) m_physicalDir.open(m_physicalFileUrl2, FLAGS_FILE);
78                  Buffer buffer = BufferFactory.createBuffer(DEFAULT_CONTENT_2.getBytes());
79                  physicalFile.write(buffer);
80                  physicalFile.close();
81              }
82              if (m_dirUrl2 != null) {
83                  // to be removed by this.tearDown()
84                  m_dir2 = NSFactory.createNSDirectory(m_session, m_dirUrl2, FLAGS_DIR);
85                  if (m_fileUrl2 != null) {
86                      NSEntry file2 = m_dir2.open(m_fileUrl2, FLAGS_FILE);
87                      if (file2 instanceof File) {
88                          Buffer buffer = BufferFactory.createBuffer(DEFAULT_CONTENT_2.getBytes());
89                          ((File)file2).write(buffer);
90                      } else if (file2 instanceof LogicalFile) {
91                          ((LogicalFile)file2).addLocation(m_physicalFileUrl2);
92                      }
93                      file2.close();
94                  }
95              }
96          } catch(Exception e) {
97  //            try{this.tearDown();}catch(Exception e2){/**/}
98              throw e;
99          }
100     }
101 
102     @After
103 	public void tearDown() throws Exception {
104         if (m_dir2 != null) {
105             m_dir2.remove(Flags.RECURSIVE.getValue());
106             m_dir2.close();
107         }
108         super.tearDown();
109     }
110 
111     //////////////////////////////////////// protected methods ////////////////////////////////////////
112     protected void checkCopied(URL url, String expectedContent) throws Exception {
113     	checkCopied(url, expectedContent, 1024);
114     }
115     
116     protected void checkCopied(URL url, String expectedContent, int bufferSize) throws Exception {
117         NSEntry entry = NSFactory.createNSEntry(m_session, url, Flags.READ.getValue());
118         File reader;
119         if (entry instanceof LogicalFile) {
120             List<URL> physicalUrls = ((LogicalFile)entry).listLocations();
121             assertNotNull(physicalUrls);
122             assertTrue(physicalUrls.size() > 0);
123             reader = (File) NSFactory.createNSEntry(m_session, physicalUrls.get(0), Flags.READ.getValue());
124         } else {
125             reader = (File) entry;
126         }
127         Buffer buffer = BufferFactory.createBuffer(bufferSize);
128         int len = reader.read(buffer);
129         assertEquals(
130                 expectedContent.length(),
131                 len);
132         assertEquals(
133                 expectedContent,
134                 new String(buffer.getData(), 0, len));
135         reader.close();
136         entry.close();
137     }
138 }