Search in sources :

Example 16 with GenericXmlApplicationContext

use of org.springframework.context.support.GenericXmlApplicationContext in project spring-integration by spring-projects.

the class InnerPollerParserTests method testRefDefaultTrue.

@Test
public void testRefDefaultTrue() {
    try {
        // Load context from a String to avoid IDEs reporting the invalid configuration
        String badContext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<beans xmlns=\"http://www.springframework.org/schema/beans\"" + "		xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + "		xmlns:int=\"http://www.springframework.org/schema/integration\"" + "		xmlns:int-jdbc=\"http://www.springframework.org/schema/integration/jdbc\"" + "		xsi:schemaLocation=\"http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd" + "			http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd" + "			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd\">" + "" + "	<int:poller id=\"outer\" fixed-rate=\"5000\"/>" + "" + "	<int:channel id=\"someChannel\"/>" + "" + "	<int-jdbc:inbound-channel-adapter channel=\"someChannel\" jdbc-operations=\"ops\"" + "			query=\"select 1\">" + // <<<<< default true not allowed here
        "		<int:poller ref=\"outer\" default=\"true\"/>" + "	</int-jdbc:inbound-channel-adapter>" + "" + "	<bean id=\"ops\" class=\"org.mockito.Mockito\" factory-method=\"mock\">" + "		<constructor-arg value=\"org.springframework.jdbc.core.JdbcOperations\"/>" + "	</bean>" + "</beans>";
        Resource resource = new ByteArrayResource(badContext.getBytes());
        new GenericXmlApplicationContext(resource).close();
        fail("Expected Failure to load ApplicationContext");
    } catch (BeanDefinitionParsingException bdpe) {
        assertTrue(bdpe.getMessage().startsWith("Configuration problem: A 'poller' element that provides a 'ref' must have no other attributes."));
    }
}
Also used : BeanDefinitionParsingException(org.springframework.beans.factory.parsing.BeanDefinitionParsingException) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) GenericXmlApplicationContext(org.springframework.context.support.GenericXmlApplicationContext) Test(org.junit.Test)

Example 17 with GenericXmlApplicationContext

use of org.springframework.context.support.GenericXmlApplicationContext in project spring-integration by spring-projects.

the class InnerPollerParserTests method testRefExtraAttribute.

@Test
public void testRefExtraAttribute() {
    try {
        // Load context from a String to avoid IDEs reporting the invalid configuration
        String badContext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<beans xmlns=\"http://www.springframework.org/schema/beans\"" + "		xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + "		xmlns:int=\"http://www.springframework.org/schema/integration\"" + "		xmlns:int-jdbc=\"http://www.springframework.org/schema/integration/jdbc\"" + "		xsi:schemaLocation=\"http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd" + "			http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd" + "			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd\">" + "" + "	<int:poller id=\"outer\" fixed-rate=\"5000\"/>" + "" + "	<int:channel id=\"someChannel\"/>" + "" + "	<int-jdbc:inbound-channel-adapter channel=\"someChannel\" jdbc-operations=\"ops\"" + "			query=\"select 1\">" + // <<<<< fixed-rate not allowed here
        "		<int:poller ref=\"outer\" fixed-rate=\"1000\"/>" + "	</int-jdbc:inbound-channel-adapter>" + "" + "	<bean id=\"ops\" class=\"org.mockito.Mockito\" factory-method=\"mock\">" + "		<constructor-arg value=\"org.springframework.jdbc.core.JdbcOperations\"/>" + "	</bean>" + "</beans>";
        Resource resource = new ByteArrayResource(badContext.getBytes());
        new GenericXmlApplicationContext(resource).close();
        fail("Expected Failure to load ApplicationContext");
    } catch (BeanDefinitionParsingException bdpe) {
        assertTrue(bdpe.getMessage().startsWith("Configuration problem: A 'poller' element that provides a 'ref' must have no other attributes."));
    }
}
Also used : BeanDefinitionParsingException(org.springframework.beans.factory.parsing.BeanDefinitionParsingException) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) GenericXmlApplicationContext(org.springframework.context.support.GenericXmlApplicationContext) Test(org.junit.Test)

Example 18 with GenericXmlApplicationContext

use of org.springframework.context.support.GenericXmlApplicationContext in project spring-integration-samples by spring-projects.

the class Main method main.

/**
 * @param args Not used.
 */
