Skip to content

Commit 33bcf69

Browse files
committed
Fix license check logic error in ZenDsl
1 parent 1b1ac11 commit 33bcf69

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ dependencies {
4141
implementation("io.reactivex.rxjava2:rxjava:2.2.21")
4242

4343
testImplementation("junit:junit:4.12")
44+
testImplementation("org.powermock:powermock-module-junit4:2.0.2")
45+
testImplementation("org.powermock:powermock-api-mockito2:2.0.2")
4446
testImplementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10")
4547
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.21.0")
4648
testImplementation("org.spockframework:spock-core:2.2-groovy-4.0") {

src/com/zenuml/dsl/ZenDsl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public static String escape(String input) {
3030
}
3131

3232
String getDsl() {
33-
final Boolean isLicensed = CheckLicense.isLicensed();
34-
if (Boolean.TRUE.equals(isLicensed)) {
33+
boolean isLicensed = Boolean.TRUE.equals(CheckLicense.isLicensed());
34+
if (!isLicensed) {
3535
return LICENSE_IS_NOT_VALID;
3636
}
3737
return dsl.toString();

test/src/com/zenuml/dsl/ZenDslTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
package com.zenuml.dsl;
22

3+
import com.zenuml.license.CheckLicense;
4+
import org.junit.Before;
35
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.mockito.Mockito;
8+
import org.powermock.api.mockito.PowerMockito;
9+
import org.powermock.core.classloader.annotations.PrepareForTest;
10+
import org.powermock.modules.junit4.PowerMockRunner;
411

512
import static org.hamcrest.core.Is.is;
613
import static org.junit.Assert.assertEquals;
714
import static org.junit.Assert.assertThat;
815

16+
@RunWith(PowerMockRunner.class)
17+
@PrepareForTest(CheckLicense.class)
918
public class ZenDslTest {
1019

20+
@Before
21+
public void setUp() {
22+
PowerMockito.mockStatic(CheckLicense.class);
23+
}
24+
1125
@Test
1226
public void testEscape() {
1327
// remove the following characters: \, ", \n, \t, \r, `
@@ -18,6 +32,7 @@ public void testEscape() {
1832

1933
@Test
2034
public void test_quoted() {
35+
Mockito.when(CheckLicense.isLicensed()).thenReturn(true);
2136
assertThat(new ZenDsl().quoted("a").getDsl(), is("\"a\""));
2237
assertThat(new ZenDsl().quoted("a\nb").getDsl(), is("\"ab\""));
2338
assertThat(new ZenDsl()
@@ -28,10 +43,23 @@ public void test_quoted() {
2843

2944
@Test
3045
public void addComment() {
46+
Mockito.when(CheckLicense.isLicensed()).thenReturn(true);
3147
assertThat(new ZenDsl().comment("a").getDsl(), is("// a\n"));
3248
assertThat(new ZenDsl().comment("a\nb").getDsl(), is("// a\n// b\n"));
3349
assertThat(new ZenDsl()
3450
.startBlock().comment("a\nb")
3551
.closeBlock().getDsl(), is(" {\n\t// a\n\t// b\n}\n"));
3652
}
53+
54+
@Test
55+
public void testNotLicensed() {
56+
Mockito.when(CheckLicense.isLicensed()).thenReturn(false);
57+
assertThat(new ZenDsl().getDsl(), is(ZenDsl.LICENSE_IS_NOT_VALID));
58+
}
59+
60+
@Test
61+
public void testNotLicensedWithNull() {
62+
Mockito.when(CheckLicense.isLicensed()).thenReturn(null);
63+
assertThat(new ZenDsl().getDsl(), is(ZenDsl.LICENSE_IS_NOT_VALID));
64+
}
3765
}

0 commit comments

Comments
 (0)