diff --git a/code/mindmap-model/pom.xml b/code/mindmap-model/pom.xml index f6feaf2d..a5132a60 100644 --- a/code/mindmap-model/pom.xml +++ b/code/mindmap-model/pom.xml @@ -27,7 +27,7 @@ commons-io commons-io - 2.11.0 + ${commons.io.version} org.apache.commons diff --git a/code/mindmap-model/src/main/java/com/igormaznitsa/mindmap/model/MindMap.java b/code/mindmap-model/src/main/java/com/igormaznitsa/mindmap/model/MindMap.java index 9197aa02..4318392d 100644 --- a/code/mindmap-model/src/main/java/com/igormaznitsa/mindmap/model/MindMap.java +++ b/code/mindmap-model/src/main/java/com/igormaznitsa/mindmap/model/MindMap.java @@ -149,11 +149,11 @@ public T findNext(File baseFolder, T start, } boolean findPluginNote = (extrasToFind != null && !extrasToFind.isEmpty()) - && (topicFinders != null && !topicFinders.isEmpty()) - && extrasToFind.contains(Extra.ExtraType.NOTE); + && (topicFinders != null && !topicFinders.isEmpty()) + && extrasToFind.contains(Extra.ExtraType.NOTE); boolean findPluginFile = (extrasToFind != null && !extrasToFind.isEmpty()) - && (topicFinders != null && !topicFinders.isEmpty()) - && extrasToFind.contains(Extra.ExtraType.FILE); + && (topicFinders != null && !topicFinders.isEmpty()) + && extrasToFind.contains(Extra.ExtraType.FILE); T result = null; @@ -527,9 +527,15 @@ public Optional findFirstInTree(T parent, Predicate predicate) { List children = parent.getChildren(); if (children != null) { for (T child : children) { - if (findFirstInTree(child, predicate).isPresent()) { + if (predicate.test(child)) { return Optional.ofNullable(child); } + else { + Optional inChildren = findFirstInTree(child, predicate); + if (inChildren.isPresent()) { + return inChildren; + } + } } } return Optional.empty(); @@ -552,9 +558,9 @@ public T findTopicForLink(ExtraTopic link) { } /** - * @deprecated * @param type * @return + * @deprecated */ public List findAllTopicsForExtraType(Extra.ExtraType type) { List result = new ArrayList<>(); diff --git a/code/mindmap-model/src/test/java/com/igormaznitsa/mindmap/model/MindMapTest.java b/code/mindmap-model/src/test/java/com/igormaznitsa/mindmap/model/MindMapTest.java index 0fd64d4d..e408be06 100644 --- a/code/mindmap-model/src/test/java/com/igormaznitsa/mindmap/model/MindMapTest.java +++ b/code/mindmap-model/src/test/java/com/igormaznitsa/mindmap/model/MindMapTest.java @@ -30,8 +30,10 @@ import java.io.StringWriter; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.regex.Pattern; +import org.junit.Assert; import org.junit.Test; public class MindMapTest { @@ -44,36 +46,36 @@ public void testMindMapParse_Error_Empty() throws Exception { @Test(expected = IllegalArgumentException.class) public void testMindMapParse_Error_JustTextString() throws Exception { - MindMapmap = new MindMap<>(new StringReader("Who is here"), TestTopicNode.testTopicCreator); + MindMap map = new MindMap<>(new StringReader("Who is here"), TestTopicNode.testTopicCreator); } @Test public void testMindMapParse_Error_OnlyHeader() throws Exception { - MindMapmap = new MindMap<>(new StringReader("Who is here\n-"), TestTopicNode.testTopicCreator); + MindMap map = new MindMap<>(new StringReader("Who is here\n-"), TestTopicNode.testTopicCreator); assertNull(map.getRoot()); } @Test public void testMindMapParse_Error_HeaderAndOnlyLevelChar() throws Exception { - MindMapmap = new MindMap<>(new StringReader("Who is here\n-\n#"), TestTopicNode.testTopicCreator); + MindMap map = new MindMap<>(new StringReader("Who is here\n-\n#"), TestTopicNode.testTopicCreator); assertNull(map.getRoot()); } @Test public void testMindMapParse_Error_HeaderAndOnlySpacesAfterLevelChar() throws Exception { - MindMapmap = new MindMap<>(new StringReader("Who is here\n-\n# "), TestTopicNode.testTopicCreator); + MindMap map = new MindMap<>(new StringReader("Who is here\n-\n# "), TestTopicNode.testTopicCreator); assertEquals(" ", map.getRoot().getText()); } @Test public void testFindNext_Null() throws Exception { - MindMapmap = new MindMap<>(new StringReader("test\n---\n# Solar\n## Mercury\n## Venus\n## Earth\n### Moon\n## Mars\n### Phobos\n### Deimos"), TestTopicNode.testTopicCreator); + MindMap map = new MindMap<>(new StringReader("test\n---\n# Solar\n## Mercury\n## Venus\n## Earth\n### Moon\n## Mars\n### Phobos\n### Deimos"), TestTopicNode.testTopicCreator); assertEquals("Mercury", map.findNext(null, null, Pattern.compile(Pattern.quote("cury")), true, null).getText()); } @Test public void testFindNext_NonNull() throws Exception { - MindMapmap = new MindMap<>(new StringReader("test\n---\n# Solar\n## Mercury\n## Venus\n## Earth\n### Moon\n## Mars\n### Phobos\n### Deimos"), TestTopicNode.testTopicCreator); + MindMap map = new MindMap<>(new StringReader("test\n---\n# Solar\n## Mercury\n## Venus\n## Earth\n### Moon\n## Mars\n### Phobos\n### Deimos"), TestTopicNode.testTopicCreator); TestTopicNode base = map.findNext(null, null, Pattern.compile(Pattern.quote("cury")), true, null); assertNull(map.findNext(null, base, Pattern.compile(Pattern.quote("cury")), true, null)); TestTopicNode earth = map.findNext(null, base, Pattern.compile(Pattern.quote("ar")), true, null); @@ -85,13 +87,13 @@ public void testFindNext_NonNull() throws Exception { @Test public void testFindPrev_Null() throws Exception { - MindMapmap = new MindMap<>(new StringReader("test\n---\n# Solar\n## Mercury\n## Venus\n## Earth\n### Moon\n## Mars\n### Phobos\n### Deimos"), TestTopicNode.testTopicCreator); + MindMap map = new MindMap<>(new StringReader("test\n---\n# Solar\n## Mercury\n## Venus\n## Earth\n### Moon\n## Mars\n### Phobos\n### Deimos"), TestTopicNode.testTopicCreator); assertEquals("Solar", map.findPrev(null, null, Pattern.compile(Pattern.quote("lar")), true, null).getText()); } @Test public void testFindPrev_NonNull() throws Exception { - MindMapmap = new MindMap<>(new StringReader("test\n---\n# Solar\n## Mercury\n## Venus\n## Earth\n### Moon\n## Mars\n### Phobos\n### Deimos"), TestTopicNode.testTopicCreator); + MindMap map = new MindMap<>(new StringReader("test\n---\n# Solar\n## Mercury\n## Venus\n## Earth\n### Moon\n## Mars\n### Phobos\n### Deimos"), TestTopicNode.testTopicCreator); TestTopicNode base = map.findNext(null, null, Pattern.compile(Pattern.quote("Deimos")), true, null); TestTopicNode mars = map.findPrev(null, base, Pattern.compile(Pattern.quote("ar")), true, null); assertEquals("Mars", mars.getText()); @@ -104,42 +106,42 @@ public void testFindPrev_NonNull() throws Exception { @Test public void testMindMapParse_NoAttributes() throws Exception { - MindMapmap = new MindMap<>(new StringReader("lkf\n---\n# Hello"), TestTopicNode.testTopicCreator); + MindMap map = new MindMap<>(new StringReader("lkf\n---\n# Hello"), TestTopicNode.testTopicCreator); assertNull(map.getAttribute("test")); assertEquals("Hello", map.getRoot().getText()); } @Test public void testMindMapParse_oneAttributeDoubleNewLine() throws Exception { - MindMapmap = new MindMap<>(new StringReader("lkf\n> test=`Hi`\n\n---\n# Hello"), TestTopicNode.testTopicCreator); + MindMap map = new MindMap<>(new StringReader("lkf\n> test=`Hi`\n\n---\n# Hello"), TestTopicNode.testTopicCreator); assertEquals("Hi", map.getAttribute("test")); assertEquals("Hello", map.getRoot().getText()); } @Test public void testMindMapParse_oneAttributeSingleNewLine() throws Exception { - MindMapmap = new MindMap<>(new StringReader("lkf\n> test=`Hi`\n---\n# Hello"), TestTopicNode.testTopicCreator); + MindMap map = new MindMap<>(new StringReader("lkf\n> test=`Hi`\n---\n# Hello"), TestTopicNode.testTopicCreator); assertEquals("Hi", map.getAttribute("test")); assertEquals("Hello", map.getRoot().getText()); } @Test public void testMindMapParse_overridenAttributes_SingleNewLine() throws Exception { - MindMapmap = new MindMap<>(new StringReader("lkf\n> test=`Hi`\n> test=`Lo`\n---\n# Hello"), TestTopicNode.testTopicCreator); + MindMap map = new MindMap<>(new StringReader("lkf\n> test=`Hi`\n> test=`Lo`\n---\n# Hello"), TestTopicNode.testTopicCreator); assertEquals("Lo", map.getAttribute("test")); assertEquals("Hello", map.getRoot().getText()); } @Test public void testMindMapParse_overridenAttributes_SeveralNewLine() throws Exception { - MindMapmap = new MindMap<>(new StringReader("lkf\n> test=`Hi`\n> test=`Lo`\n---\n\n\n\n# Hello"), TestTopicNode.testTopicCreator); + MindMap map = new MindMap<>(new StringReader("lkf\n> test=`Hi`\n> test=`Lo`\n---\n\n\n\n# Hello"), TestTopicNode.testTopicCreator); assertEquals("Lo", map.getAttribute("test")); assertEquals("Hello", map.getRoot().getText()); } @Test public void testMindMapWrite_WithoutAttributes() throws Exception { - MindMapmap = new MindMap<>(); + MindMap map = new MindMap<>(); StringWriter writer = new StringWriter(); map.write(writer); assertEquals("Mind Map generated by Mindolph \n> __version__=`1.1`\n---\n", writer.toString()); @@ -147,7 +149,7 @@ public void testMindMapWrite_WithoutAttributes() throws Exception { @Test public void testMindMapWrite_WithAttribute() throws Exception { - MindMapmap = new MindMap<>(); + MindMap map = new MindMap<>(); map.setAttribute("hello", "World"); StringWriter writer = new StringWriter(); map.write(writer); @@ -166,7 +168,7 @@ public void testIteration_TwoLevel() throws Exception { @Test public void testIteration_Empty() throws Exception { - MindMapmap = new MindMap<>(new StringReader("---\n"), TestTopicNode.testTopicCreator); + MindMap map = new MindMap<>(new StringReader("---\n"), TestTopicNode.testTopicCreator); assertFalse(map.iterator().hasNext()); } @@ -182,7 +184,7 @@ public void testIteration_OnlyRoot() throws Exception { @Test public void testIteration_OnlyRoot_WithCodeSnippetsJavaAndShell() throws Exception { - MindMapmap = new MindMap<>(new StringReader("---\n# root\n```Java\nSystem.exit(0);\n```\n```Shell\nexit\n```"), TestTopicNode.testTopicCreator); + MindMap map = new MindMap<>(new StringReader("---\n# root\n```Java\nSystem.exit(0);\n```\n```Shell\nexit\n```"), TestTopicNode.testTopicCreator); assertEquals(2, map.getRoot().getCodeSnippets().size()); assertEquals("System.exit(0);\n", map.getRoot().getCodeSnippet("Java")); assertEquals("exit\n", map.getRoot().getCodeSnippet("Shell")); @@ -199,4 +201,28 @@ public void testSerializableDeserializable_NoErrors() throws Exception { ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); assertTrue(in.readObject() instanceof MindMap); } + + @Test + public void testFindFirstInTree() throws Exception { + MindMap map = new MindMap<>(new StringReader(""" + --- + # root + ## child1 + ### child1.1 + ### child1.2 + ## child2 + ### child2.1 + ### child2.2 + """), TestTopicNode.testTopicCreator); + map.traverseTopicTree(t -> { + System.out.println(t.getText()); + }); + map.anyMatchInTree(t -> "child2.2".equals(t.getText())); + + Optional firstInTree = map.findFirstInTree(t -> "child2.2".equals(t.getText())); + Assert.assertNotNull(firstInTree); + Assert.assertTrue(firstInTree.isPresent()); + Assert.assertEquals("child2.2", firstInTree.get().getText()); + System.out.println(firstInTree.get().getText()); + } }