Skip to content

Commit 9fa0d36

Browse files
authored
Merge pull request #220 from kbase/sdkdev
Make tests rely on gradle dependencies & split out test deps
2 parents 5b24cc5 + 7dfec7f commit 9fa0d36

2 files changed

Lines changed: 34 additions & 37 deletions

File tree

build.gradle

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,22 @@ java -cp \$CLASSPATH us.kbase.sdk.ModuleBuilder \$@
116116

117117
// this is used in TypeGeneratorTest to supply dependencies for compiler tests
118118
def genCodeLibDir = file("$buildDir/generated-code-libs")
119+
def genCodeImplDir = new File(genCodeLibDir, "impl")
120+
def genCodeTestDir = new File(genCodeLibDir, "test")
119121

120122
task resolveGeneratedCodeDeps {
121123
inputs.files { configurations.generatedCodeClasspath }
124+
inputs.files { configurations.generatedTestCodeClasspath }
122125
outputs.dir genCodeLibDir
123126
doLast {
124127
delete genCodeLibDir
125128
copy {
126129
from configurations.generatedCodeClasspath
127-
into genCodeLibDir
130+
into genCodeImplDir
131+
}
132+
copy {
133+
from configurations.generatedTestCodeClasspath
134+
into genCodeTestDir
128135
}
129136
}
130137
}
@@ -276,6 +283,7 @@ configurations {
276283
testimpl.extendsFrom testImplementation
277284
// isolate the sdk generated code dependencies from the standard dependencies
278285
generatedCodeClasspath
286+
generatedTestCodeClasspath
279287
}
280288

281289
configurations.all {
@@ -369,9 +377,10 @@ dependencies {
369377
exclude group: 'com.fasterxml.jackson.core' // don't upgrade yet, breaks tests
370378
}
371379
generatedCodeClasspath('javax.annotation:javax.annotation-api:1.3.2')
372-
generatedCodeClasspath('junit:junit:4.12')
373380
generatedCodeClasspath('org.ini4j:ini4j:0.5.2')
374381
generatedCodeClasspath('org.syslog4j:syslog4j:0.9.46')
382+
383+
generatedTestCodeClasspath('junit:junit:4.12')
375384
}
376385

