diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-05-28 18:03:37 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-06-01 17:08:29 +0200 |
commit | 375e12fb4ef764a8feaff122a2355b205adf2f2b (patch) | |
tree | d9c330712e8141bf1dd649e05195fc5a120b2620 | |
parent | 6327a718ef8197caec2ea8ced400cd9dae79c2d1 (diff) | |
download | sonarqube-375e12fb4ef764a8feaff122a2355b205adf2f2b.tar.gz sonarqube-375e12fb4ef764a8feaff122a2355b205adf2f2b.zip |
SONAR-6589 EventRepository should store data based on ref
also added missing unit test
3 files changed, 79 insertions, 9 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/event/EventRepositoryImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/event/EventRepositoryImpl.java index b65f94e46f9..f309541e051 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/event/EventRepositoryImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/event/EventRepositoryImpl.java @@ -21,26 +21,20 @@ package org.sonar.server.computation.event; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; -import java.util.Collection; -import java.util.Collections; import org.sonar.server.computation.component.Component; import static java.util.Objects.requireNonNull; public class EventRepositoryImpl implements EventRepository { - private final Multimap<Component, Event> events = HashMultimap.create(); + private final Multimap<Integer, Event> events = HashMultimap.create(); @Override public void add(Component component, Event event) { - events.put(requireNonNull(component), requireNonNull(event)); + events.put(component.getRef(), requireNonNull(event)); } @Override public Iterable<Event> getEvents(Component component) { - Collection<Event> res = this.events.get(component); - if (res == null) { - return Collections.emptySet(); - } - return res; + return this.events.get(component.getRef()); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/event/EventRepositoryImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/event/EventRepositoryImplTest.java new file mode 100644 index 00000000000..354057a95b5 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/event/EventRepositoryImplTest.java @@ -0,0 +1,71 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.computation.event; + +import org.junit.Test; +import org.sonar.server.computation.component.Component; +import org.sonar.server.computation.component.DumbComponent; + +import static org.assertj.core.api.Assertions.assertThat; + +public class EventRepositoryImplTest { + private static final Component COMPONENT_1 = newComponent(1); + private static final Component COMPONENT_2 = newComponent(2); + private static final Event EVENT_1 = Event.createProfile("event_1", null, null); + private static final Event EVENT_2 = Event.createProfile("event_2", null, null); + + private EventRepositoryImpl underTest = new EventRepositoryImpl(); + + @Test + public void getEvents_returns_empty_iterable_when_repository_is_empty() { + assertThat(underTest.getEvents(COMPONENT_1)).isEmpty(); + } + + @Test + public void getEvents_discriminates_per_component() { + underTest.add(COMPONENT_1, EVENT_1); + underTest.add(COMPONENT_2, EVENT_2); + + assertThat(underTest.getEvents(COMPONENT_1)).extracting("name").containsExactly(EVENT_1.getName()); + assertThat(underTest.getEvents(COMPONENT_2)).extracting("name").containsExactly(EVENT_2.getName()); + } + + @Test(expected = NullPointerException.class) + public void add_throws_NPE_if_component_arg_is_null() { + underTest.add(null, EVENT_1); + } + + @Test(expected = NullPointerException.class) + public void add_throws_NPE_if_even_arg_is_null() { + underTest.add(COMPONENT_1, null); + } + + @Test + public void can_add_and_retrieve_many_events_per_component() { + underTest.add(COMPONENT_1, EVENT_1); + underTest.add(COMPONENT_1, EVENT_2); + + assertThat(underTest.getEvents(COMPONENT_1)).extracting("name").containsOnly(EVENT_1.getName(), EVENT_2.getName()); + } + + private static Component newComponent(int i) { + return new DumbComponent(Component.Type.FILE, i, null, null); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/event/EventTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/event/EventTest.java index 4726dd2dfed..b3a25ffa67d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/event/EventTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/event/EventTest.java @@ -57,4 +57,9 @@ public class EventTest { assertThat(event.getDescription()).isEqualTo(SOME_DESCRIPTION); } + @Test + public void same_name_and_category_make_equal_events() { + assertThat(Event.createAlert(SOME_NAME, null, null)).isEqualTo(Event.createAlert(SOME_NAME, null, null)); + + } } |