com.dotcms.LicenseTestUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.dotcms.LicenseTestUtil.java

Source

package com.dotcms;

import javax.servlet.http.HttpServletRequest;

import com.dotcms.repackage.org.apache.commons.httpclient.NameValuePair;
import com.dotcms.repackage.org.apache.commons.httpclient.methods.PostMethod;
import com.dotcms.repackage.org.apache.commons.httpclient.HttpClient;
import com.dotcms.repackage.org.mockito.stubbing.Answer;
import com.dotcms.repackage.org.mockito.Mockito;
import com.dotcms.enterprise.LicenseUtil;

/*
 * Util class created to emulate the get license form request
 * should only be used inside the test suite. 
 */
public class LicenseTestUtil {

    public static void getLicense() throws Exception {

        if (LicenseUtil.getLevel() < 200) {
            String license;
            HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
            Mockito.when(req.getParameter("iwantTo")).thenReturn("request_code");
            Mockito.when(req.getParameter("license_type")).thenReturn("trial");
            Mockito.when(req.getParameter("license_level")).thenReturn("400");

            final StringBuilder reqcode = new StringBuilder();

            Mockito.doAnswer(new Answer() {
                public Object answer(com.dotcms.repackage.org.mockito.invocation.InvocationOnMock invocation)
                        throws Throwable {
                    reqcode.append(invocation.getArguments()[1].toString());
                    return null;
                }
            }).when(req).setAttribute(Mockito.eq("requestCode"), Mockito.any(String.class));

            LicenseUtil.processForm(req);

            HttpClient client = new HttpClient();
            PostMethod post = new PostMethod("http://support.dotcms.com/app/licenseRequest3");
            post.setRequestBody(new NameValuePair[] { new NameValuePair("code", reqcode.toString()) });
            client.executeMethod(post);

            if (post.getStatusCode() == 200) {
                license = post.getResponseBodyAsString();
                HttpServletRequest req2 = Mockito.mock(HttpServletRequest.class);
                Mockito.when(req2.getParameter("iwantTo")).thenReturn("paste_license");
                Mockito.when(req2.getParameter("license_text")).thenReturn(license);
                LicenseUtil.processForm(req2);
            }
        }
    }
}