Skip to content

Commit 2630351

Browse files
authored
Return empty hierarchy if not Dataflow project (#2225)
1 parent 57b7036 commit 2630351

3 files changed

Lines changed: 83 additions & 9 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.tools.eclipse.dataflow.core.launcher;
18+
19+
import static org.hamcrest.CoreMatchers.instanceOf;
20+
import static org.junit.Assert.assertThat;
21+
import static org.mockito.Mockito.mock;
22+
import static org.mockito.Mockito.when;
23+
24+
import com.google.cloud.tools.eclipse.dataflow.core.launcher.options.PipelineOptionsHierarchy;
25+
import com.google.cloud.tools.eclipse.dataflow.core.launcher.options.PipelineOptionsNamespaces;
26+
import com.google.cloud.tools.eclipse.dataflow.core.project.MajorVersion;
27+
import org.eclipse.core.resources.IProject;
28+
import org.eclipse.core.runtime.NullProgressMonitor;
29+
import org.eclipse.jdt.core.IJavaElement;
30+
import org.eclipse.jdt.core.IJavaProject;
31+
import org.eclipse.jdt.core.IType;
32+
import org.eclipse.jdt.core.JavaModelException;
33+
import org.junit.Before;
34+
import org.junit.Test;
35+
import org.junit.runner.RunWith;
36+
import org.mockito.Mock;
37+
import org.mockito.runners.MockitoJUnitRunner;
38+
39+
@RunWith(MockitoJUnitRunner.class)
40+
public class ClasspathPipelineOptionsHierarchyFactoryTest {
41+
42+
@Mock private IProject project;
43+
@Mock private IJavaProject javaProject;
44+
45+
@Before
46+
public void setUp() {
47+
IJavaElement javaElement = mock(IJavaElement.class);
48+
when(project.getAdapter(IJavaElement.class)).thenReturn(javaElement);
49+
when(javaElement.getJavaProject()).thenReturn(javaProject);
50+
}
51+
52+
@Test
53+
public void testForProject_projectHasNoPipelineOptionsType()
54+
throws PipelineOptionsRetrievalException {
55+
PipelineOptionsHierarchy optionsHeierarchy = new ClasspathPipelineOptionsHierarchyFactory()
56+
.forProject(project, null, new NullProgressMonitor());
57+
assertThat(optionsHeierarchy, instanceOf(EmptyPipelineOptionsHierarchy.class));
58+
}
59+
60+
@Test
61+
public void testForProject_pipelineOptionsTypeDoesNotExistInProject()
62+
throws PipelineOptionsRetrievalException, JavaModelException {
63+
String version = PipelineOptionsNamespaces.rootType(MajorVersion.ONE);
64+
when(javaProject.findType(version)).thenReturn(mock(IType.class));
65+
66+
PipelineOptionsHierarchy optionsHeierarchy = new ClasspathPipelineOptionsHierarchyFactory()
67+
.forProject(project, MajorVersion.ONE, new NullProgressMonitor());
68+
assertThat(optionsHeierarchy, instanceOf(EmptyPipelineOptionsHierarchy.class));
69+
}
70+
}

plugins/com.google.cloud.tools.eclipse.dataflow.core/src/com/google/cloud/tools/eclipse/dataflow/core/launcher/ClasspathPipelineOptionsHierarchyFactory.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121
import com.google.cloud.tools.eclipse.dataflow.core.DataflowCorePlugin;
2222
import com.google.cloud.tools.eclipse.dataflow.core.launcher.options.JavaProjectPipelineOptionsHierarchy;
2323
import com.google.cloud.tools.eclipse.dataflow.core.launcher.options.PipelineOptionsHierarchy;
24+
import com.google.cloud.tools.eclipse.dataflow.core.launcher.options.PipelineOptionsNamespaces;
2425
import com.google.cloud.tools.eclipse.dataflow.core.project.MajorVersion;
2526
import org.eclipse.core.resources.IProject;
2627
import org.eclipse.core.runtime.IProgressMonitor;
2728
import org.eclipse.jdt.core.IJavaElement;
29+
import org.eclipse.jdt.core.IJavaProject;
30+
import org.eclipse.jdt.core.IType;
2831
import org.eclipse.jdt.core.JavaModelException;
2932

3033
/**
@@ -44,15 +47,19 @@ public PipelineOptionsHierarchy global(IProgressMonitor monitor) {
4447
public PipelineOptionsHierarchy forProject(
4548
IProject project, MajorVersion version, IProgressMonitor monitor)
4649
throws PipelineOptionsRetrievalException {
47-
IJavaElement javaProject = project.getAdapter(IJavaElement.class);
50+
IJavaElement javaElement = project.getAdapter(IJavaElement.class);
4851
checkNotNull(
49-
javaProject,
52+
javaElement,
5053
"%s cannot be created for a non-java project: %s",
5154
JavaProjectPipelineOptionsHierarchy.class.getSimpleName(),
5255
project);
5356
try {
54-
return new JavaProjectPipelineOptionsHierarchy(
55-
javaProject.getJavaProject(), version, monitor);
57+
IJavaProject javaProject = javaElement.getJavaProject();
58+
IType rootType = javaProject.findType(PipelineOptionsNamespaces.rootType(version));
59+
if (rootType == null || !rootType.exists()) {
60+
return global(monitor);
61+
}
62+
return new JavaProjectPipelineOptionsHierarchy(javaProject, version, monitor);
5663
} catch (JavaModelException e) {
5764
DataflowCorePlugin.logError(e,
5865
"Error while constructing Pipeline Options Hierarchy for project %s", project.getName());

plugins/com.google.cloud.tools.eclipse.dataflow.core/src/com/google/cloud/tools/eclipse/dataflow/core/launcher/options/JavaProjectPipelineOptionsHierarchy.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,8 @@ public JavaProjectPipelineOptionsHierarchy(
6868
IJavaProject project, MajorVersion version, IProgressMonitor monitor)
6969
throws JavaModelException {
7070
IType rootType = project.findType(PipelineOptionsNamespaces.rootType(version));
71-
if (rootType == null || !rootType.exists()) {
72-
throw new IllegalArgumentException(
73-
"Tried to create a TypeHierarchyPipelineOptionsHierarchy for a Java Project "
74-
+ "where no PipelineOptions type exists");
75-
}
71+
Preconditions.checkNotNull(rootType, "project has no PipelineOptions type");
72+
Preconditions.checkArgument(rootType.exists(), "PipelineOptions does not exist in project");
7673

7774
// Flatten the class hierarchy, recording all the classes present
7875
ITypeHierarchy hierarchy = rootType.newTypeHierarchy(monitor);

0 commit comments

Comments
 (0)