Search in sources :

Example 1 with ContentMapping

use of com.cloudbees.jenkins.support.filter.ContentMapping in project support-core-plugin by jenkinsci.

the class RunningBuildsTest method addContentsFiltered.

@Test
public void addContentsFiltered() throws Exception {
    ContentFilters.get().setEnabled(true);
    ContentMapping mapping = ContentMapping.of(SENSITIVE_WORD, FILTERED_SENSITIVE_WORD);
    ContentMappings.get().getMappingOrCreate(mapping.getOriginal(), original -> mapping);
    ContentFilter filter = SupportPlugin.getContentFilter().orElseThrow(AssertionFailedError::new);
    FreeStyleProject p = j.createFreeStyleProject(SENSITIVE_JOB_NAME);
    SemaphoreBuilder semaphore = new SemaphoreBuilder();
    p.getBuildersList().add(semaphore);
    FreeStyleBuild build = p.scheduleBuild2(0).waitForStart();
    String output = SupportTestUtils.invokeComponentToString(new RunningBuilds(), filter);
    semaphore.release();
    j.waitForCompletion(build);
    assertThat(output, not(containsString(String.format(EXPECTED_OUTPUT_FORMAT, p.getName(), build.getNumber()))));
    assertThat(output, containsString(String.format(EXPECTED_OUTPUT_FORMAT, FILTERED_JOB_NAME, build.getNumber())));
}
Also used : ContentMapping(com.cloudbees.jenkins.support.filter.ContentMapping) FreeStyleBuild(hudson.model.FreeStyleBuild) Matchers.containsString(org.hamcrest.Matchers.containsString) AssertionFailedError(junit.framework.AssertionFailedError) FreeStyleProject(hudson.model.FreeStyleProject) ContentFilter(com.cloudbees.jenkins.support.filter.ContentFilter) Test(org.junit.Test)

Example 2 with ContentMapping

use of com.cloudbees.jenkins.support.filter.ContentMapping in project support-core-plugin by jenkinsci.

the class WordReplacerTest method performanceTest.

@Ignore("It was useful to make the decision to move out of Reg Exp. As the implementation of the Content Mapping is" + "now WordReplacer, it has no sense to test. We keep it here for future cases.")
@Test
public void performanceTest() {
    // Create a lot of word and replaces (each character letter or digit. Aprox: 4070)
    List<String> words = new ArrayList<>();
    List<String> replaceList = new ArrayList<>();
    for (char c = 0; c < Character.MAX_VALUE; c++) {
        if (Character.isLetterOrDigit(c)) {
            words.add(String.valueOf(c));
            replaceList.add("**" + c + "**");
        }
    }
    String[] searches = words.toArray(new String[0]);
    String[] replaces = replaceList.toArray(new String[0]);
    // Generate a fake long text (well, not so long to avoid lasts too much)
    List<String> text = generateFakeListString(10);
    // Result with 100 lines in i7
    // Point 'RegExp': 31.16 seconds since last point
    // Point 'WordReplacer': 731.0 milliseconds since last point
    // Create the content mappings
    ContentMapping[] contentMappings = getContentMappings(searches, replaces);
    // Create the searches and replaces for the WordReplacer mimicking the behavior of the Content Mapping
    String[][][] tokens = getWordsLikeContentMapping(searches, replaces);
    Chrono c = new Chrono("Test ContentMapping Vs WordReplacer");
    // Filter using ContentMappings
    for (String line : text) {
        String resultCM = line;
        for (ContentMapping cm : contentMappings) {
            resultCM = cm.filter(resultCM);
        }
    }
    c.markFromPrevious("ContentMapping#filter");
    // Filter using WordReplacer in the same way as ContentMapping
    for (String line : text) {
        String resultWR = line;
        for (String[][] token : tokens) {
            resultWR = WordReplacer.replaceWordsIgnoreCase(resultWR, token[0], token[1]);
        }
    }
    c.markFromPrevious("WordReplacer");
    System.out.println(c.printMeasure("ContentMapping#filter"));
    System.out.println(c.printMeasure("WordReplacer"));
    assertTrue(c.getMeasure("ContentMapping#filter") > c.getMeasure("WordReplacer"));
}
Also used : ArrayList(java.util.ArrayList) ContentMapping(com.cloudbees.jenkins.support.filter.ContentMapping) Ignore(org.junit.Ignore) Test(org.junit.Test) FilteredOutputStreamTest(com.cloudbees.jenkins.support.filter.FilteredOutputStreamTest)

