Skip to content

Commit

Permalink
Move ConfigurationFileUtils to .test.util (#3185)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanseokoh authored Jul 2, 2018
1 parent f909706 commit 27a8334
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.google.cloud.tools.eclipse.appengine.facets.ui.navigator.model.DenialOfServiceDescriptor;
import com.google.cloud.tools.eclipse.appengine.facets.ui.navigator.model.DispatchRoutingDescriptor;
import com.google.cloud.tools.eclipse.appengine.facets.ui.navigator.model.TaskQueuesDescriptor;
import com.google.cloud.tools.eclipse.test.util.project.ConfigurationFileUtils;
import com.google.cloud.tools.eclipse.test.util.project.TestProjectCreator;
import com.google.common.io.ByteSource;
import java.io.ByteArrayInputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import com.google.cloud.tools.appengine.api.AppEngineException;
import com.google.cloud.tools.eclipse.appengine.facets.AppEngineStandardFacet;
import com.google.cloud.tools.eclipse.appengine.facets.ui.navigator.ConfigurationFileUtils;
import com.google.cloud.tools.eclipse.test.util.project.ConfigurationFileUtils;
import com.google.cloud.tools.eclipse.test.util.project.ProjectUtils;
import com.google.cloud.tools.eclipse.test.util.project.TestProjectCreator;
import com.google.cloud.tools.eclipse.util.io.ResourceUtils;
Expand Down Expand Up @@ -78,8 +78,7 @@ public void testAppEngineProjectElementCreate_initial() throws AppEngineExceptio
IFile dosXml = ConfigurationFileUtils.createEmptyDosXml(project);
IFile queueXml = ConfigurationFileUtils.createEmptyQueueXml(project);

AppEngineProjectElement projectElement =
AppEngineProjectElement.create(project);
AppEngineProjectElement projectElement = AppEngineProjectElement.create(project);
AppEngineResourceElement[] subElements = projectElement.getConfigurations();
assertNotNull(subElements);
assertEquals(5, subElements.length);
Expand All @@ -105,8 +104,7 @@ public void testAppEngineProjectElementCreate_initial() throws AppEngineExceptio
@Test
public void testAppEngineProjectElementCreate_staggered() throws AppEngineException {
IProject project = projectCreator.getProject();
AppEngineProjectElement projectElement =
AppEngineProjectElement.create(project);
AppEngineProjectElement projectElement = AppEngineProjectElement.create(project);
AppEngineResourceElement[] subElements = projectElement.getConfigurations();
assertNotNull(subElements);
assertEquals(0, subElements.length);
Expand All @@ -119,8 +117,7 @@ public void testAppEngineProjectElementCreate_staggered() throws AppEngineExcept
CronDescriptor cron = findInstance(subElements, CronDescriptor.class);
assertThat(subElements, hasItemInArray(cron));

IFile datastoreIndexesXml =
ConfigurationFileUtils.createEmptyDatastoreIndexesXml(project);
IFile datastoreIndexesXml = ConfigurationFileUtils.createEmptyDatastoreIndexesXml(project);
projectElement.resourcesChanged(Collections.singleton(datastoreIndexesXml));
subElements = projectElement.getConfigurations();
assertNotNull(subElements);
Expand Down Expand Up @@ -181,9 +178,8 @@ public void testChangeToDefaultPreservesConfigurationElements() throws AppEngine
ConfigurationFileUtils.createEmptyDispatchXml(project);
ConfigurationFileUtils.createEmptyDosXml(project);
ConfigurationFileUtils.createEmptyQueueXml(project);
AppEngineProjectElement projectElement =
AppEngineProjectElement.create(project);
final AppEngineResourceElement[] subElements = projectElement.getConfigurations();
AppEngineProjectElement projectElement = AppEngineProjectElement.create(project);
AppEngineResourceElement[] subElements = projectElement.getConfigurations();
assertEquals(5, subElements.length);
assertThat(subElements, hasItemInArray(instanceOf(CronDescriptor.class)));
assertThat(subElements, hasItemInArray(instanceOf(DatastoreIndexesDescriptor.class)));
Expand Down Expand Up @@ -213,9 +209,8 @@ public void testChangeToNonDefaultDiscardsConfigurationElements() throws AppEngi
ConfigurationFileUtils.createEmptyDispatchXml(project);
ConfigurationFileUtils.createEmptyDosXml(project);
ConfigurationFileUtils.createEmptyQueueXml(project);
AppEngineProjectElement projectElement =
AppEngineProjectElement.create(project);
final AppEngineResourceElement[] subElements = projectElement.getConfigurations();
AppEngineProjectElement projectElement = AppEngineProjectElement.create(project);
AppEngineResourceElement[] subElements = projectElement.getConfigurations();
assertEquals(5, subElements.length);
assertThat(subElements, hasItemInArray(instanceOf(CronDescriptor.class)));
assertThat(subElements, hasItemInArray(instanceOf(DatastoreIndexesDescriptor.class)));
Expand Down Expand Up @@ -245,10 +240,9 @@ public void testChildElementPreservedOnChange() throws AppEngineException {
files.add(ConfigurationFileUtils.createEmptyDosXml(project));
files.add(ConfigurationFileUtils.createEmptyQueueXml(project));

AppEngineProjectElement projectElement =
AppEngineProjectElement.create(project);
AppEngineProjectElement projectElement = AppEngineProjectElement.create(project);
files.add(projectElement.getDescriptorFile());
final AppEngineResourceElement[] subElements = projectElement.getConfigurations();
AppEngineResourceElement[] subElements = projectElement.getConfigurations();

for (IFile file : files) {
boolean changed = projectElement.resourcesChanged(Collections.singleton(file));
Expand All @@ -268,8 +262,7 @@ public void testNonDefaultServiceIgnoresNewFiles() throws AppEngineException {
IProject project = projectCreator.getProject();
ConfigurationFileUtils.createAppEngineWebXml(project, "non-default");

AppEngineProjectElement projectElement =
AppEngineProjectElement.create(project);
AppEngineProjectElement projectElement = AppEngineProjectElement.create(project);
AppEngineResourceElement[] subElements = projectElement.getConfigurations();
assertEquals(0, subElements.length);

Expand All @@ -289,26 +282,25 @@ public void testRejigDeploymentAssembly() throws AppEngineException, CoreExcepti
IProject project = projectCreator.getProject();
// verify the new files are not picked up yet
IFile oldCronXml = ConfigurationFileUtils.createEmptyCronXml(project);
AppEngineProjectElement projectElement =
AppEngineProjectElement.create(project);
final AppEngineResourceElement[] oldElements = projectElement.getConfigurations();
AppEngineProjectElement projectElement = AppEngineProjectElement.create(project);
AppEngineResourceElement[] oldElements = projectElement.getConfigurations();
assertEquals(1, oldElements.length);
assertThat(oldElements, hasItemInArray(instanceOf(CronDescriptor.class)));

// create the new WEB-INF location and populate it
final IFolder newWebRoot = project.getFolder("newWebRoot");
final IFolder newWebInf = newWebRoot.getFolder("WEB-INF");
IFolder newWebRoot = project.getFolder("newWebRoot");
IFolder newWebInf = newWebRoot.getFolder("WEB-INF");
ResourceUtils.createFolders(newWebInf, null);
final IFile newDispatchXml = newWebInf.getFile("dispatch.xml");
IFile newDispatchXml = newWebInf.getFile("dispatch.xml");
newDispatchXml.create(
new ByteArrayInputStream("<dispatch-entries/>".getBytes(StandardCharsets.UTF_8)),
true,
null);
assertTrue("error creating new dispatch.xml", newDispatchXml.exists());

// now link in the new WEB-INF into the overlay
final IWorkspace workspace = project.getWorkspace();
final Set<IFile> changed =
IWorkspace workspace = project.getWorkspace();
Set<IFile> changed =
recordChangedFilesDuring(
workspace,
monitor -> {
Expand All @@ -325,7 +317,7 @@ public void testRejigDeploymentAssembly() throws AppEngineException, CoreExcepti
Iterables.getOnlyElement(changed));
assertTrue(projectElement.resourcesChanged(changed));

final AppEngineResourceElement[] newElements = projectElement.getConfigurations();
AppEngineResourceElement[] newElements = projectElement.getConfigurations();
assertEquals(2, newElements.length);
assertThat(newElements, hasItemInArray(instanceOf(CronDescriptor.class)));
assertThat(newElements, hasItemInArray(instanceOf(DispatchRoutingDescriptor.class)));
Expand All @@ -339,8 +331,8 @@ public void testRejigDeploymentAssembly() throws AppEngineException, CoreExcepti
/** Record and return the set of files altered when running the provided block. */
private static Set<IFile> recordChangedFilesDuring(IWorkspace workspace, ICoreRunnable block)
throws CoreException {
final Set<IFile> changed = new LinkedHashSet<>();
final IResourceChangeListener listener =
Set<IFile> changed = new LinkedHashSet<>();
IResourceChangeListener listener =
event -> {
try {
changed.addAll(ResourceUtils.getAffectedFiles(event.getDelta()).values());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static boolean hasAppEngineDescriptor(Collection<IFile> changedFiles) {

private AppEngineProjectElement(IProject project) throws AppEngineException {
this.project = project;
this.descriptorFile = findAppEngineDescriptor(project);
descriptorFile = findAppEngineDescriptor(project);
}

/** Return the project. */
Expand Down Expand Up @@ -211,15 +211,17 @@ public String getServiceId() {
}

/** Return the App Engine environment type: {@code standard} or {@code flex}. */
public String getEnvironmentType() {
@VisibleForTesting
String getEnvironmentType() {
return environmentType;
}

/**
* Return the App Engine runtime type, which may depend on the {@link #getEnvironmentType()
* environment type}.
*/
public String getRuntime() {
@VisibleForTesting
String getRuntime() {
return runtime;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public static IProject importMavenProject(
}

/**
* Spin until the given project actually exists and is facetd.
* Spin until the given project actually exists and is faceted.
*
* @return the project
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ public void testInvalidPath() {
assertTrue(area.getStatus().isOK());
}

// TODO(chanseok): can become "@Before setUp()" once we remove the managed SDK debug feature flag.
private void createPreferenceArea() {
shell = shellResource.getShell();
area = new CloudSdkPreferenceArea(cloudSdkManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.google.cloud.tools.eclipse.appengine.facets.ui.navigator;
package com.google.cloud.tools.eclipse.test.util.project;

import com.google.cloud.tools.eclipse.appengine.facets.WebProjectUtil;
import com.google.common.base.Strings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
import com.google.cloud.tools.eclipse.util.ClasspathUtil;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -170,7 +167,7 @@ private void addContainerPathToRawClasspath() throws JavaModelException {

private void addFacets() throws CoreException {
if (makeFaceted) {
facetedProject = ProjectFacetsManager.create(getProject(), true, null);
facetedProject = ProjectFacetsManager.create(project, true, null);
}
if (projectFacetVersions.isEmpty()) {
return;
Expand All @@ -184,7 +181,7 @@ private void addFacets() throws CoreException {

if (facetedProject.hasProjectFacet(AppEngineStandardFacet.FACET)) {
// App Engine runtime is added via a Job, so wait.
ProjectUtils.waitForProjects(getProject());
ProjectUtils.waitForProjects(project);
}

if (facetedProject.hasProjectFacet(JavaFacet.FACET)) {
Expand All @@ -193,15 +190,12 @@ private void addFacets() throws CoreException {
}
}

private void setAppEngineServiceId(String serviceId) throws CoreException {
private void setAppEngineServiceId(String serviceId) {
IFile appEngineWebXml =
AppEngineConfigurationUtil.findConfigurationFile(
getProject(), new Path("appengine-web.xml"));
assertTrue("Project should have AppEngine Standard facet", appEngineWebXml.exists());
String contents = "<appengine-web-app xmlns='http://appengine.google.com/ns/1.0'>\n"
+ "<service>" + serviceId + "</service>\n</appengine-web-app>\n";
InputStream in = new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8));
appEngineWebXml.setContents(in, IFile.FORCE, null);
ConfigurationFileUtils.createAppEngineWebXml(project, serviceId);
}

}

0 comments on commit 27a8334

Please sign in to comment.