DefaultC10NInspector implements C10NInspector { @Override public List<C10NUnit> inspect(String... packagePrefixes) { List<C10NUnit> res = new ArrayList<>(); @SuppressWarnings("deprecation") C10NMsgFactory c10NMsgFactory = C10N.createMsgFactory(configuredC10NModule); Set<Class<?>> c10nInterfaces = c10NInterfaceSearch.find(C10NMessages.class, packagePrefixes); for (Class<?> c10nInterface : c10nInterfaces) { Set<Map.Entry<Class<? extends Annotation>, Set<Locale>>> annotationEntries = configuredC10NModule.getAnnotationBindings(c10nInterface).entrySet(); List<C10NUnit> unitsForInterface = new ArrayList<>(); for (Method method : c10nInterface.getDeclaredMethods()) { String keyAnnotationValue = ReflectionUtils.getKeyAnnotationValue(method); String bundleKey = ReflectionUtils.getC10NKey(configuredC10NModule.getKeyPrefix(), method); boolean isCustom = ReflectionUtils.getKeyAnnotationBasedKey(method) != null; C10NBundleKey key = new C10NBundleKey(isCustom, bundleKey, keyAnnotationValue); C10NUnit unit = new C10NUnit(c10nInterface, method, key, localesToCheck); for (Map.Entry<Class<? extends Annotation>, Set<Locale>> entry : annotationEntries) { Class<? extends Annotation> annotationClass = entry.getKey(); for (Locale locale : entry.getValue()) { if (localesToCheck.contains(locale)) { String translatedValue = extractTranslatedValue(c10NMsgFactory, c10nInterface, method, locale); C10NTranslations trs = addTranslations(unit, locale, translatedValue); Annotation annotation = method.getAnnotation(annotationClass); if (null != annotation) { trs.getAnnotations().add(annotation); } } } } unitsForInterface.add(unit); } for (Locale locale : localesToCheck) { List<ResourceBundle> bundles = configuredC10NModule.getBundleBindings(c10nInterface, locale); for (C10NUnit unit : unitsForInterface) { String translatedValue = extractTranslatedValue(c10NMsgFactory, c10nInterface, unit.getDeclaringMethod(), locale); C10NTranslations trs = addTranslations(unit, locale, translatedValue); for (ResourceBundle bundle : bundles) { if (bundle.containsKey(unit.getKey().getKey())) { trs.getBundles().add(bundle); } } } } res.addAll(unitsForInterface); } return res; } DefaultC10NInspector(C10NInterfaceSearch c10NInterfaceSearch,
                         ConfiguredC10NModule configuredC10NModule,
                         DummyInstanceProvider dummyInstanceProvider,
                         Set<Locale> localesToCheck,
                         boolean fetchTranslations); @Override List<C10NUnit> inspect(String... packagePrefixes);  }
