Skip to content

Commit 9856808

Browse files
committed
refactor Java unit tests into multiple files.
1 parent 2850d7a commit 9856808

File tree

3 files changed

+139
-114
lines changed

3 files changed

+139
-114
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.vcell.libvcell;
2+
3+
import cbit.util.xml.VCLoggerException;
4+
import cbit.vcell.mapping.MappingException;
5+
import cbit.vcell.xml.XmlParseException;
6+
import org.junit.jupiter.api.Test;
7+
import org.vcell.sbml.SbmlException;
8+
9+
import javax.xml.stream.XMLStreamException;
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.nio.file.Files;
13+
14+
import static org.vcell.libvcell.ModelUtils.*;
15+
import static org.vcell.libvcell.TestUtils.getFileContentsAsString;
16+
17+
public class ModelEntrypointsTest {
18+
19+
@Test
20+
public void test_sbml_to_vcml() throws MappingException, IOException, XmlParseException, VCLoggerException {
21+
String sbmlContent = getFileContentsAsString("/TinySpatialProject_Application0.xml");
22+
File parent_dir = Files.createTempDirectory("sbmlToVcml").toFile();
23+
File vcml_temp_file = new File(parent_dir, "temp.vcml");
24+
sbml_to_vcml(sbmlContent, vcml_temp_file.toPath());
25+
assert(vcml_temp_file.exists());
26+
}
27+
28+
@Test
29+
public void test_vcml_to_sbml() throws MappingException, IOException, XmlParseException, XMLStreamException, SbmlException {
30+
String vcmlContent = getFileContentsAsString("/TinySpatialProject_Application0.vcml");
31+
File parent_dir = Files.createTempDirectory("vcmlToSbml").toFile();
32+
File sbml_temp_file = new File(parent_dir, "temp.sbml");
33+
String applicationName = "unnamed_spatialGeom";
34+
vcml_to_sbml(vcmlContent, applicationName, sbml_temp_file.toPath());
35+
assert(sbml_temp_file.exists());
36+
}
37+
38+
@Test
39+
public void test_vcml_to_vcml() throws MappingException, IOException, XmlParseException, XMLStreamException, SbmlException {
40+
String vcmlContent = getFileContentsAsString("/TinySpatialProject_Application0.vcml");
41+
File parent_dir = Files.createTempDirectory("vcmlToVcml").toFile();
42+
File vcml_temp_file = new File(parent_dir, "temp.vcml");
43+
vcml_to_vcml(vcmlContent, vcml_temp_file.toPath());
44+
assert(vcml_temp_file.exists());
45+
}
46+
47+
}

vcell-native/src/test/java/org/vcell/libvcell/EntrypointsTest.java renamed to vcell-native/src/test/java/org/vcell/libvcell/SolverEntrypointsTest.java

Lines changed: 6 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,21 @@
66
import cbit.vcell.parser.ExpressionException;
77
import cbit.vcell.solver.SolverException;
88
import cbit.vcell.xml.XmlParseException;
9-
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
10-
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
119
import org.junit.jupiter.api.Test;
12-
import org.vcell.sbml.SbmlException;
1310

14-
import javax.xml.stream.XMLStreamException;
1511
import java.beans.PropertyVetoException;
16-
import java.io.*;
17-
import java.nio.charset.StandardCharsets;
12+
import java.io.File;
13+
import java.io.IOException;
1814
import java.nio.file.Files;
1915
import java.nio.file.Path;
20-
import java.util.Objects;
2116
import java.util.UUID;
22-
import java.util.stream.Collectors;
23-
import java.util.zip.GZIPInputStream;
2417

2518
import static org.junit.jupiter.api.Assertions.*;
2619
import static org.vcell.libvcell.SolverUtils.sbmlToFiniteVolumeInput;
2720
import static org.vcell.libvcell.SolverUtils.vcmlToFiniteVolumeInput;
28-
import static org.vcell.libvcell.ModelUtils.sbml_to_vcml;
29-
import static org.vcell.libvcell.ModelUtils.vcml_to_sbml;
30-
import static org.vcell.libvcell.ModelUtils.vcml_to_vcml;
21+
import static org.vcell.libvcell.TestUtils.*;
3122

