/slim3/src/main/java/org/slim3/tester/TestEnvironment.java

http://slim3.googlecode.com/ · Java · 249 lines · 102 code · 32 blank · 115 comment · 3 complexity · 4d2dbbcd95677584343f09c8939e2ddc MD5 · raw file

  1. /*
  2. * Copyright 2004-2010 the Seasar Foundation and the Others.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  13. * either express or implied. See the License for the specific language
  14. * governing permissions and limitations under the License.
  15. */
  16. package org.slim3.tester;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import org.slim3.util.AppEngineUtil;
  20. import org.slim3.util.StringUtil;
  21. import com.google.appengine.api.NamespaceManager;
  22. import com.google.apphosting.api.ApiProxy;
  23. import com.google.apphosting.api.ApiProxy.Environment;
  24. /**
  25. * The test environment.
  26. *
  27. * @author higa
  28. * @since 1.0.0
  29. *
  30. */
  31. public class TestEnvironment implements ApiProxy.Environment {
  32. /**
  33. * The application identifier.
  34. */
  35. protected String appId = "Unit Tests";
  36. /**
  37. * The version identifier.
  38. */
  39. protected String versionId = "1.0";
  40. /**
  41. * The authority domain.
  42. */
  43. protected String authDomain = "gmail.com";
  44. /**
  45. * The email address.
  46. */
  47. protected String email;
  48. /**
  49. * Whether the current user is an administrator.
  50. */
  51. protected boolean admin = false;
  52. /**
  53. * The remaining millisecond.
  54. */
  55. protected long remainingMillis;
  56. /**
  57. * The environment attributes.
  58. */
  59. protected Map<String, Object> attributes = new HashMap<String, Object>();
  60. /**
  61. * Constructor.
  62. */
  63. public TestEnvironment() {
  64. attributes.put("com.google.appengine.server_url_key", "dummy");
  65. }
  66. /**
  67. * Constructor.
  68. *
  69. * @param other
  70. * the other environment
  71. * @throws NullPointerException
  72. * if the other parameter is null
  73. */
  74. public TestEnvironment(Environment other) throws NullPointerException {
  75. if (other == null) {
  76. throw new NullPointerException(
  77. "The other parameter must not be null.");
  78. }
  79. appId = other.getAppId();
  80. versionId = other.getVersionId();
  81. authDomain = other.getAuthDomain();
  82. email = other.getEmail();
  83. admin = other.isAdmin();
  84. attributes = other.getAttributes();
  85. }
  86. /**
  87. * Constructor.
  88. *
  89. * @param email
  90. * the email address
  91. */
  92. public TestEnvironment(String email) {
  93. this(email, true);
  94. }
  95. /**
  96. * Constructor.
  97. *
  98. * @param email
  99. * the email address
  100. * @param admin
  101. * whether the current user is an administrator
  102. */
  103. public TestEnvironment(String email, boolean admin) {
  104. this();
  105. setEmail(email);
  106. setAdmin(admin);
  107. }
  108. public String getAppId() {
  109. return appId;
  110. }
  111. /**
  112. * Sets the application identifier.
  113. *
  114. * @param appId
  115. * the application identifier
  116. */
  117. public void setAppId(String appId) {
  118. assertNotProduction();
  119. this.appId = appId;
  120. }
  121. public String getVersionId() {
  122. return versionId;
  123. }
  124. /**
  125. * Sets the version identifier.
  126. *
  127. * @param versionId
  128. * the version identifier
  129. */
  130. public void setVersionId(String versionId) {
  131. assertNotProduction();
  132. this.versionId = versionId;
  133. }
  134. @SuppressWarnings("deprecation")
  135. public String getRequestNamespace() {
  136. return NamespaceManager.get();
  137. }
  138. public String getAuthDomain() {
  139. return authDomain;
  140. }
  141. /**
  142. * Sets the authority domain.
  143. *
  144. * @param authDomain
  145. * the authority domain
  146. */
  147. public void setAuthDomain(String authDomain) {
  148. assertNotProduction();
  149. this.authDomain = authDomain;
  150. }
  151. public String getEmail() {
  152. return email;
  153. }
  154. /**
  155. * Sets the email address.
  156. *
  157. * @param email
  158. * the email address
  159. */
  160. public void setEmail(String email) {
  161. assertNotProduction();
  162. this.email = email;
  163. }
  164. public boolean isLoggedIn() {
  165. return !StringUtil.isEmpty(email);
  166. }
  167. public boolean isAdmin() {
  168. return admin;
  169. }
  170. /**
  171. * Sets whether the current user is an administrator.
  172. *
  173. * @param admin
  174. * whether the current user is an administrator
  175. */
  176. public void setAdmin(boolean admin) {
  177. assertNotProduction();
  178. this.admin = admin;
  179. }
  180. public Map<String, Object> getAttributes() {
  181. return attributes;
  182. }
  183. /**
  184. * Sets the attributes.
  185. *
  186. * @param attributes
  187. * the attributes
  188. */
  189. public void setAttributes(Map<String, Object> attributes) {
  190. assertNotProduction();
  191. this.attributes = attributes;
  192. }
  193. /**
  194. * Asserts that the current environment is not production.
  195. *
  196. * @throws IllegalStateException
  197. * if the current environment is production
  198. */
  199. protected void assertNotProduction() throws IllegalStateException {
  200. if (AppEngineUtil.isProduction()) {
  201. throw new IllegalStateException(
  202. "This feature is not supported on production server.");
  203. }
  204. }
  205. @Override
  206. public long getRemainingMillis() {
  207. return remainingMillis;
  208. }
  209. /**
  210. * Sets the remaining millisecond.
  211. *
  212. * @param remainingMillis the remaining millisecond
  213. */
  214. public void setRemainingMillis(long remainingMillis) {
  215. this.remainingMillis = remainingMillis;
  216. }
  217. }