PageRenderTime 269ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/servlet/src/test/java/org/jclouds/chef/servlet/suppliers/ServletContextInfoSupplierTest.java

https://github.com/jclouds/legacy-jclouds-chef
Java | 162 lines | 109 code | 31 blank | 22 comment | 0 complexity | 8b54e34c051bbb5eb62da6f143c59a1c 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.servlet.suppliers;
  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.util.Properties;
  25. import javax.servlet.ServletContext;
  26. import org.jclouds.chef.servlet.functions.InitParamsToProperties;
  27. import org.jclouds.json.Json;
  28. import org.jclouds.json.config.GsonModule;
  29. import org.jclouds.ohai.servlet.config.ServletOhaiModule;
  30. import org.jclouds.ohai.servlet.suppliers.ServletContextInfoSupplier;
  31. import org.testng.annotations.Test;
  32. import com.google.inject.Guice;
  33. import com.google.inject.Injector;
  34. import com.google.inject.Provides;
  35. /**
  36. *
  37. * @author Adrian Cole
  38. */
  39. @Test(groups = {"unit"})
  40. public class ServletContextInfoSupplierTest {
  41. @Test
  42. public void testApply() {
  43. final ServletContext servletContext = createMock(ServletContext.class);
  44. final InitParamsToProperties converter = createMock(InitParamsToProperties.class);
  45. Properties props = new Properties();
  46. props.setProperty("foo", "bar");
  47. expect(servletContext.getContextPath()).andReturn("path");
  48. expect(servletContext.getServerInfo()).andReturn("serverinfo");
  49. expect(converter.apply(servletContext)).andReturn(props);
  50. replay(servletContext);
  51. replay(converter);
  52. Injector injector = Guice.createInjector(new GsonModule(),
  53. new ServletOhaiModule() {
  54. @SuppressWarnings("unused")
  55. @Provides
  56. protected ServletContext provideServletContext() {
  57. return servletContext;
  58. }
  59. @SuppressWarnings("unused")
  60. @Provides
  61. protected InitParamsToProperties provideInitParamsToProperties() {
  62. return converter;
  63. }
  64. });
  65. Json json = injector.getInstance(Json.class);
  66. ServletContextInfoSupplier ohai = injector
  67. .getInstance(ServletContextInfoSupplier.class);
  68. assertEquals(json.toJson(ohai.get()),
  69. "{\"path\":{\"server_info\":\"serverinfo\",\"init_params\":{\"foo\":\"bar\"}}}");
  70. }
  71. @Test
  72. public void testApplyNullPath() {
  73. final ServletContext servletContext = createMock(ServletContext.class);
  74. final InitParamsToProperties converter = createMock(InitParamsToProperties.class);
  75. Properties props = new Properties();
  76. props.setProperty("foo", "bar");
  77. expect(servletContext.getContextPath()).andReturn(null);
  78. expect(servletContext.getServerInfo()).andReturn("serverinfo");
  79. expect(converter.apply(servletContext)).andReturn(props);
  80. replay(servletContext);
  81. replay(converter);
  82. Injector injector = Guice.createInjector(new GsonModule(),
  83. new ServletOhaiModule() {
  84. @SuppressWarnings("unused")
  85. @Provides
  86. protected ServletContext provideServletContext() {
  87. return servletContext;
  88. }
  89. @SuppressWarnings("unused")
  90. @Provides
  91. protected InitParamsToProperties provideInitParamsToProperties() {
  92. return converter;
  93. }
  94. });
  95. Json json = injector.getInstance(Json.class);
  96. ServletContextInfoSupplier ohai = injector
  97. .getInstance(ServletContextInfoSupplier.class);
  98. assertEquals(json.toJson(ohai.get()),
  99. "{\"/\":{\"server_info\":\"serverinfo\",\"init_params\":{\"foo\":\"bar\"}}}");
  100. }
  101. @Test
  102. public void testApplyEmptyPath() {
  103. final ServletContext servletContext = createMock(ServletContext.class);
  104. final InitParamsToProperties converter = createMock(InitParamsToProperties.class);
  105. Properties props = new Properties();
  106. props.setProperty("foo", "bar");
  107. expect(servletContext.getContextPath()).andReturn("");
  108. expect(servletContext.getServerInfo()).andReturn("serverinfo");
  109. expect(converter.apply(servletContext)).andReturn(props);
  110. replay(servletContext);
  111. replay(converter);
  112. Injector injector = Guice.createInjector(new GsonModule(),
  113. new ServletOhaiModule() {
  114. @SuppressWarnings("unused")
  115. @Provides
  116. protected ServletContext provideServletContext() {
  117. return servletContext;
  118. }
  119. @SuppressWarnings("unused")
  120. @Provides
  121. protected InitParamsToProperties provideInitParamsToProperties() {
  122. return converter;
  123. }
  124. });
  125. Json json = injector.getInstance(Json.class);
  126. ServletContextInfoSupplier ohai = injector
  127. .getInstance(ServletContextInfoSupplier.class);
  128. assertEquals(json.toJson(ohai.get()),
  129. "{\"/\":{\"server_info\":\"serverinfo\",\"init_params\":{\"foo\":\"bar\"}}}");
  130. }
  131. }