4242 */
4343public final class TestUtil {
4444
45- private static final String userDir = System .getProperty ("user.dir" );
46-
4745 /**
4846 * TestNG group constants
4947 */
5048 public static final String GENERATE_JAVA_FILES = "generate_java_files" ;
5149 public static final String CHECK_CPP_FILES = "check_cpp_files" ;
5250 public static final String CHECK_GO_FILES = "check_go_files" ;
51+ public static final String CHECK_RUST_FILES = "check_rust_files" ;
5352 public static final String CHECK_CPP_HISTORICAL_FILES = "check_cpp_historical_files" ;
5453
5554 /**
@@ -67,34 +66,74 @@ public final class TestUtil {
6766 */
6867 public static final Path goPath = Path .of ("." , "serialization_test_data" , "go_generated_files" );
6968
69+ /**
70+ * The project relative Path for Rust serialized sketches to be tested by Java.
71+ */
72+ public static final Path rustPath = Path .of ("." , "serialization_test_data" , "rust_generated_files" );
73+
7074 /**
7175 * The project relative Path for /src/test/resources
7276 */
7377 public static final Path resPath = Path .of ("." ,"src" ,"test" ,"resources" );
7478
7579
76- //Get Resources
77-
78- private static final int BUF_SIZE = 1 << 13 ;
79-
80- public static byte [] getFileBytes (Path basePath , String fileName ) throws RuntimeException {
80+ /**
81+ * Gets all the bytes of a file as a byte array.
82+ * If the file is missing, this writes a warning message to the console.
83+ * @param basePath the base directory path where the file is located
84+ * @param fileName the simple file name of the file
85+ * @return a byte array
86+ * @throws RuntimeException for IO errors, or if resolved path is not a file or not readable.
87+ */
88+ public static byte [] getFileBytes (final Path basePath , final String fileName ) throws RuntimeException {
8189 Objects .requireNonNull (basePath , "input parameter 'Path basePath' cannot be null." );
8290 Objects .requireNonNull (fileName , "input parameter 'String fileName' cannot be null." );
8391 Path path = Path .of (basePath .toString (), fileName );
84- Path absPath = path .toAbsolutePath (); //for debugging
92+ Path absPath = path .toAbsolutePath (); //for error output
8593 byte [] bytes = new byte [0 ]; //or null
86- if (Files .notExists (path )) {
94+ if (Files .notExists (path )) { //In this specific case, just issue warning.
8795 System .err .println ("File disappeared or not found: " + absPath );
88- return bytes ; //or null
96+ return bytes ; //empty
8997 }
9098 if (!Files .isRegularFile (path ) || !Files .isReadable (path )) {
9199 throw new RuntimeException ("Path is not a regular file or not readable: " + absPath );
92100 }
93101 try {
94- bytes = Files .readAllBytes (path );
95- return bytes ;
96- } catch (IOException e ) {
97- throw new RuntimeException ("System Error reading file: " + absPath + " " + e );
98- }
99- }
102+ bytes = Files .readAllBytes (path );
103+ return bytes ;
104+ } catch (IOException e ) {
105+ throw new RuntimeException ("System IO Error reading file: " + absPath + " " + e );
106+ }
107+ }
108+
109+ /**
110+ * Puts all the bytes of the given byte array to a file with the given fileName.
111+ * This assumes that the base directory path is {@link #javaPath javaPath}.
112+ * @param fileName the name of the target file
113+ * @param bytes the given byte array
114+ */
115+ public static void putBytesToJavaPathFile (final String fileName , final byte [] bytes ) {
116+ putBytesToFile (javaPath , fileName , bytes );
117+ }
118+
119+ /**
120+ * Puts all the bytes of the given byte array to a basePath file with the given fileName.
121+ * @param basePath the directory path for the given fileName
122+ * @param fileName the name of the target file
123+ * @param bytes the given byte array
124+ * @throws RuntimeException for IO errors,
125+ */
126+ public static void putBytesToFile (final Path basePath , final String fileName , final byte [] bytes ) {
127+ Objects .requireNonNull (basePath , "input parameter 'Path basePath' cannot be null." );
128+ Objects .requireNonNull (fileName , "input parameter 'String fileName' cannot be null." );
129+ Objects .requireNonNull (bytes , "input parameter 'byte[] bytes' cannot be null." );
130+ Path filePath = null ;
131+ try {
132+ Files .createDirectories (basePath ); //create the directory if it doesn't exist.
133+ filePath = basePath .resolve (fileName );
134+ Files .write (filePath , bytes );
135+ } catch (IOException e ) {
136+ throw new RuntimeException ("System IO Error writing file: " + filePath .toString () + " " + e );
137+ }
138+ }
100139}
0 commit comments