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.assertEquals;
17 import static org.junit.Assert.assertFalse;
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assert.fail;
20 
21 import java.io.File;
22 
23 import hdf.hdf5lib.H5;
24 import hdf.hdf5lib.HDF5Constants;
25 import hdf.hdf5lib.exceptions.HDF5FunctionArgumentException;
26 import hdf.hdf5lib.structs.H5F_info2_t;
27 
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Ignore;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.TestName;
34 
35 public class TestH5Fparams {
36     @Rule public TestName testname = new TestName();
37 
38     @Before
checkOpenIDs()39     public void checkOpenIDs() {
40         assertTrue("H5 open ids is 0",H5.getOpenIDCount()==0);
41         System.out.print(testname.getMethodName());
42     }
43     @After
nextTestName()44     public void nextTestName() {
45         System.out.println();
46     }
47 
48     @Test(expected = NullPointerException.class)
testH5Fcreate_null()49     public void testH5Fcreate_null() throws Throwable {
50         H5.H5Fcreate(null, HDF5Constants.H5F_ACC_TRUNC,
51                 HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
52     }
53 
54     @Test(expected = NullPointerException.class)
testH5Fopen_null()55     public void testH5Fopen_null() throws Throwable {
56         H5.H5Fopen(null, HDF5Constants.H5F_ACC_RDWR, HDF5Constants.H5P_DEFAULT);
57     }
58 
59     @Test(expected = NullPointerException.class)
testH5Fis_hdf5_null()60     public void testH5Fis_hdf5_null() throws Throwable {
61         H5.H5Fis_hdf5(null);
62     }
63 
64     @Test(expected = NullPointerException.class)
testH5Fmount_null()65     public void testH5Fmount_null() throws Throwable {
66         H5.H5Fmount(-1, null, -1, HDF5Constants.H5P_DEFAULT);
67     }
68 
69     @Test(expected = NullPointerException.class)
testH5Funmount_null()70     public void testH5Funmount_null() throws Throwable {
71         H5.H5Funmount(-1, null);
72     }
73 
74     @Test
testH5Fis_hdf5_text()75     public void testH5Fis_hdf5_text() {
76         File txtFile = null;
77         boolean isH5 = false;
78 
79         try {
80             txtFile = new File("test.txt");
81             if (!txtFile.exists())
82                 txtFile.createNewFile();
83             isH5 = H5.H5Fis_hdf5("test.txt");
84         }
85         catch (Throwable err) {
86             fail("H5.H5Fis_hdf5 failed on test.txt: " + err);
87         }
88 
89         assertFalse(isH5);
90 
91         try {
92             txtFile.delete();
93         }
94         catch (SecurityException e) {
95             ;// e.printStackTrace();
96         }
97     }
98 
99     @Test//(expected = HDF5LibraryException.class)
testH5Fclose_negative()100     public void testH5Fclose_negative() throws Throwable {
101         // cannot close a file with negative id.
102         int fid = H5.H5Fclose(-1);
103         assertTrue(fid == 0);
104     }
105 
106     @Test
testH5Fcreate()107     public void testH5Fcreate() {
108         long fid = -1;
109         File file = null;
110 
111         try {
112             fid = H5.H5Fcreate("test.h5", HDF5Constants.H5F_ACC_TRUNC,
113                 HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
114             if (fid > 0) {
115                 H5.H5Fclose(fid);
116             }
117             file = new File("test.h5");
118         }
119         catch (Throwable err) {
120             fail("H5.H5Fopen: " + err);
121         }
122 
123         if (file.exists()) {
124             try {
125                 file.delete();
126             }
127             catch (SecurityException e) {
128                 ;// e.printStackTrace();
129             }
130         }
131     }
132 
133     @Test
testH5Fflush_global()134     public void testH5Fflush_global() {
135         long fid = -1;
136 
137         try {
138             fid = H5.H5Fcreate("test.h5", HDF5Constants.H5F_ACC_TRUNC,
139                     HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
140         }
141         catch (Throwable err) {
142             fail("H5.H5Fopen: " + err);
143         }
144 
145         try {
146             H5.H5Fflush(fid, HDF5Constants.H5F_SCOPE_GLOBAL);
147         }
148         catch (Throwable err) {
149             fail("H5.H5Fflush: " + err);
150         }
151 
152         try {
153             H5.H5Fclose(fid);
154         }
155         catch (Exception ex) {
156         }
157     }
158 
159     @Test
testH5Fflush_local()160     public void testH5Fflush_local() {
161         long fid = -1;
162 
163         try {
164             fid = H5.H5Fcreate("test.h5", HDF5Constants.H5F_ACC_TRUNC,
165                     HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
166         }
167         catch (Throwable err) {
168             fail("H5.H5Fopen: " + err);
169         }
170 
171         try {
172             H5.H5Fflush(fid, HDF5Constants.H5F_SCOPE_LOCAL);
173         }
174         catch (Throwable err) {
175             fail("H5.H5Fflush: " + err);
176         }
177 
178         try {
179             H5.H5Fclose(fid);
180         }
181         catch (Exception ex) {
182         }
183     }
184 
185     @Test
testH5Fget_info()186     public void testH5Fget_info() {
187         long fid = -1;
188 
189         try {
190             try {
191                 fid = H5.H5Fcreate("test.h5", HDF5Constants.H5F_ACC_TRUNC,
192                         HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
193             }
194             catch (Throwable err) {
195                 fail("H5.H5Fcreate: " + err);
196             }
197 
198             try {
199                 H5F_info2_t finfo = H5.H5Fget_info(fid);
200                 assertEquals(finfo.super_version, 0);
201                 assertEquals(finfo.free_version, 0);
202                 assertEquals(finfo.sohm_version, 0);
203             }
204             catch (Throwable err) {
205                 fail("H5.H5Fget_info: " + err);
206             }
207         }
208         catch (Exception e) {
209            e.printStackTrace();
210         }
211         finally {
212             try {H5.H5Fclose(fid);} catch (Exception ex) {}
213         }
214     }
215 
216     @Ignore//(expected = HDF5FunctionArgumentException.class)
testH5Fset_libver_bounds_invalidlow()217     public void testH5Fset_libver_bounds_invalidlow() throws Throwable {
218         long fid = -1;
219 
220         try {
221             try {
222                 fid = H5.H5Fcreate("test.h5", HDF5Constants.H5F_ACC_TRUNC,
223                         HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
224             }
225             catch (Throwable err) {
226                 fail("H5.H5Fcreate: " + err);
227             }
228             H5.H5Fset_libver_bounds(fid, 5, HDF5Constants.H5F_LIBVER_LATEST);
229         }
230         finally {
231             try {H5.H5Fclose(fid);} catch (Exception ex) {}
232         }
233     }
234 
235     @Ignore//(expected = HDF5FunctionArgumentException.class)
testH5Fset_libver_bounds_invalidhigh()236     public void testH5Fset_libver_bounds_invalidhigh() throws Throwable {
237         long fid = -1;
238 
239         try {
240             try {
241                 fid = H5.H5Fcreate("test.h5", HDF5Constants.H5F_ACC_TRUNC,
242                         HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
243             }
244             catch (Throwable err) {
245                 fail("H5.H5Fcreate: " + err);
246             }
247             H5.H5Fset_libver_bounds(fid, HDF5Constants.H5F_LIBVER_V110, HDF5Constants.H5F_LIBVER_V110+1);
248         }
249         finally {
250             try {H5.H5Fclose(fid);} catch (Exception ex) {}
251         }
252     }
253 }
254