Skip to content

Commit 39c8bcc

Browse files
authored
don't double add classpath entries (#1578)
* don't double add classpath entries * code review * don't run unnecessary runContainerResolverJob * fix monitor size
1 parent 23a4a66 commit 39c8bcc

4 files changed

Lines changed: 47 additions & 14 deletions

File tree

plugins/com.google.cloud.tools.eclipse.appengine.libraries.test/src/com/google/cloud/tools/eclipse/appengine/libraries/BuildPathTest.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.eclipse.core.resources.IProject;
2323
import org.eclipse.core.runtime.CoreException;
24+
import org.eclipse.core.runtime.IProgressMonitor;
2425
import org.eclipse.core.runtime.NullProgressMonitor;
2526
import org.eclipse.jdt.core.IClasspathEntry;
2627
import org.eclipse.jdt.core.IJavaProject;
@@ -31,38 +32,53 @@
3132
import com.google.cloud.tools.eclipse.appengine.libraries.model.Library;
3233

3334
public class BuildPathTest {
35+
36+
private final List<Library> libraries = new ArrayList<>();
37+
private final IJavaProject project = Mockito.mock(IJavaProject.class);
3438

3539
@Test
3640
public void testAddLibraries_emptyList() throws CoreException {
3741
IProject project = null;
38-
List<Library> libraries = new ArrayList<>();
3942
BuildPath.addLibraries(project, libraries, new NullProgressMonitor());
4043
}
4144

4245
@Test
4346
public void testAddLibraries() throws CoreException {
44-
IJavaProject project = Mockito.mock(IJavaProject.class);
4547
IClasspathEntry[] rawClasspath = new IClasspathEntry[0];
4648
Mockito.when(project.getRawClasspath()).thenReturn(rawClasspath);
4749

48-
List<Library> libraries = new ArrayList<>();
4950
Library library = new Library("libraryId");
5051
libraries.add(library);
5152
IClasspathEntry[] result =
5253
BuildPath.addLibraries(project, libraries, new NullProgressMonitor());
5354
Assert.assertEquals(1, result.length);
55+
56+
Mockito.verify(project)
57+
.setRawClasspath(Mockito.any(IClasspathEntry[].class), Mockito.any(IProgressMonitor.class));
58+
}
59+
60+
@Test
61+
public void testListAdditionalLibraries() throws CoreException {
62+
IClasspathEntry[] rawClasspath = new IClasspathEntry[0];
63+
Mockito.when(project.getRawClasspath()).thenReturn(rawClasspath);
64+
65+
Library library = new Library("libraryId");
66+
libraries.add(library);
67+
IClasspathEntry[] result =
68+
BuildPath.listAdditionalLibraries(project, libraries, new NullProgressMonitor());
69+
Assert.assertEquals(1, result.length);
70+
71+
Mockito.verify(project, Mockito.never())
72+
.setRawClasspath(Mockito.any(IClasspathEntry[].class), Mockito.any(IProgressMonitor.class));
5473
}
5574

5675
@Test
5776
public void testAddLibraries_noDuplicates() throws CoreException {
5877
Library library = new Library("libraryId");
59-
IClasspathEntry entry = BuildPath.makeClasspathEntry(library);
60-
61-
IJavaProject project = Mockito.mock(IJavaProject.class);
78+
IClasspathEntry entry = BuildPath.makeClasspathEntry(library);
6279
IClasspathEntry[] rawClasspath = {entry};
6380
Mockito.when(project.getRawClasspath()).thenReturn(rawClasspath);
6481

65-
List<Library> libraries = new ArrayList<>();
6682
libraries.add(library);
6783
IClasspathEntry[] result =
6884
BuildPath.addLibraries(project, libraries, new NullProgressMonitor());
@@ -75,11 +91,9 @@ public void testAddLibraries_withDuplicates() throws CoreException {
7591
Library library2 = new Library("library2");
7692
IClasspathEntry entry = BuildPath.makeClasspathEntry(library1);
7793

78-
IJavaProject project = Mockito.mock(IJavaProject.class);
7994
IClasspathEntry[] rawClasspath = {entry};
8095
Mockito.when(project.getRawClasspath()).thenReturn(rawClasspath);
8196

82-
List<Library> libraries = new ArrayList<>();
8397
libraries.add(library1);
8498
libraries.add(library2);
8599
IClasspathEntry[] result =

plugins/com.google.cloud.tools.eclipse.appengine.libraries.ui/src/com/google/cloud/tools/eclipse/appengine/libraries/ui/AppEngineLibrariesPage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public IClasspathEntry[] getNewContainers() {
102102
return new IClasspathEntry[0];
103103
} else {
104104
IClasspathEntry[] added =
105-
BuildPath.addLibraries(project, libraries, new NullProgressMonitor());
105+
BuildPath.listAdditionalLibraries(project, libraries, new NullProgressMonitor());
106106
return added;
107107
}
108108
} catch (CoreException ex) {

plugins/com.google.cloud.tools.eclipse.appengine.libraries/src/com/google/cloud/tools/eclipse/appengine/libraries/BuildPath.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,18 @@ public static IClasspathEntry[] addLibraries(
8989
IJavaProject javaProject, List<Library> libraries, IProgressMonitor monitor)
9090
throws JavaModelException, CoreException {
9191

92+
return prepareLibraries(javaProject, libraries, monitor, true);
93+
}
94+
95+
private static IClasspathEntry[] prepareLibraries(IJavaProject javaProject,
96+
List<Library> libraries, IProgressMonitor monitor, boolean addToClasspath)
97+
throws JavaModelException, CoreException {
9298
SubMonitor subMonitor = SubMonitor.convert(monitor,
93-
Messages.getString("adding.app.engine.libraries"), libraries.size()); //$NON-NLS-1$
99+
Messages.getString("adding.app.engine.libraries"), //$NON-NLS-1$
100+
libraries.size() + 1); // + 1 because we pass the submonitor along below
94101

95102
IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
103+
96104
List<IClasspathEntry> newRawClasspath = new ArrayList<>(rawClasspath.length + libraries.size());
97105
newRawClasspath.addAll(Arrays.asList(rawClasspath));
98106
List<IClasspathEntry> newEntries = new ArrayList<>();
@@ -104,12 +112,23 @@ public static IClasspathEntry[] addLibraries(
104112
}
105113
subMonitor.worked(1);
106114
}
107-
javaProject.setRawClasspath(newRawClasspath.toArray(new IClasspathEntry[0]), subMonitor);
108115

109-
runContainerResolverJob(javaProject);
116+
if (addToClasspath) {
117+
javaProject.setRawClasspath(newRawClasspath.toArray(new IClasspathEntry[0]), subMonitor);
118+
runContainerResolverJob(javaProject);
119+
}
110120

111121
return newEntries.toArray(new IClasspathEntry[0]);
112122
}
123+
124+
/**
125+
* @return the entries to be added to the classpath. Does not add them to the classpath.
126+
*/
127+
public static IClasspathEntry[] listAdditionalLibraries(
128+
IJavaProject javaProject, List<Library> libraries, IProgressMonitor monitor)
129+
throws JavaModelException, CoreException {
130+
return prepareLibraries(javaProject, libraries, monitor, false);
131+
}
113132

114133
@VisibleForTesting
115134
static IClasspathEntry makeClasspathEntry(Library library) throws CoreException {

plugins/com.google.cloud.tools.eclipse.appengine.libraries/src/com/google/cloud/tools/eclipse/appengine/libraries/ILibraryClasspathContainerResolverService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ enum AppEngineRuntime {
5656

5757

5858
/**
59-
* Verifies that dependencies of a given runtime are available either locally or can be downloaded
59+
* Verifies that dependencies of a given runtime are available locally or can be downloaded.
6060
*
6161
* @return {@link Status#OK_STATUS} if all dependencies are available,
6262
* {@link Status#CANCEL_STATUS} if the operation was cancelled via the <code>monitor</code>, or a

0 commit comments

Comments
 (0)