aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2015-07-05 10:06:55 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-07-06 09:22:44 +0200
commit32af292b745b7226bacc3f34d612437664af0ba3 (patch)
tree538d0b9852eaeb08ad685d426ea61c8170210e40 /sonar-core
parent1df148803610cd54f182b8636f01c0e6ece92b19 (diff)
downloadsonarqube-32af292b745b7226bacc3f34d612437664af0ba3.tar.gz
sonarqube-32af292b745b7226bacc3f34d612437664af0ba3.zip
Move some classes from sonar-server to sonar-db
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/util/ProgressLogger.java113
-rw-r--r--sonar-core/src/main/java/org/sonar/core/util/Slug.java39
-rw-r--r--sonar-core/src/test/java/org/sonar/core/util/ProgressLoggerTest.java66
-rw-r--r--sonar-core/src/test/java/org/sonar/core/util/SlugTest.java39
4 files changed, 257 insertions, 0 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/util/ProgressLogger.java b/sonar-core/src/main/java/org/sonar/core/util/ProgressLogger.java
new file mode 100644
index 00000000000..c77da8eefc9
--- /dev/null
+++ b/sonar-core/src/main/java/org/sonar/core/util/ProgressLogger.java
@@ -0,0 +1,113 @@
+/*
+ * 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.core.util;
+
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.concurrent.atomic.AtomicLong;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
+
+/**
+ * Background thread that logs the state of a counter at fixed intervals.
+ */
+public class ProgressLogger {
+
+ public static final long DEFAULT_PERIOD_MS = 60000L;
+
+ private final Timer timer;
+ private final LoggerTimerTask task;
+ private long periodMs = DEFAULT_PERIOD_MS;
+
+ public ProgressLogger(String threadName, AtomicLong counter, Logger logger) {
+ this.timer = new Timer(threadName);
+ this.task = new LoggerTimerTask(counter, logger);
+ }
+
+ public static ProgressLogger create(Class clazz, AtomicLong counter) {
+ String threadName = String.format("ProgressLogger[%s]", clazz.getSimpleName());
+ Logger logger = Loggers.get(clazz);
+ return new ProgressLogger(threadName, counter, logger);
+ }
+
+ /**
+ * Warning, does not check if already started.
+ */
+ public void start() {
+ // first log after {periodMs} milliseconds
+ timer.schedule(task, periodMs, periodMs);
+ }
+
+ public void stop() {
+ timer.cancel();
+ timer.purge();
+ }
+
+ /**
+ * Default is 1 minute
+ */
+ public ProgressLogger setPeriodMs(long l) {
+ this.periodMs = l;
+ return this;
+ }
+
+ public long getPeriodMs() {
+ return periodMs;
+ }
+
+ /**
+ * For example "issues", "measures", ... Default is "rows".
+ */
+ public ProgressLogger setPluralLabel(String s) {
+ task.pluralLabel = s;
+ return this;
+ }
+
+ public String getPluralLabel() {
+ return task.pluralLabel;
+ }
+
+ public void log() {
+ task.log();
+ }
+
+ private class LoggerTimerTask extends TimerTask {
+ private final AtomicLong counter;
+ private final Logger logger;
+ private String pluralLabel = "rows";
+ private long previousCounter = 0L;
+
+ private LoggerTimerTask(AtomicLong counter, Logger logger) {
+ this.counter = counter;
+ this.logger = logger;
+ }
+
+ @Override
+ public void run() {
+ log();
+ }
+
+ private void log() {
+ long current = counter.get();
+ logger.info(String.format("%d %s processed (%d items/sec)", current, pluralLabel, 1000 * (current - previousCounter) / periodMs));
+ previousCounter = current;
+ }
+ }
+}
diff --git a/sonar-core/src/main/java/org/sonar/core/util/Slug.java b/sonar-core/src/main/java/org/sonar/core/util/Slug.java
new file mode 100644
index 00000000000..47e820d98f5
--- /dev/null
+++ b/sonar-core/src/main/java/org/sonar/core/util/Slug.java
@@ -0,0 +1,39 @@
+/*
+ * 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.core.util;
+
+import java.text.Normalizer;
+import java.util.Locale;
+
+public class Slug {
+
+ private Slug() {
+ }
+
+ public static String slugify(String s) {
+ return Normalizer.normalize(s, Normalizer.Form.NFD)
+ .replaceAll("[^\\p{ASCII}]", "")
+ .replaceAll("[^\\w+]", "-")
+ .replaceAll("\\s+", "-")
+ .replaceAll("[-]+", "-")
+ .replaceAll("^-", "")
+ .replaceAll("-$", "").toLowerCase(Locale.ENGLISH);
+ }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/util/ProgressLoggerTest.java b/sonar-core/src/test/java/org/sonar/core/util/ProgressLoggerTest.java
new file mode 100644
index 00000000000..ecbaee63bef
--- /dev/null
+++ b/sonar-core/src/test/java/org/sonar/core/util/ProgressLoggerTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.core.util;
+
+import java.util.concurrent.atomic.AtomicLong;
+import org.junit.Test;
+import org.sonar.api.utils.log.Logger;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.atLeast;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.startsWith;
+import static org.mockito.Mockito.verify;
+
+public class ProgressLoggerTest {
+
+ @Test(timeout = 1000L)
+ public void log_at_fixed_intervals() throws Exception {
+ Logger logger = mock(Logger.class);
+ AtomicLong counter = new AtomicLong(42L);
+ ProgressLogger progress = new ProgressLogger("ProgressLoggerTest", counter, logger);
+ progress.setPeriodMs(1L);
+ progress.start();
+ Thread.sleep(80L);
+ progress.stop();
+ verify(logger, atLeast(1)).info(startsWith("42 rows processed"));
+
+ // ability to manual log, generally final status
+ counter.incrementAndGet();
+ progress.log();
+ verify(logger).info(startsWith("43 rows processed"));
+ }
+
+ @Test
+ public void create() {
+ ProgressLogger progress = ProgressLogger.create(getClass(), new AtomicLong());
+
+ // default values
+ assertThat(progress.getPeriodMs()).isEqualTo(60000L);
+ assertThat(progress.getPluralLabel()).isEqualTo("rows");
+
+ // override values
+ progress.setPeriodMs(10L);
+ progress.setPluralLabel("issues");
+ assertThat(progress.getPeriodMs()).isEqualTo(10L);
+ assertThat(progress.getPluralLabel()).isEqualTo("issues");
+
+ }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/util/SlugTest.java b/sonar-core/src/test/java/org/sonar/core/util/SlugTest.java
new file mode 100644
index 00000000000..5e45bb86112
--- /dev/null
+++ b/sonar-core/src/test/java/org/sonar/core/util/SlugTest.java
@@ -0,0 +1,39 @@
+/*
+ * 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.core.util;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class SlugTest {
+
+ @Test
+ public void slugify() {
+ assertThat(Slug.slugify("foo")).isEqualTo("foo");
+ assertThat(Slug.slugify(" FOO ")).isEqualTo("foo");
+ assertThat(Slug.slugify("he's here")).isEqualTo("he-s-here");
+ assertThat(Slug.slugify("foo-bar")).isEqualTo("foo-bar");
+ assertThat(Slug.slugify("foo_bar")).isEqualTo("foo_bar");
+ assertThat(Slug.slugify("accents éà")).isEqualTo("accents-ea");
+ assertThat(Slug.slugify("<foo>")).isEqualTo("foo");
+ assertThat(Slug.slugify("<\"foo:\">")).isEqualTo("foo");
+ }
+}