public static void main(String... args) throws Exception {
    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
    final ConfigurableEnvironment env = context.getEnvironment();
    boolean mapQuestApiKeyDefined = env.containsProperty("mapquest.apikey");
    if (mapQuestApiKeyDefined) {
        env.setActiveProfiles("mapquest");
    }
    context.load("classpath:META-INF/spring/*.xml");
    context.refresh();
    final TravelGateway travelGateway = context.getBean("travelGateway", TravelGateway.class);
    final Scanner scanner = new Scanner(System.in);
    System.out.println("\n=========================================================" + "\n                                                         " + "\n    Welcome to the Spring Integration Travel App!        " + "\n                                                         " + "\n    For more information please visit:                   " + "\n    http://www.springintegration.org/                    " + "\n                                                         " + "\n=========================================================");
    System.out.println("Please select the city, for which you would like to get traffic and weather information:");
    for (City city : City.values()) {
        System.out.println(String.format("\t%s. %s", city.getId(), city.getName()));
    }
    System.out.println("\tq. Quit the application");
    System.out.print("Enter your choice: ");
    while (true) {
        final String input = scanner.nextLine();
        if ("q".equals(input.trim())) {
            System.out.println("Exiting application...bye.");
            break;
        } else {
            final Integer cityId = Integer.valueOf(input);
            final City city = City.getCityForId(cityId);
            final String weatherReply = travelGateway.getWeatherByCity(city);
            System.out.println("\n=========================================================" + "\n    Weather:" + "\n=========================================================");
            System.out.println(weatherReply);
            if (mapQuestApiKeyDefined) {
                final String trafficReply = travelGateway.getTrafficByCity(city);
                System.out.println("\n=========================================================" + "\n    Traffic:" + "\n=========================================================");
                System.out.println(trafficReply);
            } else {
                LOGGER.warn("Skipping Traffic Information call. Did you setup your MapQuest API Key? " + "e.g. by calling:\n\n    $ mvn exec:java -Dmapquest.apikey=\"your_mapquest_api_key_url_decoded\"");
            }
            System.out.println("\n=========================================================" + "\n    Done." + "\n=========================================================");
            System.out.print("Enter your choice: ");
        }
    }
    scanner.close();
    context.close();
}
Also used : Scanner(java.util.Scanner) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) GenericXmlApplicationContext(org.springframework.context.support.GenericXmlApplicationContext)

Example 19 with GenericXmlApplicationContext

use of org.springframework.context.support.GenericXmlApplicationContext in project spring-integration-samples by spring-projects.

the class Main method executeSample2.

private static void executeSample2() {
    final Scanner scanner = new Scanner(System.in);
    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
    context.load("classpath:META-INF/spring/integration/spring-integration-sample2-context.xml");
    context.registerShutdownHook();
    context.refresh();
    final CoffeeService service = context.getBean(CoffeeService.class);
    final String message = "\n\n" + "* Please enter 'list' and press <enter> to get a list of coffees.\n" + "* Enter a coffee id, e.g. '1' and press <enter> to get a description.\n" + "* Please press 'q + Enter' to quit the application.\n";
    System.out.println(message);
    while (!scanner.hasNext("q")) {
        String input = scanner.nextLine();
        if ("list".equalsIgnoreCase(input)) {
            List<CoffeeBeverage> coffeeBeverages = service.findAllCoffeeBeverages();
            for (CoffeeBeverage coffeeBeverage : coffeeBeverages) {
                System.out.println(String.format("%s - %s", coffeeBeverage.getId(), coffeeBeverage.getName()));
            }
        } else {
            System.out.println("Retrieving coffee information...");
            String coffeeDescription = service.findCoffeeBeverage(Integer.valueOf(input));
            System.out.println(String.format("Searched for '%s' - Found: '%s'.", input, coffeeDescription));
            System.out.print("To try again, please enter another coffee beverage and press <enter>:\n\n");
        }
    }
    scanner.close();
    context.close();
    System.out.println("Back to main menu.");
}
Also used : Scanner(java.util.Scanner) CoffeeBeverage(org.springframework.integration.model.CoffeeBeverage) GenericXmlApplicationContext(org.springframework.context.support.GenericXmlApplicationContext) CoffeeService(org.springframework.integration.service.CoffeeService)

Example 20 with GenericXmlApplicationContext

use of org.springframework.context.support.GenericXmlApplicationContext in project spring-framework by spring-projects.

the class EnvironmentSystemIntegrationTests method genericXmlApplicationContext.

@Test
void genericXmlApplicationContext() {
    GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
    assertHasStandardEnvironment(ctx);
    ctx.setEnvironment(prodEnv);
    ctx.load(XML_PATH);
    ctx.refresh();
    assertHasEnvironment(ctx, prodEnv);
    assertEnvironmentBeanRegistered(ctx);
    assertEnvironmentAwareInvoked(ctx, prodEnv);
    assertThat(ctx.containsBean(DEV_BEAN_NAME)).isFalse();
    assertThat(ctx.containsBean(PROD_BEAN_NAME)).isTrue();
}
Also used : GenericXmlApplicationContext(org.springframework.context.support.GenericXmlApplicationContext) Test(org.junit.jupiter.api.Test)

Aggregations

GenericXmlApplicationContext (org.springframework.context.support.GenericXmlApplicationContext)61 Test (org.junit.Test)21 Test (org.junit.jupiter.api.Test)13 MBeanServer (javax.management.MBeanServer)5 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)5 Scanner (java.util.Scanner)4 ObjectName (javax.management.ObjectName)4 Resource (org.springframework.core.io.Resource)4 QueueChannel (org.springframework.integration.channel.QueueChannel)4 MessageChannel (org.springframework.messaging.MessageChannel)4 ApplicationContext (org.springframework.context.ApplicationContext)3 ByteArrayResource (org.springframework.core.io.ByteArrayResource)3 Message (org.springframework.messaging.Message)3 MBeanOperationInfo (javax.management.MBeanOperationInfo)2 Server (org.eclipse.jetty.server.Server)2 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)2 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)2 BatchStatus (org.springframework.batch.core.BatchStatus)2 BeanDefinitionParsingException (org.springframework.beans.factory.parsing.BeanDefinitionParsingException)2 CacheInterceptor (org.springframework.cache.interceptor.CacheInterceptor)2