377386
task showTestClassPath {

src/test/java/us/kbase/test/sdk/scripts/TypeGeneratorTest.java

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import java.io.ByteArrayOutputStream;
88
import java.io.File;
9-
import java.io.FileInputStream;
109
import java.io.FileNotFoundException;
1110
import java.io.FileOutputStream;
1211
import java.io.IOException;
@@ -27,6 +26,7 @@
2726
import java.util.LinkedHashMap;
2827
import java.util.List;
2928
import java.util.Map;
29+
import java.util.stream.Stream;
3030
import java.util.zip.ZipEntry;
3131
import java.util.zip.ZipInputStream;
3232

@@ -82,7 +82,9 @@ public class TypeGeneratorTest {
8282
private static final String SERVICE_WIZARD = "ServiceWizard";
8383

8484
// specified in the build.gradle file
85-
private static final Path BUILD_LIB_DIR = Paths.get("build/generated-code-libs")
85+
private static final Path BUILD_LIB_IMPL_DIR = Paths.get("build/generated-code-libs/impl")
86+
.toAbsolutePath();
87+
private static final Path BUILD_LIB_TEST_DIR = Paths.get("build/generated-code-libs/test")
8688
.toAbsolutePath();
8789

8890
private static boolean debugClientTimes = false;
@@ -176,7 +178,7 @@ public void testSyslog() throws Exception {
176178
File binDir = new File(workDir, "bin");
177179
JavaData parsingData = prepareJavaCode(testNum, workDir, testPackage, libDir, binDir, null, true);
178180
javaServerCorrectionForTestCallback(srcDir, testPackage, parsingData, testPackage + ".Test" + testNum);
179-
String classPath = prepareClassPath(libDir, new ArrayList<URL>());
181+
String classPath = buildClassPath(libDir, new ArrayList<URL>());
180182
runJavac(workDir, srcDir, classPath, binDir, "src/us/kbase/test5/syslogtest/SyslogTestServer.java");
181183
int portNum = findFreePort();
182184
runJavaServerTest(testNum, true, workDir, testPackage, libDir, binDir, parsingData, null, portNum);
@@ -224,7 +226,7 @@ public void testServerCodeStoring() throws Exception {
224226
serverJavaFile.createNewFile();
225227
File libDir = new File(workDir, "lib");
226228
// Test for empty server file
227-
setUpTestJars(new DiskFileSaver(libDir));
229+
setUpJars(new DiskFileSaver(libDir));
228230
try {
229231
JavaTypeGenerator.processSpec(new File(workDir, testFileName),
230232
srcDir, testPackage, true, null);
@@ -245,7 +247,7 @@ public void testServerCodeStoring() throws Exception {
245247
new File(workDir, testFileName), srcDir, testPackage, true, null
246248
);
247249
List<URL> cpUrls = new ArrayList<URL>();
248-
String classPath = prepareClassPath(libDir, cpUrls);
250+
String classPath = buildClassPath(libDir, cpUrls);
249251
File binDir = new File(workDir, "bin");
250252
cpUrls.add(binDir.toURI().toURL());
251253
compileModulesIntoBin(workDir, srcDir, testPackage, parsingData, classPath, binDir);
@@ -827,7 +829,7 @@ protected static JavaData prepareJavaCode(
827829
parsingData = processSpec(workDir, testPackage, libDir, testFileName,
828830
srcDir, defaultUrl, isDynamic, isAsync);
829831
List<URL> cpUrls = new ArrayList<URL>();
830-
String classPath = prepareClassPath(libDir, cpUrls);
832+
String classPath = buildClassPath(libDir, cpUrls);
831833
cpUrls.add(binDir.toURI().toURL());
832834
compileModulesIntoBin(workDir, srcDir, testPackage, parsingData, classPath, binDir);
833835
String testJavaFileName = "Test" + testNum + ".java";
@@ -860,7 +862,7 @@ private static JavaData processSpec(
860862
final boolean isDynamic,
861863
final boolean isAsync
862864
) throws Exception {
863-
setUpTestJars(new DiskFileSaver(libDir));
865+
setUpJars(new DiskFileSaver(libDir));
864866
File specFile = new File(workDir, testFileName);
865867
List<KbService> services = KidlParser.parseSpec(specFile, null);
866868
JavaData parsingData = JavaTypeGenerator.processSpec(services, new DiskFileSaver(srcDir),
@@ -869,32 +871,20 @@ private static JavaData processSpec(
869871
return parsingData;
870872
}
871873

872-
private static void setUpTestJars(final FileSaver libOutDir) throws Exception {
873-
// TODO TEST CLEANUP remove this method and figure out some other way of handling test deps
874-
// maybe mark deps in gradle?
875-
checkLib(libOutDir, "jackson-annotations-2.2.3");
876-
checkLib(libOutDir, "jackson-core-2.2.3");
877-
checkLib(libOutDir, "jackson-databind-2.2.3");
878-
checkLib(libOutDir, "auth2_client_java-0.5.0");
879-
checkLib(libOutDir, "java_common-0.3.1");
880-
checkLib(libOutDir, "javax.annotation-api-1.3.2");
881-
checkLib(libOutDir, "servlet-api-2.5");
882-
checkLib(libOutDir, "jetty-all-7.0.0.v20091005");
883-
checkLib(libOutDir, "ini4j-0.5.2");
884-
checkLib(libOutDir, "syslog4j-0.9.46");
885-
checkLib(libOutDir, "jna-3.4.0");
886-
checkLib(libOutDir, "joda-time-2.2");
887-
checkLib(libOutDir, "logback-core-1.1.2");
888-
checkLib(libOutDir, "logback-classic-1.1.2");
889-
checkLib(libOutDir, "slf4j-api-1.7.7");
874+
private static void setUpJars(final FileSaver libOutDir) throws Exception {
875+
copyJarsFromDir(BUILD_LIB_IMPL_DIR, libOutDir);
876+
copyJarsFromDir(BUILD_LIB_TEST_DIR, libOutDir);
890877
}
891878

892-
private static void checkLib(FileSaver libDir, String libName) throws Exception {
893-
// TODO TEST CLEANUP try to eliminate this method entirely
894-
final Path lib = BUILD_LIB_DIR.resolve(libName + ".jar");
895-
InputStream is = new FileInputStream(lib.toFile());
896-
OutputStream os = libDir.openStream(lib.getFileName().toString());
897-
TextUtils.copyStreams(is, os);
879+
private static void copyJarsFromDir(final Path srcDir, final FileSaver dest) throws Exception {
880+
try (final Stream<Path> jars = Files.list(srcDir)) {
881+
for (final Path jar: jars.filter(p -> p.getFileName().toString().endsWith(".jar"))
882+
.toList()) {
883+
final InputStream is = Files.newInputStream(jar);
884+
final OutputStream os = dest.openStream(jar.getFileName().toString());
885+
TextUtils.copyStreams(is, os);
886+
}
887+
}
898888
}
899889

900890
private static File findPythonServerScript(File dir) {
@@ -918,10 +908,8 @@ private static void compileModulesIntoBin(File workDir, File srcDir, String test
918908
}
919909
}
920910

921-
private static String prepareClassPath(File libDir, List<URL> cpUrls)
911+
private static String buildClassPath(File libDir, List<URL> cpUrls)
922912
throws Exception {
923-
checkLib(new DiskFileSaver(libDir), "junit-4.12");
924-
checkLib(new DiskFileSaver(libDir), "hamcrest-core-1.3");
925913
StringBuilder classPathSB = new StringBuilder();
926914
for (File jarFile : libDir.listFiles()) {
927915
if (!jarFile.getName().endsWith(".jar"))
@@ -943,7 +931,7 @@ private static Class<?> createServerServletInstance(JavaModule module,
943931
private static URLClassLoader prepareUrlClassLoader(File libDir, File binDir)
944932
throws Exception, MalformedURLException {
945933
List<URL> cpUrls = new ArrayList<URL>();
946-
prepareClassPath(libDir, cpUrls);
934+
buildClassPath(libDir, cpUrls);
947935
cpUrls.add(binDir.toURI().toURL());
948936
URLClassLoader urlcl = URLClassLoader.newInstance(cpUrls.toArray(new URL[cpUrls.size()]));
949937
return urlcl;

0 commit comments

Comments
 (0)