Skip to content

Commit

Permalink
Add test for build path access rules (issue #166)
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jan 29, 2017
1 parent e131c04 commit e29b19c
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 150 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eclipse.jdt.core.groovy.tests.builder;

import junit.framework.Test;

import org.codehaus.groovy.eclipse.core.builder.GroovyClasspathContainer;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IAccessRule;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.tests.builder.BuilderTests;
import org.eclipse.jdt.internal.core.ClasspathAccessRule;

/**
* Tests for build path access rules (i.e. restrictions placed on classpath entries).
* <p>
* Example:<pre>
* &lt;classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
* &lt;accessrules>
* &lt;accessrule kind="nonaccessible" pattern="java/beans/**"/>
* &lt;/accessrules>
* &lt;/classpathentry>
* </pre>
*/
public final class BuildAccessRulesTests extends BuilderTests {

public static Test suite() {
return buildTestSuite(BuildAccessRulesTests.class);
}

public BuildAccessRulesTests(String name) {
super(name);
}

protected IPath src;

@Override
protected void setUp() throws Exception {
super.setUp();

IPath projectPath = env.addProject("Project", "1.5");
env.addGroovyNature("Project");
env.setClasspath(projectPath, new IClasspathEntry[] {
JavaCore.newSourceEntry(src = projectPath.append("src")),
JavaCore.newContainerEntry(GroovyClasspathContainer.CONTAINER_ID),
JavaCore.newContainerEntry(new Path("org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"),
new IAccessRule[] {new ClasspathAccessRule(new Path("java/beans/**"), IAccessRule.K_NON_ACCESSIBLE)}, null, false) // create access restriction
});
env.createFolder(src);
}

//

public void testAccessForExtends() {
env.addGroovyClass(src, "Foo",
"import java.beans.*\n" +
"class Foo extends BeanDescriptor {}");
fullBuild();

expectingProblemsFor(src.append("Foo.groovy"), "Problem : Access restriction: The type 'BeanDescriptor' is not API" +
" (restriction on required library '##') [ resource : </Project/src/Foo.groovy> range : <38,52> category : <150> severity : <2>]");
}

public void testAccessForImplements() {
env.addGroovyClass(src, "Foo",
"import java.beans.*\n" +
"abstract class Foo implements BeanInfo {}");
fullBuild();

expectingProblemsFor(src.append("Foo.groovy"), "Problem : Access restriction: The type 'BeanInfo' is not API" +
" (restriction on required library '##') [ resource : </Project/src/Foo.groovy> range : <50,59> category : <150> severity : <2>]");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2016 the original author or authors.
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,7 @@
import org.eclipse.jdt.core.IProblemRequestor;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.groovy.tests.compiler.ReconcilerUtils;
import org.eclipse.jdt.core.tests.builder.BuilderTests;
import org.eclipse.jdt.core.tests.util.GroovyUtils;
import org.eclipse.jdt.core.tests.util.Util;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
Expand All @@ -49,11 +50,8 @@
* </ul>
* <p>
* Once setup like that it is usable for testing here.
*
* @author Andy CLement
* @since 2.5.1
*/
public class FullProjectTests extends GroovierBuilderTests {
public class FullProjectTests extends BuilderTests {

public FullProjectTests(String name) {
super(name);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2016 the original author or authors.
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,10 +20,10 @@
import java.util.Collections;
import java.util.List;

import junit.framework.Test;

import groovy.lang.GroovyClassLoader;

import junit.framework.Test;

import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
import org.codehaus.groovy.ast.ClassNode;
Expand All @@ -38,14 +38,12 @@
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.ErrorCollector;
import org.codehaus.groovy.control.SourceUnit;
import org.eclipse.jdt.core.groovy.tests.builder.GroovierBuilderTests;
import org.eclipse.jdt.core.tests.builder.BuilderTests;
/**
* Test the source locations of ASTNodes to ensure they are correct, especially
* look into the changes that we force into them.
* @author andrew
* @created Jun 9, 2010
*/
public class ASTNodeSourceLocationsTests extends GroovierBuilderTests {
public class ASTNodeSourceLocationsTests extends BuilderTests {

public ASTNodeSourceLocationsTests(String name) {
super(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2016 the original author or authors.
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,7 +41,7 @@
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.groovy.tests.builder.GroovierBuilderTests;
import org.eclipse.jdt.core.tests.builder.BuilderTests;
import org.eclipse.jdt.core.tests.builder.Problem;
import org.eclipse.jdt.core.tests.util.Util;
import org.eclipse.jdt.groovy.core.util.JavaConstants;
Expand All @@ -57,11 +57,8 @@
* markers /*m1en* / /*f1en* / /*t1en* / indicate end of method, field and type names
* markers /*m1sb* / indicate the start of a method body
* NOTE: the start of a type body is not being calculated correctly
*
* @author Andrew Eisenberg
* @created Jun 29, 2009
*/
public class SourceLocationsTests extends GroovierBuilderTests {
public class SourceLocationsTests extends BuilderTests {
public SourceLocationsTests(String name) {
super(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected void expectingNoPresenceOf(IPath[] paths) {

/** Verifies that given classes have been compiled.
*/
protected void expectingCompiledClasses(String[] expected) {
protected void expectingCompiledClasses(String... expected) {
String[] actual = debugRequestor.getCompiledClasses();
org.eclipse.jdt.internal.core.util.Util.sort(actual);
org.eclipse.jdt.internal.core.util.Util.sort(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private void addBuilderSpecs(String projectName) {
try {
IProject project = getProject(projectName);
IProjectDescription description = project.getDescription();
description.setNatureIds(new String[] { JavaCore.NATURE_ID, GroovyNature.GROOVY_NATURE });
description.setNatureIds(new String[] { GroovyNature.GROOVY_NATURE, JavaCore.NATURE_ID });
project.setDescription(description, null);
} catch (CoreException e) {
handleCoreException(e);
Expand Down Expand Up @@ -565,7 +565,7 @@ private IFile createFile(IPath path, byte[] contents) {
return null;
}

private IFolder createFolder(IPath path) {
public IFolder createFolder(IPath path) {
checkAssertion("root", !path.isRoot());

/* don't create folders for projects */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ public static void assertStringEquals(String expected, String actual, boolean sh
public static void assertStringEquals(String message, String expected, String actual, boolean showLineSeparators) {
if (expected == null && actual == null)
return;
if (expected != null && expected.equals(actual))
if (expected != null && actual.matches("\\Q" + expected.replace("##", "\\E.*\\Q") + "\\E"))
return;
final StringBuffer formatted;
final StringBuilder formatted;
if (message != null) {
formatted = new StringBuffer(message).append('.');
formatted = new StringBuilder(message).append('.');
} else {
formatted = new StringBuffer();
formatted = new StringBuilder();
}
if (showLineSeparators) {
final String expectedWithLineSeparators = showLineSeparators(expected);
Expand All @@ -234,15 +234,15 @@ public static void assertStringEquals(String message, String expected, String ac
formatted.append("\n------------ but was ------------\n");
formatted.append(actualWithLineSeparators);
formatted.append("\n--------- Difference is ----------\n");
throw new ComparisonFailure(formatted.toString(),
expectedWithLineSeparators,
actualWithLineSeparators);

throw new ComparisonFailure(formatted.toString(), expectedWithLineSeparators, actualWithLineSeparators);
} else {
formatted.append("\n----------- Expected ------------\n");
formatted.append(expected);
formatted.append("\n------------ but was ------------\n");
formatted.append(actual);
formatted.append("\n--------- Difference is ----------\n");

throw new ComparisonFailure(formatted.toString(), expected, actual);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2016 the original author or authors.
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,15 +17,31 @@

import junit.framework.Test;
import junit.framework.TestSuite;

// From org.eclipse.jdt.groovy.core.tests.builder plug-in:
import org.eclipse.jdt.core.groovy.tests.builder.*;
import org.eclipse.jdt.core.groovy.tests.compiler.*;
import org.eclipse.jdt.core.groovy.tests.locations.*;
import org.eclipse.jdt.core.groovy.tests.model.*;
import org.eclipse.jdt.core.groovy.tests.search.*;
import org.eclipse.jdt.core.groovy.tests.builder.BasicGroovyBuildTests;
import org.eclipse.jdt.core.groovy.tests.builder.BuildAccessRulesTests;
import org.eclipse.jdt.core.groovy.tests.builder.FullProjectTests;
import org.eclipse.jdt.core.groovy.tests.compiler.STCScriptsTests;
import org.eclipse.jdt.core.groovy.tests.compiler.ScriptFolderTests;
import org.eclipse.jdt.core.groovy.tests.locations.ASTConverterTests;
import org.eclipse.jdt.core.groovy.tests.locations.ASTNodeSourceLocationsTests;
import org.eclipse.jdt.core.groovy.tests.locations.LocationSupportTests;
import org.eclipse.jdt.core.groovy.tests.locations.SourceLocationsTests;
import org.eclipse.jdt.core.groovy.tests.model.ASTTransformsTests;
import org.eclipse.jdt.core.groovy.tests.model.GroovyClassFileTests;
import org.eclipse.jdt.core.groovy.tests.model.GroovyCompilationUnitTests;
import org.eclipse.jdt.core.groovy.tests.model.GroovyContentTypeTests;
import org.eclipse.jdt.core.groovy.tests.model.GroovyPartialModelTests;
import org.eclipse.jdt.core.groovy.tests.model.MoveRenameCopyTests;
import org.eclipse.jdt.core.groovy.tests.search.AllSearchTests;
// From org.eclipse.jdt.groovy.core.tests.compiler plug-in:
import org.eclipse.jdt.groovy.core.tests.basic.*;
import org.eclipse.jdt.groovy.core.tests.basic.AnnotationsTests;
import org.eclipse.jdt.groovy.core.tests.basic.ErrorRecoveryTests;
import org.eclipse.jdt.groovy.core.tests.basic.GenericsTests;
import org.eclipse.jdt.groovy.core.tests.basic.GroovySimpleTest;
import org.eclipse.jdt.groovy.core.tests.basic.GroovySimpleTests_Compliance_1_8;
import org.eclipse.jdt.groovy.core.tests.basic.TraitsTests;
import org.eclipse.jdt.groovy.core.tests.basic.TransformationsTests;

/**
* All Groovy-JDT integration tests.
Expand All @@ -45,6 +61,7 @@ public static Test suite() throws Exception {

// Builder tests
suite.addTest(BasicGroovyBuildTests.suite());
suite.addTest(BuildAccessRulesTests.suite());
suite.addTest(FullProjectTests.suite());

// Compiler tests
Expand Down

0 comments on commit e29b19c

Please sign in to comment.