From faa74b33eb99d62ad8c6ee760a9367ec62b8dc00 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Thu, 3 Dec 2015 18:37:23 +0100 Subject: SONAR-7049 check max length of rule/rule param fields --- .../test/java/org/sonar/db/rule/RuleDtoTest.java | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'sonar-db/src/test/java') diff --git a/sonar-db/src/test/java/org/sonar/db/rule/RuleDtoTest.java b/sonar-db/src/test/java/org/sonar/db/rule/RuleDtoTest.java index 7c170e7c8df..c6ac840691c 100644 --- a/sonar-db/src/test/java/org/sonar/db/rule/RuleDtoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/rule/RuleDtoTest.java @@ -19,8 +19,14 @@ */ package org.sonar.db.rule; +import com.google.common.collect.ImmutableSet; +import java.util.Collections; +import java.util.Set; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; +import static org.apache.commons.lang.StringUtils.repeat; import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.db.rule.RuleDto.DISABLED_CHARACTERISTIC_ID; @@ -29,6 +35,9 @@ public class RuleDtoTest { public static final int FAKE_SUB_CHAR_1 = 27; public static final int FAKE_SUB_CHAR_2 = 42; + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Test public void effective_sub_characteristic_id() { RuleDto dto = new RuleDto(); @@ -57,4 +66,35 @@ public class RuleDtoTest { dto.setSubCharacteristicId(DISABLED_CHARACTERISTIC_ID).setDefaultSubCharacteristicId(FAKE_SUB_CHAR_2); assertThat(dto.getEffectiveSubCharacteristicId()).isNull(); } + + @Test + public void fail_if_key_is_too_long() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Rule key is too long: "); + + new RuleDto().setRuleKey(repeat("x", 250)); + } + + @Test + public void fail_if_name_is_too_long() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Rule name is too long: "); + + new RuleDto().setName(repeat("x", 300)); + } + + @Test + public void fail_if_tags_are_too_long() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Rule tags are too long: "); + + Set tags = ImmutableSet.of(repeat("a", 2000), repeat("b", 1000), repeat("c", 2000)); + new RuleDto().setTags(tags); + } + + @Test + public void tags_are_optional() { + RuleDto dto = new RuleDto().setTags(Collections.emptySet()); + assertThat(dto.getTags()).isEmpty(); + } } -- cgit v1.2.3