• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 Square, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.squareup.okhttp.internal.framed.hpackjson;
17 
18 import com.google.gson.Gson;
19 import com.google.gson.GsonBuilder;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.net.URISyntaxException;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29 
30 /**
31  * Utilities for reading HPACK tests.
32  */
33 public final class HpackJsonUtil {
34   /** Earliest draft that is code-compatible with latest. */
35   private static final int BASE_DRAFT = 9;
36 
37   private static final String STORY_RESOURCE_FORMAT = "/hpack-test-case/%s/story_%02d.json";
38 
39   private static final Gson GSON = new GsonBuilder().create();
40 
readStory(InputStream jsonResource)41   private static Story readStory(InputStream jsonResource) throws IOException {
42     return GSON.fromJson(new InputStreamReader(jsonResource, "UTF-8"), Story.class);
43   }
44 
45   /** Iterate through the hpack-test-case resources, only picking stories for the current draft. */
storiesForCurrentDraft()46   public static String[] storiesForCurrentDraft() throws URISyntaxException {
47     File testCaseDirectory = new File(HpackJsonUtil.class.getResource("/hpack-test-case").toURI());
48     List<String> storyNames = new ArrayList<String>();
49     for (File path : testCaseDirectory.listFiles()) {
50       if (path.isDirectory() && Arrays.asList(path.list()).contains("story_00.json")) {
51         try {
52           Story firstStory = readStory(new FileInputStream(new File(path, "story_00.json")));
53           if (firstStory.getDraft() >= BASE_DRAFT) {
54             storyNames.add(path.getName());
55           }
56         } catch (IOException ignored) {
57           // Skip this path.
58         }
59       }
60     }
61     return storyNames.toArray(new String[storyNames.size()]);
62   }
63 
64   /**
65    * Reads stories named "story_xx.json" from the folder provided.
66    */
readStories(String testFolderName)67   public static List<Story> readStories(String testFolderName) throws Exception {
68     List<Story> result = new ArrayList<>();
69     int i = 0;
70     while (true) { // break after last test.
71       String storyResourceName = String.format(STORY_RESOURCE_FORMAT, testFolderName, i);
72       InputStream storyInputStream = HpackJsonUtil.class.getResourceAsStream(storyResourceName);
73       if (storyInputStream == null) {
74         break;
75       }
76       try {
77         Story story = readStory(storyInputStream);
78         story.setFileName(storyResourceName);
79         result.add(story);
80         i++;
81       } finally {
82         storyInputStream.close();
83       }
84     }
85     return result;
86   }
87 
HpackJsonUtil()88   private HpackJsonUtil() { } // Utilities only.
89 }
90