Browse Source

remove dead code: RubyUtils

tags/8.0
Sébastien Lesaint 4 years ago
parent
commit
036ff26ff5

+ 0
- 176
server/sonar-server/src/main/java/org/sonar/server/util/RubyUtils.java View File

@@ -1,176 +0,0 @@
/*
* SonarQube
* Copyright (C) 2009-2019 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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.util;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.utils.DateUtils;

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import java.util.Date;
import java.util.List;

/**
* @since 3.6
*/
public class RubyUtils {

private RubyUtils() {
// only static methods
}

@CheckForNull
public static List<String> toStrings(@Nullable Object o) {
List<String> result = null;
if (o != null) {
if (o instanceof List) {
// assume that it contains only strings
result = (List) o;
} else if (o instanceof CharSequence) {
result = Lists.newArrayList(Splitter.on(',').omitEmptyStrings().split((CharSequence) o));
}
}
return result;
}

@CheckForNull
public static <E extends Enum<E>> List<E> toEnums(@Nullable Object o, Class<E> enumClass) {
if (o == null) {
return null;
}
List<E> result = Lists.newArrayList();
if (o instanceof List) {
for (String s : (List<String>) o) {
result.add(Enum.valueOf(enumClass, s));
}
} else if (o instanceof CharSequence) {
for (String s : Splitter.on(',').omitEmptyStrings().split((CharSequence) o)) {
result.add(Enum.valueOf(enumClass, s));
}
} else {
throw new IllegalArgumentException("Unsupported type: " + o.getClass());
}
return result;
}

@CheckForNull
public static Integer toInteger(@Nullable Object o) {
if (o == null) {
return null;
}
if (o instanceof Integer) {
return (Integer) o;
}
if (o instanceof Long) {
return Ints.checkedCast((Long) o);
}
if (o instanceof String) {
if (StringUtils.isBlank((String) o)) {
return null;
}
return Integer.parseInt((String) o);
}
throw new IllegalArgumentException("Unsupported type for integer: " + o.getClass());
}

@CheckForNull
public static Double toDouble(@Nullable Object o) {
if (o == null) {
return null;
}
if (o instanceof Double) {
return (Double) o;
}
if (o instanceof Integer) {
return ((Integer) o).doubleValue();
}
if (o instanceof Long) {
return ((Long) o).doubleValue();
}
if (o instanceof String) {
if (StringUtils.isBlank((String) o)) {
return null;
}
return Double.parseDouble((String) o);
}
throw new IllegalArgumentException("Unsupported type for double: " + o.getClass());
}

@CheckForNull
public static Date toDate(@Nullable Object o) {
if (o == null) {
return null;
}
if (o instanceof Date) {
return (Date) o;
}
if (o instanceof String) {
if (StringUtils.isBlank((String) o)) {
return null;
}
Date date = DateUtils.parseDateTimeQuietly((String) o);
if (date != null) {
return date;
}
return DateUtils.parseDate((String) o);
}
throw new IllegalArgumentException("Unsupported type for date: " + o.getClass());
}

@CheckForNull
public static Boolean toBoolean(@Nullable Object o) {
if (o == null) {
return null;
}
if (o instanceof Boolean) {
return (Boolean) o;
}
if (o instanceof String) {
if (StringUtils.isBlank((String) o)) {
return null;
}
return Boolean.parseBoolean((String) o);
}
throw new IllegalArgumentException("Unsupported type for boolean: " + o.getClass());
}

@CheckForNull
public static Long toLong(@Nullable Object o) {
if (o == null) {
return null;
}
if (o instanceof Integer) {
return ((Integer) o).longValue();
}
if (o instanceof Long) {
return (Long) o;
}
if (o instanceof String) {
if (StringUtils.isBlank((String) o)) {
return null;
}
return Long.parseLong((String) o);
}
throw new IllegalArgumentException("Unsupported type for long: " + o.getClass());
}
}

+ 0
- 160
server/sonar-server/src/test/java/org/sonar/server/util/RubyUtilsTest.java View File

