package no.uio.ifi.refaktor.tests; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import no.uio.ifi.refaktor.extractors.LongestCommonPrefixExtractor; import no.uio.ifi.refaktor.utils.SmartTextSelection; import org.eclipse.jface.text.Document; import org.junit.Before; import org.junit.Test; public class LongestCommonPrefixExtractorTest { private String classStart; private String methodText; private String endStartFoo; private String startFoo; private String testCode; private String singleStatement; private String testExpression; private String blockText; @Before public void setUp() throws Exception { classStart = "class TestClass {"; endStartFoo = "b.c.d();a.b.c.f();"; startFoo = "a." + endStartFoo; singleStatement = "a.b.c.g(a.c);"; testExpression = "a.b.d.f()"; blockText = "{" + startFoo + "if(" + testExpression + "){" + singleStatement + "return a.b.d();}}"; methodText = "public void foo() " + blockText; testCode = classStart + methodText + "}"; } @Test public void testSelectionIsValidStartFoo() { makeStartFooExtractor(); } @Test public void testExtractPropertyStartFoo() { LongestCommonPrefixExtractor extractor = makeStartFooExtractor(); extractor.extractProperty(); assertEquals("a.b.c", extractor.stringProperty()); } private LongestCommonPrefixExtractor makeStartFooExtractor() { SmartTextSelection selection = new SmartTextSelection(new Document(testCode), testCode.indexOf(startFoo), startFoo.length()); LongestCommonPrefixExtractor extractor = new LongestCommonPrefixExtractor(selection); assertTrue(extractor.selectionIsValid()); return extractor; } @Test public void testSelectionIsValidEndStartFoo() { SmartTextSelection selection = new SmartTextSelection(new Document(testCode), testCode.indexOf(endStartFoo), endStartFoo.length()); LongestCommonPrefixExtractor extractor = new LongestCommonPrefixExtractor(selection); assertFalse("Partial selection of statements shall not be allowed", extractor.selectionIsValid()); } @Test public void testSelectionIsValidSingleStatement() { makeSingleStatementExtractor(); } @Test public void testExtractPropertySingleStatement() { LongestCommonPrefixExtractor extractor = makeSingleStatementExtractor(); extractor.extractProperty(); assertEquals("a.b.c", extractor.stringProperty()); } private LongestCommonPrefixExtractor makeSingleStatementExtractor() { SmartTextSelection selection = new SmartTextSelection(new Document(testCode), testCode.indexOf(singleStatement), singleStatement.length()); LongestCommonPrefixExtractor extractor = new LongestCommonPrefixExtractor(selection); assertTrue("Single statements shall be allowed", extractor.selectionIsValid()); return extractor; } @Test public void testSelectionIsValidTestExpression() { SmartTextSelection selection = new SmartTextSelection(new Document(testCode), testCode.indexOf(testExpression), testExpression.length()); LongestCommonPrefixExtractor extractor = new LongestCommonPrefixExtractor(selection); assertFalse("Expressions shall not be allowed", extractor.selectionIsValid()); } @Test public void testSelectionIsValidMethodText() { makeMethodTextExtractor(); } @Test public void testExtractPropertyMethodText() { LongestCommonPrefixExtractor extractor = makeMethodTextExtractor(); extractor.extractProperty(); assertEquals("a.b", extractor.stringProperty()); } private LongestCommonPrefixExtractor makeMethodTextExtractor() { SmartTextSelection selection = new SmartTextSelection(new Document(testCode), testCode.indexOf(methodText), methodText.length()); LongestCommonPrefixExtractor extractor = new LongestCommonPrefixExtractor(selection); assertTrue("Selecting whole methods should be allowed", extractor.selectionIsValid()); return extractor; } @Test public void testSelectionIsValidBlockText() { makeBlockTextExtractor(); } @Test public void textExtractPropertyBlockText() { LongestCommonPrefixExtractor extractor = makeBlockTextExtractor(); extractor.extractProperty(); assertEquals("a.b", extractor.stringProperty()); } private LongestCommonPrefixExtractor makeBlockTextExtractor() { SmartTextSelection selection = new SmartTextSelection(new Document(testCode), testCode.indexOf(blockText), blockText.length()); LongestCommonPrefixExtractor extractor = new LongestCommonPrefixExtractor(selection); assertTrue("Selecting blocks should be allowed", extractor.selectionIsValid()); return extractor; } @Test public void textExtractPropertyBug20130906() { String start = classStart + "public void foo() {"; String methodBody = "A a = new A(); a.c.d.bar(); a.c.b.bar(); a.c.b.bar();"; String end = "}}"; String testCode = start + methodBody + end ; SmartTextSelection selection = new SmartTextSelection(new Document(testCode), testCode.indexOf(methodBody), methodBody.length()); LongestCommonPrefixExtractor extractor = new LongestCommonPrefixExtractor(selection); assertTrue(extractor.selectionIsValid()); extractor.extractProperty(); assertEquals("a.c", extractor.stringProperty()); } }