<commit_before>/*
 * Copyright 2007 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package twitter4j;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;

import java.lang.reflect.Field;

/**
 * Test case for HttpCient
 *
 * @author <NAME> - <EMAIL>
 * @since Twitter4J 3.0.6
 */
public class Http2ClientTest extends TestCase {

    // specify running order
    public static Test suite() {

        TestSuite suite = new TestSuite();

        suite.addTest(new Http2ClientTest("testNoPreferOption"));    // must be called first
        suite.addTest(new Http2ClientTest("testHttp2"));

        return suite;
    }

    private Http2ClientTest(String name) {
        super(name);
    }

    protected void tearDown() {
    }

    public void testNoPreferOption() throws Exception {
        AlternativeHttpClientImpl http = callOembed();

        // check HTTP/2.0
        Field f = http.getClass().getDeclaredField("okHttpClient");
        f.setAccessible(true);
        OkHttpClient client = (OkHttpClient) f.get(http);
        assertNotNull("ensure that OkHttpClient is used", client);

        ConnectionPool p = client.connectionPool();
        assertEquals(1, p.connectionCount());

        assertEquals(Protocol.HTTP_2, http.getLastRequestProtocol());
    }

    public void testHttp2() throws Exception {
        AlternativeHttpClientImpl.sPreferSpdy = false;
        AlternativeHttpClientImpl.sPreferHttp2 = true;
        AlternativeHttpClientImpl http = callOembed();

        // check HTTP/2.0
        Field f = http.getClass().getDeclaredField("okHttpClient");
        f.setAccessible(true);
        OkHttpClient client = (OkHttpClient) f.get(http);
        assertNotNull("ensure that OkHttpClient is used", client);

        ConnectionPool p = client.connectionPool();
        assertEquals(1, p.connectionCount());

        assertEquals(Protocol.HTTP_2, http.getLastRequestProtocol());
    }

    private AlternativeHttpClientImpl callOembed() throws TwitterException {
        AlternativeHttpClientImpl http = new AlternativeHttpClientImpl();
        String url = "https://api.twitter.com/1/statuses/oembed.json?id=441617258578583554";

        HttpRequest req = new HttpRequest(RequestMethod.GET, url, null, null, null);
        // just to ensure the response body is consumed
        http.request(req).asJSONObject();

        return http;
    }
    
    public void testUploadMediaFromStream() throws Exception {
        Twitter twitter = TwitterFactory.getSingleton();
        UploadedMedia media2 = twitter.uploadMedia("fromInputStream", 
                Http2ClientTest.class.getResourceAsStream("/twitter4j.jpg"));

        StatusUpdate update = new StatusUpdate("from input stream");
        update.setMediaIds(media2.getMediaId());
        Status status = twitter.updateStatus(update);
        assertEquals("from input stream", status.getText());
    }
}<commit_msg>run Http2ClientTest test cases only when alpn jar is found in the boot classpath<commit_after>/*
 * Copyright 2007 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package twitter4j;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;

import java.lang.reflect.Field;

/**
 * Test case for HttpCient
 * The fllowing argument is requred to run this test case:
 * -Xbootclasspath/p:~/.m2/repository/org/mortbay/jetty/alpn/alpn-boot/8.1.12.v20180117/alpn-boot-8.1.12.v20180117.jar
 *
 * @author <NAME> - <EMAIL> at <EMAIL>
 * @since Twitter4J 3.0.6
 */
public class Http2ClientTest extends TestCase {

    static boolean alpnBootJarFoundInBootClassPath;

    // specify running order
    public static Test suite() {

        TestSuite suite = new TestSuite();
        alpnBootJarFoundInBootClassPath = System.getProperty("sun.boot.class.path").matches(".*alpn-boot-[0-9.]+\\.v[0-9]+\\.jar.*");
        if (alpnBootJarFoundInBootClassPath) {
            System.out.println("alpn jar found in boot classpath.");
        } else {
            System.out.println("alpn jar not found in boot classpath.");
        }

        suite.addTest(new Http2ClientTest("testNoPreferOption"));    // must be called first
        suite.addTest(new Http2ClientTest("testHttp2"));

        return suite;
    }

    private Http2ClientTest(String name) {
        super(name);
    }

    protected void tearDown() {
    }

    public void testNoPreferOption() throws Exception {
        if (alpnBootJarFoundInBootClassPath) {
            AlternativeHttpClientImpl http = callOembed();

            // check HTTP/2.0
            Field f = http.getClass().getDeclaredField("okHttpClient");
            f.setAccessible(true);
            OkHttpClient client = (OkHttpClient) f.get(http);
            assertNotNull("ensure that OkHttpClient is used", client);

            ConnectionPool p = client.connectionPool();
            assertEquals(1, p.connectionCount());

            assertEquals(Protocol.HTTP_2, http.getLastRequestProtocol());
        }
    }

    public void testHttp2() throws Exception {
        if (alpnBootJarFoundInBootClassPath) {
            AlternativeHttpClientImpl.sPreferSpdy = false;
            AlternativeHttpClientImpl.sPreferHttp2 = true;
            AlternativeHttpClientImpl http = callOembed();

            // check HTTP/2.0
            Field f = http.getClass().getDeclaredField("okHttpClient");
            f.setAccessible(true);
            OkHttpClient client = (OkHttpClient) f.get(http);
            assertNotNull("ensure that OkHttpClient is used", client);

            ConnectionPool p = client.connectionPool();
            assertEquals(1, p.connectionCount());

            assertEquals(Protocol.HTTP_2, http.getLastRequestProtocol());
        }
    }

    private AlternativeHttpClientImpl callOembed() throws TwitterException {
        AlternativeHttpClientImpl http = new AlternativeHttpClientImpl();
        String url = "https://api.twitter.com/1/statuses/oembed.json?id=441617258578583554";

        HttpRequest req = new HttpRequest(RequestMethod.GET, url, null, null, null);
        // just to ensure the response body is consumed
        http.request(req).asJSONObject();

        return http;
    }

    public void testUploadMediaFromStream() throws Exception {
        if (alpnBootJarFoundInBootClassPath) {
            Twitter twitter = TwitterFactory.getSingleton();
            UploadedMedia media2 = twitter.uploadMedia("fromInputStream",
                    Http2ClientTest.class.getResourceAsStream("/twitter4j.jpg"));

            StatusUpdate update = new StatusUpdate("from input stream");
            update.setMediaIds(media2.getMediaId());
            Status status = twitter.updateStatus(update);
            assertEquals("from input stream", status.getText());
        }
    }
}
{'fraction_non_alphanumeric': 0.09829113134588999, 'fraction_numerical': 0.017213421479356367, 'mean_word_length': 3.0110055027513756, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6721312', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2133, 'n_words': 581}