Skip to content

Commit 24d4bf8

Browse files
author
Suszyński Krzysztof
committed
Skip on non-existing issue file
1 parent 1dee9a3 commit 24d4bf8

File tree

5 files changed

+71
-40
lines changed

5 files changed

+71
-40
lines changed

.travis.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ notifications:
77
matrix:
88
include:
99
- jdk: openjdk6
10-
env: JACOCO=true COVERALLS=true
10+
env: JACOCO=true
1111
- jdk: openjdk7
1212
env: JACOCO=true COVERALLS=true
1313
- jdk: oraclejdk7
14-
env: JACOCO=true COVERALLS=true
14+
env: JACOCO=true
1515
- jdk: oraclejdk8
16-
env: JACOCO=true COVERALLS=true
16+
env: JACOCO=true
17+
- jdk: openjdk7
18+
env: JACOCO=true GDMSESSION=sonar
1719
- jdk: openjdk7
18-
env: JACOCO=true GDMSESSION=sonar SONAR_ANALYSIS_MODE=publish
19-
script: mvn clean install -Dsonar.analysis.mode=publish --fail-at-end
20+
env: JACOCO=true SONAR=publish
21+
script: mvn clean install sonar:sonar --fail-at-end
2022
- jdk: openjdk6
2123
env: JACOCO=false
2224
- jdk: openjdk7

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@
322322

323323
<profiles>
324324
<profile>
325-
<id>ci</id>
325+
<id>jacoco</id>
326326
<activation>
327327
<property>
328328
<name>env.JACOCO</name>
@@ -389,6 +389,7 @@
389389
<defaults>
390390
<sonar.issues.file>${sonar.working.directory}/${sonar.report.export.path}
391391
</sonar.issues.file>
392+
<sonar.skip>${sonar.skip}</sonar.skip>
392393
</defaults>
393394
<source>${project.basedir}/src/test/groovy/verify-sonar-issues.groovy</source>
394395
</configuration>

