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.JSAGABaseTest;
6   import org.ogf.saga.buffer.Buffer;
7   import org.ogf.saga.buffer.BufferFactory;
8   import org.ogf.saga.error.DoesNotExistException;
9   import org.ogf.saga.error.NotImplementedException;
10  import org.ogf.saga.file.Directory;
11  import org.ogf.saga.file.File;
12  import org.ogf.saga.logicalfile.LogicalFile;
13  import org.ogf.saga.namespace.Flags;
14  import org.ogf.saga.namespace.NSDirectory;
15  import org.ogf.saga.namespace.NSEntry;
16  import org.ogf.saga.namespace.NSFactory;
17  import org.ogf.saga.session.Session;
18  import org.ogf.saga.session.SessionFactory;
19  import org.ogf.saga.url.URL;
20  import org.ogf.saga.url.URLFactory;
21  
22  /* ***************************************************
23  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
24  * ***             http://cc.in2p3.fr/             ***
25  * ***************************************************
26  * File:   DataBaseTest.java
27  * Author: L Schwarz (lionel.schwarz@in2p3.fr)
28  * Date:   5 NOV 2013
29  * ***************************************************
30  * Description:                                      */
31  /**
32   *
33   */
34  public abstract class AbstractData extends JSAGABaseTest {
35      // test data
36      protected static final String DEFAULT_DIRNAME = "dir/";
37      protected static final String DEFAULT_SUBDIRNAME = "subdir/";
38      protected static final String DEFAULT_FILENAME = "file1.txt";
39      protected static final String DEFAULT_FILEPATTERN = "file*";
40      protected static final String DEFAULT_CONTENT = "Content of file 1...\n";
41      protected static final String DEFAULT_CONTENT2 = "Content of file 2...\n";
42      protected static final String DEFAULT_PHYSICAL = "physical1.txt";
43      protected static final String DEFAULT_PHYSICAL2 = "physical2.txt";
44      protected static final int FLAGS_DIR = Flags.CREATE.or(Flags.EXCL);
45      protected static final int FLAGS_FILE = Flags.WRITE.or(Flags.EXCL.or(Flags.CREATEPARENTS));
46  
47      // configuration
48      protected URL m_dirUrl;
49      protected URL m_subDirUrl;
50      protected URL m_fileUrl;
51      protected URL m_physicalDirUrl;
52      protected URL m_physicalFileUrl;
53      protected URL m_physicalFileUrl2;
54      protected Session m_session;
55  
56      // setup
57      protected NSDirectory m_dir;
58      protected NSEntry m_file;
59      protected Directory m_physicalDir;
60      protected boolean m_toBeRemoved;
61  
62      public AbstractData(String protocol) throws Exception {
63          super();
64  
65          // configure
66          URL baseUrl = URLFactory.createURL(getRequiredProperty(protocol, CONFIG_BASE_URL));
67          m_dirUrl = createURL(baseUrl, DEFAULT_DIRNAME);
68          m_subDirUrl = createURL(m_dirUrl, DEFAULT_SUBDIRNAME);
69          m_fileUrl = createURL(m_subDirUrl, DEFAULT_FILENAME);
70          m_session = SessionFactory.createSession(true);
71          if (getOptionalProperty(protocol, CONFIG_PHYSICAL_PROTOCOL) != null) {
72              String physicalProtocol = getOptionalProperty(protocol, CONFIG_PHYSICAL_PROTOCOL);
73              URL basePhysicalUrl = URLFactory.createURL(getRequiredProperty(physicalProtocol, CONFIG_BASE_URL));
74              m_physicalDirUrl = createURL(basePhysicalUrl, DEFAULT_DIRNAME);
75              m_physicalFileUrl = createURL(m_physicalDirUrl, DEFAULT_PHYSICAL);
76              m_physicalFileUrl2 = createURL(m_physicalDirUrl, DEFAULT_PHYSICAL2);
77          }
78      }
79  
80      /** Implicitely invoked before executing each test method */
81      @Before
82      public void setUp() throws Exception {
83          try {
84              // read-write protocol
85              m_toBeRemoved = true;
86              m_dir = NSFactory.createNSDirectory(m_session, m_dirUrl, FLAGS_DIR);
87              m_file = m_dir.open(m_fileUrl, FLAGS_FILE);
88              if (m_file instanceof File) {
89                  Buffer buffer = BufferFactory.createBuffer(DEFAULT_CONTENT.getBytes());
90                  ((File)m_file).write(buffer);
91              } else if (m_file instanceof LogicalFile) {
92                  if (m_physicalDirUrl == null) {
93                      throw new Exception("Configuration is missing required property: "+CONFIG_PHYSICAL_PROTOCOL);
94                  }
95                  // create physical file
96                  m_physicalDir = (Directory) NSFactory.createNSDirectory(m_session, m_physicalDirUrl, FLAGS_DIR);
97                  File physicalFile = (File) m_physicalDir.open(m_physicalFileUrl, FLAGS_FILE);
98                  Buffer buffer = BufferFactory.createBuffer(DEFAULT_CONTENT.getBytes());
99                  physicalFile.write(buffer);
100                 physicalFile.close();
101                 // register it
102                 ((LogicalFile)m_file).addLocation(m_physicalFileUrl);
103             }
104             m_file.close();
105         } catch(NotImplementedException e) {
106             // read-only protocol
107             m_toBeRemoved = false;
108             try {
109                 m_dir = NSFactory.createNSDirectory(m_session, m_dirUrl, Flags.NONE.getValue());
110                 m_file = m_dir.open(m_fileUrl, Flags.NONE.getValue());
111             } catch(DoesNotExistException e2) {
112                 throw new DoesNotExistException("Please create the expected files and directories for test suite (http://software.in2p3.fr/jsaga/latest-release/faq.html#create-test-entries)", e2);
113             }
114         } catch(Exception e) {
115 //            try{this.tearDown();}catch(Exception e2){/**/}
116             throw e;
117         }
118     }
119 
120     /** Implicitely invoked after executing each test method */
121     @After
122     public void tearDown() throws Exception {
123         if (m_file instanceof LogicalFile &&  m_physicalDir != null) {
124             m_physicalDir.remove(Flags.RECURSIVE.getValue());
125             m_physicalDir.close();
126         }
127 
128         if (m_file != null) {
129             m_file.close();
130         }
131         if (m_toBeRemoved && m_dir != null) {
132             m_dir.remove(Flags.RECURSIVE.getValue());
133         }
134         if (m_dir != null) {
135             m_dir.close();
136         }
137 //        super.tearDown();
138     }
139 
140     //////////////////////////////////////// protected methods ////////////////////////////////////////
141 
142     protected void checkWrited(URL url, String expected) throws Exception {
143         Buffer buffer = BufferFactory.createBuffer(1024);
144         File reader = (File) NSFactory.createNSEntry(m_session, url, Flags.READ.getValue());
145         int len = reader.read(buffer);
146         assertEquals(
147                 expected,
148                 new String(buffer.getData(), 0, len));
149         reader.close();
150     }
151 }