From 587489edd1dd57dd58e3c64e623fa484ce1c4adf Mon Sep 17 00:00:00 2001 From: Andrea Burattin <andrea.burattin@gmail.com> Date: Fri, 25 Mar 2022 22:45:13 +0100 Subject: [PATCH] Worked to remove code smells and code quality --- .classpath | 2 +- .settings/org.eclipse.jdt.core.prefs | 6 ++--- .../InfiniteSizeDirectlyFollowsMapper.java | 2 +- .../algorithms/StreamMiningAlgorithm.java | 4 +-- .../java/beamline/sources/XesLogSource.java | 27 ++++++++++++++++--- .../java/beamline/tests/AlgorithmTest.java | 4 +-- .../java/beamline/tests/ResponsesTest.java | 8 +++--- src/test/java/beamline/tests/SourcesTest.java | 4 --- 8 files changed, 36 insertions(+), 21 deletions(-) diff --git a/.classpath b/.classpath index 002ad57..0fb79cf 100644 --- a/.classpath +++ b/.classpath @@ -24,7 +24,7 @@ <attribute name="test" value="true"/> </attributes> </classpathentry> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 2f5cc74..2af1e7b 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,8 +1,8 @@ eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 -org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 +org.eclipse.jdt.core.compiler.compliance=11 org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore org.eclipse.jdt.core.compiler.release=disabled -org.eclipse.jdt.core.compiler.source=1.8 +org.eclipse.jdt.core.compiler.source=11 diff --git a/src/main/java/beamline/models/algorithms/InfiniteSizeDirectlyFollowsMapper.java b/src/main/java/beamline/models/algorithms/InfiniteSizeDirectlyFollowsMapper.java index 6eadf94..0df27ff 100644 --- a/src/main/java/beamline/models/algorithms/InfiniteSizeDirectlyFollowsMapper.java +++ b/src/main/java/beamline/models/algorithms/InfiniteSizeDirectlyFollowsMapper.java @@ -26,7 +26,7 @@ public class InfiniteSizeDirectlyFollowsMapper extends StreamMiningAlgorithm<Dir private Map<String, BEvent> map = new HashMap<>(); @Override - public DirectlyFollowsRelation ingest(BEvent event) throws Exception { + public DirectlyFollowsRelation ingest(BEvent event) { String caseId = event.getTraceName(); DirectlyFollowsRelation toRet = null; diff --git a/src/main/java/beamline/models/algorithms/StreamMiningAlgorithm.java b/src/main/java/beamline/models/algorithms/StreamMiningAlgorithm.java index 9c249a7..0a4e694 100644 --- a/src/main/java/beamline/models/algorithms/StreamMiningAlgorithm.java +++ b/src/main/java/beamline/models/algorithms/StreamMiningAlgorithm.java @@ -44,10 +44,8 @@ public abstract class StreamMiningAlgorithm<T extends Response> extends RichFlat * * @param event the new event being observed * @return the result of the mining of the event - * @throws Exception general exception that can occur with the ingestion of - * the event */ - public abstract T ingest(BEvent event) throws Exception; + public abstract T ingest(BEvent event); /** * Returns the total number of events processed so far diff --git a/src/main/java/beamline/sources/XesLogSource.java b/src/main/java/beamline/sources/XesLogSource.java index 7aa3893..71098b9 100644 --- a/src/main/java/beamline/sources/XesLogSource.java +++ b/src/main/java/beamline/sources/XesLogSource.java @@ -4,12 +4,18 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.FileAttribute; +import java.nio.file.attribute.PosixFilePermission; +import java.nio.file.attribute.PosixFilePermissions; import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; +import org.apache.commons.lang3.SystemUtils; import org.deckfour.xes.extension.std.XConceptExtension; import org.deckfour.xes.extension.std.XTimeExtension; import org.deckfour.xes.in.XMxmlGZIPParser; @@ -61,9 +67,9 @@ public class XesLogSource extends BeamlineAbstractSource { * is created */ public XesLogSource(XLog log) throws IOException { - File tmpFile = Files.createTempFile("file", ".xes.gz").toFile(); - new XesXmlGZIPSerializer().serialize(log, new FileOutputStream(tmpFile)); - this.fileName = tmpFile.getAbsolutePath(); + Path tmpFile = getTempFile(); + new XesXmlGZIPSerializer().serialize(log, new FileOutputStream(tmpFile.toFile())); + this.fileName = tmpFile.toFile().getAbsolutePath(); } @Override @@ -148,4 +154,19 @@ public class XesLogSource extends BeamlineAbstractSource { // sort events Collections.sort(events); } + + private Path getTempFile() throws IOException { + Path p = null; + if (SystemUtils.IS_OS_UNIX) { + FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------")); + p = Files.createTempFile("log", ".xes.gz", attr); // Compliant + } else { + File f = Files.createTempFile("log", ".xes.gz").toFile(); // Compliant + f.setReadable(true, true); + f.setWritable(true, true); + f.setExecutable(true, true); + p = f.toPath(); + } + return p; + } } diff --git a/src/test/java/beamline/tests/AlgorithmTest.java b/src/test/java/beamline/tests/AlgorithmTest.java index 53c2843..e1ca19e 100644 --- a/src/test/java/beamline/tests/AlgorithmTest.java +++ b/src/test/java/beamline/tests/AlgorithmTest.java @@ -22,7 +22,7 @@ public class AlgorithmTest { private static final long serialVersionUID = -8445717838576941924L; @Override - public StringResponse ingest(BEvent event) throws Exception { + public StringResponse ingest(BEvent event) { return new StringResponse(event.getProcessName() + event.getEventName() + event.getTraceName()); } }; @@ -51,7 +51,7 @@ public class AlgorithmTest { private static final long serialVersionUID = -8445717838576941924L; @Override - public StringResponse ingest(BEvent event) throws Exception { + public StringResponse ingest(BEvent event) { return new StringResponse(event.getProcessName() + event.getEventName() + event.getTraceName()); } }; diff --git a/src/test/java/beamline/tests/ResponsesTest.java b/src/test/java/beamline/tests/ResponsesTest.java index 26d2258..3b933e2 100644 --- a/src/test/java/beamline/tests/ResponsesTest.java +++ b/src/test/java/beamline/tests/ResponsesTest.java @@ -5,7 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.util.Date; +import java.util.Random; import java.util.UUID; import org.junit.jupiter.api.Test; @@ -22,9 +22,9 @@ public class ResponsesTest { public void response_tests() { Response r = new Response(); - Date now = new Date(); - r.setProcessedEvents(now.getTime()); - assertEquals(now, r.getProcessedEvents()); + long rand = new Random().nextLong(); + r.setProcessedEvents(rand); + assertEquals(rand, r.getProcessedEvents()); } @Test diff --git a/src/test/java/beamline/tests/SourcesTest.java b/src/test/java/beamline/tests/SourcesTest.java index 95d18d4..415d548 100644 --- a/src/test/java/beamline/tests/SourcesTest.java +++ b/src/test/java/beamline/tests/SourcesTest.java @@ -212,12 +212,10 @@ public class SourcesTest { java.nio.file.Files.write(tmpFile.toPath(), toWrite.getBytes(), StandardOpenOption.APPEND); } }); -// JobClient job = env.executeAsync(); env.executeAsync(); Thread.sleep(2000); - System.out.println("going"); MqttClient client = new MqttClient("tcp://localhost:9999", "clientid", new MemoryPersistence()); client.connect(); publish(client, "c1", "a11"); @@ -227,8 +225,6 @@ public class SourcesTest { publish(client, "c2", "a23"); Thread.sleep(2000); -// job.cancel(); - System.out.println("Done"); assertEquals( "name-c1-a11/name-c2-a21/name-c2-a22/name-c1-a12/name-c2-a23/", org.apache.commons.io.FileUtils.readFileToString(tmpFile, "utf-8")); -- GitLab