diff --git a/src/test/java/spoon/test/template/TemplateTest.java b/src/test/java/spoon/test/template/TemplateTest.java index 5b6d0fd6c39..8751a927124 100644 --- a/src/test/java/spoon/test/template/TemplateTest.java +++ b/src/test/java/spoon/test/template/TemplateTest.java @@ -12,6 +12,7 @@ import spoon.reflect.declaration.CtElement; import spoon.reflect.declaration.CtField; import spoon.reflect.declaration.CtMethod; +import spoon.reflect.declaration.CtNamedElement; import spoon.reflect.declaration.CtParameter; import spoon.reflect.factory.Factory; import spoon.reflect.visitor.ModelConsistencyChecker; @@ -325,4 +326,27 @@ public void testTemplateMatcherWithWholePackage() throws Exception { assertEquals("Parent of the method is not a class called BServiceImpl", "BServiceImpl", bservice.getSimpleName()); } + @Test + public void testTemplateMatcherMatchTwoSnippets() throws Exception { + Launcher spoon = new Launcher(); + spoon.addInputResource("./src/test/java/spoon/test/template/testclasses/TwoSnippets.java"); + spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SecurityCheckerTemplate.java")); + + spoon.buildModel(); + Factory factory = spoon.getFactory(); + + CtClass templateKlass = factory.Class().get(SecurityCheckerTemplate.class); + CtMethod templateMethod = (CtMethod) templateKlass.getElements(new NameFilter("matcher1")).get(0); + CtIf templateRoot = (CtIf) templateMethod.getBody().getStatement(0); + TemplateMatcher matcher = new TemplateMatcher(templateRoot); + + List matches = matcher.find(factory.getModel().getRootPackage()); + + assertEquals(2, matches.size()); + + CtElement match1 = matches.get(0); + CtElement match2 = matches.get(1); + + assertTrue(match1.equals(match2)); + } } diff --git a/src/test/java/spoon/test/template/testclasses/TwoSnippets.java b/src/test/java/spoon/test/template/testclasses/TwoSnippets.java new file mode 100644 index 00000000000..8a1187ba844 --- /dev/null +++ b/src/test/java/spoon/test/template/testclasses/TwoSnippets.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2006-2016 INRIA and contributors + * Spoon - http://spoon.gforge.inria.fr/ + * + * This software is governed by the CeCILL-C License under French law and abiding by the rules of distribution of free software. You can use, modify and/or redistribute the software under the terms of the CeCILL-C license as circulated by CEA, CNRS and INRIA at http://www.cecill.info. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details. + * + * The fact that you are presently reading this means that you have had knowledge of the CeCILL-C license and that you accept its terms. + */ + +package spoon.test.template.testclasses; + +/** + * Created by urli on 08/12/2016. + */ +public class TwoSnippets { + + private String bDao; + + private ContextHelper contextHelper; + + public void hello() { + if(!contextHelper.hasPermission("c")) { + throw new SecurityException(); + } + bDao.toString(); + } + + public void toto() { + if(!contextHelper.hasPermission("c")) { + throw new SecurityException(); + } + bDao.toString(); + } +} +