From 33bcf6904bf7072f291eb44566e15dbd35cc14df Mon Sep 17 00:00:00 2001 From: Yanhui Li Date: Fri, 19 Apr 2024 23:01:30 +1000 Subject: [PATCH] Fix license check logic error in ZenDsl --- build.gradle.kts | 2 ++ src/com/zenuml/dsl/ZenDsl.java | 4 ++-- test/src/com/zenuml/dsl/ZenDslTest.java | 28 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 177ed7e7..81156c17 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -41,6 +41,8 @@ dependencies { implementation("io.reactivex.rxjava2:rxjava:2.2.21") testImplementation("junit:junit:4.12") + testImplementation("org.powermock:powermock-module-junit4:2.0.2") + testImplementation("org.powermock:powermock-api-mockito2:2.0.2") testImplementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10") detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.21.0") testImplementation("org.spockframework:spock-core:2.2-groovy-4.0") { diff --git a/src/com/zenuml/dsl/ZenDsl.java b/src/com/zenuml/dsl/ZenDsl.java index 3c4cd88a..7a678b9c 100644 --- a/src/com/zenuml/dsl/ZenDsl.java +++ b/src/com/zenuml/dsl/ZenDsl.java @@ -30,8 +30,8 @@ public static String escape(String input) { } String getDsl() { - final Boolean isLicensed = CheckLicense.isLicensed(); - if (Boolean.TRUE.equals(isLicensed)) { + boolean isLicensed = Boolean.TRUE.equals(CheckLicense.isLicensed()); + if (!isLicensed) { return LICENSE_IS_NOT_VALID; } return dsl.toString(); diff --git a/test/src/com/zenuml/dsl/ZenDslTest.java b/test/src/com/zenuml/dsl/ZenDslTest.java index d8b0c3ef..3b3d5136 100644 --- a/test/src/com/zenuml/dsl/ZenDslTest.java +++ b/test/src/com/zenuml/dsl/ZenDslTest.java @@ -1,13 +1,27 @@ package com.zenuml.dsl; +import com.zenuml.license.CheckLicense; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; +@RunWith(PowerMockRunner.class) +@PrepareForTest(CheckLicense.class) public class ZenDslTest { + @Before + public void setUp() { + PowerMockito.mockStatic(CheckLicense.class); + } + @Test public void testEscape() { // remove the following characters: \, ", \n, \t, \r, ` @@ -18,6 +32,7 @@ public void testEscape() { @Test public void test_quoted() { + Mockito.when(CheckLicense.isLicensed()).thenReturn(true); assertThat(new ZenDsl().quoted("a").getDsl(), is("\"a\"")); assertThat(new ZenDsl().quoted("a\nb").getDsl(), is("\"ab\"")); assertThat(new ZenDsl() @@ -28,10 +43,23 @@ public void test_quoted() { @Test public void addComment() { + Mockito.when(CheckLicense.isLicensed()).thenReturn(true); assertThat(new ZenDsl().comment("a").getDsl(), is("// a\n")); assertThat(new ZenDsl().comment("a\nb").getDsl(), is("// a\n// b\n")); assertThat(new ZenDsl() .startBlock().comment("a\nb") .closeBlock().getDsl(), is(" {\n\t// a\n\t// b\n}\n")); } + + @Test + public void testNotLicensed() { + Mockito.when(CheckLicense.isLicensed()).thenReturn(false); + assertThat(new ZenDsl().getDsl(), is(ZenDsl.LICENSE_IS_NOT_VALID)); + } + + @Test + public void testNotLicensedWithNull() { + Mockito.when(CheckLicense.isLicensed()).thenReturn(null); + assertThat(new ZenDsl().getDsl(), is(ZenDsl.LICENSE_IS_NOT_VALID)); + } }