BundleAnalyzer implements ArtifactAnalyzer { @Override @CheckReturnValue public ImmutableList<Suggestion> analyze(File artifactFile) { ImmutableList.Builder<Suggestion> resultBuilder = ImmutableList.<Suggestion>builder(); try (ZipFile zipFile = new ZipFile(artifactFile)) { AppBundle appBundle = AppBundle.buildFromZip(zipFile); ImmutableMap<BundleModuleName, BundleContext> contextPerModule = appBundle.getModules().entrySet().stream() .collect( toImmutableMap( entry -> entry.getKey(), entry -> createContext(entry.getValue()))); BundleContext baseContext = contextPerModule.get(BundleModuleName.BASE_MODULE_NAME); bundleSuggesters.forEach( suggester -> resultBuilder.addAll(suggester.processBundle(baseContext, appBundle, zipFile))); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); BundleContext context = findContext(contextPerModule, entry); for (BundleEntrySuggester suggester : bundleEntrySuggesters) { ZipFileData zipFileData = new ZipFileData(zipFile, entry); resultBuilder.addAll(suggester.processBundleZipEntry(context, zipFileData)); } } } catch (IOException e) { throw new UncheckedIOException(e); } return resultBuilder.build(); } BundleAnalyzer(
      ImmutableList<BundleSuggester> bundleSuggesters,
      ImmutableList<BundleEntrySuggester> bundleEntrySuggesters); @Override @CheckReturnValue ImmutableList<Suggestion> analyze(File artifactFile);  }
@Test public void analyze_callsSuggesters() throws Exception { BundleAnalyzer analyzer = new BundleAnalyzer(testArtifactSuggesters, testEntrySuggesters); File bundleFile = TestUtils.getTestDataFile(APP_BUNDLE); BundleContext context = BundleContext.create( 23); Suggestion stubSuggestion = Suggestion.create( Suggestion.IssueType.WEBP, Suggestion.Category.WEBP, Payload.getDefaultInstance(), "Stub Suggestion", null, null); Suggestion stubArtifactSuggestion = Suggestion.create( Suggestion.IssueType.WEBP, Suggestion.Category.WEBP, Payload.getDefaultInstance(), "Stub Artifact Suggestion", null, null); suggester.setEntrySuggestions( ImmutableMultimap.of( ContextAndEntryPath.create(context, "base/manifest/AndroidManifest.xml"), stubSuggestion)); suggester.setArtifactSuggestions(ImmutableMultimap.of(context, stubArtifactSuggestion)); ImmutableList<Suggestion> suggestions = analyzer.analyze(bundleFile); assertThat(suggestions).containsExactly(stubSuggestion, stubArtifactSuggestion); }
@Test public void analyze_callsSuggesters_embeddedWearApk() throws Exception { BundleAnalyzer analyzer = new BundleAnalyzer(testArtifactSuggesters, testEntrySuggesters); File bundleFile = TestUtils.getTestDataFile(WEARABLE_APP_BUNDLE); BundleContext context = BundleContext.create( 21, false, true); Suggestion stubSuggestion = Suggestion.create( Suggestion.IssueType.WEBP, Suggestion.Category.WEBP, Payload.getDefaultInstance(), "Stub Suggestion", null, null); Suggestion stubArtifactSuggestion = Suggestion.create( Suggestion.IssueType.WEBP, Suggestion.Category.WEBP, Payload.getDefaultInstance(), "Stub Artifact Suggestion", null, null); suggester.setEntrySuggestions( ImmutableMultimap.of( ContextAndEntryPath.create(context, "base/manifest/AndroidManifest.xml"), stubSuggestion)); suggester.setArtifactSuggestions(ImmutableMultimap.of(context, stubArtifactSuggestion)); ImmutableList<Suggestion> suggestions = analyzer.analyze(bundleFile); assertThat(suggestions).containsExactly(stubSuggestion, stubArtifactSuggestion); }