@Test public void allKeysAreDiscovered() { assertThat(Iterables.size(checker.inspect(testMsgPackage)), is(17)); }
@Test public void extendingMethodOnlyBundleKeyInterface() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, SubInterfaceOfMethodOnlyKey.class, "sub_both", customKey("scope.submethod", "scope.submethod"), bundleTr(ENGLISH, "[en]@bundle SubInterfaceOfMethodOnlyKey.sub_both"), bundleTr(JAPANESE, "[ja]@bundle SubInterfaceOfMethodOnlyKey.sub_both")); }
@Test public void annotationOnlyUnitsWithParams() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, AnnotationOnly.class, method("bothWithParams", String.class, int.class), anyKey(), annotationTr(ENGLISH, "[en]@annotation AnnotationOnly.bothWithParams_{0}_1"), annotationTr(JAPANESE, "[ja]@annotation AnnotationOnly.bothWithParams_{0}_1")); }
@Test public void classScopedBundleKeysWithParams() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, ClassScopeKey.class, method("bothInBundleWithParams", Integer.class, byte.class), customKey("scope.class.bothInBundleWithParams_Integer_byte", null), bundleTr(ENGLISH, "[en]@bundle ClassScopeKey.bothInBundleWithParams_0_1"), bundleTr(JAPANESE, "[ja]@bundle ClassScopeKey.bothInBundleWithParams_0_1")); }
@Test public void inspectorHonorsGlobalKeyPrefix() { C10NConfigBase conf = new C10NConfigBase() { @Override protected void configure() { install(new DefaultC10NAnnotations()); bindBundle("com.github.rodionmoiseev.c10n.tools.inspector.test1.Test1"); setKeyPrefix("prefix"); } }; C10NInspector checker = new DefaultC10NInspector(SearchModule.reflectionsSearch(), C10N.configure(conf), new DefaultDummyInstanceProvider(), localesToCheck, true); List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, AutoKey.class, "both", autoKey("prefix.com.github.rodionmoiseev.c10n.tools.inspector.test1.AutoKey.both"), bundleTr(ENGLISH, "[en]@bundle AutoKey.both (prefix)"), bundleTr(JAPANESE, "[ja]@bundle AutoKey.both (prefix)")); checkUnit(units, ClassScopeKey.class, "both", customKey("prefix.scope.class.both", null), bundleTr(ENGLISH, "[en]@bundle ClassScopeKey.both (prefix)"), bundleTr(JAPANESE, "[ja]@bundle ClassScopeKey.both (prefix)")); checkUnit(units, MethodScopeKey.class, "both", customKey("prefix.scope.class.scope.method.both", "scope.method.both"), bundleTr(ENGLISH, "[en]@bundle MethodScopeKey.both (prefix)"), bundleTr(JAPANESE, "[ja]@bundle MethodScopeKey.both (prefix)")); }
@Test public void annotationOnlyUnits() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, AnnotationOnly.class, "both", anyKey(), annotationTr(ENGLISH, "[en]@annotation AnnotationOnly.both"), annotationTr(JAPANESE, "[ja]@annotation AnnotationOnly.both")); checkUnit(units, AnnotationOnly.class, "onlyEn", anyKey(), annotationTr(ENGLISH, "[en]@annotation AnnotationOnly.onlyEn")); }
@Test public void autoGeneratedBundleKeys() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, AutoKey.class, "both", autoKey("com.github.rodionmoiseev.c10n.tools.inspector.test1.AutoKey.both"), bundleTr(ENGLISH, "[en]@bundle AutoKey.both"), bundleTr(JAPANESE, "[ja]@bundle AutoKey.both")); checkUnit(units, AutoKey.class, "onlyJa", autoKey("com.github.rodionmoiseev.c10n.tools.inspector.test1.AutoKey.onlyJa"), bundleTr(JAPANESE, "[ja]@bundle AutoKey.onlyJa")); checkUnit(units, AutoKey.class, "enInAnnotationJaInBundle", autoKey("com.github.rodionmoiseev.c10n.tools.inspector.test1.AutoKey.enInAnnotationJaInBundle"), annotationTr(ENGLISH, "[en]@annotation AutoKey.enInAnnotationJaInBundle"), bundleTr(JAPANESE, "[ja]@bundle AutoKey.enInAnnotationJaInBundle")); checkUnit(units, AutoKey.class, "none", anyKey()); }
@Test public void classScopeBundleKeys() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, ClassScopeKey.class, "both", customKey("scope.class.both", null), bundleTr(ENGLISH, "[en]@bundle ClassScopeKey.both"), bundleTr(JAPANESE, "[ja]@bundle ClassScopeKey.both")); checkUnit(units, ClassScopeKey.class, "bothInAnnotationAndInBundle", customKey("scope.class.bothInAnnotationAndInBundle", null), annotationTr(ENGLISH, "[en]@bundle ClassScopeKey.bothInAnnotationAndInBundle", "[en]@annotation ClassScopeKey.bothInAnnotationAndInBundle"), annotationTr(JAPANESE, "[ja]@bundle ClassScopeKey.bothInAnnotationAndInBundle", "[ja]@annotation ClassScopeKey.bothInAnnotationAndInBundle"), bundleTr(ENGLISH, "[en]@bundle ClassScopeKey.bothInAnnotationAndInBundle"), bundleTr(JAPANESE, "[ja]@bundle ClassScopeKey.bothInAnnotationAndInBundle")); }
@Test public void methodKeyOnlyBundleKeys() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, MethodOnlyKey.class, "both", customKey("scope.method", "scope.method"), bundleTr(ENGLISH, "[en]@bundle MethodOnlyKey.both"), bundleTr(JAPANESE, "[ja]@bundle MethodOnlyKey.both")); }
@Test public void classAndMethodScopeBundleKeys() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, MethodScopeKey.class, "both", customKey("scope.class.scope.method.both", "scope.method.both"), bundleTr(ENGLISH, "[en]@bundle MethodScopeKey.both"), bundleTr(JAPANESE, "[ja]@bundle MethodScopeKey.both")); }
@Test public void extendingAnnotationOnlyInterface() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, SubInterfaceOfAnnotationOnly.class, "sub_both", anyKey(), annotationTr(ENGLISH, "[en]@annotation SubInterfaceOfAnnotationOnly.sub_both"), annotationTr(JAPANESE, "[ja]@annotation SubInterfaceOfAnnotationOnly.sub_both")); }
@Test public void extendingAutoGeneratedBundleKeyInterface() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, SubInterfaceOfAutoKey.class, "sub_both", autoKey("com.github.rodionmoiseev.c10n.tools.inspector.test1.SubInterfaceOfAutoKey.sub_both"), bundleTr(ENGLISH, "[en]@bundle SubInterfaceOfAutoKey.sub_both"), bundleTr(JAPANESE, "[ja]@bundle SubInterfaceOfAutoKey.sub_both")); }
@Test public void extendingClassScopedBundleKeyInterface() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, SubInterfaceOfClassScopedKey.class, "sub_both", customKey("scope.subclass.sub_both", null), bundleTr(ENGLISH, "[en]@bundle SubInterfaceOfClassScopedKey.sub_both"), bundleTr(JAPANESE, "[ja]@bundle SubInterfaceOfClassScopedKey.sub_both")); }
@Test public void extendingMethodScopeBundleKeyInterface() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, SubInterfaceOfMethodScopedKey.class, "sub_both", customKey("scope.subclass.scope.submethod", "scope.submethod"), bundleTr(ENGLISH, "[en]@bundle SubInterfaceOfMethodScopedKey.sub_both"), bundleTr(JAPANESE, "[ja]@bundle SubInterfaceOfMethodScopedKey.sub_both")); }