]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3016 More work on extension point for filter template
authorDavid Gageot <david@gageot.net>
Tue, 22 May 2012 08:51:06 +0000 (10:51 +0200)
committerDavid Gageot <david@gageot.net>
Tue, 22 May 2012 08:51:06 +0000 (10:51 +0200)
Added mappers and dots for criteria and fiter_columns

16 files changed:
sonar-core/src/main/java/org/sonar/core/filter/CriteriaDto.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/filter/CriteriaMapper.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/filter/FilterColumnDto.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/filter/FilterColumnMapper.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/filter/FilterDao.java
sonar-core/src/main/java/org/sonar/core/filter/FilterDto.java
sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java
sonar-core/src/main/resources/org/sonar/core/filter/CriteriaMapper-oracle.xml [new file with mode: 0644]
sonar-core/src/main/resources/org/sonar/core/filter/CriteriaMapper.xml [new file with mode: 0644]
sonar-core/src/main/resources/org/sonar/core/filter/FilterColumnMapper-oracle.xml [new file with mode: 0644]
sonar-core/src/main/resources/org/sonar/core/filter/FilterColumnMapper.xml [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/filters/FilterDaoTest.java
sonar-core/src/test/resources/org/sonar/core/filters/FilterDaoTest/shouldInsert-result.xml
sonar-plugin-api/src/main/java/org/sonar/api/web/Filter.java
sonar-server/src/main/java/org/sonar/server/startup/RegisterNewFilters.java
sonar-server/src/test/java/org/sonar/server/startup/RegisterNewFiltersTest.java

diff --git a/sonar-core/src/main/java/org/sonar/core/filter/CriteriaDto.java b/sonar-core/src/main/java/org/sonar/core/filter/CriteriaDto.java
new file mode 100644 (file)
index 0000000..4d9aaa0
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.core.filter;
+
+/**
+ * @since 3.1
+ */
+public final class CriteriaDto {
+  private Long id;
+  private Long filterId;
+  private String family;
+  private String key;
+  private String operator;
+  private Float value;
+  private String textValue;
+  private Boolean variation;
+
+  public Long getId() {
+    return id;
+  }
+
+  public CriteriaDto setId(Long id) {
+    this.id = id;
+    return this;
+  }
+
+  public Long getFilterId() {
+    return filterId;
+  }
+
+  public CriteriaDto setFilterId(Long filterId) {
+    this.filterId = filterId;
+    return this;
+  }
+
+  public String getFamily() {
+    return family;
+  }
+
+  public CriteriaDto setFamily(String family) {
+    this.family = family;
+    return this;
+  }
+
+  public String getKey() {
+    return key;
+  }
+
+  public CriteriaDto setKey(String key) {
+    this.key = key;
+    return this;
+  }
+
+  public String getOperator() {
+    return operator;
+  }
+
+  public CriteriaDto setOperator(String operator) {
+    this.operator = operator;
+    return this;
+  }
+
+  public Float getValue() {
+    return value;
+  }
+
+  public CriteriaDto setValue(Float value) {
+    this.value = value;
+    return this;
+  }
+
+  public String getTextValue() {
+    return textValue;
+  }
+
+  public CriteriaDto setTextValue(String textValue) {
+    this.textValue = textValue;
+    return this;
+  }
+
+  public Boolean getVariation() {
+    return variation;
+  }
+
+  public CriteriaDto setVariation(Boolean variation) {
+    this.variation = variation;
+    return this;
+  }
+}
diff --git a/sonar-core/src/main/java/org/sonar/core/filter/CriteriaMapper.java b/sonar-core/src/main/java/org/sonar/core/filter/CriteriaMapper.java
new file mode 100644 (file)
index 0000000..9d21cd6
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.core.filter;
+
+/**
+ * @since 3.1
+ */
+public interface CriteriaMapper {
+  void insert(CriteriaDto criteriaDto);
+}
diff --git a/sonar-core/src/main/java/org/sonar/core/filter/FilterColumnDto.java b/sonar-core/src/main/java/org/sonar/core/filter/FilterColumnDto.java
new file mode 100644 (file)
index 0000000..e7141a5
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.core.filter;
+
+/**
+ * @since 3.1
+ */
+public final class FilterColumnDto {
+  private Long id;
+  private Long filterId;
+  private String family;
+  private String key;
+  private String sortDirection;
+  private Long orderIndex;
+  private Boolean variation;
+
+  public Long getId() {
+    return id;
+  }
+
+  public FilterColumnDto setId(Long id) {
+    this.id = id;
+    return this;
+  }
+
+  public Long getFilterId() {
+    return filterId;
+  }
+
+  public FilterColumnDto setFilterId(Long filterId) {
+    this.filterId = filterId;
+    return this;
+  }
+
+  public String getFamily() {
+    return family;
+  }
+
+  public FilterColumnDto setFamily(String family) {
+    this.family = family;
+    return this;
+  }
+
+  public String getKey() {
+    return key;
+  }
+
+  public FilterColumnDto setKey(String key) {
+    this.key = key;
+    return this;
+  }
+
+  public String getSortDirection() {
+    return sortDirection;
+  }
+
+  public FilterColumnDto setSortDirection(String sortDirection) {
+    this.sortDirection = sortDirection;
+    return this;
+  }
+
+  public Long getOrderIndex() {
+    return orderIndex;
+  }
+
+  public FilterColumnDto setOrderIndex(Long orderIndex) {
+    this.orderIndex = orderIndex;
+    return this;
+  }
+
+  public Boolean getVariation() {
+    return variation;
+  }
+
+  public FilterColumnDto setVariation(Boolean variation) {
+    this.variation = variation;
+    return this;
+  }
+}
diff --git a/sonar-core/src/main/java/org/sonar/core/filter/FilterColumnMapper.java b/sonar-core/src/main/java/org/sonar/core/filter/FilterColumnMapper.java
new file mode 100644 (file)
index 0000000..b5cc739
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.core.filter;
+
+/**
+ * @since 3.1
+ */
+public interface FilterColumnMapper {
+  void insert(FilterColumnDto filterColumnDto);
+}
index be2f7b75504f653a57e8066aadb6d7602b748294..809d91315b1d93931c230c284bdf184e7fa7cdfc 100644 (file)
@@ -46,9 +46,19 @@ public class FilterDao implements BatchComponent, ServerComponent {
 
   public void insert(FilterDto filterDto) {
     SqlSession session = mybatis.openSession();
-    FilterMapper mapper = session.getMapper(FilterMapper.class);
+    FilterMapper filterMapper = session.getMapper(FilterMapper.class);
+    CriteriaMapper criteriaMapper = session.getMapper(CriteriaMapper.class);
+    FilterColumnMapper columnMapper = session.getMapper(FilterColumnMapper.class);
     try {
-      mapper.insert(filterDto);
+      filterMapper.insert(filterDto);
+      for (CriteriaDto criteriaDto : filterDto.getCriterias()) {
+        criteriaDto.setFilterId(filterDto.getId());
+        criteriaMapper.insert(criteriaDto);
+      }
+      for (FilterColumnDto filterColumnDto : filterDto.getColumns()) {
+        filterColumnDto.setFilterId(filterDto.getId());
+        columnMapper.insert(filterColumnDto);
+      }
       session.commit();
     } finally {
       MyBatis.closeQuietly(session);
index 1075788ae1abcaf722f4fe0867b993d7a30c5f55..f37abd80254f63869d2138da9ac2a84bf36b4bd3 100644 (file)
  */
 package org.sonar.core.filter;
 
-import com.google.common.base.Objects;
-import org.apache.commons.lang.builder.ToStringBuilder;
-import org.apache.commons.lang.builder.ToStringStyle;
+import com.google.common.collect.Lists;
+
+import java.util.Collection;
+import java.util.List;
 
 /**
  * @since 3.1
@@ -30,12 +31,14 @@ public final class FilterDto {
   private Long id;
   private String name;
   private Long userId;
-  private boolean shared;
-  private boolean favourites;
+  private Boolean shared;
+  private Boolean favourites;
   private Long resourceId;
   private String defaultView;
   private Long pageSize;
   private Long periodIndex;
+  private List<CriteriaDto> criteriaDtos = Lists.newArrayList();
+  private List<FilterColumnDto> filterColumnDtos = Lists.newArrayList();
 
   public Long getId() {
     return id;
@@ -64,20 +67,20 @@ public final class FilterDto {
     return this;
   }
 
-  public boolean isShared() {
+  public Boolean isShared() {
     return shared;
   }
 
-  public FilterDto setShared(boolean shared) {
+  public FilterDto setShared(Boolean shared) {
     this.shared = shared;
     return this;
   }
 
-  public boolean isFavourites() {
+  public Boolean isFavourites() {
     return favourites;
   }
 
-  public FilterDto setFavourites(boolean favourites) {
+  public FilterDto setFavourites(Boolean favourites) {
     this.favourites = favourites;
     return this;
   }
@@ -118,35 +121,21 @@ public final class FilterDto {
     return this;
   }
 
-
-  @Override
-  public String toString() {
-    return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
+  public Collection<CriteriaDto> getCriterias() {
+    return criteriaDtos;
   }
 
-  @Override
-  public boolean equals(Object o) {
-    if (this == o) {
-      return true;
-    }
-    if (o == null || getClass() != o.getClass()) {
-      return false;
-    }
+  public FilterDto addCriteria(CriteriaDto criteriaDto) {
+    criteriaDtos.add(criteriaDto);
+    return this;
+  }
 
-    FilterDto other = (FilterDto) o;
-    return Objects.equal(id, other.id) &&
-      Objects.equal(name, other.name) &&
-      Objects.equal(userId, other.userId) &&
-      Objects.equal(shared, other.shared) &&
-      Objects.equal(favourites, other.favourites) &&
-      Objects.equal(resourceId, other.resourceId) &&
-      Objects.equal(defaultView, other.defaultView) &&
-      Objects.equal(pageSize, other.pageSize) &&
-      Objects.equal(periodIndex, other.periodIndex);
+  public Collection<FilterColumnDto> getColumns() {
+    return filterColumnDtos;
   }
 
-  @Override
-  public int hashCode() {
-    return Objects.hashCode(id);
+  public FilterDto addColumn(FilterColumnDto filterColumnDto) {
+    filterColumnDtos.add(filterColumnDto);
+    return this;
   }
 }
index 7c71f71ebc1485951f909dabf8ddecc40c819297..7847470ddf3b7ef71b4de663b5248d49bdbcbf6e 100644 (file)
  */
 package org.sonar.core.persistence;
 
-import org.sonar.core.filter.FilterDto;
-import org.sonar.core.filter.FilterMapper;
-
-
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.ibatis.builder.xml.XMLMapperBuilder;
@@ -47,6 +43,12 @@ import org.sonar.core.dashboard.WidgetPropertyDto;
 import org.sonar.core.dashboard.WidgetPropertyMapper;
 import org.sonar.core.duplication.DuplicationMapper;
 import org.sonar.core.duplication.DuplicationUnitDto;
+import org.sonar.core.filter.CriteriaDto;
+import org.sonar.core.filter.CriteriaMapper;
+import org.sonar.core.filter.FilterColumnDto;
+import org.sonar.core.filter.FilterColumnMapper;
+import org.sonar.core.filter.FilterDto;
+import org.sonar.core.filter.FilterMapper;
 import org.sonar.core.properties.PropertiesMapper;
 import org.sonar.core.properties.PropertyDto;
 import org.sonar.core.purge.PurgeMapper;
@@ -91,6 +93,8 @@ public class MyBatis implements BatchComponent, ServerComponent {
     loadAlias(conf, "ActiveDashboard", ActiveDashboardDto.class);
     loadAlias(conf, "Author", AuthorDto.class);
     loadAlias(conf, "Filter", FilterDto.class);
+    loadAlias(conf, "Criteria", CriteriaDto.class);
+    loadAlias(conf, "FilterColumn", FilterColumnDto.class);
     loadAlias(conf, "Dashboard", DashboardDto.class);
     loadAlias(conf, "DuplicationUnit", DuplicationUnitDto.class);
     loadAlias(conf, "LoadedTemplate", LoadedTemplateDto.class);
@@ -109,6 +113,8 @@ public class MyBatis implements BatchComponent, ServerComponent {
     loadMapper(conf, ActiveDashboardMapper.class);
     loadMapper(conf, AuthorMapper.class);
     loadMapper(conf, FilterMapper.class);
+    loadMapper(conf, CriteriaMapper.class);
+    loadMapper(conf, FilterColumnMapper.class);
     loadMapper(conf, DashboardMapper.class);
     loadMapper(conf, DuplicationMapper.class);
     loadMapper(conf, LoadedTemplateMapper.class);
diff --git a/sonar-core/src/main/resources/org/sonar/core/filter/CriteriaMapper-oracle.xml b/sonar-core/src/main/resources/org/sonar/core/filter/CriteriaMapper-oracle.xml
new file mode 100644 (file)
index 0000000..257e54e
--- /dev/null
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="org.sonar.core.filter.CriteriaMapper">
+
+  <insert id="insert" parameterType="Criteria" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
+    INSERT INTO criteria (id, filter_id, family, kee, operator, value, text_value, variation)
+    VALUES (criteria_seq.NEXTVAL, #{filterId, jdbcType=INTEGER}, #{family, jdbcType=VARCHAR}, #{key, jdbcType=VARCHAR}, #{operator, jdbcType=VARCHAR}, #{value, jdbcType=FLOAT},
+     #{textValue, jdbcType=VARCHAR}, #{variation})
+  </insert>
+
+</mapper>
diff --git a/sonar-core/src/main/resources/org/sonar/core/filter/CriteriaMapper.xml b/sonar-core/src/main/resources/org/sonar/core/filter/CriteriaMapper.xml
new file mode 100644 (file)
index 0000000..1201f28
--- /dev/null
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="org.sonar.core.filter.CriteriaMapper">
+
+  <insert id="insert" parameterType="Criteria" useGeneratedKeys="true" keyProperty="id">
+    INSERT INTO criteria (filter_id, family, kee, operator, value, text_value, variation)
+    VALUES (#{filterId, jdbcType=INTEGER}, #{family, jdbcType=VARCHAR}, #{key, jdbcType=VARCHAR}, #{operator, jdbcType=VARCHAR}, #{value, jdbcType=FLOAT},
+     #{textValue, jdbcType=VARCHAR}, #{variation})
+  </insert>
+
+</mapper>
diff --git a/sonar-core/src/main/resources/org/sonar/core/filter/FilterColumnMapper-oracle.xml b/sonar-core/src/main/resources/org/sonar/core/filter/FilterColumnMapper-oracle.xml
new file mode 100644 (file)
index 0000000..e770500
--- /dev/null
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="org.sonar.core.filter.FilterColumnMapper">
+
+  <insert id="insert" parameterType="FilterColumn" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
+    INSERT INTO filter_columns (id, filter_id, family, kee, sort_direction, order_index, variation)
+    VALUES (filter_columns_seq.NEXTVAL, #{filterId, jdbcType=INTEGER}, #{family, jdbcType=VARCHAR}, #{key, jdbcType=VARCHAR}, #{sortDirection, jdbcType=VARCHAR},
+     #{orderIndex, jdbcType=INTEGER}, #{variation})
+  </insert>
+
+</mapper>
diff --git a/sonar-core/src/main/resources/org/sonar/core/filter/FilterColumnMapper.xml b/sonar-core/src/main/resources/org/sonar/core/filter/FilterColumnMapper.xml
new file mode 100644 (file)
index 0000000..a7f7146
--- /dev/null
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="org.sonar.core.filter.FilterColumnMapper">
+
+  <insert id="insert" parameterType="FilterColumn" useGeneratedKeys="true" keyProperty="id">
+    INSERT INTO filter_columns (filter_id, family, kee, sort_direction, order_index, variation)
+    VALUES (#{filterId, jdbcType=INTEGER}, #{family, jdbcType=VARCHAR}, #{key, jdbcType=VARCHAR}, #{sortDirection, jdbcType=VARCHAR},
+     #{orderIndex, jdbcType=INTEGER}, #{variation})
+  </insert>
+
+</mapper>
index 3ba871e4ba81f95e82fb98e65ee5de07d7a4ebc4..14460634ef593b79b126a4c82413ef316d1459b5 100644 (file)
  */
 package org.sonar.core.filters;
 
+import org.sonar.core.filter.FilterColumnDto;
+
+import org.sonar.core.filter.CriteriaDto;
+
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.core.filter.FilterDao;
@@ -60,8 +64,25 @@ public class FilterDaoTest extends DaoTestCase {
     filterDto.setPageSize(10L);
     filterDto.setPeriodIndex(1L);
 
+    CriteriaDto criteriaDto = new CriteriaDto();
+    criteriaDto.setFamily("family");
+    criteriaDto.setKey("key");
+    criteriaDto.setOperator("=");
+    criteriaDto.setValue(1.5f);
+    criteriaDto.setTextValue("1.5");
+    criteriaDto.setVariation(true);
+    filterDto.addCriteria(criteriaDto);
+
+    FilterColumnDto filterColumnDto = new FilterColumnDto();
+    filterColumnDto.setFamily("family");
+    filterColumnDto.setKey("key");
+    filterColumnDto.setSortDirection("ASC");
+    filterColumnDto.setOrderIndex(2L);
+    filterColumnDto.setVariation(true);
+    filterDto.addColumn(filterColumnDto);
+
     dao.insert(filterDto);
 
-    checkTables("shouldInsert", "filters");
+    checkTables("shouldInsert", "filters", "criteria", "filter_columns");
   }
 }
\ No newline at end of file
index 5bef3836c4bb0af63b621ae1dff15ba85c7e4bde..2164c2bedce9aa81e9befcfbd0fea1dca6249eaf 100644 (file)
     period_index="1"
     />
 
+  <criteria
+    id="1"
+    filter_id="1"
+    family="family"
+    kee="key"
+    operator="="
+    value="1.5"
+    text_value="1.5"
+    variation="[true]"
+    />
+
+  <filter_columns
+    id="1"
+    filter_id="1"
+    family="family"
+    kee="key"
+    sort_direction="ASC"
+    order_index="2"
+    variation="[true]"
+    />
+
 </dataset>
index 5c65c30febf074ff2d6db43cf271c082078235ef..ef395f6b71f523c0e1fd0fd9bf6c9f82e334aa8b 100644 (file)
@@ -27,10 +27,41 @@ package org.sonar.api.web;
  * @since 3.1
  */
 public class Filter {
+  // Criteria
+  // Columns
+  //
+  private boolean shared;
+  private boolean favouritesOnly;
+  private String defaultPeriod;
+
   private Filter() {
     // The factory method should be used
   }
 
+  public boolean isShared() {
+    return shared;
+  }
+
+  public void setShared(boolean shared) {
+    this.shared = shared;
+  }
+
+  public boolean isFavouritesOnly() {
+    return favouritesOnly;
+  }
+
+  public void setFavouritesOnly(boolean favouritesOnly) {
+    this.favouritesOnly = favouritesOnly;
+  }
+
+  public String getDefaultPeriod() {
+    return defaultPeriod;
+  }
+
+  public void setDefaultPeriod(String defaultPeriod) {
+    this.defaultPeriod = defaultPeriod;
+  }
+
   /**
    * Creates a new {@link Filter}.
    */
index 4cb131361fb68455ab44e28092ab1ae14b69ec7e..757765d3726cc48da81d99859e649c17cce0cc4f 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.server.startup;
 
 import com.google.common.collect.ImmutableList;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.utils.TimeProfiler;
@@ -31,19 +30,17 @@ import org.sonar.core.filter.FilterDto;
 import org.sonar.core.template.LoadedTemplateDao;
 import org.sonar.core.template.LoadedTemplateDto;
 
-import java.util.Comparator;
 import java.util.List;
 
 /**
  * @since 3.1
  */
 public final class RegisterNewFilters {
-
   private static final Logger LOG = LoggerFactory.getLogger(RegisterNewFilters.class);
 
-  private List<FilterTemplate> filterTemplates;
-  private FilterDao filterDao;
-  private LoadedTemplateDao loadedTemplateDao;
+  private final List<FilterTemplate> filterTemplates;
+  private final FilterDao filterDao;
+  private final LoadedTemplateDao loadedTemplateDao;
 
   /**
    * Constructor used in case there is no {@link FilterTemplate}.
@@ -71,7 +68,7 @@ public final class RegisterNewFilters {
     profiler.stop();
   }
 
-  protected boolean shouldRegister(String filterName) {
+  private boolean shouldRegister(String filterName) {
     return loadedTemplateDao.countByTypeAndKey(LoadedTemplateDto.FILTER_TYPE, filterName) == 0;
   }
 
@@ -87,22 +84,10 @@ public final class RegisterNewFilters {
   }
 
   protected FilterDto createDtoFromExtension(String name, Filter filter) {
-    FilterDto filterDto = new FilterDto();
-    filterDto.setName(name);
-
-    // filterDto.setShared();
-    // filterDto.setFavourites();
-    // filterDto.setResourceId();
-    // filterDto.setDefaultView();
-    // filterDto.setPageSize();
-    // filterDto.setPeriodIndex();
-
-    return filterDto;
-  }
-
-  protected static class FilterDtoComparator implements Comparator<FilterDto> {
-    public int compare(FilterDto f1, FilterDto f2) {
-      return f1.getName().compareToIgnoreCase(f2.getName());
-    }
+    return new FilterDto()
+        .setName(name)
+        .setShared(filter.isShared())
+        .setFavourites(filter.isFavouritesOnly())
+        .setDefaultView(filter.getDefaultPeriod());
   }
-}
\ No newline at end of file
+}
index e1e7c751eef73571bcbe07c6281df9fa845ee8f5..47e6853eb19ba7d4273fae63fd5db807a734bdde 100644 (file)
@@ -21,7 +21,6 @@ package org.sonar.server.startup;
 
 import org.junit.Before;
 import org.junit.Test;
-import org.mockito.Mockito;
 import org.sonar.api.web.Filter;
 import org.sonar.api.web.FilterTemplate;
 import org.sonar.core.filter.FilterDao;
@@ -29,13 +28,12 @@ import org.sonar.core.filter.FilterDto;
 import org.sonar.core.template.LoadedTemplateDao;
 import org.sonar.core.template.LoadedTemplateDto;
 
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
 import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -43,20 +41,22 @@ public class RegisterNewFiltersTest {
   private RegisterNewFilters register;
   private FilterDao filterDao;
   private LoadedTemplateDao loadedTemplateDao;
-  private FilterTemplate fakeFilterTemplate;
+  private FilterTemplate filterTemplate;
 
   @Before
   public void init() {
-    filterDao = Mockito.mock(FilterDao.class);
-    loadedTemplateDao = Mockito.mock(LoadedTemplateDao.class);
-
-    fakeFilterTemplate = new FakeFilter();
+    filterDao = mock(FilterDao.class);
+    loadedTemplateDao = mock(LoadedTemplateDao.class);
+    filterTemplate = mock(FilterTemplate.class);
 
-    register = new RegisterNewFilters(new FilterTemplate[] {fakeFilterTemplate}, filterDao, loadedTemplateDao);
+    register = new RegisterNewFilters(new FilterTemplate[] {filterTemplate}, filterDao, loadedTemplateDao);
   }
 
   @Test
-  public void should_start() {
+  public void should_insert_filters_on_start() {
+    when(loadedTemplateDao.countByTypeAndKey(eq(LoadedTemplateDto.FILTER_TYPE), anyString())).thenReturn(0);
+    when(filterTemplate.createFilter()).thenReturn(Filter.create());
+
     register.start();
 
     verify(filterDao).insert(any(FilterDto.class));
@@ -64,26 +64,29 @@ public class RegisterNewFiltersTest {
   }
 
   @Test
-  public void should_register_filter_if_not_already_loaded() {
-    when(loadedTemplateDao.countByTypeAndKey(LoadedTemplateDto.FILTER_TYPE, "Fake")).thenReturn(0);
-
-    boolean shouldRegister = register.shouldRegister("Fake");
+  public void should_insert_nothing_if_no_template_is_available() {
+    register = new RegisterNewFilters(filterDao, loadedTemplateDao);
+    register.start();
 
-    assertThat(shouldRegister).isTrue();
+    verify(filterDao, never()).insert(any(FilterDto.class));
+    verify(loadedTemplateDao, never()).insert(any(LoadedTemplateDto.class));
   }
 
   @Test
-  public void should_not_register_if_already_loaded() {
-    when(loadedTemplateDao.countByTypeAndKey(LoadedTemplateDto.FILTER_TYPE, "Fake")).thenReturn(1);
+  public void should_insert_nothing_if_templates_are_alreday_loaded() {
+    when(loadedTemplateDao.countByTypeAndKey(eq(LoadedTemplateDto.FILTER_TYPE), anyString())).thenReturn(1);
 
-    boolean shouldRegister = register.shouldRegister("Fake");
+    register.start();
 
-    assertThat(shouldRegister).isFalse();
+    verify(filterDao, never()).insert(any(FilterDto.class));
+    verify(loadedTemplateDao, never()).insert(any(LoadedTemplateDto.class));
   }
 
   @Test
   public void should_register_filter() {
-    FilterDto filterDto = register.register("Fake", fakeFilterTemplate.createFilter());
+    when(filterTemplate.createFilter()).thenReturn(Filter.create());
+
+    FilterDto filterDto = register.register("Fake", filterTemplate.createFilter());
 
     assertThat(filterDto).isNotNull();
     verify(filterDao).insert(filterDto);
@@ -91,34 +94,30 @@ public class RegisterNewFiltersTest {
   }
 
   @Test
-  public void should_create_dto_from_extension() {
-    FilterDto dto = register.createDtoFromExtension("Fake", fakeFilterTemplate.createFilter());
+  public void should_not_recreate_filter() {
+    when(filterDao.findFilter("Fake")).thenReturn(new FilterDto());
 
-    assertThat(dto.getUserId()).isNull();
-    assertThat(dto.getName()).isEqualTo("Fake");
+    FilterDto filterDto = register.register("Fake", null);
+
+    assertThat(filterDto).isNull();
+    verify(filterDao, never()).insert(filterDto);
+    verify(loadedTemplateDao).insert(eq(new LoadedTemplateDto("Fake", LoadedTemplateDto.FILTER_TYPE)));
   }
 
   @Test
-  public void should_compare_filter() {
-    FilterDto f1 = new FilterDto().setName("foo");
-    FilterDto f2 = new FilterDto().setName("Bar");
-
-    List<FilterDto> filterDtos = Arrays.asList(f1, f2);
-    Collections.sort(filterDtos, new RegisterNewFilters.FilterDtoComparator());
+  public void should_create_dto_from_extension() {
+    Filter filter = Filter.create();
+    filter.setShared(true);
+    filter.setFavouritesOnly(false);
+    filter.setDefaultPeriod("list");
+    when(filterTemplate.createFilter()).thenReturn(filter);
 
-    assertThat(filterDtos).onProperty("name").containsExactly("Bar", "foo");
-  }
+    FilterDto dto = register.createDtoFromExtension("Fake", filterTemplate.createFilter());
 
-  public class FakeFilter extends FilterTemplate {
-    @Override
-    public String getName() {
-      return "Fake";
-    }
-
-    @Override
-    public Filter createFilter() {
-      Filter filter = Filter.create();
-      return filter;
-    }
+    assertThat(dto.getUserId()).isNull();
+    assertThat(dto.getName()).isEqualTo("Fake");
+    assertThat(dto.isShared()).isTrue();
+    assertThat(dto.isFavourites()).isFalse();
+    assertThat(dto.getDefaultView()).isEqualTo("list");
   }
 }