Example 3 with ContentMapping

use of com.cloudbees.jenkins.support.filter.ContentMapping in project support-core-plugin by jenkinsci.

the class WordReplacerTest method sameResultTest.

@Test
public void sameResultTest() {
    String[] searches = new String[] { "one", "two" };
    String[] replaces = new String[] { "111", "111" };
    // Populate the content mapping objects to filter
    ContentMapping[] contentMappings = getContentMappings(searches, replaces);
    // Texts to test against
    // regexp doesn't replace ;word, we do. It's even better but we can't check it
    String[] inputs = new String[] { "", "a", "none", "one should be replaced", "onecar shouldn't be replaced", "two is replaced", "must replace two", "shouldn't replace twoo", "one!one,one!onetwoone_twoone#one?one:one->one\"one", "'one|twoone, \"one\\two one\ntwo one\ttwo one=two (one+two*one) all replaced ", "one\\two one\ntwo one\ttwo one=two (one+two*one) all replaced " };
    // For each text, filter using ContentMapping and with WordReplacer as well
    for (String input : inputs) {
        String resultCM = input;
        for (ContentMapping cm : contentMappings) {
            resultCM = cm.filter(resultCM);
        }
        String resultWordReplacer = WordReplacer.replaceWordsIgnoreCase(input, searches, replaces);
        // Both filtered strings should be the same
        assertEquals("The string replaced should be the same using RegExp or WordReplacer", resultCM, resultWordReplacer);
    }
}
Also used : ContentMapping(com.cloudbees.jenkins.support.filter.ContentMapping) Test(org.junit.Test) FilteredOutputStreamTest(com.cloudbees.jenkins.support.filter.FilteredOutputStreamTest)

Example 4 with ContentMapping

use of com.cloudbees.jenkins.support.filter.ContentMapping in project support-core-plugin by jenkinsci.

the class FileDescriptorLimitTest method addContentsFiltered.

@Test
public void addContentsFiltered() throws Exception {
    Assume.assumeTrue(!Functions.isWindows());
    Assume.assumeTrue(SystemPlatform.LINUX == SystemPlatform.current());
    ContentFilters.get().setEnabled(true);
    ContentMapping mapping = ContentMapping.of(SENSITIVE_WORD, FILTERED_SENSITIVE_WORD);
    ContentMappings.get().getMappingOrCreate(mapping.getOriginal(), original -> mapping);
    ContentFilter filter = SupportPlugin.getContentFilter().orElseThrow(AssertionFailedError::new);
    FreeStyleProject p = j.createFreeStyleProject(SENSITIVE_JOB_NAME);
    String output;
    // Hold an open File Descriptor
    try (FileInputStream ignored = new FileInputStream(p.getConfigFile().getFile())) {
        output = SupportTestUtils.invokeComponentToString(new FileDescriptorLimit(), filter);
    }
    MatcherAssert.assertThat(output, containsString("core file size"));
    MatcherAssert.assertThat(output, containsString("Open File Descriptor Count:"));
    MatcherAssert.assertThat(output, containsString("All open files\n=============="));
    MatcherAssert.assertThat(output, not(containsString(SENSITIVE_JOB_NAME)));
    MatcherAssert.assertThat(output, containsString(FILTERED_JOB_NAME));
}
Also used : ContentMapping(com.cloudbees.jenkins.support.filter.ContentMapping) Matchers.containsString(org.hamcrest.Matchers.containsString) AssertionFailedError(junit.framework.AssertionFailedError) FreeStyleProject(hudson.model.FreeStyleProject) ContentFilter(com.cloudbees.jenkins.support.filter.ContentFilter) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

ContentMapping (com.cloudbees.jenkins.support.filter.ContentMapping)4 Test (org.junit.Test)4 ContentFilter (com.cloudbees.jenkins.support.filter.ContentFilter)2 FilteredOutputStreamTest (com.cloudbees.jenkins.support.filter.FilteredOutputStreamTest)2 FreeStyleProject (hudson.model.FreeStyleProject)2 AssertionFailedError (junit.framework.AssertionFailedError)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 FreeStyleBuild (hudson.model.FreeStyleBuild)1 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 Ignore (org.junit.Ignore)1