2016年7月30日星期六

解决mvn test没有运行Scala Test

http://stackoverflow.com/questions/13036561/trying-to-get-scalatest-working-there-are-no-tests-to-run-when-doing-mvn-tes

在pom的plugin中增加:
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.7</version>
  <configuration>
    <skipTests>true</skipTests>
  </configuration>
</plugin>
<plugin>
  <groupId>org.scalatest</groupId>
  <artifactId>scalatest-maven-plugin</artifactId>
  <version>1.0</version>
  <configuration>
    <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
    <junitxml>.</junitxml>
    <filereports>WDF TestSuite.txt</filereports>
  </configuration>
  <executions>
    <execution>
      <id>test</id>
      <goals>
        <goal>test</goal>
      </goals>
    </execution>
  </executions>
</plugin>
参考http://www.scalatest.org/user_guide/using_the_scalatest_maven_plugin

2016年7月29日星期五

Spark单元测试中使用mockito打桩

pom构建添加依赖:
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-core_2.10</artifactId>
      <version>${spark.version}</version>
      <type>test-jar</type>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <version>1.9.5</version>
    </dependency>

sbt构建添加依赖:
"org.apache.spark" %% "spark-core" % "2.0.0" % "test" classifier "tests",
"org.mockito" % "mockito-all" % "1.9.5" % "test"


对class中的方法打桩(不能对class中变量打桩),使用spy。一个例子:
ReadFile.scala:
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD

class ReadFile(sc: SparkContext) extends Serializable{
  def input: RDD[String] = {
    sc.textFile("hdfs://ip_address/xxx/data.txt")
  }

  def output = input.collect()
}

ReadFileSuite.scala:
import org.apache.spark.LocalSparkContext.withSpark
import org.apache.spark.{LocalSparkContext, SparkContext, SparkFunSuite}
import org.mockito.Mockito.{spy, when}

class ReadFileSuite extends SparkFunSuite with LocalSparkContext {
  test("test1") {
    withSpark(new SparkContext("local", "test")) { sc =>
      val data = spy(new ReadFile(sc))
      val stub_file = sc.textFile(getClass.getResource("/data.txt").getFile)
      when(data.input).thenReturn(stub_file)
      assert(data.output.length === 3)
    }
  }
}

ReadFileSuite中文件是从test/resources中读取的,而不是ReadFile中从hdfs读的路径。

如果class没有构造参数,可以使用mock(classOf[yourClass])创建mock的class。

参考了http://qiuguo0205.iteye.com/blog/1443344http://qiuguo0205.iteye.com/blog/1456528

2016年7月18日星期一

2016年7月16日星期六

windows中自定义键位

将capslock键替换为ctrl:

参考http://hushicai.com/2016/01/07/win7-jiang-CapsLock-ying-she-wei-Ctrl.html

新建caps_lock_to_ctrl.reg文件,内容如下:
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,1D,00,3A,00,00,00,00,00

运行后重启,完成。

交换左win键和左alt键:

参考http://blog.sina.com.cn/s/blog_69a4fbd70100rj59.html

"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,5B,E0,38,00,38,00,5B,E0

将capslock键替换为左ctrl,同时交换win键和左alt键:
"Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,1D,00,3A,00,5B,E0,38,00,38,00,5B,E0

如何设置Scancode Map的值,参考:http://blog.chinaunix.net/uid-174325-id-3912617.html

2016年7月5日星期二

jetty调试和部署web项目

本地调试:
<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.3.10.v20160621</version>
  <configuration>
    <httpConnector>
      <port>8081</port>
    </httpConnector>
    <stopPort>9966</stopPort>
    <stopKey>foo</stopKey>
  </configuration>
</plugin>

配置参考:https://www.eclipse.org/jetty/documentation/9.3.x/jetty-maven-plugin.html#jetty-stop-goal
jetty插件版本:http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.eclipse.jetty%22%20AND%20a%3A%22jetty-maven-plugin%22

mvn jetty:run

部署:
java -jar start-server.jar xxx port  // xxx为war包名