Search in sources :

Example 1 with HTTPServer

use of org.languagetool.server.HTTPServer in project languagetool by languagetool-org.

the class RemoteLanguageToolIntegrationTest method testClient.

@Test
public void testClient() throws MalformedURLException {
    HTTPServerConfig config = new HTTPServerConfig(HTTPTools.getDefaultPort());
    HTTPServer server = new HTTPServer(config);
    try {
        server.run();
        RemoteLanguageTool lt = new RemoteLanguageTool(new URL(serverUrl));
        assertThat(lt.check("This is a correct sentence.", "en").getMatches().size(), is(0));
        assertThat(lt.check("Sentence wiht a typo not detected.", "en").getMatches().size(), is(0));
        assertThat(lt.check("Sentence wiht a typo detected.", "en-US").getMatches().size(), is(1));
        assertThat(lt.check("A sentence with a error.", "en").getMatches().size(), is(1));
        assertThat(lt.check("Test escape: %", "en").getMatches().size(), is(0));
        RemoteResult result1 = lt.check("A sentence with a error, and and another one", "en");
        assertThat(result1.getLanguage(), is("English"));
        assertThat(result1.getLanguageCode(), is("en"));
        assertThat(result1.getRemoteServer().getSoftware(), is("LanguageTool"));
        assertNotNull(result1.getRemoteServer().getVersion());
        assertThat(result1.getMatches().size(), is(2));
        assertThat(result1.getMatches().get(0).getRuleId(), is("EN_A_VS_AN"));
        assertThat(result1.getMatches().get(1).getRuleId(), is("ENGLISH_WORD_REPEAT_RULE"));
        CheckConfiguration disabledConfig = new CheckConfigurationBuilder("en").disabledRuleIds("EN_A_VS_AN").build();
        RemoteResult result2 = lt.check("A sentence with a error, and and another one", disabledConfig);
        assertThat(result2.getMatches().size(), is(1));
        assertThat(result2.getMatches().get(0).getRuleId(), is("ENGLISH_WORD_REPEAT_RULE"));
        CheckConfiguration enabledConfig = new CheckConfigurationBuilder("en").enabledRuleIds("EN_A_VS_AN").build();
        RemoteResult result3 = lt.check("A sentence with a error, and and another one", enabledConfig);
        assertThat(result3.getMatches().size(), is(2));
        CheckConfiguration enabledOnlyConfig = new CheckConfigurationBuilder("en").enabledRuleIds("EN_A_VS_AN").enabledOnly().build();
        RemoteResult result4 = lt.check("A sentence with a error, and and another one", enabledOnlyConfig);
        assertThat(result4.getMatches().size(), is(1));
        assertThat(result4.getMatches().get(0).getRuleId(), is("EN_A_VS_AN"));
        CheckConfiguration config1 = new CheckConfigurationBuilder().build();
        RemoteResult result5 = lt.check("Ein Satz in Deutsch, mit etwas mehr Text, damit es auch geht.", config1);
        assertThat(result5.getLanguage(), is("German (Germany)"));
        assertThat(result5.getLanguageCode(), is("de-DE"));
        CheckConfiguration config2 = new CheckConfigurationBuilder().build();
        // too short, fallback will be used
        RemoteResult result6 = lt.check("x", config2);
        assertThat(result6.getLanguage(), is("English (US)"));
        assertThat(result6.getLanguageCode(), is("en-US"));
        RemoteResult result7 = lt.check("Das Häuser ist schön.", "de");
        assertThat(result7.getMatches().size(), is(1));
        assertThat(result7.getMatches().get(0).getRuleId(), is("DE_AGREEMENT"));
        try {
            System.err.println("=== Testing invalid language code - ignore the following exception: ===");
            lt.check("foo", "xy");
            fail();
        } catch (RuntimeException e) {
            assertTrue(e.getMessage().contains("is not a language code known to LanguageTool"));
        }
    } finally {
        server.stop();
    }
}
Also used : HTTPServer(org.languagetool.server.HTTPServer) HTTPServerConfig(org.languagetool.server.HTTPServerConfig) URL(java.net.URL) Test(org.junit.Test)

Example 2 with HTTPServer

use of org.languagetool.server.HTTPServer in project languagetool by languagetool-org.

the class Main method maybeStartServer.

private boolean maybeStartServer() {
    Configuration config = ltSupport.getConfig();
    if (config.getRunServer()) {
        try {
            HTTPServerConfig serverConfig = new HTTPServerConfig(config.getServerPort(), false);
            httpServer = new HTTPServer(serverConfig, true);
            httpServer.run();
            if (enableHttpServerItem != null) {
                enableHttpServerItem.setState(httpServer.isRunning());
                setTrayIcon();
            }
        } catch (PortBindingException e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    return httpServer != null && httpServer.isRunning();
}
Also used : HTTPServer(org.languagetool.server.HTTPServer) HTTPServerConfig(org.languagetool.server.HTTPServerConfig) PortBindingException(org.languagetool.server.PortBindingException)

Example 3 with HTTPServer

use of org.languagetool.server.HTTPServer in project omegat by omegat-org.

the class LanguageToolTest method testRemoteServer.

@Test
public void testRemoteServer() throws Exception {
    HTTPServer server = new HTTPServer();
    try {
        server.run();
        try {
            new LanguageToolNetworkBridge(SOURCE_LANG, TARGET_LANG, "http://localhost:8081");
            fail("URL not specifying API v2 should fail due to XML response instead of JSON");
        // TODO: LanguageTool will drop XML entirely in version 3.6; this
        // test might need to be adjusted then.
        } catch (Exception e) {
        // OK
        }
        ILanguageToolBridge bridge = new LanguageToolNetworkBridge(SOURCE_LANG, TARGET_LANG, "http://localhost:8081/v2/check");
        // Set some rules to prevent the server from looking at config files.
        // User config files can specify languages we aren't providing at test
        // runtime, in which case queries will fail.
        bridge.applyRuleFilters(Collections.singleton("FOO"), Collections.emptySet(), Collections.emptySet());
        // We don't care about the actual content of the results as long as
        // there are some: we just want to make sure we are parsing the JSON
        // result correctly.
        List<LanguageToolResult> results = bridge.getCheckResults("foo", "foo bar");
        assertFalse(results.isEmpty());
    } finally {
        server.stop();
    }
}
Also used : HTTPServer(org.languagetool.server.HTTPServer) Test(org.junit.Test)

Aggregations

HTTPServer (org.languagetool.server.HTTPServer)3 Test (org.junit.Test)2 HTTPServerConfig (org.languagetool.server.HTTPServerConfig)2 URL (java.net.URL)1 PortBindingException (org.languagetool.server.PortBindingException)1