Skip to content

Commit 3ef53e2

Browse files
author
Hierlmeier, Richard
committed
Improvement e-gineering#131': Additional tests for goal set-git-properties
1 parent f0418ae commit 3ef53e2

File tree

2 files changed

+165
-2
lines changed

2 files changed

+165
-2
lines changed

src/test/java/com/e_gineering/maven/gitflowhelper/TestPropertyMapper.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package com.e_gineering.maven.gitflowhelper;
22

3+
import org.apache.commons.io.IOUtils;
34
import org.apache.maven.plugin.MojoExecutionException;
45
import org.junit.Assert;
56
import org.junit.Test;
67

8+
import java.nio.charset.StandardCharsets;
79
import java.util.function.BiFunction;
810

11+
/**
12+
* Unit-Test for the class {@link PropertyMapper}
13+
*/
914
public class TestPropertyMapper
1015
{
1116
/**
12-
* Test the methode {@link PropertyMapper#map(GitBranchInfo)} with a Java Mapper
17+
* Tests the methode {@link PropertyMapper#map(GitBranchInfo)} with a Java Mapper
1318
* @throws MojoExecutionException on error
1419
*/
1520
@Test
@@ -26,7 +31,7 @@ public void testPropertyMapperWithJava() throws MojoExecutionException
2631
}
2732

2833
/**
29-
* Test the methode {@link PropertyMapper#map(GitBranchInfo)} with a Javascript Mapper
34+
* Tests the methode {@link PropertyMapper#map(GitBranchInfo)} with a Javascript Mapper
3035
* @throws MojoExecutionException on error
3136
*/
3237
@Test
@@ -47,6 +52,47 @@ public void testPropertyMapperWithJavascript() throws MojoExecutionException
4752

4853
}
4954