32-
public class EntrypointsTest {
23+
public class SolverEntrypointsTest {
3324

3425
@Test
3526
public void testSbmlToFiniteVolumeInput() throws PropertyVetoException, SolverException, ExpressionException, MappingException, VCLoggerException, IOException {
@@ -66,7 +57,7 @@ public void testVcmlToFiniteVolumeInput_field_data() throws SolverException, Exp
6657
File output_dir = new File(parent_dir, "output_dir");
6758
File ext_data_dir = new File(parent_dir, "test2_lsm_DEMO");
6859
assertEquals(0, countFiles(ext_data_dir));
69-
extractTgz(EntrypointsTest.class.getResourceAsStream("/test2_lsm_DEMO.tgz"), parent_dir);
60+
extractTgz(SolverEntrypointsTest.class.getResourceAsStream("/test2_lsm_DEMO.tgz"), parent_dir);
7061
listFilesInDirectory(ext_data_dir);
7162
assertEquals(10, countFiles(ext_data_dir));
7263

@@ -87,7 +78,7 @@ public void testVcmlToFiniteVolumeInput_field_data_not_found() throws SolverExce
8778
File ext_data_dir_MISSPELLED = new File(parent_dir, "test2_lsm_DEMO_MISSPELLED");
8879
assertEquals(0, countFiles(ext_data_dir));
8980
assertEquals(0, countFiles(ext_data_dir_MISSPELLED));
90-
extractTgz(EntrypointsTest.class.getResourceAsStream("/test2_lsm_DEMO.tgz"), parent_dir);
81+
extractTgz(SolverEntrypointsTest.class.getResourceAsStream("/test2_lsm_DEMO.tgz"), parent_dir);
9182
Files.move(ext_data_dir.toPath(), ext_data_dir_MISSPELLED.toPath()).toFile();
9283
assertEquals(0, countFiles(ext_data_dir));
9384
listFilesInDirectory(ext_data_dir_MISSPELLED);
@@ -112,34 +103,6 @@ public void testVcmlToFiniteVolumeInput() throws SolverException, ExpressionExce
112103
assertEquals(4, countFiles(output_dir));
113104
}
114105

115-
@Test
116-
public void test_sbml_to_vcml() throws MappingException, IOException, XmlParseException, VCLoggerException {
117-
String sbmlContent = getFileContentsAsString("/TinySpatialProject_Application0.xml");
118-
File parent_dir = Files.createTempDirectory("sbmlToVcml").toFile();
119-
File vcml_temp_file = new File(parent_dir, "temp.vcml");
120-
sbml_to_vcml(sbmlContent, vcml_temp_file.toPath());
121-
assert(vcml_temp_file.exists());
122-
}
123-
124-
@Test
125-
public void test_vcml_to_sbml() throws MappingException, IOException, XmlParseException, XMLStreamException, SbmlException {
126-
String vcmlContent = getFileContentsAsString("/TinySpatialProject_Application0.vcml");
127-
File parent_dir = Files.createTempDirectory("vcmlToSbml").toFile();
128-
File sbml_temp_file = new File(parent_dir, "temp.sbml");
129-
String applicationName = "unnamed_spatialGeom";
130-
vcml_to_sbml(vcmlContent, applicationName, sbml_temp_file.toPath());
131-
assert(sbml_temp_file.exists());
132-
}
133-
134-
@Test
135-
public void test_vcml_to_vcml() throws MappingException, IOException, XmlParseException, XMLStreamException, SbmlException {
136-
String vcmlContent = getFileContentsAsString("/TinySpatialProject_Application0.vcml");
137-
File parent_dir = Files.createTempDirectory("vcmlToVcml").toFile();
138-
File vcml_temp_file = new File(parent_dir, "temp.vcml");
139-
vcml_to_vcml(vcmlContent, vcml_temp_file.toPath());
140-
assert(vcml_temp_file.exists());
141-
}
142-
143106
@Test
144107
public void testVcmlToFiniteVolumeInput_bad_simname() throws IOException {
145108
String vcmlContent = getFileContentsAsString("/TinySpatialProject_Application0.vcml");
@@ -177,75 +140,4 @@ public void testVcmlToFiniteVolumeInput_sbml_instead() throws IOException {
177140
assertEquals("expecting VCML content, not SBML", exc.getMessage());
178141
}
179142

180-
private static String getFileContentsAsString(String filename) throws IOException {
181-
try (InputStream inputStream = EntrypointsTest.class.getResourceAsStream(filename)) {
182-
if (inputStream == null) {
183-
throw new FileNotFoundException("file not found! " + filename);
184-
}
185-
return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
186-
.lines().collect(Collectors.joining("\n"));
187-
}
188-
}
189-
190-
private static byte[] getFileContentsAsBytes(String filename) throws IOException {
191-
try (InputStream inputStream = EntrypointsTest.class.getResourceAsStream(filename)) {
192-
if (inputStream == null) {
193-
throw new FileNotFoundException("file not found! " + filename);
194-
}
195-
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
196-
byte[] buffer = new byte[1024];
197-
int length;
198-
while ((length = inputStream.read(buffer)) != -1) {
199-
byteArrayOutputStream.write(buffer, 0, length);
200-
}
201-
return byteArrayOutputStream.toByteArray();
202-
}
203-
}
204-
}
205-
206-
private int countFiles(File dir) {
207-
File[] files = dir.listFiles();
208-
if (files == null) {
209-
return 0;
210-
}
211-
return Objects.requireNonNull(dir.listFiles()).length;
212-
}
213-
214-
private void listFilesInDirectory(File dir) {
215-
File[] files = dir.listFiles();
216-
if (files != null) {
217-
for (File file : files) {
218-
System.out.println(file.getAbsolutePath());
219-
}
220-
}
221-
}
222-
223-
public static void extractTgz(InputStream tgzFileStream, File outputDir) throws IOException {
224-
try (GZIPInputStream gis = new GZIPInputStream(tgzFileStream);
225-
TarArchiveInputStream tis = new TarArchiveInputStream(gis)) {
226-
227-
TarArchiveEntry entry;
228-
while ((entry = tis.getNextTarEntry()) != null) {
229-
File outputFile = new File(outputDir, entry.getName());
230-
if (entry.isDirectory()) {
231-
if (!outputFile.exists()) {
232-
outputFile.mkdirs();
233-
}
234-
} else {
235-
File parent = outputFile.getParentFile();
236-
if (!parent.exists()) {
237-
parent.mkdirs();
238-
}
239-
try (OutputStream os = Files.newOutputStream(outputFile.toPath())) {
240-
byte[] buffer = new byte[1024];
241-
int len;
242-
while ((len = tis.read(buffer)) != -1) {
243-
os.write(buffer, 0, len);
244-
}
245-
}
246-
}
247-
}
248-
}
249-
}
250-
251143
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.vcell.libvcell;
2+
3+
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
4+
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
5+
6+
import java.io.*;
7+
import java.nio.charset.StandardCharsets;
8+
import java.nio.file.Files;
9+
import java.util.Objects;
10+
import java.util.stream.Collectors;
11+
import java.util.zip.GZIPInputStream;
12+
13+
public class TestUtils {
14+
public static String getFileContentsAsString(String filename) throws IOException {
15+
try (InputStream inputStream = ModelEntrypointsTest.class.getResourceAsStream(filename)) {
16+
if (inputStream == null) {
17+
throw new FileNotFoundException("file not found! " + filename);
18+
}
19+
return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
20+
.lines().collect(Collectors.joining("\n"));
21+
}
22+
}
23+
24+
public static byte[] getFileContentsAsBytes(String filename) throws IOException {
25+
try (InputStream inputStream = SolverEntrypointsTest.class.getResourceAsStream(filename)) {
26+
if (inputStream == null) {
27+
throw new FileNotFoundException("file not found! " + filename);
28+
}
29+
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
30+
byte[] buffer = new byte[1024];
31+
int length;
32+
while ((length = inputStream.read(buffer)) != -1) {
33+
byteArrayOutputStream.write(buffer, 0, length);
34+
}
35+
return byteArrayOutputStream.toByteArray();
36+
}
37+
}
38+
}
39+
40+
public static int countFiles(File dir) {
41+
File[] files = dir.listFiles();
42+
if (files == null) {
43+
return 0;
44+
}
45+
return Objects.requireNonNull(dir.listFiles()).length;
46+
}
47+
48+
public static void listFilesInDirectory(File dir) {
49+
File[] files = dir.listFiles();
50+
if (files != null) {
51+
for (File file : files) {
52+
System.out.println(file.getAbsolutePath());
53+
}
54+
}
55+
}
56+
57+
public static void extractTgz(InputStream tgzFileStream, File outputDir) throws IOException {
58+
try (GZIPInputStream gis = new GZIPInputStream(tgzFileStream);
59+
TarArchiveInputStream tis = new TarArchiveInputStream(gis)) {
60+
61+
TarArchiveEntry entry;
62+
while ((entry = tis.getNextTarEntry()) != null) {
63+
File outputFile = new File(outputDir, entry.getName());
64+
if (entry.isDirectory()) {
65+
if (!outputFile.exists()) {
66+
outputFile.mkdirs();
67+
}
68+
} else {
69+
File parent = outputFile.getParentFile();
70+
if (!parent.exists()) {
71+
parent.mkdirs();
72+
}
73+
try (OutputStream os = Files.newOutputStream(outputFile.toPath())) {
74+
byte[] buffer = new byte[1024];
75+
int len;
76+
while ((len = tis.read(buffer)) != -1) {
77+
os.write(buffer, 0, len);
78+
}
79+
}
80+
}
81+
}
82+
}
83+
}
84+
85+
86+
}

0 commit comments

Comments
 (0)