1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.enforcer.rules;
20
21 import java.util.Iterator;
22
23 import org.apache.maven.enforcer.rule.api.EnforcerLogger;
24 import org.apache.maven.enforcer.rule.api.EnforcerRuleError;
25 import org.apache.maven.execution.MavenExecutionRequest;
26 import org.apache.maven.execution.MavenSession;
27 import org.apache.maven.model.profile.activation.OperatingSystemProfileActivator;
28 import org.apache.maven.plugin.logging.Log;
29 import org.apache.maven.plugin.logging.SystemStreamLog;
30 import org.apache.maven.project.MavenProject;
31 import org.codehaus.plexus.util.Os;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.assertj.core.api.Assertions.assertThatCode;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
39
40
41
42
43
44
45 class TestRequireOS {
46
47 private MavenSession mavenSession;
48
49 @BeforeEach
50 void setup() {
51 mavenSession = mock(MavenSession.class);
52 when(mavenSession.getRequest()).thenReturn(mock(MavenExecutionRequest.class));
53 when(mavenSession.getCurrentProject()).thenReturn(mock(MavenProject.class));
54 }
55
56
57
58
59 @Test
60 void testOS() {
61 Log log = new SystemStreamLog();
62
63 RequireOS rule = new RequireOS(new OperatingSystemProfileActivator(), mavenSession);
64
65 Iterator<String> iter = Os.getValidFamilies().iterator();
66 String validFamily;
67 String invalidFamily = null;
68 while (iter.hasNext()) {
69 String fam = iter.next();
70 if (!Os.isFamily(fam)) {
71 invalidFamily = fam;
72 break;
73 }
74 }
75
76 validFamily = Os.OS_FAMILY;
77
78 log.info("Testing Mojo Using Valid Family: " + validFamily + " Invalid Family: " + invalidFamily);
79
80 rule.setFamily(validFamily);
81 assertThat(rule.isAllowed()).isTrue();
82
83 rule.setFamily(invalidFamily);
84 assertThat(rule.isAllowed()).isFalse();
85
86 rule.setFamily("!" + invalidFamily);
87 assertThat(rule.isAllowed()).isTrue();
88
89 rule.setFamily(null);
90 rule.setArch(Os.OS_ARCH);
91 assertThat(rule.isAllowed()).isTrue();
92
93 rule.setArch("somecrazyarch");
94 assertThat(rule.isAllowed()).isFalse();
95
96 rule.setArch("!somecrazyarch");
97 assertThat(rule.isAllowed()).isTrue();
98
99 rule.setArch(null);
100
101 rule.setName(Os.OS_NAME);
102 assertThat(rule.isAllowed()).isTrue();
103
104 rule.setName("somecrazyname");
105 assertThat(rule.isAllowed()).isFalse();
106
107 rule.setName("!somecrazyname");
108 assertThat(rule.isAllowed()).isTrue();
109
110 rule.setName(null);
111
112 rule.setVersion(Os.OS_VERSION);
113 assertThat(rule.isAllowed()).isTrue();
114
115 rule.setVersion("somecrazyversion");
116 assertThat(rule.isAllowed()).isFalse();
117
118 rule.setVersion("!somecrazyversion");
119 assertThat(rule.isAllowed()).isTrue();
120 }
121
122 @Test
123 void testInvalidFamily() {
124 RequireOS rule = new RequireOS(new OperatingSystemProfileActivator(), mavenSession);
125 rule.setLog(mock(EnforcerLogger.class));
126
127 rule.setFamily("junk");
128 assertThatCode(rule::execute)
129 .isInstanceOf(EnforcerRuleError.class)
130 .hasMessageStartingWith("Invalid Family type used. Valid family types are: ");
131 }
132
133 @Test
134 void testId() {
135 RequireOS rule = new RequireOS(new OperatingSystemProfileActivator(), mavenSession);
136 rule.setVersion("1.2");
137 assertThat(rule.getCacheId()).isNotEmpty();
138 }
139 }