2929import org .eclipse .jdt .testplugin .JavaProjectHelper ;
3030import org .eclipse .jdt .testplugin .TestOptions ;
3131
32+ import org .eclipse .core .runtime .Path ;
33+
3234import org .eclipse .jface .preference .IPreferenceStore ;
3335
36+ import org .eclipse .jdt .core .IAccessRule ;
37+ import org .eclipse .jdt .core .IClasspathAttribute ;
38+ import org .eclipse .jdt .core .IClasspathEntry ;
3439import org .eclipse .jdt .core .ICompilationUnit ;
3540import org .eclipse .jdt .core .IJavaProject ;
3641import org .eclipse .jdt .core .IPackageFragment ;
@@ -57,6 +62,10 @@ public class UnresolvedVariablesQuickFixTest extends QuickFixTest {
5762 public ProjectTestSetup projectSetup = new ProjectTestSetup ();
5863
5964 private IJavaProject fJProject1 ;
65+ IJavaProject p1 = null ;
66+ IJavaProject referencing1 = null ;
67+ IJavaProject referencing2 = null ;
68+
6069 private IPackageFragmentRoot fSourceFolder ;
6170
6271 @ Before
@@ -1547,6 +1556,105 @@ private float foo() {
15471556 }
15481557 }
15491558
1559+ @ Test
1560+ public void testStaticImportFavorite_Issue2893 () throws Exception { //https://github.com/eclipse-jdt/eclipse.jdt.ui/issues/2893
1561+ IPreferenceStore preferenceStore = PreferenceConstants .getPreferenceStore ();
1562+ preferenceStore .setValue (PreferenceConstants .CODEASSIST_FAVORITE_STATIC_MEMBERS , "test.Assertions.*" );
1563+ try {
1564+ p1 = JavaProjectHelper .createJavaProject ("p1" , "bin" );
1565+ referencing1 = JavaProjectHelper .createJavaProject ("p2" , "bin" );
1566+ referencing2 = JavaProjectHelper .createJavaProject ("p3" , "bin" );
1567+
1568+ JavaProjectHelper .addRTJar (p1 );
1569+ IPackageFragmentRoot p1SourceFolder = JavaProjectHelper .addSourceContainer (p1 , "src" );
1570+ IPackageFragment pack = p1SourceFolder .createPackageFragment ("test" , false , null );
1571+
1572+ String str = """
1573+ package test;
1574+
1575+ public class Assertions {
1576+ public static boolean assertEquals(Object obj1, Object obj2) throws Exception {
1577+ if (Object.equals(obj1, obj2)) {
1578+ throw new Exception();
1579+ }
1580+ }
1581+ }
1582+ """ ;
1583+ pack .createCompilationUnit ("Assertions.java" , str , false , null );
1584+
1585+ JavaProjectHelper .addRTJar (referencing1 );
1586+ JavaProjectHelper .addRequiredProject (referencing1 , p1 );
1587+ IPackageFragmentRoot ref1SourceFolder = JavaProjectHelper .addSourceContainer (referencing1 , "src" );
1588+ IPackageFragment pack1 = ref1SourceFolder .createPackageFragment ("test1" , false , null );
1589+ String str1 = """
1590+ package test1;
1591+
1592+ public class E {
1593+ public boolean foo(Object obj1, Object obj2) throws Exception {
1594+ assertEquals(obj1, obj2);
1595+ }
1596+ }
1597+ """ ;
1598+ ICompilationUnit cu1 = pack1 .createCompilationUnit ("E.java" , str1 , false , null );
1599+
1600+ JavaProjectHelper .addRTJar (referencing2 );
1601+ IPackageFragmentRoot ref2SourceFolder = JavaProjectHelper .addSourceContainer (referencing2 , "src" );
1602+ IPackageFragment pack2 = ref2SourceFolder .createPackageFragment ("test2" , false , null );
1603+ String str2 = """
1604+ package test2;
1605+
1606+ public class E2 {
1607+ public boolean foo2(Object obj1, Object obj2) throws Exception {
1608+ assertEquals(obj1, obj2);
1609+ }
1610+ }
1611+ """ ;
1612+ ICompilationUnit cu2 = pack2 .createCompilationUnit ("E2.java" , str2 , false , null );
1613+
1614+
1615+
1616+ IAccessRule [] accessRules2 = new IAccessRule [] {
1617+ JavaCore .newAccessRule (new Path ("**/*" ), IAccessRule .K_NON_ACCESSIBLE )
1618+ };
1619+ IClasspathAttribute [] extraAttributes2 = new IClasspathAttribute [] {
1620+ JavaCore .newClasspathAttribute ("myTestAttribute" , "val" )
1621+ };
1622+ IClasspathEntry cpe2 = JavaCore .newProjectEntry (p1 .getProject ().getFullPath (), accessRules2 , true , extraAttributes2 , false );
1623+ JavaProjectHelper .addToClasspath (referencing2 , cpe2 );
1624+
1625+ CompilationUnit astRoot2 = getASTRoot (cu2 );
1626+ ArrayList <IJavaCompletionProposal > proposals = collectCorrections (cu2 , astRoot2 , 1 );
1627+ assertProposalDoesNotExist (proposals , "Add static import for 'Assertions.assertEquals'" );
1628+ CompilationUnit astRoot1 = getASTRoot (cu1 );
1629+ ArrayList <IJavaCompletionProposal > proposals1 = collectCorrections (cu1 , astRoot1 , 1 );
1630+ String expected = """
1631+ package test1;
1632+
1633+ import static test.Assertions.assertEquals;
1634+
1635+ public class E {
1636+ public boolean foo(Object obj1, Object obj2) throws Exception {
1637+ assertEquals(obj1, obj2);
1638+ }
1639+ }
1640+ """ ;
1641+ assertExpectedExistInProposals (proposals1 , new String [] { expected });
1642+ } finally {
1643+ preferenceStore .setValue (PreferenceConstants .CODEASSIST_FAVORITE_STATIC_MEMBERS , "" );
1644+ if (referencing1 != null && referencing1 .exists ())
1645+ JavaProjectHelper .removeSourceContainer (referencing1 , "src" );
1646+ if (referencing2 != null && referencing2 .exists ())
1647+ JavaProjectHelper .removeSourceContainer (referencing2 , "src" );
1648+
1649+ if (p1 != null && p1 .exists ())
1650+ JavaProjectHelper .delete (p1 );
1651+ if (referencing1 != null && referencing1 .exists ())
1652+ JavaProjectHelper .delete (referencing1 );
1653+ if (referencing2 != null && referencing2 .exists ())
1654+ JavaProjectHelper .delete (referencing2 );
1655+ }
1656+ }
1657+
15501658 @ Test
15511659 public void testLongVarRef () throws Exception {
15521660 IPackageFragment pack1 = fSourceFolder .createPackageFragment ("test1" , false , null );
0 commit comments