|
15 | 15 | */
|
16 | 16 | package org.eclipse.jdt.core.groovy.tests.builder;
|
17 | 17 |
|
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
18 | 21 | import junit.framework.Test;
|
19 | 22 |
|
20 | 23 | import org.codehaus.groovy.eclipse.core.builder.GroovyClasspathContainer;
|
@@ -64,25 +67,143 @@ protected void setUp() throws Exception {
|
64 | 67 | env.createFolder(src);
|
65 | 68 | }
|
66 | 69 |
|
67 |
| - // |
| 70 | + private void assertAccessRestriction(String source, String... types) { |
| 71 | + IPath foo = env.addGroovyClass(src, "Foo", source); |
| 72 | + fullBuild(); |
| 73 | + |
| 74 | + // read back contents in case of line delimeters change or package statement addition or ... |
| 75 | + source = env.readTextFile(foo); |
| 76 | + |
| 77 | + List<String> problems = new ArrayList<String>(); |
| 78 | + for (String type : types) { |
| 79 | + int offset = -1; |
| 80 | + while ((offset = source.indexOf(type.trim(), offset + 1)) != -1) { |
| 81 | + problems.add("Problem : Access restriction: The type '" + type.trim() + "' is not API (restriction on required library '##')" + |
| 82 | + " [ resource : </Project/src/Foo.groovy> range : <" + offset + "," + (offset + type.length()) + "> category : <150> severity : <2>]"); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + expectingProblemsFor(foo, problems); |
| 87 | + } |
| 88 | + |
| 89 | + //-------------------------------------------------------------------------- |
| 90 | + |
| 91 | + public void testAccessForImport() { |
| 92 | + String source = "import java.beans.BeanDescriptor"; |
| 93 | + |
| 94 | + assertAccessRestriction(source, "BeanDescriptor"); |
| 95 | + } |
68 | 96 |
|
69 | 97 | public void testAccessForExtends() {
|
70 |
| - env.addGroovyClass(src, "Foo", |
71 |
| - "import java.beans.*\n" + |
72 |
| - "class Foo extends BeanDescriptor {}"); |
73 |
| - fullBuild(); |
| 98 | + String source = "import java.beans.*\n" + |
| 99 | + "class Foo extends BeanDescriptor {}"; |
74 | 100 |
|
75 |
| - expectingProblemsFor(src.append("Foo.groovy"), "Problem : Access restriction: The type 'BeanDescriptor' is not API" + |
76 |
| - " (restriction on required library '##') [ resource : </Project/src/Foo.groovy> range : <38,52> category : <150> severity : <2>]"); |
| 101 | + assertAccessRestriction(source, "BeanDescriptor"); |
77 | 102 | }
|
78 | 103 |
|
79 | 104 | public void testAccessForImplements() {
|
80 |
| - env.addGroovyClass(src, "Foo", |
81 |
| - "import java.beans.*\n" + |
82 |
| - "abstract class Foo implements BeanInfo {}"); |
83 |
| - fullBuild(); |
| 105 | + String source = "import java.beans.*\n" + |
| 106 | + "abstract class Foo implements BeanInfo {}"; |
| 107 | + |
| 108 | + assertAccessRestriction(source, "BeanInfo "); // interface has +1 sloc... |
| 109 | + } |
| 110 | + |
| 111 | + public void testAccessForExtendsGenerics() { |
| 112 | + String source = "import java.beans.*\n" + |
| 113 | + "class Foo extends ArrayList<BeanDescriptor> {}"; |
| 114 | + |
| 115 | + assertAccessRestriction(source, "BeanDescriptor"); |
| 116 | + } |
| 117 | + |
| 118 | + public void testAccessForImplementsGenerics() { |
| 119 | + String source = "import java.beans.*\n" + |
| 120 | + "abstract class Foo implements List<BeanInfo> {}"; |
| 121 | + |
| 122 | + assertAccessRestriction(source, "BeanInfo"); |
| 123 | + } |
| 124 | + |
| 125 | + public void testAccessForField() { |
| 126 | + String source = "import java.beans.*\n" + |
| 127 | + "class Foo { private BeanInfo info }"; |
| 128 | + |
| 129 | + assertAccessRestriction(source, "BeanInfo"); |
| 130 | + } |
| 131 | + |
| 132 | + public void testAccessForProperty() { |
| 133 | + String source = "import java.beans.*\n" + |
| 134 | + "class Foo { BeanInfo info }"; |
| 135 | + |
| 136 | + assertAccessRestriction(source, "BeanInfo"); |
| 137 | + } |
| 138 | + |
| 139 | + public void testAccessForFieldGenerics() { |
| 140 | + String source = "import java.beans.*\n" + |
| 141 | + "class Foo { private List<BeanInfo> info }"; |
| 142 | + |
| 143 | + assertAccessRestriction(source, "BeanInfo"); |
| 144 | + } |
| 145 | + |
| 146 | + public void testAccessForPropertyGenerics() { |
| 147 | + String source = "import java.beans.*\n" + |
| 148 | + "class Foo { List<BeanInfo> info }"; |
| 149 | + |
| 150 | + assertAccessRestriction(source, "BeanInfo"); |
| 151 | + } |
| 152 | + |
| 153 | + public void testAccessForLazyProperty() { |
| 154 | + String source = "import java.beans.*\n" + |
| 155 | + "abstract class Foo {\n" + |
| 156 | + " @Lazy BeanInfo info = init()\n" + |
| 157 | + " abstract def init()\n" + |
| 158 | + "}"; |
| 159 | + |
| 160 | + assertAccessRestriction(source, "BeanInfo"); |
| 161 | + } |
| 162 | + |
| 163 | + public void testAccessForMethodParameter() { |
| 164 | + String source = "import java.beans.*\n" + |
| 165 | + "class Foo {\n" + |
| 166 | + " def meth(BeanInfo info) { }\n" + |
| 167 | + "}"; |
| 168 | + |
| 169 | + assertAccessRestriction(source, "BeanInfo"); |
| 170 | + } |
| 171 | + |
| 172 | + public void testAccessForMethodReturnType() { |
| 173 | + String source = "import java.beans.*\n" + |
| 174 | + "class Foo {\n" + |
| 175 | + " BeanInfo meth() { }\n" + |
| 176 | + "}"; |
| 177 | + |
| 178 | + assertAccessRestriction(source, "BeanInfo"); |
| 179 | + } |
| 180 | + |
| 181 | + public void testAccessForMethodParameterGenerics() { |
| 182 | + String source = "import java.beans.*\n" + |
| 183 | + "class Foo {\n" + |
| 184 | + " def meth(List<BeanInfo> info) { }\n" + |
| 185 | + " }"; |
| 186 | + |
| 187 | + assertAccessRestriction(source, "BeanInfo"); |
| 188 | + } |
| 189 | + |
| 190 | + public void testAccessForMethodReturnTypeGenerics() { |
| 191 | + String source = "import java.beans.*\n" + |
| 192 | + "class Foo {\n" + |
| 193 | + " List<BeanInfo> meth() { }\n" + |
| 194 | + "}"; |
| 195 | + |
| 196 | + assertAccessRestriction(source, "BeanInfo"); |
| 197 | + } |
| 198 | + |
| 199 | + public void testAccessForLocalVariable() { |
| 200 | + String source = "import java.beans.*\n" + |
| 201 | + "class Foo {\n" + |
| 202 | + " def meth() {\n" + |
| 203 | + " BeanInfo info = null\n" + |
| 204 | + " }\n" + |
| 205 | + "}"; |
84 | 206 |
|
85 |
| - expectingProblemsFor(src.append("Foo.groovy"), "Problem : Access restriction: The type 'BeanInfo' is not API" + |
86 |
| - " (restriction on required library '##') [ resource : </Project/src/Foo.groovy> range : <50,59> category : <150> severity : <2>]"); |
| 207 | + assertAccessRestriction(source, "BeanInfo"); |
87 | 208 | }
|
88 | 209 | }
|
0 commit comments