Skip to content

Commit

Permalink
[ROCKETMQ-53] Polish unit tests for MixAll
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouxinyu committed Jan 22, 2017
1 parent f529670 commit 6f60b4e
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion common/src/test/java/org/apache/rocketmq/common/MixAllTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,60 @@

package org.apache.rocketmq.common;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class MixAllTest {

@Test
public void testGetLocalInetAddress() throws Exception {
List<String> localInetAddress = MixAll.getLocalInetAddress();
String local = InetAddress.getLocalHost().getHostAddress();
assertThat(localInetAddress).contains("127.0.0.1");
assertThat(localInetAddress).contains(local);
}

@Test
public void testBrokerVIPChannel() {
assertThat(MixAll.brokerVIPChannel(true, "127.0.0.1:10911")).isEqualTo("127.0.0.1:10909");
}

@Test
public void testCompareAndIncreaseOnly() {
AtomicLong target = new AtomicLong(5);
assertThat(MixAll.compareAndIncreaseOnly(target, 6)).isTrue();
assertThat(target.get()).isEqualTo(6);

assertThat(MixAll.compareAndIncreaseOnly(target, 4)).isFalse();
assertThat(target.get()).isEqualTo(6);
}

@Test
public void testFile2String() throws IOException {
String fileName = System.getProperty("java.io.tmpdir") + File.separator + "MixAllTest" + System.currentTimeMillis();
File file = new File(fileName);
if (file.exists()) {
file.delete();
}
file.createNewFile();
try( PrintWriter out = new PrintWriter( fileName ) ){
out.write("TestForMixAll");
}
String string = MixAll.file2String(fileName);
assertThat(string).isEqualTo("TestForMixAll");
file.delete();
}

@Test
public void testString2File() throws IOException {
String fileName = System.getProperty("java.io.tmpdir") + File.separator + "MixAllTest" + System.currentTimeMillis();
MixAll.string2File("MixAll_testString2File", fileName);
assertThat(MixAll.file2String(fileName)).isEqualTo("MixAll_testString2File");
}
}

0 comments on commit 6f60b4e

Please sign in to comment.