0%

vscode用着用着用出bug了,放弃了。

vscode强大的插件

MetaGo: 字符跳转选择
Markdown Preview Enhanced: MarkDown预览插件,cmd-k v(边栏预览),cmd-shift-v(新窗口预览)

Read more »

iptables是一个命令行防火墙实用程序,它使用策略链来允许或阻止流量。 当连接尝试在您的系上建立自己时,iptables会在其列表中查找与其匹配的规则。 如果不到,则转到默认操作。

drop 掉所有类型的端口

1
2
3
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP

查看当前规则

1
iptables -L -n

只打开22端口的进出配置以及保存命令如下:

1
2
3
iptables -A INPUT -p tcp --dport 26666 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 26666 -j ACCEPT
service iptables save

-A 参数就看成是添加一条 INPUT 的规则
-p 指定是什么协议 我们常用的tcp 协议
–dport 就是目标端口 当数据从外部进入服务器为目标端口
反之 数据从服务器出去 则为数据源端口 使用 –sport
-j 就是指定是 ACCEPT 接收 或者 DROP 不接收

删除规则

查看规则行标:

Read more »

@RunWith

查阅官方API文档如下:
When a class is annotated with @RunWith or extends a class annotated with @RunWith, JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit. We added this feature late in development. While it seems powerful we expect the runner API to change as we learn how people really use it. Some of the classes that are currently internal will likely be refined and become public. For example, suites in JUnit 4 are built using RunWith, and a custom runner named Suite:

1
2
3
4
@RunWith(Suite.class)
@SuiteClasses(ATest.class, BTest.class, CTest.class)
public class ABCSuite {
}

翻译:当一个类被@RunWith所注解或者继承了一个被@RunWith注解的父类,那么Junit将会调用到这个类它所引用到的测试类,而不是通过Junit构建的runner(运行器)去调用。

可以参考:
https://www.tutorialspoint.com/junit/junit_suite_test.htm
https://www.codejava.net/testing/junit-test-suite-example-how-to-create-and-run-test-suite-in-command-line-and-eclipse

结合Spring上下文做测试

在以往的Spring项目中,很多同学可能没有做到真正的单元测试,可能都是基于测试环境启动整个应用来针对功能做测试,这种方式其实效率是非常低的,利用JUnit的特性做Spring的单元测试可以如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/*
* 代码来源:https://my.oschina.net/itblog/blog/1550753
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class UserManagerTest {
  @Autowired
  ApplicationContext ctx;

  @Test
  public void testAddUser() {
    try {
      UserManager userManager = ctx.getBean(UserManager.class);
      userManager.addUser();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

上面的例子是指定一个Spring上下文配置文件的方式进行单元测试,这样子就加载很多本次单元测试不用到的注入,Spring官方文档给出了更加高效的方案:
Context Configuration with Annotated Classes
To load an ApplicationContext for your tests by using annotated classes (see Java-based container configuration), you can annotate your test class with @ContextConfiguration and configure the classes attribute with an array that contains references to annotated classes. The following example shows how to do so:
翻译:使用@ContextConfiguration里面的classes属性来指定加载的配置类信息

1
2
3
4
5
6
@RunWith(SpringRunner.class)
// ApplicationContext will be loaded from AppConfig and TestConfig
@ContextConfiguration(classes = {AppConfig.class, TestConfig.class})
public class MyTest {
// class body...
}
Read more »