Search in sources :

Example 16 with ToolContext

use of org.apache.cxf.tools.common.ToolContext in project cxf by apache.

the class JAXRSContainerTest method testComplexPath.

@Test
public void testComplexPath() {
    try {
        JAXRSContainer container = new JAXRSContainer(null);
        ToolContext context = new ToolContext();
        context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
        context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/testComplexPath.xml"));
        context.put(WadlToolConstants.CFG_COMPILE, "true");
        container.setContext(context);
        container.execute();
        assertNotNull(output.list());
        List<File> files = FileUtils.getFilesRecurse(output, ".+\\." + "class" + "$");
        assertEquals(1, files.size());
        assertTrue(checkContains(files, "application.Resource.class"));
        @SuppressWarnings("resource") ClassLoader loader = new URLClassLoader(new URL[] { output.toURI().toURL() });
        Class<?> test1 = loader.loadClass("application.Resource");
        Method[] test1Methods = test1.getDeclaredMethods();
        assertEquals(2, test1Methods.length);
        assertEquals(2, test1Methods[0].getAnnotations().length);
        if ("getGetaddmethod2".equals(test1Methods[0].getName())) {
            Method tmp = test1Methods[0];
            test1Methods[0] = test1Methods[1];
            test1Methods[1] = tmp;
        }
        checkComplexPathMethod(test1Methods[0], "");
        checkComplexPathMethod(test1Methods[1], "2");
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) ToolContext(org.apache.cxf.tools.common.ToolContext) Method(java.lang.reflect.Method) File(java.io.File) URISyntaxException(java.net.URISyntaxException) Test(org.junit.Test)

Example 17 with ToolContext

use of org.apache.cxf.tools.common.ToolContext in project cxf by apache.

the class JAXRSContainerTest method testOnewayMethod.

@Test
public void testOnewayMethod() throws Exception {
    JAXRSContainer container = new JAXRSContainer(null);
    final String onewayMethod = "deleteRepository";
    ToolContext context = new ToolContext();
    context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
    context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/test.xml"));
    context.put(WadlToolConstants.CFG_COMPILE, "true");
    context.put(WadlToolConstants.CFG_ONEWAY, onewayMethod);
    container.setContext(context);
    container.execute();
    assertNotNull(output.list());
    ClassCollector cc = context.get(ClassCollector.class);
    assertEquals(1, cc.getServiceClassNames().size());
    try (URLClassLoader loader = new URLClassLoader(new URL[] { output.toURI().toURL() })) {
        final Class<?> generatedClass = loader.loadClass(cc.getServiceClassNames().values().iterator().next());
        Method m = generatedClass.getMethod(onewayMethod, String.class);
        assertNotNull(m.getAnnotation(Oneway.class));
    }
}
Also used : Oneway(org.apache.cxf.jaxrs.ext.Oneway) ClassCollector(org.apache.cxf.tools.util.ClassCollector) URLClassLoader(java.net.URLClassLoader) ToolContext(org.apache.cxf.tools.common.ToolContext) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 18 with ToolContext

use of org.apache.cxf.tools.common.ToolContext in project cxf by apache.

the class JAXRSContainerTest method testQueryMultipartParam.

@Test
public void testQueryMultipartParam() {
    try {
        JAXRSContainer container = new JAXRSContainer(null);
        ToolContext context = new ToolContext();
        context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
        context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/testQueryMultipartParam.wadl"));
        context.put(WadlToolConstants.CFG_COMPILE, "true");
        container.setContext(context);
        container.execute();
        assertNotNull(output.list());
        List<File> files = FileUtils.getFilesRecurse(output, ".+\\." + "class" + "$");
        assertEquals(2, files.size());
        assertTrue(checkContains(files, "application.Test1.class"));
        assertTrue(checkContains(files, "application.Test2.class"));
        @SuppressWarnings("resource") ClassLoader loader = new URLClassLoader(new URL[] { output.toURI().toURL() });
        Class<?> test1 = loader.loadClass("application.Test1");
        Method[] test1Methods = test1.getDeclaredMethods();
        assertEquals(1, test1Methods.length);
        assertEquals(2, test1Methods[0].getAnnotations().length);
        assertNotNull(test1Methods[0].getAnnotation(PUT.class));
        Consumes consumes1 = test1Methods[0].getAnnotation(Consumes.class);
        assertNotNull(consumes1);
        assertEquals(1, consumes1.value().length);
        assertEquals("multipart/mixed", consumes1.value()[0]);
        assertEquals("put", test1Methods[0].getName());
        Class<?>[] paramTypes = test1Methods[0].getParameterTypes();
        assertEquals(3, paramTypes.length);
        Annotation[][] paramAnns = test1Methods[0].getParameterAnnotations();
        assertEquals(Boolean.class, paramTypes[0]);
        assertEquals(1, paramAnns[0].length);
        QueryParam test1QueryParam1 = (QueryParam) paramAnns[0][0];
        assertEquals("standalone", test1QueryParam1.value());
        assertEquals(String.class, paramTypes[1]);
        assertEquals(1, paramAnns[1].length);
        Multipart test1MultipartParam1 = (Multipart) paramAnns[1][0];
        assertEquals("action", test1MultipartParam1.value());
        assertTrue(test1MultipartParam1.required());
        assertEquals(String.class, paramTypes[2]);
        assertEquals(1, paramAnns[2].length);
        Multipart test1MultipartParam2 = (Multipart) paramAnns[2][0];
        assertEquals("sources", test1MultipartParam2.value());
        assertFalse(test1MultipartParam2.required());
        Class<?> test2 = loader.loadClass("application.Test2");
        Method[] test2Methods = test2.getDeclaredMethods();
        assertEquals(1, test2Methods.length);
        assertEquals(2, test2Methods[0].getAnnotations().length);
        assertNotNull(test2Methods[0].getAnnotation(PUT.class));
        Consumes consumes2 = test2Methods[0].getAnnotation(Consumes.class);
        assertNotNull(consumes2);
        assertEquals(1, consumes2.value().length);
        assertEquals("application/json", consumes2.value()[0]);
        assertEquals("put", test2Methods[0].getName());
        Class<?>[] paramTypes2 = test2Methods[0].getParameterTypes();
        assertEquals(2, paramTypes2.length);
        Annotation[][] paramAnns2 = test2Methods[0].getParameterAnnotations();
        assertEquals(boolean.class, paramTypes2[0]);
        assertEquals(1, paramAnns2[0].length);
        QueryParam test2QueryParam1 = (QueryParam) paramAnns2[0][0];
        assertEquals("snapshot", test2QueryParam1.value());
        assertEquals(String.class, paramTypes2[1]);
        assertEquals(0, paramAnns2[1].length);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : Multipart(org.apache.cxf.jaxrs.ext.multipart.Multipart) ToolContext(org.apache.cxf.tools.common.ToolContext) Method(java.lang.reflect.Method) PUT(javax.ws.rs.PUT) URISyntaxException(java.net.URISyntaxException) Consumes(javax.ws.rs.Consumes) QueryParam(javax.ws.rs.QueryParam) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File) Test(org.junit.Test)

Example 19 with ToolContext

use of org.apache.cxf.tools.common.ToolContext in project cxf by apache.

the class JAXRSContainerTest method testCodeGenWithImportedSchemaAndCatalog.

@Test
public void testCodeGenWithImportedSchemaAndCatalog() {
    try {
        JAXRSContainer container = new JAXRSContainer(null);
        ToolContext context = new ToolContext();
        context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
        context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/bookstoreImportCatalog.xml"));
        context.put(WadlToolConstants.CFG_CATALOG, getLocation("/wadl/jax-rs-catalog.xml"));
        context.put(WadlToolConstants.CFG_COMPILE, "true");
        container.setContext(context);
        container.execute();
        assertNotNull(output.list());
        verifyFiles("java", false, false, "superbooks", "org.apache.cxf.jaxrs.model.wadl", 9);
        verifyFiles("class", false, false, "superbooks", "org.apache.cxf.jaxrs.model.wadl", 9);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : ToolContext(org.apache.cxf.tools.common.ToolContext) URISyntaxException(java.net.URISyntaxException) Test(org.junit.Test)

Example 20 with ToolContext

use of org.apache.cxf.tools.common.ToolContext in project cxf by apache.

the class JAXRSContainerTest method doTestInlinedSchemasWithImport.

private void doTestInlinedSchemasWithImport(String loc) {
    try {
        JAXRSContainer container = new JAXRSContainer(null);
        ToolContext context = new ToolContext();
        context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
        context.put(WadlToolConstants.CFG_WADLURL, getLocation(loc));
        context.put(WadlToolConstants.CFG_COMPILE, "true");
        container.setContext(context);
        container.execute();
        assertNotNull(output.list());
        List<File> files = FileUtils.getFilesRecurse(output, ".+\\." + "class" + "$");
        assertEquals(8, files.size());
        assertTrue(checkContains(files, "org.apache.cxf.jaxrs.model.wadl" + ".BookStore.class"));
        assertTrue(checkContains(files, "org.apache.cxf.jaxrs.model.wadl" + ".PATCH.class"));
        assertTrue(checkContains(files, "superbooks" + ".Book.class"));
        assertTrue(checkContains(files, "superbooks" + ".ObjectFactory.class"));
        assertTrue(checkContains(files, "superbooks" + ".package-info.class"));
        assertTrue(checkContains(files, "superchapters" + ".Chapter.class"));
        assertTrue(checkContains(files, "superchapters" + ".ObjectFactory.class"));
        assertTrue(checkContains(files, "superchapters" + ".package-info.class"));
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : ToolContext(org.apache.cxf.tools.common.ToolContext) File(java.io.File) URISyntaxException(java.net.URISyntaxException)

Aggregations

ToolContext (org.apache.cxf.tools.common.ToolContext)69 Test (org.junit.Test)45 URISyntaxException (java.net.URISyntaxException)35 File (java.io.File)23 ToolException (org.apache.cxf.tools.common.ToolException)13 BadUsageException (org.apache.cxf.tools.common.toolspec.parser.BadUsageException)7 WADLToJava (org.apache.cxf.tools.wadlto.WADLToJava)5 BufferedReader (java.io.BufferedReader)4 Method (java.lang.reflect.Method)4 URLClassLoader (java.net.URLClassLoader)4 QName (javax.xml.namespace.QName)4 Before (org.junit.Before)4 FileReader (java.io.FileReader)3 URI (java.net.URI)3 LinkedHashSet (java.util.LinkedHashSet)3 JavaInterface (org.apache.cxf.tools.common.model.JavaInterface)3 Artifact (org.apache.maven.artifact.Artifact)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 IOException (java.io.IOException)2 Writer (java.io.Writer)2