Skip to content

Commit

Permalink
Added Unit Tests (apache#625)
Browse files Browse the repository at this point in the history
* Added Unit Tests to increase code coverage

* Removed star imports

* Removed remaining star import
  • Loading branch information
TheRealHaui authored and nichunen committed May 3, 2019
1 parent 245cf97 commit 2076e39
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@

package org.apache.kylin.common;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Map;
import java.util.Properties;

Expand All @@ -32,6 +26,13 @@

import com.google.common.collect.Maps;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;


public class KylinConfigTest extends HotLoadKylinPropertiesTestCase {

@Test
Expand Down Expand Up @@ -166,4 +167,35 @@ public void testCalciteExtrasProperties() {
assertEquals("BRACKET", extras.getProperty("quoting"));
assertEquals("DEFAULT", extras.getProperty("conformance"));
}

@Test
public void testSetKylinConfigInEnvIfMissingTakingEmptyProperties() {
Properties properties = new Properties();
KylinConfig.setKylinConfigInEnvIfMissing(properties);

assertEquals(0, properties.size());
assertTrue(properties.isEmpty());

KylinConfig.setKylinConfigInEnvIfMissing(properties);

assertEquals(0, properties.size());
assertTrue(properties.isEmpty());
}

@Test(expected = IllegalStateException.class)
public void testCreateInstanceFromUriThrowsIllegalStateExceptionOne() {
KylinConfig.createInstanceFromUri("cb%MnlG]3:nav");
}

@Test(expected = RuntimeException.class)
public void testCreateInstanceFromUriThrowsRuntimeException() {
Properties properties = new Properties();
KylinConfig.setKylinConfigInEnvIfMissing(properties);

assertEquals(0, properties.size());
assertTrue(properties.isEmpty());

KylinConfig.createInstanceFromUri("dHy3K~m");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,22 @@
import org.junit.Assert;
import org.junit.Test;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.Vector;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


public class StringUtilTest {
@Test
Expand Down Expand Up @@ -63,14 +78,128 @@ public void testJoin() {
stringListNormal.add("bbb");
stringListNormal.add("ccc");
String joinedNormal = StringUtil.join(stringListNormal, ",");
Assert.assertEquals("aaa,bbb,ccc", joinedNormal);
assertEquals("aaa,bbb,ccc", joinedNormal);

stringListEmpty.add("");
stringListEmpty.add("aa");
stringListEmpty.add("");
stringListEmpty.add("bb");
stringListEmpty.add("");
String joinedEmpty = StringUtil.join(stringListEmpty, ",");
Assert.assertEquals(",aa,,bb,", joinedEmpty);
assertEquals(",aa,,bb,", joinedEmpty);
}

@Test
public void testDropSuffixWithNonEmptyString() {
assertEquals("", StringUtil.dropSuffix("Oo}T^z88/U", "Oo}T^z88/U"));
}

@Test
public void testDropSuffixWithEmptyString() {
String string = StringUtil.dropSuffix("", "^Fahs");

assertEquals("", string);
}

@Test
public void testNoBlankWithNull() {
assertEquals("%W=U~)O|0'#?,zA", StringUtil.noBlank(null, "%W=U~)O|0'#?,zA"));
}

@Test
public void testNoBlankWithNonEmptyString() {
assertEquals("N(sg", StringUtil.noBlank("N(sg", "H=!Cp(Ed5gral0qzo"));
}

@Test
public void testToUpperCaseArrayWithNonEmptyArray() {
String[] stringArray = new String[7];
stringArray[0] = "org.apache.kylin.common.util.StringUtil";
StringUtil.toUpperCaseArray(stringArray, stringArray);

assertEquals(7, stringArray.length);
assertEquals("[ORG.APACHE.KYLIN.COMMON.UTIL.STRINGUTIL, null, null, null, null, null, null]",
Arrays.asList(stringArray).toString()
);
}

@Test
public void testJoinReturningNonEmptyString() {
List<String> arrayList = new ArrayList<String>();
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>(arrayList);
linkedHashSet.add(")'<Mw@ZR0IYF_l%*>");
linkedHashSet.add(null);
String resultString = StringUtil.join(linkedHashSet, null);

assertNotNull(resultString);
assertEquals(")'<Mw@ZR0IYF_l%*>", resultString);

}

@Test
public void testJoinOne() {
Vector<String> vector = new Vector<>();
vector.add(null);
String resultString = StringUtil.join(vector, "PB");

assertNotNull(resultString);
assertEquals("", resultString);
}

@Test
public void testJoinTwo() {
Set<String> set = Locale.CHINESE.getUnicodeLocaleAttributes();
String resultString = StringUtil.join(set, "Op");

assertTrue(set.isEmpty());
assertEquals(0, set.size());

assertNotNull(resultString);
assertEquals("", resultString);
}

@Test
public void testJoinReturningNull() {
String string = StringUtil.join((Iterable<String>) null, ")Y>1v&V0GU6a");

assertNull(string);
}

@Test
public void testTrimSuffixWithEmptyString() {
String string = StringUtil.trimSuffix(" 8VKQ&I*pSVr", "");

assertEquals(" 8VKQ&I*pSVr", string);
}

@Test
public void testTrimSuffixWithNonEmptyString() {
String string = StringUtil.trimSuffix(",", "5I;.t0F*5HV4");

assertEquals(",", string);
}

@Test
public void testFilterSystemArgsThrowsIllegalArgumentException() {
String[] stringArray = new String[4];
stringArray[0] = "-D";
try {
StringUtil.filterSystemArgs(stringArray);
fail("Expecting exception: IllegalArgumentException");

} catch(IllegalArgumentException e) {
}
}

@Test
public void testFilterSystemArgs() {
String[] stringArray = new String[1];
stringArray[0] = "J";
String[] stringArrayTwo = StringUtil.filterSystemArgs(stringArray);

assertFalse(stringArrayTwo.equals(stringArray));
assertEquals(1, stringArrayTwo.length);
assertEquals("J", stringArrayTwo[0]);
}

}

0 comments on commit 2076e39

Please sign in to comment.