Skip to content

Commit

Permalink
fix ome#2098 - silencing FileNotFoundException
Browse files Browse the repository at this point in the history
git-svn-id: file:///home/svn/omero/trunk@6557 05709c45-44f0-0310-885b-81a1db45b4a6
  • Loading branch information
joshmoore committed Apr 8, 2010
1 parent 74a363c commit b01cd70
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
5 changes: 5 additions & 0 deletions components/server/src/ome/services/fulltext/FileParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ final public Iterable<Reader> parse(File file) {
return EMPTY;
}

if (!file.exists() && !file.canRead()) {
log.debug("empty|unreadable file: " + file.getAbsoluteFile());
return EMPTY;
}

try {
Iterable<Reader> it = doParse(file);
if (it == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package ome.server.itests.search;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.sql.Timestamp;
Expand All @@ -19,9 +20,14 @@
import ome.model.annotations.FileAnnotation;
import ome.model.core.Image;
import ome.model.core.OriginalFile;
import ome.model.enums.Format;
import ome.services.fulltext.FileParser;
import ome.services.util.Executor;
import ome.system.ServiceFactory;
import ome.testing.FileUploader;

import org.hibernate.Session;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.testng.annotations.Test;

Expand All @@ -35,16 +41,29 @@ public void testStringFromParser() throws Exception {
"/dev/null");
upload.run();

String path = getFileService().getFilesPath(upload.getId());
File file = new File(path);
FileParser fp = new FileParser();
final String path = getFileService().getFilesPath(upload.getId());
final File file = new File(path);
final FileParser fp = new FileParser();
fp.setApplicationContext(this.applicationContext);
StringWriter sw = new StringWriter();
for (Reader test : fp.parse(file)) {
while (test.ready()) {
sw.write(test.read());
}
}
final StringWriter sw = new StringWriter();

// Has to be run in an executor in order to register the cleanup
// Though we could also pass in a mock.
executor.execute(loginAop.p, new Executor.SimpleWork(this, "parse") {
@Transactional(readOnly = true)
public Object doWork(Session session, ServiceFactory sf) {
for (Reader test : fp.parse(file)) {
try {
while (test.ready()) {
sw.write(test.read());
}
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
return null;
}});

assertEquals(str, sw.toString());
}

Expand Down Expand Up @@ -118,6 +137,32 @@ public void testPdfFile() throws Exception {
assertTrue(search.hasNext());
}

@Test(groups = "ticket:2098")
public void testParseMissingFile() {

String uuid = uuid();
OriginalFile f = new OriginalFile();
f.setName(uuid);
f.setSha1("");
f.setFormat(new Format("text/plain"));
f.setPath("/tmp/empty");
f.setSize(0L);

i = new_Image();
i.setName("annotated with file");
FileAnnotation fa = new FileAnnotation();
fa.setNs("");
fa.setFile(f);
i.linkAnnotation(fa);
i = iUpdate.saveAndReturnObject(i);
iUpdate.indexObject(i);

loginRootKeepGroup();
List<Image> imgs = iQuery.findAllByFullText(Image.class, uuid, null);
assertEquals(1, imgs.size());
assertTrue(imgs.get(0).getId().equals(i.getId()));
}

private Image new_Image() {
Image i = new Image();
i.setAcquisitionDate(new Timestamp(0));
Expand Down

0 comments on commit b01cd70

Please sign in to comment.