-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApposeUtilsTest.java
More file actions
191 lines (159 loc) · 6.58 KB
/
ApposeUtilsTest.java
File metadata and controls
191 lines (159 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*-
* #%L
* mastodon-deep-lineage
* %%
* Copyright (C) 2022 - 2025 Stefan Hahmann
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.mastodon.mamut.util.appose;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.apposed.appose.Appose;
import org.apposed.appose.Builder;
import org.junit.jupiter.api.Test;
class ApposeUtilsTest
{
@Test
void testInstallDeleteExistsSize() throws IOException
{
String testEnvName = "testenv";
String testEnvContent = "name: " + testEnvName + "\n"
+ "channels:\n"
+ " - conda-forge\n"
+ "dependencies:\n"
+ " - python=3.10\n";
Builder<?> envBuilder = Appose.mamba().scheme( "environment.yml" );
ApposeUtils.installEnvironment( testEnvContent, envBuilder );
assertTrue( ApposeUtils.checkEnvironmentInstalled( testEnvName ) );
String size = ApposeUtils.calculateEnvironmentSize( testEnvName );
String numberPart = size.split( " " )[ 0 ]; // "123,4"
String integerPart = numberPart.split( "[.,]" )[ 0 ]; // "123"
int sizeInt = Integer.parseInt( integerPart );
assertTrue( sizeInt >= 100 && sizeInt <= 499, "Environment size should be between 100 MB and 499 MB but was " + size + " MB" );
ApposeUtils.deleteEnvironment( testEnvName, null );
assertFalse( ApposeUtils.checkEnvironmentInstalled( testEnvName ) );
}
@Test
void testCalculateDirectorySize_withFilesAndSubDirectories() throws IOException
{
// Arrange: Create a temporary directory and nested structure with files
File tempDir = Files.createTempDirectory( "sizeTestDir" ).toFile();
tempDir.deleteOnExit();
File nestedDir = new File( tempDir, "nestedDir" );
assertTrue( nestedDir.mkdir() );
File file1 = new File( tempDir, "file1.txt" );
assertTrue( file1.createNewFile() );
Files.write( file1.toPath(), "Hello World!".getBytes( StandardCharsets.UTF_8 ) );
File file2 = new File( nestedDir, "file2.txt" );
assertTrue( file2.createNewFile() );
Files.write( file1.toPath(), "Java Testing!".getBytes( StandardCharsets.UTF_8 ) );
// Act: Calculate the directory size
long size = ApposeUtils.calculateDirectorySize( tempDir );
// Assert: Verify the expected size is accurate
assertTrue( size > 0 ); // Basic check for non-zero size
assertEquals( file1.length() + file2.length(), size );
}
@Test
void testCalculateDirectorySize_withEmptyDirectory() throws IOException
{
// Arrange: Create an empty temporary directory
File tempDir = Files.createTempDirectory( "emptySizeTestDir" ).toFile();
tempDir.deleteOnExit();
// Act: Calculate the directory size
long size = ApposeUtils.calculateDirectorySize( tempDir );
// Assert: Verify the size is zero
assertEquals( 0, size );
}
@Test
void testCalculateDirectorySize_withNonExistentDirectory()
{
// Arrange: Reference a non-existent directory
File nonExistentDir = new File( "nonExistentTestDirectory" );
// Act: Calculate the directory size
long size = ApposeUtils.calculateDirectorySize( nonExistentDir );
// Assert: Verify the size is zero
assertEquals( 0, size );
}
@Test
void testDeleteDirectory_withExistingDirectory() throws IOException
{
// Arrange: Create a temporary directory with nested files and subdirectories
File tempDir = Files.createTempDirectory( "testDir" ).toFile();
tempDir.deleteOnExit();
File nestedDir = new File( tempDir, "nestedDir" );
assertTrue( nestedDir.mkdir() );
File file1 = new File( tempDir, "file1.txt" );
assertTrue( file1.createNewFile() );
File file2 = new File( nestedDir, "file2.txt" );
assertTrue( file2.createNewFile() );
// Act: Delete the directory
ApposeUtils.deleteDirectory( tempDir );
// Assert: The entire structure should be deleted
assertFalse( tempDir.exists() );
}
@Test
void testDeleteDirectory_withEmptyDirectory() throws IOException
{
// Arrange: Create an empty temporary directory
File tempDir = Files.createTempDirectory( "testEmptyDir" ).toFile();
tempDir.deleteOnExit();
// Act: Delete the directory
ApposeUtils.deleteDirectory( tempDir );
// Assert: The directory should be deleted
assertFalse( tempDir.exists() );
}
@Test
void testDeleteDirectory_withNonExistentDirectory() throws IOException
{
// Arrange: Reference a non-existent directory
File nonExistentDir = new File( "nonExistentDirectory" );
// Act: Delete the directory, which shouldn't throw an exception
ApposeUtils.deleteDirectory( nonExistentDir );
// Assert: Since the directory doesn't exist, no changes occur
assertFalse( nonExistentDir.exists() );
}
@Test
void testDeleteDirectory_withNullDirectory()
{
// Act and Assert: Passing null should not throw an exception
assertDoesNotThrow( () -> ApposeUtils.deleteDirectory( null ) );
}
@Test
void testDeleteDirectory_withInvalidDirectory() throws IOException
{
// Arrange: Create a file instead of a directory
File tempFile = Files.createTempFile( "testFile", ".txt" ).toFile();
tempFile.deleteOnExit();
// Act and Assert: Trying to delete a file using deleteDirectory
// should result in the file being deleted.
ApposeUtils.deleteDirectory( tempFile );
assertFalse( tempFile.exists() );
}
}