{"action":"created","comment":{"url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/comments/30112751","id":30112751,"diff_hunk":"@@ -0,0 +1,318 @@\n+/*\n+ * SonarQube, open source software quality management tool.\n+ * Copyright (C) 2008-2014 SonarSource\n+ * mailto:contact AT sonarsource DOT com\n+ *\n+ * SonarQube is free software; you can redistribute it and/or\n+ * modify it under the terms of the GNU Lesser General Public\n+ * License as published by the Free Software Foundation; either\n+ * version 3 of the License, or (at your option) any later version.\n+ *\n+ * SonarQube is distributed in the hope that it will be useful,\n+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n+ * Lesser General Public License for more details.\n+ *\n+ * You should have received a copy of the GNU Lesser General Public License\n+ * along with this program; if not, write to the Free Software Foundation,\n+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n+ */\n+package org.sonar.server.tester;\n+\n+import com.google.common.base.Preconditions;\n+import java.util.List;\n+import java.util.Locale;\n+import java.util.Set;\n+import javax.annotation.CheckForNull;\n+import javax.annotation.Nullable;\n+import org.junit.rules.TestRule;\n+import org.junit.runner.Description;\n+import org.junit.runners.model.Statement;\n+import org.sonar.server.user.AnonymousMockUserSession;\n+import org.sonar.server.user.MockUserSession;\n+import org.sonar.server.user.ThreadLocalUserSession;\n+import org.sonar.server.user.UserSession;\n+\n+import static com.google.common.base.Preconditions.checkNotNull;\n+import static com.google.common.base.Preconditions.checkState;\n+\n+/**\n+ * {@code UserSessionRule} is intended to be used as a {@link org.junit.Rule} to easily manage {@link UserSession} in\n+ * unit tests.\n+ * <p>\n+ * It can be used as a {@link org.junit.ClassRule} but be careful not to modify its states from inside tests methods\n+ * unless you purposily want to have side effects between each tests.\n+ * </p>\n+ * <p>\n+ * {@code UserSessionRule} is intended to be used either standalone (in which case use the static factory method\n+ * {@link #standalone()} or with {@link ServerTester} (in which case use static factory method\n+ * {@link #forServerTester(ServerTester)}).\n+ * </p>\n+ * <p>\n+ * In both cases, one can define user session behavior which should apply on all tests directly on the property, eg.:\n+ * <pre>\n+ * {@literal @}Rule\n+ * public UserSessionRule userSessionRule = UserSessionRule.standalone().logon(\"admin\").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);\n+ * </pre>\n+ * </p>\n+ * <p>\n+ * Behavior defined at property-level can obviously be override at test method level. For example, one could define\n+ * all methods to use an authenticated session such as presented above but can easily overwrite that behavior in a\n+ * specific test method as follow:\n+ * <pre>\n+ * {@literal @}Test\n+ * public void test_method() {\n+ *   userSessionRule.standalone();\n+ *   {@literal [...]}\n+ * }\n+ * </pre>\n+ * </p>\n+ * <p>\n+ * {@code UserSessionRule}, being standalone or associated to a {@link ServerTester}, emulates by default an anonymous\n+ * session. Therefore, call {@code UserSessionRule.standalone()} is equivalent to calling\n+ * {@code UserSessionRule.standalone().anonymous()}.\n+ * </p>\n+ * <p>\n+ * To emulate an identified user, either use method {@link #logon(String)} if you want to specify the user's login, or\n+ * method {@link #logon()} which will do the same but using the value of {@link #DEFAULT_LOGIN} as the user's login.\n+ * </p>\n+ */\n+public class UserSessionRule implements TestRule, UserSession {\n+  public static final String DEFAULT_LOGIN = \"default_login\";\n+\n+  @CheckForNull\n+  private final ServerTester serverTester;\n+  private UserSession currentUserSession;\n+\n+  private UserSessionRule(@Nullable ServerTester serverTester) {\n+    this.serverTester = serverTester;\n+    anonymous();\n+  }\n+\n+  public static UserSessionRule standalone() {","path":"server/sonar-server/src/test/java/org/sonar/server/tester/UserSessionRule.java","position":92,"original_position":92,"commit_id":"aebb914e37c8b3934f63dadd68ea92c06c81bf1c","original_commit_id":"aebb914e37c8b3934f63dadd68ea92c06c81bf1c","user":{"login":"sns-seb","id":11717580,"avatar_url":"https://avatars.githubusercontent.com/u/11717580?v=3","gravatar_id":"","url":"https://api.github.com/users/sns-seb","html_url":"https://github.com/sns-seb","followers_url":"https://api.github.com/users/sns-seb/followers","following_url":"https://api.github.com/users/sns-seb/following{/other_user}","gists_url":"https://api.github.com/users/sns-seb/gists{/gist_id}","starred_url":"https://api.github.com/users/sns-seb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sns-seb/subscriptions","organizations_url":"https://api.github.com/users/sns-seb/orgs","repos_url":"https://api.github.com/users/sns-seb/repos","events_url":"https://api.github.com/users/sns-seb/events{/privacy}","received_events_url":"https://api.github.com/users/sns-seb/received_events","type":"User","site_admin":false},"body":"why not `create`","created_at":"2015-05-12T08:13:45Z","updated_at":"2015-05-12T08:13:45Z","html_url":"https://github.com/SonarSource/sonarqube/pull/299#discussion_r30112751","pull_request_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/299","_links":{"self":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/comments/30112751"},"html":{"href":"https://github.com/SonarSource/sonarqube/pull/299#discussion_r30112751"},"pull_request":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/299"}}},"pull_request":{"url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/299","id":35107720,"html_url":"https://github.com/SonarSource/sonarqube/pull/299","diff_url":"https://github.com/SonarSource/sonarqube/pull/299.diff","patch_url":"https://github.com/SonarSource/sonarqube/pull/299.patch","issue_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/299","number":299,"state":"open","locked":false,"title":"make UserSession non static","user":{"login":"sns-seb","id":11717580,"avatar_url":"https://avatars.githubusercontent.com/u/11717580?v=3","gravatar_id":"","url":"https://api.github.com/users/sns-seb","html_url":"https://github.com/sns-seb","followers_url":"https://api.github.com/users/sns-seb/followers","following_url":"https://api.github.com/users/sns-seb/following{/other_user}","gists_url":"https://api.github.com/users/sns-seb/gists{/gist_id}","starred_url":"https://api.github.com/users/sns-seb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sns-seb/subscriptions","organizations_url":"https://api.github.com/users/sns-seb/orgs","repos_url":"https://api.github.com/users/sns-seb/repos","events_url":"https://api.github.com/users/sns-seb/events{/privacy}","received_events_url":"https://api.github.com/users/sns-seb/received_events","type":"User","site_admin":false},"body":"* split `UserSession` into `ThreadLocalUserSession` and `ServerUserSession`, former is responsible of using a ThreadLocal to retrieve the current session, later is responsible for the dao related logic, both implement `UserSession` interface\r\n* reworked `MockUserSession` to drop dao mocks and introduced `AnonumousMockUserSession`\r\n* introduced `UserSessionRule` (itself a `UserSession`) with support for `ServerTest` to integrate nicely with the `ThreadLocalUserSession` instance in the pico container","created_at":"2015-05-11T08:45:07Z","updated_at":"2015-05-12T08:13:45Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1942c2d9a35f4ab1da28f25b635c643874af9a7e","assignee":{"login":"sns-seb","id":11717580,"avatar_url":"https://avatars.githubusercontent.com/u/11717580?v=3","gravatar_id":"","url":"https://api.github.com/users/sns-seb","html_url":"https://github.com/sns-seb","followers_url":"https://api.github.com/users/sns-seb/followers","following_url":"https://api.github.com/users/sns-seb/following{/other_user}","gists_url":"https://api.github.com/users/sns-seb/gists{/gist_id}","starred_url":"https://api.github.com/users/sns-seb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sns-seb/subscriptions","organizations_url":"https://api.github.com/users/sns-seb/orgs","repos_url":"https://api.github.com/users/sns-seb/repos","events_url":"https://api.github.com/users/sns-seb/events{/privacy}","received_events_url":"https://api.github.com/users/sns-seb/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/299/commits","review_comments_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/299/comments","review_comment_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/comments{/number}","comments_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/299/comments","statuses_url":"https://api.github.com/repos/SonarSource/sonarqube/statuses/aebb914e37c8b3934f63dadd68ea92c06c81bf1c","head":{"label":"SonarSource:feature/non_static_user_session","ref":"feature/non_static_user_session","sha":"aebb914e37c8b3934f63dadd68ea92c06c81bf1c","user":{"login":"SonarSource","id":545988,"avatar_url":"https://avatars.githubusercontent.com/u/545988?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarSource","html_url":"https://github.com/SonarSource","followers_url":"https://api.github.com/users/SonarSource/followers","following_url":"https://api.github.com/users/SonarSource/following{/other_user}","gists_url":"https://api.github.com/users/SonarSource/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarSource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarSource/subscriptions","organizations_url":"https://api.github.com/users/SonarSource/orgs","repos_url":"https://api.github.com/users/SonarSource/repos","events_url":"https://api.github.com/users/SonarSource/events{/privacy}","received_events_url":"https://api.github.com/users/SonarSource/received_events","type":"Organization","site_admin":false},"repo":{"id":1222504,"name":"sonarqube","full_name":"SonarSource/sonarqube","owner":{"login":"SonarSource","id":545988,"avatar_url":"https://avatars.githubusercontent.com/u/545988?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarSource","html_url":"https://github.com/SonarSource","followers_url":"https://api.github.com/users/SonarSource/followers","following_url":"https://api.github.com/users/SonarSource/following{/other_user}","gists_url":"https://api.github.com/users/SonarSource/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarSource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarSource/subscriptions","organizations_url":"https://api.github.com/users/SonarSource/orgs","repos_url":"https://api.github.com/users/SonarSource/repos","events_url":"https://api.github.com/users/SonarSource/events{/privacy}","received_events_url":"https://api.github.com/users/SonarSource/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/SonarSource/sonarqube","description":"Put your technical debt under control","fork":false,"url":"https://api.github.com/repos/SonarSource/sonarqube","forks_url":"https://api.github.com/repos/SonarSource/sonarqube/forks","keys_url":"https://api.github.com/repos/SonarSource/sonarqube/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SonarSource/sonarqube/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SonarSource/sonarqube/teams","hooks_url":"https://api.github.com/repos/SonarSource/sonarqube/hooks","issue_events_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/events{/number}","events_url":"https://api.github.com/repos/SonarSource/sonarqube/events","assignees_url":"https://api.github.com/repos/SonarSource/sonarqube/assignees{/user}","branches_url":"https://api.github.com/repos/SonarSource/sonarqube/branches{/branch}","tags_url":"https://api.github.com/repos/SonarSource/sonarqube/tags","blobs_url":"https://api.github.com/repos/SonarSource/sonarqube/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SonarSource/sonarqube/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SonarSource/sonarqube/git/refs{/sha}","trees_url":"https://api.github.com/repos/SonarSource/sonarqube/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SonarSource/sonarqube/statuses/{sha}","languages_url":"https://api.github.com/repos/SonarSource/sonarqube/languages","stargazers_url":"https://api.github.com/repos/SonarSource/sonarqube/stargazers","contributors_url":"https://api.github.com/repos/SonarSource/sonarqube/contributors","subscribers_url":"https://api.github.com/repos/SonarSource/sonarqube/subscribers","subscription_url":"https://api.github.com/repos/SonarSource/sonarqube/subscription","commits_url":"https://api.github.com/repos/SonarSource/sonarqube/commits{/sha}","git_commits_url":"https://api.github.com/repos/SonarSource/sonarqube/git/commits{/sha}","comments_url":"https://api.github.com/repos/SonarSource/sonarqube/comments{/number}","issue_comment_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/comments{/number}","contents_url":"https://api.github.com/repos/SonarSource/sonarqube/contents/{+path}","compare_url":"https://api.github.com/repos/SonarSource/sonarqube/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SonarSource/sonarqube/merges","archive_url":"https://api.github.com/repos/SonarSource/sonarqube/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SonarSource/sonarqube/downloads","issues_url":"https://api.github.com/repos/SonarSource/sonarqube/issues{/number}","pulls_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls{/number}","milestones_url":"https://api.github.com/repos/SonarSource/sonarqube/milestones{/number}","notifications_url":"https://api.github.com/repos/SonarSource/sonarqube/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SonarSource/sonarqube/labels{/name}","releases_url":"https://api.github.com/repos/SonarSource/sonarqube/releases{/id}","created_at":"2011-01-05T11:05:17Z","updated_at":"2015-05-11T15:47:20Z","pushed_at":"2015-05-12T08:13:20Z","git_url":"git://github.com/SonarSource/sonarqube.git","ssh_url":"git@github.com:SonarSource/sonarqube.git","clone_url":"https://github.com/SonarSource/sonarqube.git","svn_url":"https://github.com/SonarSource/sonarqube","homepage":"http://www.sonarqube.org","size":668966,"stargazers_count":603,"watchers_count":603,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":332,"mirror_url":null,"open_issues_count":8,"forks":332,"open_issues":8,"watchers":603,"default_branch":"master"}},"base":{"label":"SonarSource:master","ref":"master","sha":"cba928d505985972e13c8e895b490a52702af925","user":{"login":"SonarSource","id":545988,"avatar_url":"https://avatars.githubusercontent.com/u/545988?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarSource","html_url":"https://github.com/SonarSource","followers_url":"https://api.github.com/users/SonarSource/followers","following_url":"https://api.github.com/users/SonarSource/following{/other_user}","gists_url":"https://api.github.com/users/SonarSource/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarSource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarSource/subscriptions","organizations_url":"https://api.github.com/users/SonarSource/orgs","repos_url":"https://api.github.com/users/SonarSource/repos","events_url":"https://api.github.com/users/SonarSource/events{/privacy}","received_events_url":"https://api.github.com/users/SonarSource/received_events","type":"Organization","site_admin":false},"repo":{"id":1222504,"name":"sonarqube","full_name":"SonarSource/sonarqube","owner":{"login":"SonarSource","id":545988,"avatar_url":"https://avatars.githubusercontent.com/u/545988?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarSource","html_url":"https://github.com/SonarSource","followers_url":"https://api.github.com/users/SonarSource/followers","following_url":"https://api.github.com/users/SonarSource/following{/other_user}","gists_url":"https://api.github.com/users/SonarSource/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarSource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarSource/subscriptions","organizations_url":"https://api.github.com/users/SonarSource/orgs","repos_url":"https://api.github.com/users/SonarSource/repos","events_url":"https://api.github.com/users/SonarSource/events{/privacy}","received_events_url":"https://api.github.com/users/SonarSource/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/SonarSource/sonarqube","description":"Put your technical debt under control","fork":false,"url":"https://api.github.com/repos/SonarSource/sonarqube","forks_url":"https://api.github.com/repos/SonarSource/sonarqube/forks","keys_url":"https://api.github.com/repos/SonarSource/sonarqube/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SonarSource/sonarqube/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SonarSource/sonarqube/teams","hooks_url":"https://api.github.com/repos/SonarSource/sonarqube/hooks","issue_events_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/events{/number}","events_url":"https://api.github.com/repos/SonarSource/sonarqube/events","assignees_url":"https://api.github.com/repos/SonarSource/sonarqube/assignees{/user}","branches_url":"https://api.github.com/repos/SonarSource/sonarqube/branches{/branch}","tags_url":"https://api.github.com/repos/SonarSource/sonarqube/tags","blobs_url":"https://api.github.com/repos/SonarSource/sonarqube/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SonarSource/sonarqube/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SonarSource/sonarqube/git/refs{/sha}","trees_url":"https://api.github.com/repos/SonarSource/sonarqube/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SonarSource/sonarqube/statuses/{sha}","languages_url":"https://api.github.com/repos/SonarSource/sonarqube/languages","stargazers_url":"https://api.github.com/repos/SonarSource/sonarqube/stargazers","contributors_url":"https://api.github.com/repos/SonarSource/sonarqube/contributors","subscribers_url":"https://api.github.com/repos/SonarSource/sonarqube/subscribers","subscription_url":"https://api.github.com/repos/SonarSource/sonarqube/subscription","commits_url":"https://api.github.com/repos/SonarSource/sonarqube/commits{/sha}","git_commits_url":"https://api.github.com/repos/SonarSource/sonarqube/git/commits{/sha}","comments_url":"https://api.github.com/repos/SonarSource/sonarqube/comments{/number}","issue_comment_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/comments{/number}","contents_url":"https://api.github.com/repos/SonarSource/sonarqube/contents/{+path}","compare_url":"https://api.github.com/repos/SonarSource/sonarqube/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SonarSource/sonarqube/merges","archive_url":"https://api.github.com/repos/SonarSource/sonarqube/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SonarSource/sonarqube/downloads","issues_url":"https://api.github.com/repos/SonarSource/sonarqube/issues{/number}","pulls_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls{/number}","milestones_url":"https://api.github.com/repos/SonarSource/sonarqube/milestones{/number}","notifications_url":"https://api.github.com/repos/SonarSource/sonarqube/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SonarSource/sonarqube/labels{/name}","releases_url":"https://api.github.com/repos/SonarSource/sonarqube/releases{/id}","created_at":"2011-01-05T11:05:17Z","updated_at":"2015-05-11T15:47:20Z","pushed_at":"2015-05-12T08:13:20Z","git_url":"git://github.com/SonarSource/sonarqube.git","ssh_url":"git@github.com:SonarSource/sonarqube.git","clone_url":"https://github.com/SonarSource/sonarqube.git","svn_url":"https://github.com/SonarSource/sonarqube","homepage":"http://www.sonarqube.org","size":668966,"stargazers_count":603,"watchers_count":603,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":332,"mirror_url":null,"open_issues_count":8,"forks":332,"open_issues":8,"watchers":603,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/299"},"html":{"href":"https://github.com/SonarSource/sonarqube/pull/299"},"issue":{"href":"https://api.github.com/repos/SonarSource/sonarqube/issues/299"},"comments":{"href":"https://api.github.com/repos/SonarSource/sonarqube/issues/299/comments"},"review_comments":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/299/comments"},"review_comment":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/299/commits"},"statuses":{"href":"https://api.github.com/repos/SonarSource/sonarqube/statuses/aebb914e37c8b3934f63dadd68ea92c06c81bf1c"}}}}