Skip to content

Commit

Permalink
Prevent potential resource leaks when running tests.
Browse files Browse the repository at this point in the history
Use try-with-resources when opening test files.
  • Loading branch information
mikesname committed Apr 10, 2024
1 parent 382842b commit f9e1a71
Show file tree
Hide file tree
Showing 53 changed files with 1,055 additions and 939 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
Expand All @@ -52,34 +53,36 @@ public void testImportExportRoundtrip() throws Exception {
);
for (Map.Entry<String, String> entry : files.entrySet()) {

SkosImporter importer = new JenaSkosImporter(graph, actioner, vocabulary)
.setFormat(entry.getValue().equalsIgnoreCase("") ? null : entry.getValue())
.allowUpdates(true);
importer
.importFile(ClassLoader.getSystemResourceAsStream(entry.getKey()), "test");

List<VertexProxy> before = getGraphState(graph);
int edgeCountBefore = getEdgeCount(graph);
SkosExporter exporter = new JenaSkosExporter(graph, vocabulary)
.setFormat(entry.getValue().equalsIgnoreCase("") ? null : entry.getValue());
OutputStream outputStream = new ByteArrayOutputStream();
exporter.export(outputStream, "http://www.my.com/#");
String skos = outputStream.toString();
//System.out.println("EXPORT: " + skos);
ImportLog log = importer
.setFormat(entry.getValue())
.allowUpdates(true)
.importFile(new ByteArrayInputStream(skos.getBytes()), "test");
List<VertexProxy> after = getGraphState(graph);
assertTrue(log.getUnchanged() > 0);
assertEquals(0, log.getChanged());
assertEquals(0, log.getCreated());
assertEquals(0, log.getErrored());
GraphDiff graphDiff = diffGraph(before, after);
assertTrue(graphDiff.added.isEmpty());
assertTrue(graphDiff.removed.isEmpty());
int edgeCountAfter = getEdgeCount(graph);
assertEquals(edgeCountBefore, edgeCountAfter);
try (final InputStream stream = ClassLoader.getSystemResourceAsStream(entry.getKey());
OutputStream outputStream = new ByteArrayOutputStream()) {
SkosImporter importer = new JenaSkosImporter(graph, actioner, vocabulary)
.setFormat(entry.getValue().equalsIgnoreCase("") ? null : entry.getValue())
.allowUpdates(true);
importer
.importFile(stream, "test");

List<VertexProxy> before = getGraphState(graph);
int edgeCountBefore = getEdgeCount(graph);
SkosExporter exporter = new JenaSkosExporter(graph, vocabulary)
.setFormat(entry.getValue().equalsIgnoreCase("") ? null : entry.getValue());
exporter.export(outputStream, "http://www.my.com/#");
String skos = outputStream.toString();
//System.out.println("EXPORT: " + skos);
ImportLog log = importer
.setFormat(entry.getValue())
.allowUpdates(true)
.importFile(new ByteArrayInputStream(skos.getBytes()), "test");
List<VertexProxy> after = getGraphState(graph);
assertTrue(log.getUnchanged() > 0);
assertEquals(0, log.getChanged());
assertEquals(0, log.getCreated());
assertEquals(0, log.getErrored());
GraphDiff graphDiff = diffGraph(before, after);
assertTrue(graphDiff.added.isEmpty());
assertTrue(graphDiff.removed.isEmpty());
int edgeCountAfter = getEdgeCount(graph);
assertEquals(edgeCountBefore, edgeCountAfter);
}
}
}

