PageRenderTime 590ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/test/java/org/jclouds/ohai/config/OhaiModuleTest.java

http://github.com/jclouds/jclouds-chef
Java | 139 lines | 90 code | 26 blank | 23 comment | 0 complexity | ec8e6680ad852c4d523d92ec953749ab MD5 | raw file
Possible License(s): Apache-2.0
  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.ohai.config;
  20. import static org.jclouds.chef.util.ChefUtils.ohaiAutomaticAttributeBinder;
  21. import static org.testng.Assert.assertEquals;
  22. import java.net.SocketException;
  23. import java.util.Map;
  24. import java.util.Properties;
  25. import javax.inject.Inject;
  26. import org.jclouds.chef.config.ChefParserModule;
  27. import org.jclouds.domain.JsonBall;
  28. import org.jclouds.json.Json;
  29. import org.jclouds.json.config.GsonModule;
  30. import org.jclouds.ohai.Automatic;
  31. import org.jclouds.ohai.config.multibindings.MapBinder;
  32. import org.testng.annotations.Test;
  33. import com.google.common.base.Supplier;
  34. import com.google.common.base.Suppliers;
  35. import com.google.inject.AbstractModule;
  36. import com.google.inject.Guice;
  37. import com.google.inject.Injector;
  38. import com.google.inject.TypeLiteral;
  39. import com.google.inject.util.Providers;
  40. /**
  41. * Tests behavior of {@code OhaiModule}
  42. *
  43. * @author Adrian Cole
  44. */
  45. @Test(groups = { "unit" })
  46. public class OhaiModuleTest {
  47. @Test
  48. public void test() throws SocketException {
  49. final Properties sysProperties = new Properties();
  50. sysProperties.setProperty("os.name", "Mac OS X");
  51. sysProperties.setProperty("os.version", "10.3.0");
  52. sysProperties.setProperty("user.name", "user");
  53. Injector injector = Guice.createInjector(new ChefParserModule(), new GsonModule(), new OhaiModule() {
  54. @Override
  55. protected Long millis() {
  56. return 127999291932529l;
  57. }
  58. @Override
  59. protected Properties systemProperties() {
  60. return sysProperties;
  61. }
  62. });
  63. Ohai ohai = injector.getInstance(Ohai.class);
  64. Json json = injector.getInstance(Json.class);
  65. assertEquals(
  66. json.toJson(ohai.ohai.get(), new TypeLiteral<Map<String, JsonBall>>() {
  67. }.getType()),
  68. "{\"ohai_time\":127999291932529,\"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\"}}}");
  69. }
  70. public void test2modules() throws SocketException {
  71. final Properties sysProperties = new Properties();
  72. sysProperties.setProperty("os.name", "Mac OS X");
  73. sysProperties.setProperty("os.version", "10.3.0");
  74. sysProperties.setProperty("user.name", "user");
  75. Injector injector = Guice.createInjector(new ChefParserModule(), new GsonModule(), new OhaiModule() {
  76. @Override
  77. protected Long millis() {
  78. return 1279992919l;
  79. }
  80. @Override
  81. protected Properties systemProperties() {
  82. return sysProperties;
  83. }
  84. }, new AbstractModule() {
  85. @Override
  86. protected void configure() {
  87. MapBinder<String, Supplier<JsonBall>> mapbinder = ohaiAutomaticAttributeBinder(binder());
  88. mapbinder.addBinding("test").toProvider(
  89. Providers.of(Suppliers.ofInstance(new JsonBall("{\"prop1\":\"test1\"}"))));
  90. }
  91. }, new AbstractModule() {
  92. @Override
  93. protected void configure() {
  94. MapBinder<String, Supplier<JsonBall>> mapbinder = ohaiAutomaticAttributeBinder(binder());
  95. mapbinder.addBinding("test").toProvider(
  96. Providers.of(Suppliers.ofInstance(new JsonBall("{\"prop2\":\"test2\"}"))));
  97. }
  98. });
  99. Ohai ohai = injector.getInstance(Ohai.class);
  100. Json json = injector.getInstance(Json.class);
  101. assertEquals(
  102. json.toJson(ohai.ohai.get(), new TypeLiteral<Map<String, JsonBall>>() {
  103. }.getType()),
  104. "{\"ohai_time\":1279992919,\"platform\":\"macosx\",\"platform_version\":\"10.3.0\",\"current_user\":\"user\",\"test\":{\"prop1\":\"test1\",\"prop2\":\"test2\"},\"jvm\":{\"system\":{\"user.name\":\"user\",\"os.version\":\"10.3.0\",\"os.name\":\"Mac OS X\"}}}");
  105. }
  106. static class Ohai {
  107. private Supplier<Map<String, JsonBall>> ohai;
  108. @Inject
  109. public Ohai(@Automatic Supplier<Map<String, JsonBall>> ohai) {
  110. this.ohai = ohai;
  111. }
  112. }
  113. }