private void assertVersion(Versioned vers) { final Version v = vers.version(); assertFalse("Should find version information (got " + v + ")", v.isUknownVersion()); Version exp = PackageVersion.VERSION; assertEquals(exp.toFullString(), v.toFullString()); assertEquals(exp, v); }
/** * Helper method that will try to load version information for specified class. Implementation is * simple: class loader that loaded specified class is asked to load resource with name "VERSION" * from same location (package) as class itself had. If no version information is found, {@link * Version#unknownVersion()} is returned. */ public static Version versionFor(Class<?> cls) { InputStream in; Version version = null; try { in = cls.getResourceAsStream(VERSION_FILE); if (in != null) { try { BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); String groupStr = null, artifactStr = null; String versionStr = br.readLine(); if (versionStr != null) { groupStr = br.readLine(); if (groupStr != null) { groupStr = groupStr.trim(); artifactStr = br.readLine(); if (artifactStr != null) { artifactStr = artifactStr.trim(); } } } version = parseVersion(versionStr, groupStr, artifactStr); } finally { try { in.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } catch (IOException e) { } return (version == null) ? Version.unknownVersion() : version; }
/** * Will attempt to load the maven version for the given groupId and artifactId. Maven puts a * pom.properties file in META-INF/maven/groupId/artifactId, containing the groupId, artifactId * and version of the library. * * @param classLoader the ClassLoader to load the pom.properties file from * @param groupId the groupId of the library * @param artifactId the artifactId of the library * @return The version */ public static Version mavenVersionFor( ClassLoader classLoader, String groupId, String artifactId) { InputStream pomPoperties = classLoader.getResourceAsStream( "META-INF/maven/" + groupId.replaceAll("\\.", "/") + "/" + artifactId + "/pom.properties"); if (pomPoperties != null) { try { Properties props = new Properties(); props.load(pomPoperties); String versionStr = props.getProperty("version"); String pomPropertiesArtifactId = props.getProperty("artifactId"); String pomPropertiesGroupId = props.getProperty("groupId"); return parseVersion(versionStr, pomPropertiesGroupId, pomPropertiesArtifactId); } catch (IOException e) { // Ignore } finally { try { pomPoperties.close(); } catch (IOException e) { // Ignore } } } return Version.unknownVersion(); }
public void testSimpleKeyDeser() throws Exception { ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule("test", Version.unknownVersion()); module.addKeyDeserializer(String.class, new ContextualDeser("???")); mapper.registerModule(module); MapBean result = mapper.readValue("{\"map\":{\"a\":3}}", MapBean.class); Map<String, Integer> map = result.map; assertNotNull(map); assertEquals(1, map.size()); Map.Entry<String, Integer> entry = map.entrySet().iterator().next(); assertEquals(Integer.valueOf(3), entry.getValue()); assertEquals("map:a", entry.getKey()); }
public void testSimpleKeySer() throws Exception { ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule("test", Version.unknownVersion()); module.addKeySerializer(String.class, new ContextualKeySerializer("prefix")); mapper.registerModule(module); Map<String, Object> input = new HashMap<String, Object>(); input.put("a", Integer.valueOf(3)); String json = mapper .writerWithType( TypeFactory.defaultInstance() .constructMapType(HashMap.class, String.class, Object.class)) .writeValueAsString(input); assertEquals("{\"prefix:a\":3}", json); }
protected VersionUtil() { Version v = null; try { /* Class we pass only matters for resource-loading: can't use this Class * (as it's just being loaded at this point), nor anything that depends on it. */ v = VersionUtil.versionFor(getClass()); } catch (Exception e) { // not good to dump to stderr; but that's all we have at this low level System.err.println( "ERROR: Failed to load Version information for bundle (via " + getClass().getName() + ")."); } if (v == null) { v = Version.unknownVersion(); } _version = v; }
/** * Convenience constructor that will use specified Version, including name from {@link * Version#getArtifactId()} */ public SimpleModule(Version version) { _name = version.getArtifactId(); _version = version; }
/** Convenience constructor that will default version to {@link Version#unknownVersion()}. */ public SimpleModule(String name) { this(name, Version.unknownVersion()); }
/** * Constructors that should only be used for non-reusable convenience modules used by app code: * "real" modules should use actual name and version number information. */ public SimpleModule() { // when passing 'this', can not chain constructors... _name = "SimpleModule-" + System.identityHashCode(this); _version = Version.unknownVersion(); }