Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new test to check if TemplateMatcher can match two snippets #1059

Merged
merged 14 commits into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/test/java/spoon/test/template/TemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<CtElement> matches = matcher.find(factory.getModel().getRootPackage());

assertEquals(2, matches.size());

CtElement match1 = matches.get(0);
CtElement match2 = matches.get(1);

assertTrue(!match1.equals(match2));
}
}
37 changes: 37 additions & 0 deletions src/test/java/spoon/test/template/testclasses/TwoSnippets.java
Original file line number Diff line number Diff line change
@@ -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("b")) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to see if we match both if they are exactly the same.

throw new SecurityException();
}
bDao.toString();
}
}