Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(apollo-core): PropertiesUtilTest #4113

Merged
merged 13 commits into from
Nov 27, 2021
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2021 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ctrip.framework.apollo.core.utils;
import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Properties;

/**
* @author Wu Mingkan(Dalian University of Technology)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should delete author in community project.

* @since 2021/11/23
*
*/
public class PropertiesUtilTest {


@Test
public void TestProperties() throws IOException {
youyulan marked this conversation as resolved.
Show resolved Hide resolved
assertTrue("".equals(PropertiesUtil.toString(new Properties())));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use assertEqual or assertEquals instead of assertTrue

assertFalse(" ".equals(PropertiesUtil.toString(new Properties())));

Properties properties = new Properties();
properties.put("a","aaa");
assertTrue("a=aaa\r\n".equals(PropertiesUtil.toString(properties))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User System.lineSeperator instead of hard code \r\n \n

|| "a=aaa\n".equals(PropertiesUtil.toString(properties)));

}

@Test
public void TestFilterComment(){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void TestFilterComment(){
public void testFilterPropertiesComment(){

Keep the method name's convention of test case.

StringBuffer sb=new StringBuffer("#aaaaa\nbbb");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String.join(System.lineSepeartor, "#aaaaa“, "bbb"); ?

PropertiesUtil.filterPropertiesComment(sb);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add more test case to cover all conditional branchs

static boolean filterPropertiesComment(StringBuffer stringBuffer) {
//check whether has comment in the first line
if (stringBuffer.charAt(0) != '#') {
return false;
}
int commentLineIndex = stringBuffer.indexOf("\n");
if (commentLineIndex == -1) {
return false;
}
stringBuffer.delete(0, commentLineIndex + 1);
return true;
}

assertTrue("bbb".equals(sb.toString()));
assertFalse("#aaaaa\nbbb".equals(sb.toString()));
}
}