-
Notifications
You must be signed in to change notification settings - Fork 753
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42207 from Thushara-Piyasekara/externalDependency…
…Annotation Add ExternalDependency annotation
- Loading branch information
Showing
5 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...est/java/org/ballerinalang/test/annotations/ExternalDependencyAnnotationNegativeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you 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.ballerinalang.test.annotations; | ||
|
||
import org.ballerinalang.test.BAssertUtil; | ||
import org.ballerinalang.test.BCompileUtil; | ||
import org.ballerinalang.test.CompileResult; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* Negative tests for ExternalDependency annotation. | ||
* | ||
* @since 2201.9.0 | ||
*/ | ||
public class ExternalDependencyAnnotationNegativeTest { | ||
|
||
@Test(description = "Negative tests for ExternalDependency annotation") | ||
public void testExternalDependencyAnnotation() { | ||
CompileResult compileResult = | ||
BCompileUtil.compile("test-src/annotations/external_dependency_annotation_negative.bal"); | ||
int i = 0; | ||
BAssertUtil.validateError(compileResult, i++, | ||
"annotation 'ballerina/jballerina.java:0.0.0:ExternalDependency' is not allowed on var", 20, 1); | ||
BAssertUtil.validateError(compileResult, i++, | ||
"annotation 'ballerina/jballerina.java:0.0.0:ExternalDependency' is not allowed on const", 23, 1); | ||
Assert.assertEquals(compileResult.getErrorCount(), i); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...st/src/test/java/org/ballerinalang/test/annotations/ExternalDependencyAnnotationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you 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.ballerinalang.test.annotations; | ||
|
||
import org.ballerinalang.model.elements.AttachPoint; | ||
import org.ballerinalang.test.BCompileUtil; | ||
import org.ballerinalang.test.CompileResult; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
import org.wso2.ballerinalang.compiler.tree.BLangFunction; | ||
import org.wso2.ballerinalang.compiler.tree.BLangPackage; | ||
import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
/** | ||
* Class to test ExternalDependency annotation. | ||
* | ||
* @since 2201.9.0 | ||
*/ | ||
public class ExternalDependencyAnnotationTest { | ||
|
||
private BLangPackage pkgNode; | ||
|
||
@BeforeClass | ||
public void setup() { | ||
CompileResult result = BCompileUtil.compile("test-src/annotations/external_dependency_annotation.bal"); | ||
pkgNode = (BLangPackage) result.getAST(); | ||
} | ||
|
||
@Test(description = "Test the ExternalDependency annotation attach points") | ||
public void testExternalDependencyAttachPoints() { | ||
Set<AttachPoint> attachPoints = pkgNode.functions.get(0).annAttachments.get(0).annotationSymbol.points; | ||
List<String> expectedAttachPoints = Arrays.asList("type", "class", "function"); | ||
|
||
assertEquals(attachPoints.size(), 3); | ||
assertTrue(attachPoints.stream() | ||
.allMatch(attachPoint -> expectedAttachPoints.contains(attachPoint.point.getValue()))); | ||
} | ||
|
||
@Test(description = "Test the ExternalDependency annotation used on functions") | ||
public void testExternalDependencyAnnotOnFunctions() { | ||
List<BLangFunction> functions = pkgNode.functions; | ||
assertEquals(functions.get(0).annAttachments.get(0).annotationName.value, "ExternalDependency"); | ||
} | ||
|
||
@Test(description = "Test the ExternalDependency annotation used on classes") | ||
public void testExternalDependencyAnnotOnClasses() { | ||
assertEquals(pkgNode.classDefinitions.get(0).annAttachments.get(0).annotationName.value, "ExternalDependency"); | ||
} | ||
|
||
@Test(description = "Test the ExternalDependency annotation used on type definitions") | ||
public void testExternalDependencyAnnotOnTypeDefinitions() { | ||
List<BLangTypeDefinition> typeDefinitions = pkgNode.typeDefinitions; | ||
assertEquals(typeDefinitions.get(0).annAttachments.get(0).annotationName.value, "ExternalDependency"); | ||
} | ||
|
||
@AfterClass | ||
public void tearDown() { | ||
pkgNode = null; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...rina-unit-test/src/test/resources/test-src/annotations/external_dependency_annotation.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). | ||
// | ||
// WSO2 LLC. licenses this file to you 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. | ||
|
||
import ballerina/jballerina.java; | ||
|
||
// Functions | ||
@java:ExternalDependency | ||
function foo(int x) returns int { | ||
return x * 2; | ||
} | ||
|
||
// Classes | ||
@java:ExternalDependency | ||
class BazClass { | ||
int x = 0; | ||
string y = ""; | ||
} | ||
|
||
// Type Definitions | ||
@java:ExternalDependency | ||
type FooRec record { | ||
int x; | ||
string y; | ||
}; |
24 changes: 24 additions & 0 deletions
24
...-test/src/test/resources/test-src/annotations/external_dependency_annotation_negative.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). | ||
// | ||
// WSO2 LLC. licenses this file to you 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. | ||
|
||
import ballerina/jballerina.java; | ||
|
||
// Types not allowed as sources for ExternalDependency annotation | ||
@java:ExternalDependency | ||
var varType = (); | ||
|
||
@java:ExternalDependency | ||
const string constType = "Im not an external dependency"; |