@@ -1,160 +0,0 @@
/*
* SonarQube
* Copyright (C) 2009-2019 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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.util;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.utils.DateUtils;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

public class RubyUtilsTest {

@Rule
public ExpectedException throwable = ExpectedException.none();

@Test
public void toStrings() {
assertThat(RubyUtils.toStrings(null)).isNull();
assertThat(RubyUtils.toStrings("")).isEmpty();
assertThat(RubyUtils.toStrings("foo")).containsOnly("foo");
assertThat(RubyUtils.toStrings("foo,bar")).containsOnly("foo", "bar");
assertThat(RubyUtils.toStrings(asList("foo", "bar"))).containsOnly("foo", "bar");
}

@Test
public void toEnums() {
assertThat(RubyUtils.toEnums(null, RuleStatus.class)).isNull();
assertThat(RubyUtils.toEnums("", RuleStatus.class)).isEmpty();
assertThat(RubyUtils.toEnums("BETA", RuleStatus.class)).containsOnly(RuleStatus.BETA);
assertThat(RubyUtils.toEnums("BETA,READY", RuleStatus.class)).containsOnly(RuleStatus.BETA, RuleStatus.READY);
assertThat(RubyUtils.toEnums(asList("BETA", "READY"), RuleStatus.class)).containsOnly(RuleStatus.BETA, RuleStatus.READY);
try {
RubyUtils.toEnums("xxx", RuleStatus.class);
fail();
} catch (IllegalArgumentException e) {
// success
}
try {
RubyUtils.toEnums(1, RuleStatus.class);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Unsupported type: class java.lang.Integer");
}
}

@Test
public void toInteger() {
assertThat(RubyUtils.toInteger(null)).isNull();
assertThat(RubyUtils.toInteger("")).isNull();
assertThat(RubyUtils.toInteger(" ")).isNull();
assertThat(RubyUtils.toInteger("123")).isEqualTo(123);
assertThat(RubyUtils.toInteger(123)).isEqualTo(123);
assertThat(RubyUtils.toInteger(123L)).isEqualTo(123);
}

@Test
public void toInteger_unexpected_class() {
throwable.expect(IllegalArgumentException.class);

RubyUtils.toInteger(1.2);
}

@Test
public void toDouble() {
assertThat(RubyUtils.toDouble(null)).isNull();
assertThat(RubyUtils.toDouble("")).isNull();
assertThat(RubyUtils.toDouble(" ")).isNull();
assertThat(RubyUtils.toDouble("123")).isEqualTo(123.0);
assertThat(RubyUtils.toDouble("3.14")).isEqualTo(3.14);
assertThat(RubyUtils.toDouble(3.14)).isEqualTo(3.14);
assertThat(RubyUtils.toDouble(123)).isEqualTo(123.0);
assertThat(RubyUtils.toDouble(123L)).isEqualTo(123.0);
}

@Test
public void toDouble_unexpected_class() {
throwable.expect(IllegalArgumentException.class);

RubyUtils.toDouble(true);
}

@Test
public void toBoolean() {
assertThat(RubyUtils.toBoolean(null)).isNull();
assertThat(RubyUtils.toBoolean("")).isNull();
assertThat(RubyUtils.toBoolean(" ")).isNull();
assertThat(RubyUtils.toBoolean("true")).isTrue();
assertThat(RubyUtils.toBoolean(true)).isTrue();
assertThat(RubyUtils.toBoolean("false")).isFalse();
assertThat(RubyUtils.toBoolean(false)).isFalse();
}

@Test
public void toBoolean_unexpected_class() {
throwable.expect(IllegalArgumentException.class);

RubyUtils.toBoolean(333);
}

@Test
public void toDate() {
assertThat(RubyUtils.toDate(null)).isNull();
assertThat(RubyUtils.toDate("")).isNull();
assertThat(RubyUtils.toDate(" ")).isNull();
assertThat(RubyUtils.toDate("2013-01-18").getDate()).isEqualTo(18);
assertThat(RubyUtils.toDate("2013-01-18T15:38:19+0200").getDate()).isEqualTo(18);
assertThat(RubyUtils.toDate("2013-01-18T15:38:19+0200").getMinutes()).isEqualTo(38);
assertThat(RubyUtils.toDate(DateUtils.parseDate("2013-01-18")).getDate()).isEqualTo(18);
}

@Test
public void toDate_bad_format() {
throwable.expect(RuntimeException.class);

RubyUtils.toDate("01/02/2013");
}

@Test
public void toDate_unexpected_class() {
throwable.expect(IllegalArgumentException.class);

RubyUtils.toDate(333);
}

@Test
public void toLong() {
assertThat(RubyUtils.toLong(null)).isNull();
assertThat(RubyUtils.toLong(2)).isEqualTo(2L);
assertThat(RubyUtils.toLong(3L)).isEqualTo(3L);
assertThat(RubyUtils.toLong("4")).isEqualTo(4L);
}

@Test
public void toLong_unexpected_class() {
throwable.expect(IllegalArgumentException.class);

RubyUtils.toLong(false);
}
}

Loading…
Cancel
Save