diff --git a/autoload/SpaceVim/layers/lang/java.vim b/autoload/SpaceVim/layers/lang/java.vim index bb402351c..3e8c53e47 100644 --- a/autoload/SpaceVim/layers/lang/java.vim +++ b/autoload/SpaceVim/layers/lang/java.vim @@ -134,10 +134,10 @@ function! SpaceVim#layers#lang#java#plugins() abort let plugins = [ \ ['wsdjeg/vim-dict', { 'on_ft' : 'java'}], \ ['wsdjeg/java_getset.vim', { 'on_ft' : 'java', 'loadconf' : 1}], - \ ['wsdjeg/JavaUnit.vim', { 'on_ft' : 'java'}], \ ['vim-jp/vim-java', { 'on_ft' : 'java'}], \ ['artur-shaik/vim-javacomplete2', { 'on_ft' : ['java','jsp'], 'loadconf' : 1}], \ ] + call add(plugins, [g:_spacevim_root_dir . 'bundle/JavaUnit.vim', {'on_ft' : 'java'}]) return plugins endfunction diff --git a/bundle/JavaUnit.vim/.gitignore b/bundle/JavaUnit.vim/.gitignore new file mode 100644 index 000000000..dc0611891 --- /dev/null +++ b/bundle/JavaUnit.vim/.gitignore @@ -0,0 +1,3 @@ +*.class +*.html +test/*.java diff --git a/bundle/JavaUnit.vim/.travis.yml b/bundle/JavaUnit.vim/.travis.yml new file mode 100644 index 000000000..2abc0c19b --- /dev/null +++ b/bundle/JavaUnit.vim/.travis.yml @@ -0,0 +1,36 @@ +language: python + +cache: + pip: true + +env: + matrix: + - LINT=vimlint-errors + - LINT=vimlint + - LINT=vint-errors + - LINT=vint + +matrix: + allow_failures: + - env: LINT=vimlint + - env: LINT=vint + +install: + - | + if [ "${LINT#vimlint}" != "$LINT" ]; then + git clone --depth=1 https://github.com/syngan/vim-vimlint /tmp/vimlint + git clone --depth=1 https://github.com/ynkdir/vim-vimlparser /tmp/vimlparser + elif [ "${LINT#vint}" != "$LINT" ]; then + virtualenv /tmp/vint && source /tmp/vint/bin/activate && pip install vim-vint + fi +script: + - | + if [ "$LINT" = "vimlint" ]; then + sh /tmp/vimlint/bin/vimlint.sh -l /tmp/vimlint -p /tmp/vimlparser . + elif [ "$LINT" = "vimlint-errors" ]; then + sh /tmp/vimlint/bin/vimlint.sh -E -l /tmp/vimlint -p /tmp/vimlparser . + elif [ "$LINT" = "vint" ]; then + vint . + elif [ "$LINT" = "vint-errors" ]; then + vint --error . + fi diff --git a/bundle/JavaUnit.vim/.vintrc.yaml b/bundle/JavaUnit.vim/.vintrc.yaml new file mode 100644 index 000000000..0a60ef768 --- /dev/null +++ b/bundle/JavaUnit.vim/.vintrc.yaml @@ -0,0 +1,10 @@ +cmdargs: + # Checking more strictly + severity: style_problem + + # Enable coloring + color: true + +policies: + ProhibitImplicitScopeVariable: + enabled: false diff --git a/bundle/JavaUnit.vim/LICENSE b/bundle/JavaUnit.vim/LICENSE new file mode 100644 index 000000000..757be93e0 --- /dev/null +++ b/bundle/JavaUnit.vim/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 Wang Shidong + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/bundle/JavaUnit.vim/README.md b/bundle/JavaUnit.vim/README.md new file mode 100644 index 000000000..3ca77848d --- /dev/null +++ b/bundle/JavaUnit.vim/README.md @@ -0,0 +1,70 @@ +###JavaUnit.vim + +[![Join the chat at https://gitter.im/wsdjeg/JavaUnit.vim](https://badges.gitter.im/wsdjeg/JavaUnit.vim.svg)](https://gitter.im/wsdjeg/JavaUnit.vim?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +[![Build Status](https://travis-ci.org/wsdjeg/JavaUnit.vim.svg?branch=master)](https://travis-ci.org/wsdjeg/JavaUnit.vim) + +#### requirement + +1. jdk + +2. [artur-shaik/javacomplete2](https://github.com/artur-shaik/vim-javacomplete2) + +3. [scrooloose/syntastic](https://github.com/scrooloose/syntastic) + +> also you can use my fork which provide gradle support, [wsdjeg/syntastic](https://github.com/wsdjeg/syntastic) + +4. [Shougo/unite.vim](https://github.com/Shougo/unite.vim) + +> if you do not intstall this plugin,JavaUnit will show result in its own buffer instead of unite. + +5. [tagbar](https://github.com/majutsushi/tagbar) + + +#### install + +- [neobundle.vim](https://github.com/Shougo/neobundle.vim) + +```vim +NeoBundle 'wsdjeg/JavaUnit.vim' +``` + +- [Vundle.vim](https://github.com/VundleVim/Vundle.vim) + +```vim +Plugin 'wsdjeg/JavaUnit.vim' +``` + +#### Command + +JavaUnitExec + +test the current methond(no need put the cursor on the methodName) +![2015-11-16 23-40-05](https://cloud.githubusercontent.com/assets/13142418/11186276/e153459c-8cbb-11e5-9724-9589066176d0.png) + +JavaUnitExec [args ...] + +test specification method + +example JavaUnitExec testMethod1 testMethod2 testMethod3 ... +![2015-11-16 23-40-25](https://cloud.githubusercontent.com/assets/13142418/11186274/e1520d9e-8cbb-11e5-90e1-17e6cfbc5a09.png) + +JavaUnitTestMain + +run the main methon of current class + +also you can use `JavaUnitTestAll`,then will run all the testMethod in the current file +![2015-11-16 23-40-43](https://cloud.githubusercontent.com/assets/13142418/11186273/e132f580-8cbb-11e5-94d3-81dfda614abf.png) + +support for maven project + +JavaUnitTestMaven test current file + +![JavaUnitMavenTest](https://cloud.githubusercontent.com/assets/13142418/11186066/ef8f70aa-8cba-11e5-9869-13f39a782ad7.png) + +JavaUnitTestMavenAll test this project + +![JavaUnitMavenTestAll](https://cloud.githubusercontent.com/assets/13142418/11186033/baf6f64c-8cba-11e5-989c-cd3dacb038b3.png) + +#### Mappings + +`q` close the JavaUnit buffer. diff --git a/bundle/JavaUnit.vim/autoload/javaunit.vim b/bundle/JavaUnit.vim/autoload/javaunit.vim new file mode 100644 index 000000000..10c648a4f --- /dev/null +++ b/bundle/JavaUnit.vim/autoload/javaunit.vim @@ -0,0 +1,216 @@ +let s:save_cpo = &cpo +set cpo&vim + +if exists('g:JavaUnit_autoload') + finish +endif +let g:JavaUnit_autoload = 1 + +let s:Fsep = javaunit#util#Fsep() +let s:Psep = javaunit#util#Psep() + +let g:JavaUnit_Home = fnamemodify(expand(''), ':p:h:h:gs?\\?'. s:Fsep. '?') + +if exists("g:JavaUnit_custom_tempdir") + let s:JavaUnit_tempdir = g:JavaUnit_custom_tempdir +else + let s:JavaUnit_tempdir = g:JavaUnit_Home .s:Fsep .'bin' +endif + +let s:JavaUnit_TestMethod_Source = + \g:JavaUnit_Home + \.s:Fsep + \.join(['src' , 'com' , 'wsdjeg' , 'util' , '*.java'],s:Fsep) + +function! javaunit#Compile() abort + silent exec '!javac -encoding utf8 -d '.s:JavaUnit_tempdir.' '.s:JavaUnit_TestMethod_Source +endfunction + +if findfile(s:JavaUnit_tempdir.join(['','com','wsdjeg','util','TestMethod.class'],s:Fsep))=="" + call javaunit#Compile() +endif + + +function javaunit#TestMethod(args,...) + let line = getline(search("package","nb",getline("0$"))) + if line != '' + let currentClassName = split(split(line," ")[1],";")[0].".".expand("%:t:r") + else + let currentClassName = expand("%:t:r") + endif + if a:args == "" + let cwords = split(tagbar#currenttag('%s', '', ''),'(')[0] + if filereadable('pom.xml') + let cmd="java -cp '" + \.s:JavaUnit_tempdir + \.s:Psep + \.getcwd() + \.join(['','target','test-classes'],s:Fsep) + \.s:Psep + \.get(g:,'JavaComplete_LibsPath','.') + \."' com.wsdjeg.util.TestMethod " + \.currentClassName + \.' ' + \.cwords + else + let cmd="java -cp '" + \.s:JavaUnit_tempdir + \.s:Psep + \.get(g:,'JavaComplete_LibsPath','.') + \."' com.wsdjeg.util.TestMethod " + \.currentClassName + \.' ' + \.cwords + endif + call javaunit#util#ExecCMD(cmd) + else + if filereadable('pom.xml') + let cmd="java -cp '" + \.s:JavaUnit_tempdir + \.s:Psep + \.getcwd() + \.join(['','target','test-classes'],s:Fsep) + \.s:Psep + \.get(g:,'JavaComplete_LibsPath','.') + \."' com.wsdjeg.util.TestMethod " + \.currentClassName + \.' ' + \.a:args + else + let cmd="java -cp '" + \.s:JavaUnit_tempdir + \.s:Psep + \.get(g:,'JavaComplete_LibsPath','.') + \."' com.wsdjeg.util.TestMethod " + \.currentClassName + \.' ' + \.a:args + endif + call javaunit#util#ExecCMD(cmd) + endif +endfunction + +function javaunit#TestAllMethods() + let line = getline(search("package","nb",getline("0$"))) + let currentClassName = split(split(line," ")[1],";")[0].".".expand("%:t:r") + let cmd="java -cp '" . s:JavaUnit_tempdir.s:Psep.g:JavaComplete_LibsPath . "' com.wsdjeg.util.TestMethod " . currentClassName + call javaunit#util#ExecCMD(cmd) +endfunction + + +function javaunit#MavenTest() + let line = getline(search("package","nb",getline("0$"))) + let currentClassName = split(split(line," ")[1],";")[0].".".expand("%:t:r") + let cmd = 'mvn test -Dtest='.currentClassName + call javaunit#util#ExecCMD(cmd) +endfunction + +function javaunit#MavenTestAll() + let cmd = 'mvn test' + call javaunit#util#ExecCMD(cmd) +endfunction + +function javaunit#NewTestClass(classNAME) + let filePath = expand("%:h") + let flag = 0 + let packageName = '' + for a in split(filePath,s:Fsep) + if flag + if a == expand("%:h:t") + let packageName .= a.';' + else + let packageName .= a.'.' + endif + endif + if a == "java" + let flag = 1 + endif + endfor + call append(0,"package ".packageName) + call append(1,"import org.junit.Test;") + call append(2,"import org.junit.Assert;") + call append(3,"public class ".a:classNAME." {") + call append(4,"@Test") + call append(5,"public void testM() {") + call append(6,"//TODO") + call append(7,"}") + call append(8,"}") + call feedkeys("gg=G","n") + call feedkeys("/testM\","n") + call feedkeys("viw","n") + "call feedkeys("/TODO\","n") +endfunction +function! javaunit#Get_method_name() abort + let name = 'sss' + return name +endfunction + +function! javaunit#TestMain(...) abort + let line = getline(search("package","nb",getline("0$"))) + if line != '' + let currentClassName = split(split(line," ")[1],";")[0].".".expand("%:t:r") + else + let currentClassName = expand("%:t:r") + endif + if filereadable('pom.xml') + let cmd="java -cp '" + \.s:JavaUnit_tempdir + \.s:Psep + \.getcwd() + \.join(['','target','test-classes'],s:Fsep) + \.s:Psep + \.get(g:,'JavaComplete_LibsPath','.') + \."' " + \.currentClassName + \.' ' + \.(len(a:000) > 0 ? join(a:000,' ') : '') + else + let cmd='java -cp "' . get(g:,'JavaComplete_LibsPath','.') . s:Psep + \.s:JavaUnit_tempdir + \.s:Psep + \.'" ' + \.currentClassName + \.' ' + \.(len(a:000) > 0 ? join(a:000,' ') : '') + endif + call javaunit#util#ExecCMD(cmd) +endfunction + +fu! javaunit#GenerateTestMethods() + let testClassName = expand('%:t:r') + if stridx(testClassName, 'test') != -1 || stridx(testClassName, 'Test') != -1 + let line = getline(search("package","nb",getline("0$"))) + let testClassName = split(split(line," ")[1],";")[0]."." . testClassName + if stridx(testClassName, 'Test') == len(testClassName) - 4 + let className = strpart(testClassName, 0,len(testClassName) - 4) + let cmd="java -cp '" + \.s:JavaUnit_tempdir + \.s:Psep + \.getcwd() + \.join(['','target','test-classes'],s:Fsep) + \.s:Psep + \.get(g:,'JavaComplete_LibsPath','.') + \."' com.wsdjeg.util.GenerateMethod " + \.className + let methods = split(join(systemlist(cmd)),'|') + let curPos = getpos('.') + let classdefineline = search("class " . expand('%:t:r'),"nb",getline("0$")) + for m in methods + call append(classdefineline, "/* test " . m . " */") + call append(classdefineline + 1,"public void test" . toupper(strpart(m,0,1)) . strpart(m,1,len(m)) . "() {") + call append(classdefineline + 2,"//TODO") + call append(classdefineline + 3,"}") + endfor + call feedkeys("gg=G","n") + call cursor(curPos[1] + 1, curPos[2]) + else + echohl WarningMsg | echomsg "This is not a testClassName,now only support className end with 'Test'" | echohl None + endif + else + echohl WarningMsg | echomsg "This is not a testClassName" | echohl None + endif +endf + + +let &cpo = s:save_cpo +unlet s:save_cpo diff --git a/bundle/JavaUnit.vim/autoload/javaunit/util.vim b/bundle/JavaUnit.vim/autoload/javaunit/util.vim new file mode 100644 index 000000000..5d06d1917 --- /dev/null +++ b/bundle/JavaUnit.vim/autoload/javaunit/util.vim @@ -0,0 +1,54 @@ +let s:save_cpo = &cpo +set cpo&vim +if exists('g:javaunit_util_loaded') + finish +endif +let g:javaunit_util_loaded = 1 + + +function! s:OSX() + return has('macunix') +endfunction +function! s:LINUX() + return has('unix') && !has('macunix') && !has('win32unix') +endfunction +function! s:WINDOWS() + return (has('win16') || has('win32') || has('win64')) +endfunction + +function! javaunit#util#Fsep() abort + if s:WINDOWS() + return '\' + else + return '/' + endif +endfunction + +function! javaunit#util#Psep() abort + if s:WINDOWS() + return ';' + else + return ':' + endif +endfunction + +function! javaunit#util#ExecCMD(cmd) + if exists('g:spacevim_version') + call SpaceVim#plugins#runner#open(a:cmd) + elseif exists(':Unite') + call unite#start([['output/shellcmd', s:EscapeCMD(a:cmd)]], {'log': 1, 'wrap': 1,'start_insert':0}) + else + call javaunit#win#OpenWin(a:cmd) + endif +endfunction + +function! s:EscapeCMD(cmd) + if s:WINDOWS() + return a:cmd + else + return a:cmd + endif +endfunction + +let &cpo = s:save_cpo +unlet s:save_cpo diff --git a/bundle/JavaUnit.vim/autoload/javaunit/win.vim b/bundle/JavaUnit.vim/autoload/javaunit/win.vim new file mode 100644 index 000000000..94a27e6ce --- /dev/null +++ b/bundle/JavaUnit.vim/autoload/javaunit/win.vim @@ -0,0 +1,49 @@ +let s:save_cpo = &cpo +set cpo&vim +let s:name = '__JavaUnit__' + +fu! javaunit#win#OpenWin(cmd) + if bufwinnr('s:name') < 0 + if bufnr('s:name') != -1 + exe 'silent! split ' . '+b' . bufnr('s:name') + else + silent! split s:name + endif + else + exec bufwinnr('s:name') . 'wincmd w' + endif + setl modifiable + let result = systemlist(a:cmd) + call s:windowsinit() + call append(0, result) + setl nomodifiable +endf +fu! s:windowsinit() + " option + setl fileformat=unix + setl fileencoding=utf-8 + setl iskeyword=@,48-57,_ + setl noreadonly + setl buftype=nofile + setl bufhidden=wipe + setl noswapfile + setl nobuflisted + setl nolist + setl nonumber + setl nowrap + setl winfixwidth + setl winfixheight + setl textwidth=0 + setl nospell + setl nofoldenable + + " map + nnoremap q :bwipeout! + + " cmd + + + let b:ctrlsf_initialized = 1 +endf +let &cpo = s:save_cpo +unlet s:save_cpo diff --git a/bundle/JavaUnit.vim/bin/test.txt b/bundle/JavaUnit.vim/bin/test.txt new file mode 100644 index 000000000..e69de29bb diff --git a/bundle/JavaUnit.vim/plugin/javaunit.vim b/bundle/JavaUnit.vim/plugin/javaunit.vim new file mode 100644 index 000000000..b8471d730 --- /dev/null +++ b/bundle/JavaUnit.vim/plugin/javaunit.vim @@ -0,0 +1,43 @@ +let s:save_cpo = &cpo +set cpo&vim + +if exists('g:JavaUnit_loaded') + finish +endif + +let g:JavaUnit_loaded = 1 + +command! -nargs=* + \ JavaUnitExec + \ call javaunit#TestMethod() + +command! -nargs=* + \ JavaUnitTestMain + \ call javaunit#TestMain() + +command! -nargs=0 + \ JavaUnitTestAll + \ call javaunit#TestAllMethods() + +command! -nargs=0 + \ JavaUnitTestMaven + \ call javaunit#MavenTest() + +command! -nargs=0 + \ JavaUnitTestMavenAll + \ call javaunit#MavenTestAll() + +command! -nargs=? -complete=file + \ JavaUnitNewTestClass + \ call javaunit#NewTestClass(expand("%:t:r")) + +command! -nargs=0 + \ JavaUnitServerCompile + \ call javaunit#Compile() + +command! -nargs=0 + \ JUGenerateM + \ call javaunit#GenerateTestMethods() + +let &cpo = s:save_cpo +unlet s:save_cpo diff --git a/bundle/JavaUnit.vim/src/com/wsdjeg/util/GenerateMethod.java b/bundle/JavaUnit.vim/src/com/wsdjeg/util/GenerateMethod.java new file mode 100644 index 000000000..4771dd0bb --- /dev/null +++ b/bundle/JavaUnit.vim/src/com/wsdjeg/util/GenerateMethod.java @@ -0,0 +1,28 @@ +package com.wsdjeg.util; + +import java.lang.reflect.Method; + +public class GenerateMethod { + public static void main(String[] args) { + System.out.println(listMethos(args[0])); + } + private static String listMethos(String name){ + Class clazz = null; + try { + clazz = Class.forName(name); + } catch(Exception e){ + e.printStackTrace(); + } + Method[] mds = clazz.getDeclaredMethods(); + String result = ""; + for (int i = 0; i < mds.length; i++) { + if (result.length()>0) { + result = result + "|" +mds[i].getName(); + }else{ + result = mds[0].getName(); + } + } + return result; + } +} + diff --git a/bundle/JavaUnit.vim/src/com/wsdjeg/util/TestMethod.java b/bundle/JavaUnit.vim/src/com/wsdjeg/util/TestMethod.java new file mode 100644 index 000000000..793ee2ead --- /dev/null +++ b/bundle/JavaUnit.vim/src/com/wsdjeg/util/TestMethod.java @@ -0,0 +1,54 @@ +package com.wsdjeg.util; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +public class TestMethod{ + public static void main(String[] args) { + if(args.length == 2){ + testSpecifiedMethod(args[0],args[1]); + }else if (args.length == 1) { + testAllMethods(args[0]); + }else if (args.length > 2){ + testMethods(args); + } + } + public static void testAllMethods(String className){ + Class clazz = null; + try { + clazz = Class.forName(className); + } catch(Exception e){ + e.printStackTrace(); + } + Method[] mds = clazz.getMethods(); + for (int i = 0; i < mds.length; i++) { + if (mds[i].getName().startsWith("test")) { + testSpecifiedMethod(className,mds[i].getName()); + } + } + } + public static void testMethods(String[] args) { + for (int i = 1; i < args.length; i++) { + testSpecifiedMethod(args[0],args[i]); + } + } + @SuppressWarnings("unchecked") + public static void testSpecifiedMethod(String className,String methodName){ + try{ + System.out.println("========================= JavaUnite Test ============================="); + System.out.println("CLASS:" + className); + System.out.println("METHOD:" + methodName + "()"); + System.out.println("============================= OUTPUT ================================="); + Class clazz = Class.forName(className); + Constructor c = clazz.getDeclaredConstructor(); + c.setAccessible(true); + long startTime = System.currentTimeMillis(); + clazz.getMethod(methodName).invoke(c.newInstance()); + long endTime = System.currentTimeMillis(); + System.out.println("================ SUCCESS: " + (endTime - startTime) + "ms ======================"); + }catch(Exception e){ + System.out.println("============================= FAILED ================================"); + e.printStackTrace(); + } + } +}