PageRenderTime 94ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/demos/gae/src/test/java/org/jclouds/chef/demo/ohai/config/OhaiModuleTest.java

https://github.com/jclouds/legacy-jclouds-chef
Java | 118 lines | 74 code | 21 blank | 23 comment | 0 complexity | 135446e975c49bac622a1cdc0df71b46 MD5 | raw file
  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.chef.demo.ohai.config;
  20. import static org.easymock.EasyMock.expect;
  21. import static org.easymock.classextension.EasyMock.createMock;
  22. import static org.easymock.classextension.EasyMock.replay;
  23. import static org.testng.Assert.assertEquals;
  24. import java.net.SocketException;
  25. import java.util.Enumeration;
  26. import java.util.Map;
  27. import java.util.Properties;
  28. import javax.inject.Inject;
  29. import javax.servlet.ServletContext;
  30. import org.jclouds.chef.config.ChefParserModule;
  31. import org.jclouds.domain.JsonBall;
  32. import org.jclouds.json.Json;
  33. import org.jclouds.json.config.GsonModule;
  34. import org.jclouds.ohai.Automatic;
  35. import org.jclouds.ohai.config.OhaiModule;
  36. import org.jclouds.ohai.servlet.config.ServletOhaiModule;
  37. import org.jclouds.util.Utils;
  38. import org.testng.annotations.Test;
  39. import com.google.common.base.Supplier;
  40. import com.google.inject.Guice;
  41. import com.google.inject.Injector;
  42. import com.google.inject.Provides;
  43. import com.google.inject.TypeLiteral;
  44. /**
  45. * Tests behavior of {@code OhaiModule}
  46. *
  47. * @author Adrian Cole
  48. */
  49. @Test(groups = { "unit" })
  50. public class OhaiModuleTest {
  51. @Test
  52. public void testAllModulesOutput() throws SocketException {
  53. final ServletContext servletContext = createMock(ServletContext.class);
  54. Enumeration<?> parameters = createMock(Enumeration.class);
  55. expect(servletContext.getContextPath()).andReturn("").atLeastOnce();
  56. expect(servletContext.getServerInfo()).andReturn("server info");
  57. expect(servletContext.getInitParameterNames()).andReturn(parameters);
  58. expect(parameters.hasMoreElements()).andReturn(false);
  59. expect(servletContext.getResourceAsStream("/WEB-INF/web.xml")).andReturn(
  60. Utils.toInputStream("<tag name=\"fooy\"></tag>"));
  61. replay(servletContext);
  62. replay(parameters);
  63. final Properties sysProperties = new Properties();
  64. sysProperties.setProperty("os.name", "Mac OS X");
  65. sysProperties.setProperty("os.version", "10.3.0");
  66. sysProperties.setProperty("user.name", "user");
  67. Injector injector = Guice.createInjector(new ChefParserModule(), new GsonModule(), new WebXmlOhaiModule(),
  68. new ServletOhaiModule(), new OhaiModule() {
  69. @SuppressWarnings("unused")
  70. @Provides
  71. protected ServletContext provideServletContext() {
  72. return servletContext;
  73. }
  74. @Override
  75. protected Long millis() {
  76. return 1279992919l;
  77. }
  78. @Override
  79. protected Properties systemProperties() {
  80. return sysProperties;
  81. }
  82. });
  83. Ohai ohai = injector.getInstance(Ohai.class);
  84. Json json = injector.getInstance(Json.class);
  85. assertEquals(
  86. json.toJson(ohai.ohai.get(), new TypeLiteral<Map<String, JsonBall>>() {
  87. }.getType()),
  88. "{\"webapp\":{\"/\":{\"web_xml\":\"<tag name=\\\"fooy\\\"></tag>\",\"server_info\":\"server info\",\"init_params\":{}}},\"ohai_time\":1279992919,\"platform\":\"macosx\",\"platform_version\":\"10.3.0\",\"current_user\":\"user\",\"jvm\":{\"system\":{\"user.name\":\"user\",\"os.version\":\"10.3.0\",\"os.name\":\"Mac OS X\"}}}");
  89. }
  90. static class Ohai {
  91. private Supplier<Map<String, JsonBall>> ohai;
  92. @Inject
  93. public Ohai(@Automatic Supplier<Map<String, JsonBall>> ohai) {
  94. this.ohai = ohai;
  95. }
  96. }
  97. }