Expand All @@ -91,7 +94,7 @@ public void testExport() throws Exception {

for (String format : formats) {
String skos = exportFile(vocabulary, format, baseUri);
System.out.println(skos);
System.out.println(skos);
assertTrue(skos.contains(baseUri));
assertTrue(skos.contains(baseUri + "989"));
assertTrue(skos.contains("dc:title"));
Expand All @@ -116,15 +119,16 @@ public void testAbsoluteURIs() throws Exception {

private void importFile(Vocabulary vocabulary, String file) throws Exception {
SkosImporter importer = new JenaSkosImporter(graph, actioner, vocabulary);
importer.importFile(ClassLoader.getSystemResourceAsStream(file), "test");
try (final InputStream stream = ClassLoader.getSystemResourceAsStream(file)) {
importer.importFile(stream, "test");
}
}

private String exportFile(Vocabulary vocabulary, String format, String baseUri) throws Exception {
SkosExporter exporter = new JenaSkosExporter(graph, vocabulary)
.setFormat(format);
OutputStream outputStream = new ByteArrayOutputStream();

exporter.export(outputStream, baseUri);
return outputStream.toString();
SkosExporter exporter = new JenaSkosExporter(graph, vocabulary).setFormat(format);
try (OutputStream outputStream = new ByteArrayOutputStream()) {
exporter.export(outputStream, baseUri);
return outputStream.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,17 @@ public void testExport1() throws Exception {
@Test
public void testImportExport1() throws Exception {
AuthoritativeSet auths = manager.getEntity("auths", AuthoritativeSet.class);
InputStream ios = ClassLoader.getSystemResourceAsStream("abwehr.xml");
String logMessage = "Test EAC import/export";
SaxImportManager.create(graph, auths, adminUser,
EacImporter.class, EacHandler.class, ImportOptions.properties("eac.properties"))
.importInputStream(ios, logMessage);
HistoricalAgent repo = graph.frame(getVertexByIdentifier(graph, "381"), HistoricalAgent.class);
String xml = testExport(repo, "eng");
Document doc = parseDocument(xml);
assertXPath(doc, logMessage,
"//eac-cpf/control/maintenanceHistory/maintenanceEvent[3]/eventDescription");
try (InputStream ios = ClassLoader.getSystemResourceAsStream("abwehr.xml")) {
String logMessage = "Test EAC import/export";
SaxImportManager.create(graph, auths, adminUser,
EacImporter.class, EacHandler.class, ImportOptions.properties("eac.properties"))
.importInputStream(ios, logMessage);
HistoricalAgent repo = graph.frame(getVertexByIdentifier(graph, "381"), HistoricalAgent.class);
String xml = testExport(repo, "eng");
Document doc = parseDocument(xml);
assertXPath(doc, logMessage,
"//eac-cpf/control/maintenanceHistory/maintenanceEvent[3]/eventDescription");
}
}

@Test
Expand Down Expand Up @@ -106,12 +107,13 @@ public void testExportWithComprehensiveFixture() throws Exception {

private String testExport(HistoricalAgent agent, String lang) throws Exception {
Eac2010Exporter exporter = new Eac2010Exporter(api(adminUser));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
exporter.export(agent, baos, lang);
String xml = baos.toString("UTF-8");
//System.out.println(xml);
isValidEac(xml);
return xml;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
exporter.export(agent, baos, lang);
String xml = baos.toString("UTF-8");
//System.out.println(xml);
isValidEac(xml);
return xml;
}
}

private void isValidEac(String eacXml) throws IOException, SAXException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,27 +152,29 @@ public void testExportWithComprehensiveFixture() throws Exception {

private String testExport(DocumentaryUnit unit, String lang) throws Exception {
Ead2002Exporter exporter = new Ead2002Exporter(api(adminUser));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
exporter.export(unit, baos, lang);
String xml = baos.toString("UTF-8");
isValidEad(xml);
return xml;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
exporter.export(unit, baos, lang);
String xml = baos.toString("UTF-8");
isValidEad(xml);
return xml;
}
}

private String testImportExport(Repository repository, String resourceName,
String topLevelIdentifier, String lang) throws Exception {
InputStream ios = ClassLoader.getSystemResourceAsStream(resourceName);
SaxImportManager.create(graph, repository, adminUser,
EadImporter.class, EadHandler.class, ImportOptions.basic())
.importInputStream(ios, "Testing import/export");
DocumentaryUnit fonds = graph.frame(
getVertexByIdentifier(graph, topLevelIdentifier), DocumentaryUnit.class);
Ead2002Exporter exporter = new Ead2002Exporter(api(adminUser));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
exporter.export(fonds, baos, lang);
String xml = baos.toString("UTF-8");
isValidEad(xml);
return xml;
String topLevelIdentifier, String lang) throws Exception {
try (InputStream ios = ClassLoader.getSystemResourceAsStream(resourceName);
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
SaxImportManager.create(graph, repository, adminUser,
EadImporter.class, EadHandler.class, ImportOptions.basic())
.importInputStream(ios, "Testing import/export");
DocumentaryUnit fonds = graph.frame(
getVertexByIdentifier(graph, topLevelIdentifier), DocumentaryUnit.class);
Ead2002Exporter exporter = new Ead2002Exporter(api(adminUser));
exporter.export(fonds, baos, lang);
String xml = baos.toString("UTF-8");
isValidEad(xml);
return xml;
}
}

private void isValidEad(String eadXml) throws IOException, SAXException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ public void testExport1() throws Exception {
@Test
public void testImportExport1() throws Exception {
Country nl = manager.getEntity("nl", Country.class);
InputStream ios = ClassLoader.getSystemResourceAsStream("eag-2896.xml");
SaxImportManager importManager = SaxImportManager.create(graph, nl, adminUser,
EagImporter.class, EagHandler.class, ImportOptions.properties("eag.properties"));
importManager.importInputStream(ios, "Text EAG import/export");
Repository repo = graph.frame(getVertexByIdentifier(graph, "NL-002896"), Repository.class);
testExport(repo, "eng");
try (InputStream ios = ClassLoader.getSystemResourceAsStream("eag-2896.xml")) {
SaxImportManager importManager = SaxImportManager.create(graph, nl, adminUser,
EagImporter.class, EagHandler.class, ImportOptions.properties("eag.properties"));
importManager.importInputStream(ios, "Text EAG import/export");
Repository repo = graph.frame(getVertexByIdentifier(graph, "NL-002896"), Repository.class);
testExport(repo, "eng");
}
}

@Test
Expand Down Expand Up @@ -104,12 +105,13 @@ public void testExportWithComprehensiveFixture() throws Exception {

private String testExport(Repository repository, String lang) throws Exception {
Eag2012Exporter exporter = new Eag2012Exporter(api(adminUser));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
exporter.export(repository, baos, lang);
String xml = baos.toString("UTF-8");
//System.out.println(xml);
isValidEag(xml);
return xml;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
exporter.export(repository, baos, lang);
String xml = baos.toString("UTF-8");
//System.out.println(xml);
isValidEag(xml);
return xml;
}
}

private void isValidEag(String eagXml) throws IOException, SAXException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public class CsvConceptImporterTest extends AbstractImporterTest {
@Test
public void testImportItems() throws Exception {
AuthoritativeSet authoritativeSet = manager.getEntity("auths", AuthoritativeSet.class);
int voccount = toList(authoritativeSet.getAuthoritativeItems()).size();
assertEquals(2, voccount);
logger.debug("number of items: " + voccount);
int vocCount = toList(authoritativeSet.getAuthoritativeItems()).size();
assertEquals(2, vocCount);
logger.debug("number of items: " + vocCount);

final String logMessage = "Importing some subjects";
XmlImportProperties p = new XmlImportProperties("csvconcept.properties");
Expand All @@ -62,17 +62,18 @@ public void testImportItems() throws Exception {


int count = getNodeCount(graph);
InputStream ios = ClassLoader.getSystemResourceAsStream(SINGLE_EAD);
CsvImportManager.create(graph, authoritativeSet, adminUser, CsvConceptImporter.class, ImportOptions.basic())
.importInputStream(ios, logMessage);
try (InputStream ios = ClassLoader.getSystemResourceAsStream(SINGLE_EAD)) {
CsvImportManager.create(graph, authoritativeSet, adminUser, CsvConceptImporter.class, ImportOptions.basic())
.importInputStream(ios, logMessage);
}
/*
* 18 Concept
* 18 ConceptDesc
* 19 more import Event links (1 for every Unit, 1 for the User)
* 1 more import Event
*/
assertEquals(count+56, getNodeCount(graph));
assertEquals(voccount + 18, toList(authoritativeSet.getAuthoritativeItems()).size());
assertEquals(vocCount + 18, toList(authoritativeSet.getAuthoritativeItems()).size());

// Check permission scopes are correct.
for (Accessible subject : actionManager.getLatestGlobalEvent().getSubjects()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,25 @@ public void testImportItems() throws Exception {
// Before...
List<VertexProxy> graphState1 = getGraphState(graph);

InputStream ios = ClassLoader.getSystemResourceAsStream("dossin.csv");
ImportLog importLog = CsvImportManager.create(graph, ps, adminUser, EadImporter.class, ImportOptions.basic())
.importInputStream(ios, logMessage);
System.out.println(importLog);
// After...
List<VertexProxy> graphState2 = getGraphState(graph);
GraphDiff diff = diffGraph(graphState1, graphState2);
diff.printDebug(System.out);
/*
* null: 5
* relationship: 4
* DocumentaryUnit: 4
* documentDescription: 4
* systemEvent: 1
* datePeriod: 4
*/
assertEquals(count + 22, getNodeCount(graph));
DocumentaryUnit unit = manager.getEntity("nl-r1-kd3", DocumentaryUnit.class);
assertEquals(ps, unit.getRepository());
try (InputStream ios = ClassLoader.getSystemResourceAsStream("dossin.csv")) {
ImportLog importLog = CsvImportManager.create(graph, ps, adminUser, EadImporter.class, ImportOptions.basic())
.importInputStream(ios, logMessage);
System.out.println(importLog);
// After...
List<VertexProxy> graphState2 = getGraphState(graph);
GraphDiff diff = diffGraph(graphState1, graphState2);
diff.printDebug(System.out);
/*
* null: 5
* relationship: 4
* DocumentaryUnit: 4
* documentDescription: 4
* systemEvent: 1
* datePeriod: 4
*/
assertEquals(count + 22, getNodeCount(graph));
DocumentaryUnit unit = manager.getEntity("nl-r1-kd3", DocumentaryUnit.class);
assertEquals(ps, unit.getRepository());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public class PersonalitiesImporterTest extends AbstractImporterTest {
@Test
public void testImportItems() throws Exception {
AuthoritativeSet authoritativeSet = manager.getEntity("auths", AuthoritativeSet.class);
int voccount = toList(authoritativeSet.getAuthoritativeItems()).size();
assertEquals(2, voccount);
logger.debug("number of items: " + voccount);
int vocCount = toList(authoritativeSet.getAuthoritativeItems()).size();
assertEquals(2, vocCount);
logger.debug("number of items: " + vocCount);

final String logMessage = "Importing some WP18 Personalities records";
XmlImportProperties p = new XmlImportProperties("personalities.properties");
Expand All @@ -55,9 +55,10 @@ public void testImportItems() throws Exception {
assertTrue(p.containsProperty("Pseudonyms"));

int count = getNodeCount(graph);
InputStream ios = ClassLoader.getSystemResourceAsStream(SINGLE_EAD);
CsvImportManager.create(graph, authoritativeSet, adminUser,
PersonalitiesImporter.class, ImportOptions.basic()).importInputStream(ios, logMessage);
try (InputStream ios = ClassLoader.getSystemResourceAsStream(SINGLE_EAD)) {
CsvImportManager.create(graph, authoritativeSet, adminUser,
PersonalitiesImporter.class, ImportOptions.basic()).importInputStream(ios, logMessage);
}
SystemEvent ev = actionManager.getLatestGlobalEvent();

/*
Expand All @@ -68,7 +69,7 @@ public void testImportItems() throws Exception {
* 1 more import Event
*/
assertEquals(count + 34, getNodeCount(graph));
assertEquals(voccount + 8, toList(authoritativeSet.getAuthoritativeItems()).size());
assertEquals(vocCount + 8, toList(authoritativeSet.getAuthoritativeItems()).size());

// Check permission scopes are correct.
for (Accessible subject : ev.getSubjects()) {
Expand Down
Loading

0 comments on commit f9e1a71

Please sign in to comment.