blob: d3cb927ab026afe7a6a199a54915ba882c6eecb5 [file]
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
public class LabelVoteTest {
@Test
public void parse() {
LabelVote l;
l = LabelVote.parse("Code-Review-2");
assertEquals("Code-Review", l.label());
assertEquals((short) -2, l.value());
l = LabelVote.parse("Code-Review-1");
assertEquals("Code-Review", l.label());
assertEquals((short) -1, l.value());
l = LabelVote.parse("-Code-Review");
assertEquals("Code-Review", l.label());
assertEquals((short) 0, l.value());
l = LabelVote.parse("Code-Review");
assertEquals("Code-Review", l.label());
assertEquals((short) 1, l.value());
l = LabelVote.parse("Code-Review+1");
assertEquals("Code-Review", l.label());
assertEquals((short) 1, l.value());
l = LabelVote.parse("Code-Review+2");
assertEquals("Code-Review", l.label());
assertEquals((short) 2, l.value());
}
@Test
public void format() {
assertEquals("Code-Review-2", LabelVote.parse("Code-Review-2").format());
assertEquals("Code-Review-1", LabelVote.parse("Code-Review-1").format());
assertEquals("-Code-Review", LabelVote.parse("-Code-Review").format());
assertEquals("Code-Review+1", LabelVote.parse("Code-Review+1").format());
assertEquals("Code-Review+2", LabelVote.parse("Code-Review+2").format());
}
@Test
public void parseWithEquals() {
LabelVote l;
l = LabelVote.parseWithEquals("Code-Review=-2");
assertEquals("Code-Review", l.label());
assertEquals((short) -2, l.value());
l = LabelVote.parseWithEquals("Code-Review=-1");
assertEquals("Code-Review", l.label());
assertEquals((short) -1, l.value());
l = LabelVote.parseWithEquals("Code-Review=0");
assertEquals("Code-Review", l.label());
assertEquals((short) 0, l.value());
l = LabelVote.parseWithEquals("Code-Review=1");
assertEquals("Code-Review", l.label());
assertEquals((short) 1, l.value());
l = LabelVote.parseWithEquals("Code-Review=+1");
assertEquals("Code-Review", l.label());
assertEquals((short) 1, l.value());
l = LabelVote.parseWithEquals("Code-Review=2");
assertEquals("Code-Review", l.label());
assertEquals((short) 2, l.value());
l = LabelVote.parseWithEquals("Code-Review=+2");
assertEquals("Code-Review", l.label());
assertEquals((short) 2, l.value());
l = LabelVote.parseWithEquals("R=0");
assertEquals("R", l.label());
assertEquals((short) 0, l.value());
String longName = "A-loooooooooooooooooooooooooooooooooooooooooooooooooong-label";
// Regression test: an old bug passed the string length as a radix to Short#parseShort.
assertTrue(longName.length() > Character.MAX_RADIX);
l = LabelVote.parseWithEquals(longName + "=+1");
assertEquals(longName, l.label());
assertEquals((short) 1, l.value());
assertParseWithEqualsFails(null);
assertParseWithEqualsFails("");
assertParseWithEqualsFails("Code-Review");
assertParseWithEqualsFails("=1");
assertParseWithEqualsFails("=.1");
assertParseWithEqualsFails("=a1");
assertParseWithEqualsFails("=1a");
assertParseWithEqualsFails("=.");
}
@Test
public void formatWithEquals() {
assertEquals("Code-Review=-2", LabelVote.parseWithEquals("Code-Review=-2").formatWithEquals());
assertEquals("Code-Review=-1", LabelVote.parseWithEquals("Code-Review=-1").formatWithEquals());
assertEquals("Code-Review=0", LabelVote.parseWithEquals("Code-Review=0").formatWithEquals());
assertEquals("Code-Review=+1", LabelVote.parseWithEquals("Code-Review=+1").formatWithEquals());
assertEquals("Code-Review=+2", LabelVote.parseWithEquals("Code-Review=+2").formatWithEquals());
}
private void assertParseWithEqualsFails(String value) {
try {
LabelVote.parseWithEquals(value);
fail("expected IllegalArgumentException when parsing \"" + value + "\"");
} catch (IllegalArgumentException e) {
// Expected.
}
}
}