55+
/**
56+
* Tests the conversion of a branch name to a valid Docker image name
57+
* @throws Exception on error
58+
*/
59+
@Test
60+
public void testPropertyMapperWithJavascriptBranchNameToDockerImageNameDevelopBranch() throws Exception {
61+
62+
PropertyMapper mapper = new PropertyMapper();
63+
mapper.setPropertyName("prop");
64+
mapper.setLanguage("javascript");
65+
66+
mapper.setMapper(IOUtils.toString(getClass().getResource("PropertyMapperBranchNameToDockerName.js"), StandardCharsets.UTF_8));
67+
68+
Assert.assertEquals("", mapper.map(new GitBranchInfo("", GitBranchType.OTHER, "")));
69+
70+
Assert.assertEquals("foo", mapper.map(new GitBranchInfo("FOO", GitBranchType.OTHER, "")));
71+
Assert.assertEquals("f_f", mapper.map(new GitBranchInfo("F_f", GitBranchType.OTHER, "")));
72+
Assert.assertEquals("f_ae", mapper.map(new GitBranchInfo("F_ä", GitBranchType.OTHER, "")));
73+
Assert.assertEquals("f_ae", mapper.map(new GitBranchInfo("F_Ä", GitBranchType.OTHER, "")));
74+
Assert.assertEquals("f_oe", mapper.map(new GitBranchInfo("F_ö", GitBranchType.OTHER, "")));
75+
Assert.assertEquals("f_oe", mapper.map(new GitBranchInfo("F_Ö", GitBranchType.OTHER, "")));
76+
Assert.assertEquals("f_ue", mapper.map(new GitBranchInfo("F_ü", GitBranchType.OTHER, "")));
77+
Assert.assertEquals("f_ue", mapper.map(new GitBranchInfo("F_Ü", GitBranchType.OTHER, "")));
78+
Assert.assertEquals("f_ss", mapper.map(new GitBranchInfo("F_ß", GitBranchType.OTHER, "")));
79+
Assert.assertEquals("f_ss", mapper.map(new GitBranchInfo("F_ß", GitBranchType.OTHER, "")));
80+
81+
Assert.assertEquals("f__", mapper.map(new GitBranchInfo("F_!", GitBranchType.OTHER, "")));
82+
Assert.assertEquals("f__a_", mapper.map(new GitBranchInfo("F__!", GitBranchType.OTHER, "")));
83+
84+
Assert.assertEquals("a_f", mapper.map(new GitBranchInfo("_f", GitBranchType.OTHER, "")));
85+
Assert.assertEquals("a__a_", mapper.map(new GitBranchInfo("___", GitBranchType.OTHER, "")));
86+
87+
Assert.assertEquals("a.a", mapper.map(new GitBranchInfo(".a", GitBranchType.OTHER, "")));
88+
Assert.assertEquals("a-a", mapper.map(new GitBranchInfo("-a", GitBranchType.OTHER, "")));
89+
Assert.assertEquals("a_a", mapper.map(new GitBranchInfo("_a", GitBranchType.OTHER, "")));
90+
91+
Assert.assertEquals("a__a", mapper.map(new GitBranchInfo("__a", GitBranchType.OTHER, "")));
92+
Assert.assertEquals("a---a", mapper.map(new GitBranchInfo("---a", GitBranchType.OTHER, "")));
93+
}
94+
95+
5096
public static class ToLowerCaseMapper implements BiFunction<String,String,String>
5197
{
5298
@Override
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
3+
Sample Mapping for Mapping a Branchname into a Docker Image Name
4+
5+
Name konvention for Docker image Names see
6+
(see https://docs.docker.com/engine/reference/commandline/tag/#extended-description)
7+
8+
An image name is made up of slash-separated name components, optionally prefixed by a
9+
registry hostname. The hostname must comply with standard DNS rules, but may not contain
10+
underscores. If a hostname is present, it may optionally be followed by a port number in
11+
the format :8080. If not present, the command uses Docker’s public registry located at
12+
registry-1.docker.io by default.
13+
Name components may contain lowercase letters, digits and separators. A separator is
14+
defined as a period, one or two underscores, or one or more hyphens. A name component
15+
may not start or end with a separator.
16+
*/
17+
18+
function log(msg) {
19+
//print(msg);
20+
}
21+
22+
function toValidDockerNameChar(c) {
23+
if(c.match(/[a-z]/i)) {
24+
return c.toLowerCase();
25+
}
26+
if(c.match(/[0-9]/)) {
27+
return c;
28+
}
29+
// Special handling for German Umlaute
30+
switch(c) {
31+
case ".":
32+
return ".";
33+
case "-":
34+
return "-";
35+
case "ä":
36+
return "ae";
37+
case "Ä":
38+
return "ae";
39+
case "ü":
40+
return "ue";
41+
case "Ü":
42+
return "ue";
43+
case "ö":
44+
return "oe";
45+
case 'Ö':
46+
return "oe";
47+
case 'ß':
48+
return "ss";
49+
default:
50+
return "_";
51+
}
52+
}
53+
function branchNameToDockerImageName(branchName) {
54+
if(branchName.length === 0) {
55+
return "";
56+
}
57+
var ret = "";
58+
var firstChar = toValidDockerNameChar(branchName.charAt(0))
59+
var numDots = 0;
60+
var numUnderscores = 0;
61+
// A name component may not start or end with a separator.
62+
switch(firstChar) {
63+
case '.':
64+
ret += "a.";
65+
numDots++;
66+
break;
67+
case '_':
68+
ret += "a_";
69+
numUnderscores++;
70+
break;
71+
case '-':
72+
ret += "a-";
73+
break;
74+
default:
75+
ret += firstChar;
76+
}
77+
78+
for(var i = 1; i < branchName.length; i++) {
79+
var nextChar = toValidDockerNameChar(branchName.charAt(i));
80+
switch(nextChar) {
81+
case '.':
82+
numDots++;
83+
if(numDots > 1) {
84+
ret += "a";
85+
numDots = 0;
86+
}
87+
ret += '.';
88+
break;
89+
case '_':
90+
numUnderscores++;
91+
if(numUnderscores > 2) {
92+
ret += "a";
93+
numUnderscores = 1;
94+
}
95+
ret += '_';
96+
break;
97+
default:
98+
ret += nextChar;
99+
numUnderscores = 0;
100+
numDots = 0;
101+
}
102+
}
103+
log("ret: " + ret);
104+
105+
return ret;
106+
}
107+
108+
109+
function map(branchName, branchType) {
110+
log("branchName: [" + branchName + "], branchType=[" + branchType + "]");
111+
switch(branchType.toString()) {
112+
case 'OTHER':
113+
return branchNameToDockerImageName(branchName.trim());
114+
default:
115+
return "";
116+
}
117+
}

0 commit comments

Comments
 (0)