Search in sources :

Example 1 with HTTPServerConfig

use of org.languagetool.server.HTTPServerConfig 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 HTTPServerConfig

use of org.languagetool.server.HTTPServerConfig 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)

Aggregations

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