Skip to content

Commit

Permalink
Fix license check logic error in ZenDsl
Browse files Browse the repository at this point in the history
  • Loading branch information
whimet committed Apr 19, 2024
1 parent 1b1ac11 commit 33bcf69
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
4 changes: 2 additions & 2 deletions src/com/zenuml/dsl/ZenDsl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
28 changes: 28 additions & 0 deletions test/src/com/zenuml/dsl/ZenDslTest.java
Original file line number Diff line number Diff line change
@@ -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, `
Expand All @@ -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()
Expand All @@ -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));
}
}

0 comments on commit 33bcf69

Please sign in to comment.