Skip to content

Commit

Permalink
don't merge empty files (#3120)
Browse files Browse the repository at this point in the history
* check for empty datastore indexes

* reverse check

* remove extra end tag

* code rview
  • Loading branch information
elharo authored May 30, 2018
1 parent 76131b5 commit cd19cc1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
Expand Down Expand Up @@ -70,7 +74,16 @@ public void setUp() throws IOException {
}

@Test
public void testNoUpdate() {
public void testNoUpdateWithNoFile() {
DatastoreIndexUpdateData update =
DatastoreIndexUpdateData.detect(launchConfiguration, server, defaultService);
assertNull(update);
}

@Test
public void testNoUpdateWithEmptyFile() throws IOException {
createEmptyDatastoreIndexesAutoXml();

DatastoreIndexUpdateData update =
DatastoreIndexUpdateData.detect(launchConfiguration, server, defaultService);
assertNull(update);
Expand Down Expand Up @@ -143,10 +156,32 @@ public void testExistingDatastoreIndexesXml_noUpdate() throws IOException {
assertNull(update);
}

private void createDatastoreIndexesAutoXml() throws IOException {
private void createDatastoreIndexesAutoXml() throws IOException {
try (Writer out = openDatastoreIndexesAutoFile()) {
out.write("<datastore-indexes>");
out.write("<datastore-index kind='Employee' ancestor='false'>");
out.write("<property name='lastName' direction='asc' />");
out.write("</datastore-index>");
out.write("</datastore-indexes>");
out.flush();
}
}

private Writer openDatastoreIndexesAutoFile() throws IOException {
File appengineGenerated = new File(webInf, "appengine-generated");
File datastoreIndexesAutoXml = new File(appengineGenerated, "datastore-indexes-auto.xml");
assertTrue(appengineGenerated.mkdirs());
assertTrue(datastoreIndexesAutoXml.createNewFile());

return new OutputStreamWriter(
Files.newOutputStream(datastoreIndexesAutoXml.toPath()),
StandardCharsets.UTF_8);
}

private void createEmptyDatastoreIndexesAutoXml() throws IOException {
try (Writer out = openDatastoreIndexesAutoFile()) {
out.write("<datastore-indexes/>");
out.flush();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@

import com.google.cloud.tools.eclipse.appengine.facets.WebProjectUtil;
import com.google.common.annotations.VisibleForTesting;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.wst.server.core.IModule;
import org.eclipse.wst.server.core.IServer;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/** A simple value class to capture that updates to a module's {@code datastore-indexes.xml}. */
/** A simple value class to capture updates to a module's {@code datastore-indexes.xml}. */
public class DatastoreIndexUpdateData {
private static final Logger logger = Logger.getLogger(DatastoreIndexUpdateData.class.getName());

Expand All @@ -49,13 +55,14 @@ static DatastoreIndexUpdateData detect(ILaunchConfiguration configuration, IServ
LocalAppEngineServerBehaviour serverBehaviour = (LocalAppEngineServerBehaviour) server
.loadAdapter(LocalAppEngineServerBehaviour.class, null);
IPath deployPath = serverBehaviour.getModuleDeployDirectory(defaultService);
IPath datastoreIndexesAutoXml = deployPath.append("WEB-INF/appengine-generated/datastore-indexes-auto.xml");
if(!datastoreIndexesAutoXml.toFile().exists()) {
IPath datastoreIndexesAutoXml =
deployPath.append("WEB-INF/appengine-generated/datastore-indexes-auto.xml");
if (!indexGenerated(datastoreIndexesAutoXml)) {
return null;
}
// the datastore-indexes-auto.xml may be generated even if the datastore-indexes.xml does not exist
IFile datastoreIndexesXml =
WebProjectUtil.findInWebInf(defaultService.getProject(), new org.eclipse.core.runtime.Path("datastore-indexes.xml"));
// datastore-indexes-auto.xml may be generated even if datastore-indexes.xml does not exist
IFile datastoreIndexesXml = WebProjectUtil.findInWebInf(defaultService.getProject(),
new org.eclipse.core.runtime.Path("datastore-indexes.xml"));

if (datastoreIndexesXml != null && datastoreIndexesXml.exists()) {
long sourceTimestamp = datastoreIndexesXml.getLocalTimeStamp();
Expand All @@ -70,6 +77,23 @@ static DatastoreIndexUpdateData detect(ILaunchConfiguration configuration, IServ
datastoreIndexesAutoXml);
}

private static boolean indexGenerated(IPath datastoreIndexesAutoXml) {
if (!datastoreIndexesAutoXml.toFile().exists()) {
return false;
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(datastoreIndexesAutoXml.toFile());
return doc.getElementsByTagName("datastore-index").getLength() > 0;
} catch (ParserConfigurationException ex) {
return true;
} catch (SAXException | IOException ex) {
return false;
}
}

public final IServer server;
public final ILaunchConfiguration configuration;

Expand Down

0 comments on commit cd19cc1

Please sign in to comment.