src/main/java/pl/wavesoftware/eid/exceptions/Eid.java

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public class Eid implements Serializable {
4949

5050
private static final int MESSAGE_FORMAT_NUM_SPEC = 2;
5151

52+
private static final String EMPTY_REF = "";
53+
5254
private static String messageFormat = DEFAULT_MESSAGE_FORMAT;
5355

5456
private static UniqIdGenerator uniqIdGenerator = DEFAULT_UNIQ_ID_GENERATOR;
@@ -61,7 +63,7 @@ public class Eid implements Serializable {
6163

6264
private final String ref;
6365

64-
private final Future<String> futureUniqueId;
66+
private String uniqueId;
6567

6668
/**
6769
* Constructor
@@ -70,9 +72,8 @@ public class Eid implements Serializable {
7072
* @param ref an optional reference
7173
*/
7274
public Eid(String id, @Nullable String ref) {
73-
futureUniqueId = new UniqFuture();
7475
this.id = id;
75-
this.ref = ref == null ? "" : ref;
76+
this.ref = ref == null ? EMPTY_REF : ref;
7677
}
7778

7879
/**
@@ -81,7 +82,8 @@ public Eid(String id, @Nullable String ref) {
8182
* @param id the exception id, must be unique developer inserted string, from date
8283
*/
8384
public Eid(String id) {
84-
this(id, null);
85+
this.id = id;
86+
this.ref = EMPTY_REF;
8587
}
8688

8789
/**
@@ -177,9 +179,9 @@ public String makeLogMessage(String logMessageFormat, Object... parameters) {
177179
@Override
178180
public String toString() {
179181
if ("".equals(ref)) {
180-
return String.format(format, id, futureUniqueId.get());
182+
return String.format(format, id, getUniq());
181183
}
182-
return String.format(refFormat, id, ref, futureUniqueId.get());
184+
return String.format(refFormat, id, ref, getUniq());
183185
}
184186

185187
/**
@@ -206,7 +208,10 @@ public String getRef() {
206208
* @return a unique string
207209
*/
208210
public String getUniq() {
209-
return futureUniqueId.get();
211+
if (uniqueId == null) {
212+
uniqueId = uniqIdGenerator.generateUniqId();
213+
}
214+
return uniqueId;
210215
}
211216

212217
private static void validateFormat(String format, int numSpecifiers) {
@@ -240,23 +245,6 @@ public interface UniqIdGenerator {
240245
String generateUniqId();
241246
}
242247

243-
private interface Future<T extends Serializable> extends Serializable {
244-
T get();
245-
}
246-
247-
private static final class UniqFuture implements Future<String> {
248-
private static final long serialVersionUID = 20160325113314L;
249-
private String future;
250-
private UniqFuture() {}
251-
@Override
252-
public String get() {
253-
if (future == null) {
254-
future = uniqIdGenerator.generateUniqId();
255-
}
256-
return future;
257-
}
258-
}
259-
260248
private static final class StdUniqIdGenerator implements UniqIdGenerator {
261249

262250
private static final int BASE36 = 36;

src/test/groovy/verify-sonar-issues.groovy

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import groovy.json.JsonSlurper
22

3-
propertyName = 'sonar.issues.file'
4-
filepath = new File(properties.getAt(propertyName))
3+
String skip = properties.getAt('sonar.skip')
4+
if (skip == 'true') {
5+
log.info('sonar.skip = true: Skipping Sonar issues file analysis.')
6+
return;
7+
}
8+
String issueFile = properties.getAt('sonar.issues.file')
9+
filepath = new File(issueFile)
510
log.info('Sonar Issues file: ' + filepath)
6-
7-
def slurper = new JsonSlurper()
8-
def contents = filepath.getText('UTF-8')
11+
JsonSlurper slurper = new JsonSlurper()
12+
String contents = filepath.getText('UTF-8')
913
def json = slurper.parseText(contents)
1014

1115
def major = 0
@@ -49,4 +53,4 @@ if (red > 0) {
4953
fail(msg)
5054
} else {
5155
log.info('No new issues have been found')
52-
}
56+
}

src/test/java/pl/wavesoftware/eid/exceptions/EidIT.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package pl.wavesoftware.eid.exceptions;
22

3+
import org.junit.Before;
34
import org.junit.ClassRule;
45
import org.junit.Test;
56
import org.junit.rules.RuleChain;
67
import org.openjdk.jmh.annotations.Benchmark;
78
import org.openjdk.jmh.annotations.Mode;
9+
import org.openjdk.jmh.annotations.Scope;
10+
import org.openjdk.jmh.annotations.Setup;
11+
import org.openjdk.jmh.annotations.State;
812
import org.openjdk.jmh.infra.Blackhole;
913
import org.openjdk.jmh.results.RunResult;
1014
import org.openjdk.jmh.runner.Runner;
@@ -16,6 +20,7 @@
1620
import pl.wavesoftware.testing.JavaAgentSkip;
1721
import pl.wavesoftware.testing.JmhCleaner;
1822

23+
import java.text.NumberFormat;
1924
import java.util.Collection;
2025
import java.util.Date;
2126
import java.util.concurrent.TimeUnit;
@@ -38,6 +43,26 @@ public class EidIT {
3843
.outerRule(new JmhCleaner(EidIT.class))
3944
.around(JavaAgentSkip.ifActive());
4045

46+
@Before
47+
public void before() {
48+
printMemory();
49+
}
50+
51+
public static void printMemory() {
52+
Runtime runtime = Runtime.getRuntime();
53+
54+
NumberFormat format = NumberFormat.getInstance();
55+
56+
long maxMemory = runtime.maxMemory();
57+
long allocatedMemory = runtime.totalMemory();
58+
long freeMemory = runtime.freeMemory();
59+
60+
LOG.info("free memory: {} KiB", format.format(freeMemory / 1024));
61+
LOG.info("allocated memory: {} KiB", format.format(allocatedMemory / 1024));
62+
LOG.info("max memory: {} KiB", format.format(maxMemory / 1024));
63+
LOG.info("total free memory: {} KiB", format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024));
64+
}
65+
4166
@Test
4267
public void doBenckmarking() throws Exception {
4368
Options opt = new OptionsBuilder()
@@ -69,7 +94,7 @@ public void doBenckmarking() throws Exception {
6994

7095
String title = "method speed quotient to the control sample";
7196
String eidTitle = String.format("%s %s should be at least %.2f%%", "#eid()",
72-
title, SPEED_THRESHOLD * PERCENT);
97+
title, SPEED_THRESHOLD * PERCENT);
7398

7499
double eidTimes = eidScore / controlScore;
75100

@@ -80,17 +105,28 @@ public void doBenckmarking() throws Exception {
80105
assertThat(eidTimes).as(eidTitle).isGreaterThanOrEqualTo(SPEED_THRESHOLD);
81106
}
82107

108+
@State(Scope.Benchmark)
109+
public static class MemoryState {
110+
private String eid;
111+
112+
@Setup
113+
public void setup() {
114+
printMemory();
115+
this.eid = "20160330:124244";
116+
}
117+
}
118+
83119
@Benchmark
84-
public void control(Blackhole bh) {
120+
public void control(MemoryState state, Blackhole bh) {
85121
for (int i = 0; i < OPERATIONS; i++) {
86122
bh.consume(new Date());
87123
}
88124
}
89125

90126
@Benchmark
91-
public void eid(Blackhole bh) {
127+
public void eid(MemoryState state, Blackhole bh) {
92128
for (int i = 0; i < OPERATIONS; i++) {
93-
bh.consume(new Eid("20160324:223837"));
129+
bh.consume(new Eid(state.eid));
94130
}
95131
}
96132

0 commit comments

Comments
 (0)