Search in sources :

Example 26 with DummyHttpServletRequest

use of org.opengrok.indexer.web.DummyHttpServletRequest in project OpenGrok by OpenGrok.

the class ConfigurationControllerTest method testConfigValueSetVsThread.

@Test
void testConfigValueSetVsThread() throws InterruptedException {
    int origValue = env.getHitsPerPage();
    final int[] threadValue = new int[1];
    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch endLatch = new CountDownLatch(1);
    Thread thread = new Thread(() -> {
        HttpServletRequest req = new DummyHttpServletRequest();
        PageConfig pageConfig = PageConfig.get(req);
        RuntimeEnvironment e = pageConfig.getEnv();
        startLatch.countDown();
        // Wait for hint of termination, save the value and exit.
        try {
            endLatch.await();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        threadValue[0] = e.getHitsPerPage();
    });
    thread.start();
    startLatch.await();
    // Set brand-new configuration.
    int newValue = origValue + 42;
    Configuration config = new Configuration();
    config.setHitsPerPage(newValue);
    String configStr = config.getXMLRepresentationAsString();
    Response response = target("configuration").request().put(Entity.xml(configStr));
    waitForTask(response);
    // Unblock the thread.
    endLatch.countDown();
    thread.join();
    // Check thread's view of the variable.
    assertEquals(newValue, threadValue[0]);
    // Revert the value back to the default.
    env.setHitsPerPage(origValue);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) DummyHttpServletRequest(org.opengrok.indexer.web.DummyHttpServletRequest) Response(jakarta.ws.rs.core.Response) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) DummyHttpServletRequest(org.opengrok.indexer.web.DummyHttpServletRequest) Configuration(org.opengrok.indexer.configuration.Configuration) PageConfig(org.opengrok.web.PageConfig) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 27 with DummyHttpServletRequest

use of org.opengrok.indexer.web.DummyHttpServletRequest in project OpenGrok by OpenGrok.

the class PageConfigTest method testGetIntParam.

@Test
public void testGetIntParam() {
    String[] attrs = { "a", "b", "c", "d", "e", "f", "g", "h" };
    int[] values = { 1, 100, -1, 2, 200, 3000, -200, 3000 };
    DummyHttpServletRequest req = new DummyHttpServletRequest() {

        @Override
        public String getParameter(String name) {
            switch(name) {
                case "a":
                    return "1";
                case "b":
                    return "100";
                case "c":
                    return null;
                case "d":
                    return "2";
                case "e":
                    return "200";
                case "f":
                    return "3000";
                case "g":
                    return null;
                case "h":
                    return "abcdef";
            }
            return null;
        }
    };
    PageConfig cfg = PageConfig.get(req);
    assertEquals(attrs.length, values.length);
    for (int i = 0; i < attrs.length; i++) {
        assertEquals(values[i], cfg.getIntParam(attrs[i], values[i]));
    }
}
Also used : DummyHttpServletRequest(org.opengrok.indexer.web.DummyHttpServletRequest) Test(org.junit.jupiter.api.Test)

Aggregations

DummyHttpServletRequest (org.opengrok.indexer.web.DummyHttpServletRequest)27 Test (org.junit.jupiter.api.Test)22 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)8 User (opengrok.auth.plugin.entity.User)8 Project (org.opengrok.indexer.configuration.Project)8 Group (org.opengrok.indexer.configuration.Group)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 File (java.io.File)3 PluginClassLoader (org.opengrok.indexer.framework.PluginClassLoader)2 Cookie (jakarta.servlet.http.Cookie)1 HttpSession (jakarta.servlet.http.HttpSession)1 Response (jakarta.ws.rs.core.Response)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 EnabledOnOs (org.junit.jupiter.api.condition.EnabledOnOs)1 Configuration (org.opengrok.indexer.configuration.Configuration)1 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)1 Annotation (org.opengrok.indexer.history.Annotation)1 PageConfig (org.opengrok.web.PageConfig)1