Skip to content

Commit

Permalink
Addresses failing tests on Windows (#160)
Browse files Browse the repository at this point in the history
* #159 Addressing failing tests on Windows.

* #159 Addressing failing tests on Windows.

* #159 Addressing failing tests on Windows.
  • Loading branch information
jzonthemtn authored Nov 16, 2024
1 parent be1bea5 commit f2d105e
Show file tree
Hide file tree
Showing 24 changed files with 33 additions and 4,144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ protected PostFilterResult process(final String text, Span span) {

if(span.getText().endsWith(System.lineSeparator())) {

// Modify the span to remove the period from the span.
span.setText(StringUtils.substring(span.getText(), 0, span.getText().length() - 1));
span.setCharacterEnd(span.getCharacterEnd() - 1);
// Modify the span to remove the new line from the span.
span.setText(StringUtils.chomp(span.getText()));
span.setCharacterEnd(span.getCharacterEnd() - System.lineSeparator().length());

}

while(span.getText().endsWith("\n")) {
while(span.getText().endsWith(System.lineSeparator())) {
span = process(text, span).span();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import ai.philterd.phileas.model.policy.filters.strategies.custom.CustomDictionaryFilterStrategy;
import ai.philterd.phileas.model.policy.filters.strategies.dynamic.*;
import ai.philterd.phileas.model.policy.filters.strategies.rules.*;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -217,7 +218,7 @@ public String getIndexDirectory(String indexName) {

LOGGER.warn("Environment variable PHILEAS_BASE_DIR is not set for Lucene index test.");

final String indexDir = System.getProperty("user.dir") + "/../data/indexes/" + indexName;
final String indexDir = FilenameUtils.separatorsToSystem(System.getProperty("user.dir") + "/../data/indexes/" + indexName);

LOGGER.info("Using index directory: {}", indexDir);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ public class CityFilterTest extends AbstractFilterTest {

private static final Logger LOGGER = LogManager.getLogger(CityFilterTest.class);

private String INDEX_DIRECTORY = getIndexDirectory("cities");
private final String INDEX_DIRECTORY = getIndexDirectory("cities");

private static final AnonymizationService anonymizationService = new CityAnonymizationService(new LocalAnonymizationCacheService());
private final AlertService alertService = Mockito.mock(AlertService.class);

@BeforeEach
public void before() {
INDEX_DIRECTORY = System.getProperty( "os.name" ).contains( "indow" ) ? INDEX_DIRECTORY.substring(1) : INDEX_DIRECTORY;
LOGGER.info("Using index directory {}", INDEX_DIRECTORY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ public class CountyFilterTest extends AbstractFilterTest {

private static final Logger LOGGER = LogManager.getLogger(CountyFilterTest.class);

private String INDEX_DIRECTORY = getIndexDirectory("counties");
private final String INDEX_DIRECTORY = getIndexDirectory("counties");

private final AlertService alertService = Mockito.mock(AlertService.class);

@BeforeEach
public void before() {
INDEX_DIRECTORY = System.getProperty( "os.name" ).contains( "indow" ) ? INDEX_DIRECTORY.substring(1) : INDEX_DIRECTORY;
LOGGER.info("Using index directory {}", INDEX_DIRECTORY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ public class FirstNameFilterTest extends AbstractFilterTest {

private static final Logger LOGGER = LogManager.getLogger(FirstNameFilterTest.class);

private String INDEX_DIRECTORY = getIndexDirectory("names");
private final String INDEX_DIRECTORY = getIndexDirectory("names");

private final AlertService alertService = Mockito.mock(AlertService.class);

@BeforeEach
public void before() {
INDEX_DIRECTORY = System.getProperty( "os.name" ).contains( "indow" ) ? INDEX_DIRECTORY.substring(1) : INDEX_DIRECTORY;
LOGGER.info("Using index directory {}", INDEX_DIRECTORY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,53 +24,25 @@
import ai.philterd.phileas.model.services.AlertService;
import ai.philterd.phileas.services.anonymization.HospitalAbbreviationAnonymizationService;
import ai.philterd.phileas.services.anonymization.cache.LocalAnonymizationCacheService;
import org.apache.commons.io.IOUtils;
import org.apache.commons.text.WordUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

public class HospitalAbbreviationFilterTest extends AbstractFilterTest {

private static final Logger LOGGER = LogManager.getLogger(HospitalAbbreviationFilterTest.class);

private String INDEX_DIRECTORY = getIndexDirectory("hospital-abbreviations");
private final String INDEX_DIRECTORY = getIndexDirectory("hospital-abbreviations");

private AlertService alertService = Mockito.mock(AlertService.class);

@Test
@Disabled
public void makeAbbreviations() throws Exception {

final String file = "/mtnfog/code/bitbucket/philter/philter/index-data/hospitals";
final InputStream is = new FileInputStream(file);
final List<String> lines = IOUtils.readLines(is, Charset.defaultCharset());

for(String line : lines) {

final String abbreviated = WordUtils.initials(line, null);

System.out.println(abbreviated);

}

is.close();

}
private final AlertService alertService = Mockito.mock(AlertService.class);

@BeforeEach
public void before() {
INDEX_DIRECTORY = System.getProperty( "os.name" ).contains( "indow" ) ? INDEX_DIRECTORY.substring(1) : INDEX_DIRECTORY;
LOGGER.info("Using index directory {}", INDEX_DIRECTORY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ public class HospitalFilterTest extends AbstractFilterTest {

private static final Logger LOGGER = LogManager.getLogger(HospitalFilterTest.class);

private String INDEX_DIRECTORY = getIndexDirectory("hospitals");
private final String INDEX_DIRECTORY = getIndexDirectory("hospitals");

private final AlertService alertService = Mockito.mock(AlertService.class);

@BeforeEach
public void before() {
INDEX_DIRECTORY = System.getProperty( "os.name" ).contains( "indow" ) ? INDEX_DIRECTORY.substring(1) : INDEX_DIRECTORY;
LOGGER.info("Using index directory {}", INDEX_DIRECTORY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ public class StateFilterTest extends AbstractFilterTest {

private static final Logger LOGGER = LogManager.getLogger(StateFilterTest.class);

private String INDEX_DIRECTORY = getIndexDirectory("states");
private final String INDEX_DIRECTORY = getIndexDirectory("states");

private final AlertService alertService = Mockito.mock(AlertService.class);

@BeforeEach
public void before() {
INDEX_DIRECTORY = System.getProperty( "os.name" ).contains( "indow" ) ? INDEX_DIRECTORY.substring(1) : INDEX_DIRECTORY;
LOGGER.info("Using index directory {}", INDEX_DIRECTORY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ public class SurnameFilterTest extends AbstractFilterTest {

private static final Logger LOGGER = LogManager.getLogger(SurnameFilterTest.class);

private String INDEX_DIRECTORY = getIndexDirectory("surnames");
private final String INDEX_DIRECTORY = getIndexDirectory("surnames");

private final AlertService alertService = Mockito.mock(AlertService.class);

@BeforeEach
public void before() {
INDEX_DIRECTORY = System.getProperty( "os.name" ).contains( "indow" ) ? INDEX_DIRECTORY.substring(1) : INDEX_DIRECTORY;
LOGGER.info("Using index directory {}", INDEX_DIRECTORY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public class TrailingNewLinePostFilterTest extends AbstractFilterTest {
public void test1() {

final List<Span> spans = new LinkedList<>();
spans.add(Span.make(12, 22, FilterType.URL, "context", "docid", 0.80, "ends with\n", "*****", "", false, true, new String[0]));
spans.add(Span.make(12, 22, FilterType.URL, "context", "docid", 0.80, "ends with" + System.lineSeparator(), "*****", "", false, true, new String[0]));

final TrailingNewLinePostFilter postFilter = TrailingNewLinePostFilter.getInstance();
final List<Span> filteredSpans = postFilter.filter("doesn't matter", spans);

showSpans(filteredSpans);
Assertions.assertEquals(1, filteredSpans.size());
Assertions.assertEquals("ends with", filteredSpans.get(0).getText());
Assertions.assertEquals(21, filteredSpans.get(0).getCharacterEnd());
Assertions.assertEquals(22 - System.lineSeparator().length(), filteredSpans.get(0).getCharacterEnd());

}

Expand All @@ -63,15 +63,15 @@ public void test2() {
public void test3() {

final List<Span> spans = new LinkedList<>();
spans.add(Span.make(12, 22, FilterType.URL, "context", "docid", 0.80, "ends with\n\n", "*****", "", false, true, new String[0]));
spans.add(Span.make(12, 22, FilterType.URL, "context", "docid", 0.80, "ends with" + System.lineSeparator() + System.lineSeparator(), "*****", "", false, true, new String[0]));

final TrailingNewLinePostFilter postFilter = TrailingNewLinePostFilter.getInstance();
final List<Span> filteredSpans = postFilter.filter("doesn't matter", spans);

showSpans(filteredSpans);
Assertions.assertEquals(1, filteredSpans.size());
Assertions.assertEquals("ends with", filteredSpans.get(0).getText());
Assertions.assertEquals(20, filteredSpans.get(0).getCharacterEnd());
Assertions.assertEquals(22 - System.lineSeparator().length() * 2, filteredSpans.get(0).getCharacterEnd());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,15 @@ public LuceneDictionaryFilter(FilterType filterType,

// Write the list of terms to a file in a temporary directory.
final Path pathToIndex = Files.createTempDirectory("philter-name-index");
final FileAttribute<Set<PosixFilePermission>> fileAttributes = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-------"));
final Path fileToIndex = Files.createTempFile(pathToIndex, "philter", type, fileAttributes);

final Path fileToIndex;
if(System.getProperty("os.name").startsWith("Windows")) {
fileToIndex = Files.createTempFile(pathToIndex, "philter", type);
} else {
final FileAttribute<Set<PosixFilePermission>> fileAttributes = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-------"));
fileToIndex = Files.createTempFile(pathToIndex, "philter", type, fileAttributes);
}

FileUtils.writeLines(fileToIndex.toFile(), terms);

// Make a temp directory to hold the new index.
Expand Down Expand Up @@ -327,7 +334,7 @@ public int getOccurrences(Policy policy, String input, Map<String, String> attri
public static void main(String[] args) throws IOException {

// The location of the file containing the lines to index.
final Path filetoIndex = Paths.get("/mtnfog/code/philter/phileas/data/index-data/surnames");
final Path filetoIndex = Paths.get("../data/index-data/surnames");

// The name of the file minus the extension is the type of index.
final String type = FilenameUtils.removeExtension(filetoIndex.toFile().getName());
Expand Down
30 changes: 0 additions & 30 deletions phileas-services-split/pom.xml

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit f2d105e

Please sign in to comment.