diff options
Diffstat (limited to 'bundle-plugin/src/test/scala')
7 files changed, 424 insertions, 0 deletions
diff --git a/bundle-plugin/src/test/scala/com/yahoo/container/plugin/bundle/AnalyzeBundleTest.scala b/bundle-plugin/src/test/scala/com/yahoo/container/plugin/bundle/AnalyzeBundleTest.scala new file mode 100644 index 00000000000..7b2a840bc44 --- /dev/null +++ b/bundle-plugin/src/test/scala/com/yahoo/container/plugin/bundle/AnalyzeBundleTest.scala @@ -0,0 +1,39 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.plugin.bundle + +import org.scalatest.junit.{JUnitSuite, ShouldMatchersForJUnit} +import org.junit.Test +import com.yahoo.container.plugin.bundle.AnalyzeBundle.PublicPackages +import com.yahoo.container.plugin.osgi.ExportPackages +import java.io.File + +/** + * @author tonytv + */ +class AnalyzeBundleTest extends JUnitSuite with ShouldMatchersForJUnit { + val jarDir = new File("src/test/resources/jar") + + val PublicPackages(exports, globals) = AnalyzeBundle.publicPackagesAggregated( + List("notAOsgiBundle.jar", "simple1.jar").map(jarFile)) + + val exportsByPackageName = ExportPackages.exportsByPackageName(exports) + + def jarFile(name : String) : File = new File(jarDir, name) + + @Test + def require_that_non_osgi_bundles_are_ignored() { + exportsByPackageName should not contain key("com.yahoo.sample.exported.package.ignored") + } + + @Test + def require_that_exports_are_retrieved_from_manifest_in_jars() { + exportsByPackageName.keySet should be (Set("com.yahoo.sample.exported.package")) + } + + @Test + def require_that_invalid_exports_throws_exception() { + val exception = intercept[Exception](AnalyzeBundle.publicPackages(jarFile("errorExport.jar"))) + exception.getMessage should startWith regex ("Invalid manifest in bundle '.*errorExport.jar'") + exception.getCause.getMessage should startWith ("Failed parsing Export-Package") + } +} diff --git a/bundle-plugin/src/test/scala/com/yahoo/container/plugin/classanalysis/AnalyzeClassTest.scala b/bundle-plugin/src/test/scala/com/yahoo/container/plugin/classanalysis/AnalyzeClassTest.scala new file mode 100644 index 00000000000..3a8e67272a9 --- /dev/null +++ b/bundle-plugin/src/test/scala/com/yahoo/container/plugin/classanalysis/AnalyzeClassTest.scala @@ -0,0 +1,115 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.plugin.classanalysis + +import org.junit.Test +import org.scalatest.junit.{ShouldMatchersForJUnit, JUnitSuite} +import java.awt.Image +import java.awt.image.{ImagingOpException, Kernel} +import sampleclasses._ +import TestUtilities._ +import com.yahoo.osgi.annotation.{ExportPackage, Version} +import javax.security.auth.login.LoginException + +/** + * Tests that analysis of class files works. + * @author tonytv + */ +class AnalyzeClassTest extends JUnitSuite with ShouldMatchersForJUnit { + @Test def require_that_full_class_name_is_returned() { + analyzeClass[Base].name should be(name[Base]) + } + + @Test def require_that_base_class_is_included() { + analyzeClass[Derived].referencedClasses should contain (name[Base]) + } + + @Test def require_that_implemented_interfaces_are_included() { + analyzeClass[Base].referencedClasses should (contain (name[Interface1]) and contain (name[Interface2])) + } + + @Test def require_that_interface_can_be_analyzed() { + val classMetaData = analyzeClass[Interface1] + + classMetaData.name should be(name[Interface1]) + classMetaData.referencedClasses should contain(name[Interface2]) + } + + @Test def require_that_return_type_is_included() { + analyzeClass[Interface1].referencedClasses should contain (name[Image]) + } + + @Test def require_that_parameters_are_included() { + analyzeClass[Interface1].referencedClasses should contain (name[Kernel]) + } + + @Test def require_that_exceptions_are_included() { + analyzeClass[Interface1].referencedClasses should contain (name[ImagingOpException]) + } + + @Test def require_that_basic_types_ignored() { + analyzeClass[Interface1].referencedClasses should not (contain ("int") or contain ("float")) + } + + @Test def require_that_arrays_of_basic_types_ignored() { + analyzeClass[Interface1].referencedClasses should not (contain ("int[]") or contain ("int[][]")) + } + + @Test def require_that_instance_field_types_are_included() { + analyzeClass[Fields].referencedClasses should contain (name[String]) + } + + @Test def require_that_static_field_types_are_included() { + analyzeClass[Fields].referencedClasses should contain (name[java.util.List[_]]) + } + + @Test def require_that_field_annotation_is_included() { + analyzeClass[Fields].referencedClasses should contain (name[DummyAnnotation]) + } + + @Test def require_that_class_annotation_is_included() { + analyzeClass[ClassAnnotation].referencedClasses should contain(name[DummyAnnotation]) + } + + @Test def require_that_method_annotation_is_included() { + analyzeClass[MethodAnnotation].referencedClasses should contain(name[DummyAnnotation]) + } + + @Test def require_that_export_package_annotations_are_ignored() { + Analyze.analyzeClass(classFile("com.yahoo.container.plugin.classanalysis.sampleclasses.package-info")). + referencedClasses should not (contain (name[ExportPackage]) or contain (name[Version])) + } + + @Test def require_that_export_annotations_are_processed() { + Analyze.analyzeClass(classFile("com.yahoo.container.plugin.classanalysis.sampleclasses.package-info")). + exportPackage should be (Some(ExportPackageAnnotation(3, 1, 4, "TEST_QUALIFIER-2"))) + } + + @Test def require_that_export_annotations_are_validated() { + val exception = intercept[RuntimeException] { + Analyze.analyzeClass(classFile("com.yahoo.container.plugin.classanalysis.sampleclasses.invalid.package-info")) + } + + exception.getMessage should include ("invalid/package-info") + exception.getCause.getMessage should include ("qualifier must follow the format") + exception.getCause.getMessage should include ("'EXAMPLE INVALID QUALIFIER'") + } + + @Test def require_that_catch_clauses_are_included() { + Analyze.analyzeClass(classFile("com.yahoo.container.plugin.classanalysis.sampleclasses.CatchException")). + referencedClasses should contain(name[LoginException]) + } + + @Test def require_that_class_references_are_included() { + Analyze.analyzeClass(classFile("com.yahoo.container.plugin.classanalysis.sampleclasses.ClassReference")). + referencedClasses should contain(name[Interface1]) + } + + @Test def require_that_return_type_of_method_invocations_are_included() { + analyzeClass[MethodInvocation].referencedClasses should contain(name[Image]) + } + + @Test def require_that_attributes_are_included() { + //Uses com/coremedia/iso/Utf8.class from com.googlecode.mp4parser:isoparser:1.0-RC-1 + Analyze.analyzeClass(classFile("class/Utf8")).referencedClasses should contain("org.aspectj.weaver.MethodDeclarationLineNumber") + } +} diff --git a/bundle-plugin/src/test/scala/com/yahoo/container/plugin/classanalysis/AnalyzeMethodBodyTest.scala b/bundle-plugin/src/test/scala/com/yahoo/container/plugin/classanalysis/AnalyzeMethodBodyTest.scala new file mode 100644 index 00000000000..24fa26b97e1 --- /dev/null +++ b/bundle-plugin/src/test/scala/com/yahoo/container/plugin/classanalysis/AnalyzeMethodBodyTest.scala @@ -0,0 +1,50 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.plugin.classanalysis + +import org.scalatest.junit.{JUnitSuite, ShouldMatchersForJUnit} +import sampleclasses._ +import TestUtilities._ +import org.junit.Test +import java.io.PrintStream + +/** + * Tests that classes used in method bodies are included in the imports list. + * @author tonytv + */ +class AnalyzeMethodBodyTest extends JUnitSuite with ShouldMatchersForJUnit { + @Test def require_that_class_of_locals_are_included() { + analyzeClass[Methods].referencedClasses should contain(name[Base]) + } + + @Test def require_that_class_of_locals_in_static_method_are_included() { + analyzeClass[Methods].referencedClasses should contain(name[Derived]) + } + + @Test def require_that_field_references_are_included() { + analyzeClass[Methods].referencedClasses should (contain (name[java.util.List[_]]) and contain (name[Fields])) + } + + @Test def require_that_class_owning_field_is_included() { + analyzeClass[Methods].referencedClasses should contain (name[System]) + } + + @Test def require_that_class_containing_method_is_included() { + analyzeClass[Methods].referencedClasses should contain (name[PrintStream]) + } + + @Test def require_that_element_of_new_multidimensional_array_is_included() { + analyzeClass[Methods].referencedClasses should contain (name[Interface1]) + } + + @Test def require_that_basic_arrays_are_not_included() { + analyzeClass[Methods].referencedClasses should not (contain("int[]")) + } + + @Test def require_that_container_generic_parameters_are_included() { + analyzeClass[Methods].referencedClasses should contain(name[Dummy]) + } + + @Test def require_that_class_owning_method_handler_is_included() { + analyzeClass[Methods].referencedClasses should contain(name[ClassWithMethod]) + } +} diff --git a/bundle-plugin/src/test/scala/com/yahoo/container/plugin/classanalysis/TestUtilities.scala b/bundle-plugin/src/test/scala/com/yahoo/container/plugin/classanalysis/TestUtilities.scala new file mode 100644 index 00000000000..8c3d9bc5d5c --- /dev/null +++ b/bundle-plugin/src/test/scala/com/yahoo/container/plugin/classanalysis/TestUtilities.scala @@ -0,0 +1,18 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.plugin.classanalysis + +import scala.reflect.ClassTag +import java.io.File + +/** + * @author tonytv + */ +object TestUtilities { + def analyzeClass[T](implicit m: ClassTag[T]) = + Analyze.analyzeClass(classFile(name(m))) + + def classFile(className : String) = + new File("target/test-classes/" + className.replace('.', '/') + ".class") + + def name[T](implicit m: ClassTag[T]) = m.runtimeClass.getName +} diff --git a/bundle-plugin/src/test/scala/com/yahoo/container/plugin/osgi/ExportPackageParserTest.scala b/bundle-plugin/src/test/scala/com/yahoo/container/plugin/osgi/ExportPackageParserTest.scala new file mode 100644 index 00000000000..1a4767206a1 --- /dev/null +++ b/bundle-plugin/src/test/scala/com/yahoo/container/plugin/osgi/ExportPackageParserTest.scala @@ -0,0 +1,63 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.plugin.osgi + +import org.scalatest.junit.{JUnitSuite, ShouldMatchersForJUnit} +import org.junit.Test + +import ExportPackages.{Parameter, Export} + +/** + * @author tonytv + */ +class ExportPackageParserTest extends JUnitSuite with ShouldMatchersForJUnit { + val versionParameter = Parameter("version", "1.2.3.sample") + + @Test + def require_that_package_is_parsed_correctly() { + ExportPackageParser.parseAll("sample.exported.package").get should + be (List(Export(List("sample.exported.package"), List()))) + } + + @Test + def require_that_version_is_parsed_correctly() { + ExportPackageParser.parseAll("com.yahoo.sample.exported.package;version=\"1.2.3.sample\"").get should + be (List(Export(List("com.yahoo.sample.exported.package"), List(versionParameter)))) + } + + @Test + def require_that_multiple_packages_with_same_parameters_is_parsed_correctly() { + ExportPackageParser.parseAll("exported.package1;exported.package2;version=\"1.2.3.sample\"").get should + be (List(Export(List("exported.package1", "exported.package2"), List(versionParameter)))) + } + + @Test + def require_that_multiple_parameters_for_a_package_is_parsed_correctly() { + ExportPackageParser.parseAll("exported.package;version=\"1.2.3.sample\";param2=true").get.head.parameters should + be (List(versionParameter, Parameter("param2", "true"))) + } + + @Test + def require_that_multiple_exports_are_parsed_correctly() { + ExportPackageParser.parseAll("exported.package1,exported.package2").get should + be(List( + Export(List("exported.package1"), List()), + Export(List("exported.package2"), List()))) + + ExportPackageParser.parseAll("exported.package1;version=\"1.2.3.sample\",exported.package2").get should + be (List( + Export(List("exported.package1"), List(versionParameter)), + Export(List("exported.package2"), List()))) + + ExportPackageParser.parseAll("exported.package1,exported.package2;version=\"1.2.3.sample\"").get should + be(List( + Export(List("exported.package1"), List()), + Export(List("exported.package2"), List(versionParameter)))) + } + + @Test + def require_that_long_string_literals_do_not_cause_stack_overflow_error() { + //From jersey-server-1.13.jar + val export = """com.sun.jersey.server.impl.wadl;uses:="com.sun.jersey.api.model,com.sun.research.ws.wadl,com.sun.jersey.api.wadl.config,com.sun.jersey.server.wadl,com.sun.jersey.api.core,javax.xml.bind,javax.ws.rs.core,com.sun.jersey.server.impl.uri,com.sun.jersey.core.spi.factory,com.sun.jersey.server.impl.model.method,com.sun.jersey.api.uri,com.sun.jersey.core.header,com.sun.jersey.spi.dispatch,javax.ws.rs,com.sun.jersey.spi.resource";version="1.13.0",com.sun.jersey.server.impl.model.parameter.multivalued;uses:="com.sun.jersey.spi,javax.ws.rs.core,com.sun.jersey.api.container,com.sun.jersey.impl,javax.xml.parsers,org.xml.sax,javax.xml.transform,javax.xml.bind.annotation,javax.xml.transform.sax,com.sun.jersey.spi.inject,javax.xml.bind,javax.ws.rs.ext,com.sun.jersey.api.model,com.sun.jersey.core.reflection,javax.ws.rs,com.sun.jersey.core.spi.component,com.sun.jersey.core.header";version="1.13.0",com.sun.jersey.server.impl.model.parameter;uses:="com.sun.jersey.api.model,com.sun.jersey.core.spi.component,com.sun.jersey.server.impl.model.parameter.multivalued,com.sun.jersey.spi.inject,com.sun.jersey.api,com.sun.jersey.api.core,com.sun.jersey.server.impl.inject,javax.ws.rs.core,javax.ws.rs,com.sun.jersey.core.header,com.sun.jersey.api.representation";version="1.13.0",com.sun.jersey.server.impl.application;uses:="com.sun.jersey.core.spi.component,com.sun.jersey.api.core,com.sun.jersey.spi,com.sun.jersey.spi.inject,javax.ws.rs.core,com.sun.jersey.api.container,javax.ws.rs.ext,com.sun.jersey.spi.container,com.sun.jersey.core.reflection,com.sun.jersey.api.model,com.sun.jersey.impl,com.sun.jersey.spi.dispatch,com.sun.jersey.server.impl.model,com.sun.jersey.server.impl.wadl,com.sun.jersey.server.impl.uri,com.sun.jersey.core.spi.factory,com.sun.jersey.api.uri,com.sun.jersey.server.impl.uri.rules,com.sun.jersey.spi.uri.rules,com.sun.jersey.server.spi.component,com.sun.jersey.core.util,com.sun.jersey.core.header,com.sun.jersey.core.spi.component.ioc,javax.ws.rs,com.sun.jersey.server.impl,com.sun.jersey.server.wadl,com.sun.jersey.server.impl.inject,com.sun.jersey.server.impl.component,com.sun.jersey.spi.monitoring,com.sun.jersey.server.impl.monitoring,com.sun.jersey.api.container.filter,com.sun.jersey.server.impl.model.parameter.multivalued,com.sun.jersey.server.impl.model.parameter,com.sun.jersey.server.impl.template,com.sun.jersey.spi.template,com.sun.jersey.server.impl.resource,com.sun.jersey.server.impl.modelapi.annotation,com.sun.jersey.server.impl.container.filter,com.sun.jersey.server.impl.modelapi.validation,com.sun.jersey.api,com.sun.jersey.spi.service";version="1.13.0",com.sun.jersey.server.impl.component;uses:="com.sun.jersey.api.model,com.sun.jersey.core.spi.component,com.sun.jersey.server.spi.component,com.sun.jersey.api.core,com.sun.jersey.core.spi.component.ioc,com.sun.jersey.server.impl.inject,com.sun.jersey.server.impl.resource,com.sun.jersey.api.container,com.sun.jersey.core.reflection,com.sun.jersey.spi.inject";version="1.13.0",com.sun.jersey.server.impl.provider;uses:="com.sun.jersey.core.spi.factory,com.sun.jersey.api.container,com.sun.jersey.api.core,javax.ws.rs.core";version="1.13.0",com.sun.jersey.server.impl.template;uses:="com.sun.jersey.core.spi.component,com.sun.jersey.api.view,com.sun.jersey.spi.template,javax.ws.rs.core,com.sun.jersey.core.header,com.sun.jersey.server.impl.model.method,com.sun.jersey.spi.dispatch,com.sun.jersey.api.uri,javax.ws.rs,com.sun.jersey.spi.inject,javax.ws.rs.ext,com.sun.jersey.server.impl.uri.rules,com.sun.jersey.server.probes,com.sun.jersey.core.reflection,com.sun.jersey.spi.uri.rules,com.sun.jersey.spi.container,com.sun.jersey.api.core";version="1.13.0",com.sun.jersey.server.osgi;uses:="com.sun.jersey.server.impl.provider,org.osgi.framework,javax.ws.rs.ext";version="1.13.0",com.sun.jersey.server.wadl.generators.resourcedoc.model;uses:="javax.xml.bind.annotation,javax.xml.namespace";version="1.13.0",com.sun.jersey.server.impl.resource;uses:="com.sun.jersey.api.model,com.sun.jersey.core.spi.component,com.sun.jersey.server.spi.component,com.sun.jersey.api.core,com.sun.jersey.api.container,javax.ws.rs,com.sun.jersey.server.impl.inject,com.sun.jersey.core.spi.component.ioc,javax.ws.rs.core";version="1.13.0",com.sun.jersey.server.impl.monitoring;uses:="com.sun.jersey.spi.monitoring,com.sun.jersey.spi.service,com.sun.jersey.api.model,com.sun.jersey.spi.container,javax.ws.rs.ext,com.sun.jersey.core.spi.component";version="1.13.0",com.sun.jersey.server.impl.modelapi.annotation;uses:="com.sun.jersey.api.model,javax.ws.rs.core,javax.ws.rs,com.sun.jersey.core.reflection,com.sun.jersey.core.header,com.sun.jersey.impl";version="1.13.0",com.sun.jersey.server.impl.container;uses:="com.sun.jersey.server.impl.application,com.sun.jersey.spi.container,com.sun.jersey.api.container";version="1.13.0",com.sun.jersey.server.wadl;uses:="javax.ws.rs.core,com.sun.research.ws.wadl,javax.xml.namespace,com.sun.jersey.api.model,javax.xml.bind,javax.ws.rs,com.sun.jersey.server.wadl.generators,com.sun.jersey.server.impl.modelapi.annotation,com.sun.jersey.server.impl";version="1.13.0",com.sun.jersey.server.impl.model.method.dispatch;uses:="com.sun.jersey.api.model,com.sun.jersey.api.core,com.sun.jersey.spi.container,com.sun.jersey.server.impl.inject,com.sun.jersey.api,javax.ws.rs.core,com.sun.jersey.core.spi.factory,com.sun.jersey.spi.inject,com.sun.jersey.spi.dispatch,com.sun.jersey.core.spi.component,javax.ws.rs,com.sun.jersey.server.impl.model.parameter.multivalued,com.sun.jersey.api.representation,com.sun.jersey.api.container";version="1.13.0",com.sun.jersey.server.impl;uses:="javax.naming,com.sun.jersey.api.core,com.sun.jersey.core.header,javax.ws.rs.core,com.sun.jersey.server.impl.model,com.sun.jersey.spi.container";version="1.13.0",com.sun.jersey.server.wadl.generators.resourcedoc;uses:="com.sun.jersey.api.model,com.sun.jersey.server.wadl.generators.resourcedoc.model,com.sun.jersey.server.wadl.generators.resourcedoc.xhtml,com.sun.research.ws.wadl,javax.xml.namespace,com.sun.jersey.server.wadl,javax.xml.bind,javax.ws.rs.core";version="1.13.0",com.sun.jersey.server.impl.container.httpserver;uses:="com.sun.net.httpserver,com.sun.jersey.spi.container,javax.ws.rs.core,com.sun.jersey.core.header,com.sun.jersey.api.container,com.sun.jersey.core.util,com.sun.jersey.api.core";version="1.13.0",com.sun.jersey.server.impl.container.filter;uses:="com.sun.jersey.api.model,com.sun.jersey.spi.container,com.sun.jersey.core.spi.component,com.sun.jersey.api.core,javax.ws.rs,com.sun.jersey.server.impl.uri,javax.ws.rs.core";version="1.13.0",com.sun.jersey.server.wadl.generators.resourcedoc.xhtml;uses:="javax.xml.bind,javax.xml.namespace,javax.xml.bind.annotation";version="1.13.0",com.sun.jersey.server.impl.uri.rules;uses:="com.sun.jersey.spi.uri.rules,com.sun.jersey.api.uri,com.sun.jersey.api.core,com.sun.jersey.server.impl.model.method,com.sun.jersey.spi.dispatch,com.sun.jersey.core.header,javax.ws.rs.core,com.sun.jersey.api.model,com.sun.jersey.server.probes,com.sun.jersey.core.reflection,com.sun.jersey.server.impl.template,com.sun.jersey.spi.monitoring,com.sun.jersey.api,com.sun.jersey.spi.container,com.sun.jersey.server.impl.uri,javax.ws.rs,com.sun.jersey.api.container,com.sun.jersey.server.impl.inject,com.sun.jersey.spi.inject,com.sun.jersey.server.impl.uri.rules.automata";version="1.13.0",com.sun.jersey.server.spi.component;uses:="com.sun.jersey.spi.inject,com.sun.jersey.api.model,com.sun.jersey.core.spi.component,com.sun.jersey.api.core,com.sun.jersey.server.impl.inject,com.sun.jersey.api.container,com.sun.jersey.core.spi.component.ioc";version="1.13.0",com.sun.jersey.server.probes;version="1.13.0",com.sun.jersey.server.wadl.generators;uses:="com.sun.research.ws.wadl,javax.xml.bind.annotation,com.sun.jersey.api.model,com.sun.jersey.server.wadl,javax.xml.bind,javax.ws.rs.core,com.sun.jersey.api,javax.xml.namespace,javax.xml.transform,javax.xml.transform.stream";version="1.13.0",com.sun.jersey.server.impl.modelapi.validation;uses:="com.sun.jersey.api.model,javax.ws.rs,com.sun.jersey.impl,com.sun.jersey.api.core,com.sun.jersey.core.reflection,javax.ws.rs.core";version="1.13.0",com.sun.jersey.server.impl.model.method;uses:="com.sun.jersey.api.container,com.sun.jersey.spi.dispatch,com.sun.jersey.api.uri,com.sun.jersey.api.model,com.sun.jersey.server.impl.container.filter,com.sun.jersey.impl,com.sun.jersey.spi.container,com.sun.jersey.spi.inject,com.sun.jersey.api.core,javax.ws.rs.core,com.sun.jersey.core.header";version="1.13.0",com.sun.jersey.server.impl.model;uses:="javax.ws.rs,com.sun.jersey.impl,com.sun.jersey.api.container,com.sun.jersey.core.header,com.sun.jersey.core.header.reader,com.sun.jersey.api.core,javax.ws.rs.core,com.sun.jersey.server.impl.model.method,com.sun.jersey.server.impl.container.filter,com.sun.jersey.api.model,com.sun.jersey.server.impl.wadl,com.sun.jersey.spi.monitoring,com.sun.jersey.server.impl.uri,com.sun.jersey.spi.container,com.sun.jersey.server.impl.inject,com.sun.jersey.spi.inject,com.sun.jersey.api.uri,com.sun.jersey.core.spi.component,com.sun.jersey.server.impl.uri.rules,com.sun.jersey.server.impl.template,com.sun.jersey.api.view,com.sun.jersey.spi.uri.rules";version="1.13.0",com.sun.jersey.server.impl.uri.rules.automata;uses:="com.sun.jersey.server.impl.uri,com.sun.jersey.spi.uri.rules,com.sun.jersey.server.impl.uri.rules,com.sun.jersey.api.uri";version="1.13.0",com.sun.jersey.server.impl.uri;uses:="com.sun.jersey.api.uri,javax.ws.rs.core";version="1.13.0",com.sun.jersey.server.impl.inject;uses:="com.sun.jersey.api.core,com.sun.jersey.spi.inject,javax.ws.rs,com.sun.jersey.api.container,com.sun.jersey.api.model,com.sun.jersey.core.spi.component,com.sun.jersey.core.spi.factory";version="1.13.0",com.sun.jersey.spi.scanning;uses:="org.objectweb.asm,com.sun.jersey.core.reflection,com.sun.jersey.core.spi.scanning,javax.ws.rs,javax.ws.rs.ext";version="1.13.0",com.sun.jersey.spi.resource;uses:="com.sun.jersey.server.impl.resource,com.sun.jersey.server.spi.component";version="1.13.0",com.sun.jersey.spi.template;uses:="com.sun.jersey.api.view,javax.ws.rs.core,com.sun.jersey.api.container";version="1.13.0",com.sun.jersey.spi.dispatch;uses:="com.sun.jersey.api.core";version="1.13.0",com.sun.jersey.spi.uri.rules;uses:="com.sun.jersey.api.core,com.sun.jersey.api.model,com.sun.jersey.spi.container,com.sun.jersey.api.uri";version="1.13.0",com.sun.jersey.spi.container;uses:="javax.ws.rs,com.sun.jersey.api.representation,com.sun.jersey.core.header,com.sun.jersey.spi,javax.ws.rs.core,com.sun.jersey.api.container,com.sun.jersey.api.core,com.sun.jersey.core.util,com.sun.jersey.core.header.reader,com.sun.jersey.server.impl,com.sun.jersey.core.reflection,javax.ws.rs.ext,com.sun.jersey.server.impl.model,com.sun.jersey.api,com.sun.jersey.api.uri,com.sun.jersey.core.spi.factory,com.sun.jersey.spi.monitoring,com.sun.jersey.api.model,com.sun.jersey.core.spi.component,com.sun.jersey.server.impl.application,com.sun.jersey.impl,com.sun.jersey.spi.inject,com.sun.jersey.spi.dispatch,com.sun.jersey.server.impl.inject,com.sun.jersey.core.spi.component.ioc,com.sun.jersey.spi.service";version="1.13.0",com.sun.jersey.spi.monitoring;uses:="com.sun.jersey.api.model,com.sun.jersey.spi.container,javax.ws.rs.ext";version="1.13.0",com.sun.jersey.api;uses:="javax.ws.rs,javax.ws.rs.core,com.sun.jersey.core.header,com.sun.jersey.core.spi.factory";version="1.13.0",com.sun.jersey.api.core;uses:="javax.ws.rs.core,com.sun.jersey.core.spi.scanning,com.sun.jersey.api.model,com.sun.jersey.api.uri,javax.ws.rs,com.sun.jersey.core.header,com.sun.jersey.api.representation,com.sun.jersey.core.util,javax.ws.rs.ext,com.sun.jersey.api.container,com.sun.jersey.spi.scanning,com.sun.jersey.spi.container,com.sun.jersey.server.impl.application";version="1.13.0",com.sun.jersey.api.wadl.config;uses:="com.sun.jersey.server.wadl,com.sun.jersey.api.core,com.sun.jersey.core.reflection,com.sun.jersey.server.wadl.generators";version="1.13.0",com.sun.jersey.api.model;uses:="javax.ws.rs.core,com.sun.jersey.spi.container";version="1.13.0",com.sun.jersey.api.view;version="1.13.0",com.sun.jersey.api.container.filter;uses:="javax.ws.rs,com.sun.jersey.spi.container,javax.ws.rs.core,com.sun.jersey.api.container,com.sun.jersey.api.core,com.sun.jersey.core.util,com.sun.jersey.core.header,com.sun.jersey.api.representation,com.sun.jersey.api.model,javax.annotation.security";version="1.13.0",com.sun.jersey.api.container;uses:="com.sun.jersey.api.core,com.sun.jersey.spi.container,com.sun.jersey.spi.service,com.sun.jersey.core.spi.component.ioc";version="1.13.0",com.sun.jersey.api.container.httpserver;uses:="com.sun.net.httpserver,com.sun.jersey.api.core,com.sun.jersey.api.container,com.sun.jersey.core.spi.component.ioc";version="1.13.0",com.sun.research.ws.wadl;uses:="javax.xml.bind.annotation,javax.xml.bind.annotation.adapters,javax.xml.namespace";version="1.13.0""" + ExportPackageParser.parseAll(export) //throws StackOverflow exception on scala library 2.10.2 + } +} diff --git a/bundle-plugin/src/test/scala/com/yahoo/container/plugin/osgi/ImportPackageTest.scala b/bundle-plugin/src/test/scala/com/yahoo/container/plugin/osgi/ImportPackageTest.scala new file mode 100644 index 00000000000..c3e3dd235e3 --- /dev/null +++ b/bundle-plugin/src/test/scala/com/yahoo/container/plugin/osgi/ImportPackageTest.scala @@ -0,0 +1,95 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.plugin.osgi + +import org.scalatest.junit.{JUnitSuite, ShouldMatchersForJUnit} +import org.junit.Test + +import ImportPackages.Import +import ExportPackages.{Parameter, Export} + +/** + * @author tonytv + */ +class ImportPackageTest extends JUnitSuite with ShouldMatchersForJUnit { + val referencedPackages = Set("com.yahoo.exported") + val exports = exportByPackageName(Export(List("com.yahoo.exported"), List())) + val exportsWithVersion = exportByPackageName(exports.head._2.copy(parameters = List(Parameter("version", "1.3")))) + + def exportByPackageName(export : Export) = ExportPackages.exportsByPackageName(List(export)) + @Test + def require_that_non_implemented_import_with_matching_export_is_included() { + val imports = calculateImports(referencedPackages, implementedPackages = Set(), exportedPackages = exports) + imports should be (Set(Import("com.yahoo.exported", None))) + } + + + @Test + def require_that_non_implemented_import_without_matching_export_is_excluded() { + val imports = calculateImports(referencedPackages, implementedPackages = Set(), exportedPackages = Map()) + imports should be (Set()) + } + + @Test + def require_that_implemented_import_with_matching_export_is_excluded() { + val imports = calculateImports( + referencedPackages, + implementedPackages = referencedPackages, + exportedPackages = exports) + + imports should be (Set()) + } + + @Test + def require_that_version_is_included() { + val imports = calculateImports(referencedPackages, implementedPackages = Set(), exportedPackages = exportsWithVersion) + + imports should be (Set(Import("com.yahoo.exported", Some("1.3")))) + } + + @Test + def require_that_all_versions_up_to_the_next_major_version_is_in_range() { + Import("foo", Some("1.2")).importVersionRange should be (Some("[1.2,2)")) + } + + // TODO: Detecting guava packages should be based on bundle-symbolicName, not package name. + @Test + def require_that_for_guava_all_future_major_versions_are_in_range() { + val rangeWithInfiniteUpperLimit = Some("[18.1," + ImportPackages.InfiniteVersion + ")") + Import("com.google.common", Some("18.1")).importVersionRange should be (rangeWithInfiniteUpperLimit) + Import("com.google.common.foo", Some("18.1")).importVersionRange should be (rangeWithInfiniteUpperLimit) + + Import("com.google.commonality", Some("18.1")).importVersionRange should be (Some("[18.1,19)")) + } + + @Test + def require_that_none_version_gives_non_version_range() { + Import("foo", None).importVersionRange should be (None) + } + + @Test + def require_that_exception_is_thrown_when_major_component_is_non_numeric() { + intercept[IllegalArgumentException](Import("foo", Some("1notValid.2"))) + } + + @Test + def require_that_osgi_import_supports_missing_version() { + Import("com.yahoo.exported", None).asOsgiImport should be ("com.yahoo.exported") + } + + @Test + def require_that_osgi_import_version_range_includes_all_versions_from_the_current_up_to_the_next_major_version() { + Import("com.yahoo.exported", Some("1.2")).asOsgiImport should be ("com.yahoo.exported;version=\"[1.2,2)\"") + } + + @Test + def require_that_osgi_import_version_range_ignores_qualifier() { + Import("com.yahoo.exported", Some("1.2.3.qualifier")).asOsgiImport should be ("com.yahoo.exported;version=\"[1.2.3,2)\"") + } + + + def calculateImports(referencedPackages : Set[String], + implementedPackages : Set[String], + exportedPackages : Map[String, Export]) : Set[Import] = { + ImportPackages.calculateImports(referencedPackages, implementedPackages, exportedPackages).values.toSet + } +} diff --git a/bundle-plugin/src/test/scala/com/yahoo/container/plugin/util/IOTest.scala b/bundle-plugin/src/test/scala/com/yahoo/container/plugin/util/IOTest.scala new file mode 100644 index 00000000000..16f4f5237f1 --- /dev/null +++ b/bundle-plugin/src/test/scala/com/yahoo/container/plugin/util/IOTest.scala @@ -0,0 +1,44 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.plugin.util + +import org.scalatest.junit.{JUnitSuite, ShouldMatchersForJUnit} +import org.junit.Test + +import IO.using +import java.io.Closeable + +/** + * @author tonytv + */ +class IOTest extends JUnitSuite with ShouldMatchersForJUnit { + class ClosingException extends RuntimeException + class FunctionException extends RuntimeException + + object throwWhenClosingResource extends Closeable { + def close() { + throw new ClosingException(); + } + } + + def throwFunction(r : throwWhenClosingResource.type) = throw new FunctionException + def nonThrowingFunction(r : throwWhenClosingResource.type) = 42 + + @Test + def require_that_function_exception_is_prioritized_over_closing_exception() { + intercept[FunctionException]{ + using(throwWhenClosingResource, readOnly = false)(throwFunction) + } + } + + @Test + def require_that_closing_exception_is_ignored_when_read_only() { + using(throwWhenClosingResource, readOnly = true)(nonThrowingFunction) should be (nonThrowingFunction(null)) + } + + @Test + def require_that_closing_exception_is_rethrown_when_not_read_only() { + intercept[ClosingException] { + using(throwWhenClosingResource, readOnly = false)(nonThrowingFunction) + } + } +} |