1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 package test;
15 
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assert.fail;
19 
20 import java.io.File;
21 
22 import hdf.hdf5lib.H5;
23 import hdf.hdf5lib.HDF5Constants;
24 import hdf.hdf5lib.exceptions.HDF5LibraryException;
25 import hdf.hdf5lib.structs.H5G_info_t;
26 
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.TestName;
32 
33 public class TestH5Gbasic {
34     @Rule public TestName testname = new TestName();
35     private static final String H5_FILE = "testGb.h5";
36     long H5fid = -1;
37 
_createGroup(long fid, String name)38     private final long _createGroup(long fid, String name) {
39         long gid = -1;
40         try {
41             gid = H5.H5Gcreate(fid, name, HDF5Constants.H5P_DEFAULT,
42                         HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
43         }
44         catch (Throwable err) {
45             err.printStackTrace();
46             fail("H5.H5Gcreate: " + err);
47         }
48 
49         return gid;
50     }
51 
_deleteFile(String filename)52     private final void _deleteFile(String filename) {
53         File file = new File(filename);
54 
55         if (file.exists()) {
56             try {file.delete();} catch (SecurityException e) {}
57         }
58     }
59 
60     @Before
createH5file()61     public void createH5file()
62             throws HDF5LibraryException, NullPointerException {
63         assertTrue("H5 open ids is 0",H5.getOpenIDCount()==0);
64         System.out.print(testname.getMethodName());
65 
66         H5fid = H5.H5Fcreate(H5_FILE, HDF5Constants.H5F_ACC_TRUNC,
67                 HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
68         H5.H5Fflush(H5fid, HDF5Constants.H5F_SCOPE_LOCAL);
69     }
70 
71     @After
deleteH5file()72     public void deleteH5file() throws HDF5LibraryException {
73         if (H5fid > 0) {
74             try {H5.H5Fclose(H5fid);} catch (Exception ex) {}
75         }
76         _deleteFile(H5_FILE);
77         System.out.println();
78     }
79 
80     @Test//(expected = HDF5LibraryException.class)
testH5Gclose_invalid()81     public void testH5Gclose_invalid() throws Throwable {
82         long gid = H5.H5Gclose(-1);
83         assertTrue(gid == 0);
84     }
85 
86     @Test(expected = NullPointerException.class)
testH5Gcreate_null()87     public void testH5Gcreate_null() throws Throwable {
88         long gid = -1;
89 
90         // it should fail because the group name is null
91         gid = H5.H5Gcreate(H5fid, null, HDF5Constants.H5P_DEFAULT,
92                     HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
93 
94         try {H5.H5Gclose(gid);} catch (Exception ex) {}
95     }
96 
97     @Test(expected = HDF5LibraryException.class)
testH5Gcreate_invalid()98     public void testH5Gcreate_invalid() throws Throwable {
99         H5.H5Gcreate(-1, "Invalid ID", HDF5Constants.H5P_DEFAULT,
100                     HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
101     }
102 
103     @Test
testH5Gcreate()104     public void testH5Gcreate() {
105         long gid = -1;
106         try {
107             gid = H5.H5Gcreate(H5fid, "/testH5Gcreate",
108                         HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT,
109                         HDF5Constants.H5P_DEFAULT);
110         }
111         catch (Throwable err) {
112             err.printStackTrace();
113             fail("H5.H5Gcreate: " + err);
114         }
115         assertTrue(gid > 0);
116 
117         try {H5.H5Gclose(gid);} catch (Exception ex) {}
118     }
119 
120     @Test
testH5Gclose()121     public void testH5Gclose() {
122         long gid = _createGroup(H5fid, "/testH5Gcreate");
123         assertTrue(gid > 0);
124 
125         try {
126             H5.H5Gclose(gid);
127         }
128         catch (Throwable err) {
129             fail("H5Gclose: " + err);
130         }
131     }
132 
133     @Test(expected = HDF5LibraryException.class)
testH5Gcreate_exists()134     public void testH5Gcreate_exists() throws Throwable {
135         long gid = _createGroup(H5fid, "/testH5Gcreate");
136         assertTrue(gid > 0);
137 
138         try {H5.H5Gclose(gid);} catch (Exception ex) {}
139 
140         // it should failed now because the group already exists in file
141         gid = H5.H5Gcreate(H5fid, "/testH5Gcreate",
142                     HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT,
143                     HDF5Constants.H5P_DEFAULT);
144     }
145 
146     @Test
testH5Gcreate_anon()147     public void testH5Gcreate_anon() {
148         long gid = -1;
149         try {
150             gid = H5.H5Gcreate_anon(H5fid, HDF5Constants.H5P_DEFAULT,
151                     HDF5Constants.H5P_DEFAULT);
152         }
153         catch (Throwable err) {
154             err.printStackTrace();
155             fail("H5.H5Gcreate_anon: " + err);
156         }
157         assertTrue(gid > 0);
158 
159         try {H5.H5Gclose(gid);} catch (Exception ex) {}
160     }
161 
162     @Test(expected = NullPointerException.class)
testH5Gopen_null()163     public void testH5Gopen_null() throws Throwable {
164         long gid = -1;
165 
166         gid = H5.H5Gopen(H5fid, null, HDF5Constants.H5P_DEFAULT);
167 
168         try {H5.H5Gclose(gid);} catch (Exception ex) {}
169     }
170 
171     @Test(expected = HDF5LibraryException.class)
testH5Gopen_invalid()172     public void testH5Gopen_invalid() throws Throwable {
173         H5.H5Gopen(-1, "Invalid ID", HDF5Constants.H5P_DEFAULT);
174     }
175 
176     @Test(expected = HDF5LibraryException.class)
testH5Gopen_not_exists()177     public void testH5Gopen_not_exists() throws Throwable {
178         long gid = -1;
179 
180          gid = H5.H5Gopen(H5fid, "Never_created", HDF5Constants.H5P_DEFAULT);
181 
182         try {H5.H5Gclose(gid);} catch (Exception ex) {}
183     }
184 
185     @Test
testH5Gopen()186     public void testH5Gopen() {
187         long gid = _createGroup(H5fid, "/testH5Gcreate");
188         assertTrue(gid > 0);
189 
190         try {H5.H5Gclose(gid);} catch (Exception ex) {}
191 
192         try {
193             gid = H5.H5Gopen(H5fid, "/testH5Gcreate",
194                         HDF5Constants.H5P_DEFAULT);
195         }
196         catch (Throwable err) {
197             err.printStackTrace();
198             fail("H5.H5Gopen: " + err);
199         }
200         assertTrue(gid > 0);
201 
202         try {H5.H5Gclose(gid);} catch (Exception ex) {}
203     }
204 
205     @Test(expected = HDF5LibraryException.class)
testH5Gget_create_plist_invalid()206     public void testH5Gget_create_plist_invalid() throws Throwable {
207         H5.H5Gget_create_plist(-1);
208     }
209 
210     @Test
testH5Gget_create_plist()211     public void testH5Gget_create_plist() {
212         long pid = -1;
213         long gid = _createGroup(H5fid, "/testH5Gcreate");
214         assertTrue(gid > 0);
215 
216         try {
217             pid = H5.H5Gget_create_plist(gid);
218         }
219         catch (Throwable err) {
220             try {H5.H5Gclose(gid);} catch (Exception ex) {}
221             err.printStackTrace();
222             fail("H5.H5Gget_create_plist: " + err);
223         }
224         assertTrue(pid > 0);
225 
226         try {H5.H5Pclose(pid);} catch (Exception ex) {}
227 
228         try {H5.H5Gclose(gid);} catch (Exception ex) {}
229     }
230 
231     @Test(expected = HDF5LibraryException.class)
testH5Gget_info_invalid()232     public void testH5Gget_info_invalid() throws Throwable {
233         H5.H5Gget_info(-1);
234     }
235 
236     @Test
testH5Gget_info()237     public void testH5Gget_info() {
238         H5G_info_t info = null;
239         long gid = _createGroup(H5fid, "/testH5Gcreate");
240         assertTrue(gid > 0);
241 
242         try {
243             info = H5.H5Gget_info(gid);
244         }
245         catch (Throwable err) {
246             try {H5.H5Gclose(gid);} catch (Exception ex) {}
247             err.printStackTrace();
248             fail("H5.H5Gget_info: " + err);
249         }
250         assertNotNull(info);
251 
252         try {H5.H5Gclose(gid);} catch (Exception ex) {}
253     }
254 
255     @Test(expected = NullPointerException.class)
testH5Gget_info_by_name_null()256     public void testH5Gget_info_by_name_null() throws Throwable {
257         H5.H5Gget_info_by_name(-1, null, HDF5Constants.H5P_DEFAULT);
258     }
259 
260     @Test(expected = HDF5LibraryException.class)
testH5Gget_info_by_name_invalid()261     public void testH5Gget_info_by_name_invalid() throws Throwable {
262         H5.H5Gget_info_by_name(-1, "/testH5Gcreate", HDF5Constants.H5P_DEFAULT);
263     }
264 
265     @Test(expected = HDF5LibraryException.class)
testH5Gget_info_by_name_not_exists()266     public void testH5Gget_info_by_name_not_exists() throws Throwable {
267         H5.H5Gget_info_by_name(H5fid, "/testH5Gcreate",
268                 HDF5Constants.H5P_DEFAULT);
269     }
270 
271     @Test
testH5Gget_info_by_name()272     public void testH5Gget_info_by_name() {
273         H5G_info_t info = null;
274         long gid = _createGroup(H5fid, "/testH5Gcreate");
275         assertTrue(gid > 0);
276 
277         try {
278             info = H5.H5Gget_info_by_name(gid, "/testH5Gcreate",
279                     HDF5Constants.H5P_DEFAULT);
280         }
281         catch (Throwable err) {
282             try {H5.H5Gclose(gid);} catch (Exception ex) {}
283             err.printStackTrace();
284             fail("H5.H5Gget_info_by_name: " + err);
285         }
286         assertNotNull(info);
287 
288         try {H5.H5Gclose(gid);} catch (Exception ex) {}
289     }
290 
291     @Test
testH5Gget_info_by_name_fileid()292     public void testH5Gget_info_by_name_fileid() {
293         H5G_info_t info = null;
294         long gid = _createGroup(H5fid, "/testH5Gcreate");
295         assertTrue(gid > 0);
296         try {H5.H5Gclose(gid);} catch (Exception ex) {}
297 
298         try {
299             info = H5.H5Gget_info_by_name(H5fid, "/testH5Gcreate",
300                     HDF5Constants.H5P_DEFAULT);
301         }
302         catch (Throwable err) {
303             try {H5.H5Gclose(gid);} catch (Exception ex) {}
304             err.printStackTrace();
305             fail("H5.H5Gget_info_by_name: " + err);
306         }
307         assertNotNull(info);
308 
309         try {H5.H5Gclose(gid);} catch (Exception ex) {}
310     }
311 
312     @Test(expected = NullPointerException.class)
testH5Gget_info_by_idx_null()313     public void testH5Gget_info_by_idx_null() throws Throwable {
314         H5.H5Gget_info_by_idx(-1, null, HDF5Constants.H5_INDEX_NAME,
315                 HDF5Constants.H5_ITER_INC, 1L, HDF5Constants.H5P_DEFAULT);
316     }
317 
318     @Test(expected = HDF5LibraryException.class)
testH5Gget_info_by_idx_invalid()319     public void testH5Gget_info_by_idx_invalid() throws Throwable {
320         H5.H5Gget_info_by_idx(-1, "/testH5Gcreate", HDF5Constants.H5_INDEX_NAME,
321                 HDF5Constants.H5_ITER_INC, 1L, HDF5Constants.H5P_DEFAULT);
322     }
323 
324     @Test(expected = HDF5LibraryException.class)
testH5Gget_info_by_idx_not_exists()325     public void testH5Gget_info_by_idx_not_exists() throws Throwable {
326         H5.H5Gget_info_by_idx(H5fid, "/testH5Gcreate",
327                 HDF5Constants.H5_INDEX_NAME, HDF5Constants.H5_ITER_INC, 1L,
328                 HDF5Constants.H5P_DEFAULT);
329     }
330 
331     @Test
testH5Gget_info_by_idx()332     public void testH5Gget_info_by_idx() {
333         H5G_info_t info = null;
334         long gid = _createGroup(H5fid, "/testH5Gcreate");
335         assertTrue(gid > 0);
336 
337         try {
338             info = H5.H5Gget_info_by_idx(gid, "/", HDF5Constants.H5_INDEX_NAME,
339                     HDF5Constants.H5_ITER_INC, 0, HDF5Constants.H5P_DEFAULT);
340         }
341         catch (Throwable err) {
342             err.printStackTrace();
343             fail("H5.H5Gget_info_by_idx: " + err);
344         }
345         assertNotNull(info);
346 
347         try {H5.H5Gclose(gid);} catch (Exception ex) {}
348     }
349 
350     @Test
testH5Gget_info_by_idx_fileid()351     public void testH5Gget_info_by_idx_fileid() {
352         H5G_info_t info = null;
353         long gid = _createGroup(H5fid, "/testH5Gcreate");
354         assertTrue(gid > 0);
355         try {H5.H5Gclose(gid);} catch (Exception ex) {}
356 
357         try {
358             info = H5.H5Gget_info_by_idx(H5fid, "/",
359                     HDF5Constants.H5_INDEX_NAME, HDF5Constants.H5_ITER_INC, 0,
360                     HDF5Constants.H5P_DEFAULT);
361         }
362         catch (Throwable err) {
363             err.printStackTrace();
364             fail("H5.H5Gget_info_by_idx: " + err);
365         }
366         assertNotNull(info);
367     }
368 
369     @Test(expected = HDF5LibraryException.class)
testH5Gflush_invalid()370     public void testH5Gflush_invalid() throws Throwable {
371         H5.H5Gflush(-1);
372     }
373 
374     @Test(expected = HDF5LibraryException.class)
testH5Grefresh_invalid()375     public void testH5Grefresh_invalid() throws Throwable {
376         H5.H5Grefresh(-1);
377     }
378 
379 }
380