Skip to content

Commit

Permalink
Merge pull request #5255 from bjhargrave/fixes
Browse files Browse the repository at this point in the history
Fix regression in Open Type Hierarchy
  • Loading branch information
bjhargrave authored May 19, 2022
2 parents 69e89a4 + 9911193 commit 4f2aa03
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 167 deletions.
40 changes: 17 additions & 23 deletions aQute.libg/src/aQute/lib/utf8properties/UTF8Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -144,45 +145,38 @@ private String decode(byte[] buffer) {
return new String(buffer); // default decoding
}

private static final Pattern LINE_SPLITTER = Pattern.compile("\n\r?");

@Override
public void store(OutputStream out, String msg) throws IOException {
public void store(Writer writer, String msg) throws IOException {
CharArrayWriter sw = new CharArrayWriter();
super.store(sw, null);

String[] lines = sw.toString()
.split("\n\r?");
String[] lines = LINE_SPLITTER.split(sw.toString());

byte[] newline = "\n".getBytes(UTF_8);
for (String line : lines) {
if (line.startsWith("#"))
continue;

out.write(line.getBytes(UTF_8));
out.write(newline);
writer.write(line);
writer.write('\n');
}
}

@Override
public void store(Writer out, String msg) throws IOException {
CharArrayWriter sw = new CharArrayWriter();
super.store(sw, null);

String[] lines = sw.toString()
.split("\n\r?");

for (String line : lines) {
if (line.startsWith("#"))
continue;

out.write(line);
out.write('\n');
public void store(OutputStream out, String msg) throws IOException {
Writer writer = new OutputStreamWriter(out, UTF_8);
try {
store(writer, msg);
} finally {
writer.flush();
}
}

public void store(File out) throws IOException {
CharArrayWriter sw = new CharArrayWriter();
super.store(sw, null);
IO.store(sw.toString(), out);
public void store(File file) throws IOException {
try (OutputStream out = IO.outputStream(file)) {
store(out, null);
}
}

public void store(OutputStream out) throws IOException {
Expand Down
20 changes: 19 additions & 1 deletion aQute.libg/test/aQute/lib/utf8properties/UTF8PropertiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
Expand All @@ -12,6 +13,7 @@

import org.junit.jupiter.api.Test;

import aQute.bnd.test.jupiter.InjectTemporaryDirectory;
import aQute.lib.io.IO;
import aQute.libg.reporter.ReporterAdapter;
import aQute.service.reporter.Report.Location;
Expand Down Expand Up @@ -381,12 +383,28 @@ public void testWrite() throws IOException {
StringWriter sw = new StringWriter();
p.store(sw, null);
String s = sw.toString();
assertThat(s).contains("#comment");
assertThat(s).doesNotStartWith("#")
.contains("#comment");
UTF8Properties p1 = new UTF8Properties();
p1.load(new StringReader(s));
assertThat(p1).containsExactlyInAnyOrderEntriesOf(p);
}

@Test
public void testWriteFile(@InjectTemporaryDirectory
File tmp) throws Exception {
UTF8Properties p = new UTF8Properties();
p.put("Foo", "Foo");
p.put("Bar", "Bar");
File props = new File(tmp, "props.properties");
p.store(props);
assertThat(props).isFile();
assertThat(IO.collect(props)).doesNotStartWith("#");
UTF8Properties p1 = new UTF8Properties();
p1.load(props, null);
assertThat(p1).containsExactlyInAnyOrderEntriesOf(p);
}

private void testProperty(String content, String key, String value) throws IOException {
testProperty(content, key, value, null);
}
Expand Down
169 changes: 67 additions & 102 deletions biz.aQute.bndlib.tests/test/test/BuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -541,39 +541,29 @@ public void testOverlappingPackageMissesImportVersions() throws Exception {
*/

@Test
public void testLastModifiedForManifest() throws Exception {
File file = new File("tmp.jar");
try {
long time = System.currentTimeMillis();
public void testLastModifiedForManifest(@InjectTemporaryDirectory
File tmp) throws Exception {
long time = System.currentTimeMillis();

Builder b = new Builder();
try (Builder b = new Builder()) {
b.addClasspath(IO.getFile("jar/osgi.jar"));
b.setExportPackage("org.osgi.framework");
Jar build = b.build();
try {
assertTrue(b.check());
assertTrue(b.check());

build.write("tmp.jar");
Jar ajr = new Jar(file);
try {
Resource r = ajr.getResource("META-INF/MANIFEST.MF");
assertNotNull(r);
long t = r.lastModified();
Date date = new Date(t);
System.out.println(date + " " + t);
// TODO we need to adapt the timestamp handling
assertThat(t).as("%s %s", date, t)
.isEqualTo(1142555622000L);
} finally {
ajr.close();
}
} finally {
build.close();
File file = new File(tmp, "tmp.jar");
build.write(file);
try (Jar ajr = new Jar(file)) {
Resource r = ajr.getResource("META-INF/MANIFEST.MF");
assertNotNull(r);
long t = r.lastModified();
Date date = new Date(t);
System.out.println(date + " " + t);
// TODO we need to adapt the timestamp handling
assertThat(t).as("%s %s", date, t)
.isEqualTo(1142555622000L);
}
} finally {
file.delete();
}

}

/**
Expand Down Expand Up @@ -806,69 +796,49 @@ public void testBadPackageInfo() throws Exception {
* removes A from 'a', and checks again that the last modified data changed.
*/
@Test
public void testRemoveClassFromPackage() throws Exception {
try {
Builder b = new Builder();
try {
IO.getFile("bin_test/a1/a")
.mkdirs();
IO.copy(IO.getFile("bin_test/a/A.class"), IO.getFile("bin_test/a1/a/A.class"));
IO.copy(IO.getFile("bin_test/a/B.class"), IO.getFile("bin_test/a1/a/B.class"));
Jar classpath = new Jar(IO.getFile("bin_test/a1"));
b.addClasspath(classpath);
b.setPrivatePackage("a");
Jar result = b.build();
Resource ra = result.getResource("a/A.class");
Resource rb = result.getResource("a/B.class");
long lm1 = result.lastModified();
assertTrue(lm1 > 0, "Last modified date of bundle > 0");

// windows has a very low resolution sometimes
Thread.sleep(IO.isWindows() ? 1000 : 100);

IO.getFile("bin_test/a1/a/B.class")
.delete();
classpath.remove("a/B.class");
classpath.updateModified(System.currentTimeMillis(), "Removed file B");
result = b.build();
long lm2 = result.lastModified();
assertTrue(lm2 > lm1, "Last modified date of bundle has increased after deleting class from package");

// windows has a very low resolution sometimes
Thread.sleep(IO.isWindows() ? 1000 : 100);

IO.getFile("bin_test/a1/a/A.class")
.delete();
classpath.remove("a/A.class");
classpath.updateModified(System.currentTimeMillis(), "Removed file A");

// windows has a very low resolution sometimes
Thread.sleep(IO.isWindows() ? 1000 : 100);

result = b.build();
long lm3 = result.lastModified();
assertTrue(lm3 > lm2,
"Last modified date of bundle has increased after deleting last class from package");
} finally {
b.close();
}
} finally {
try {
IO.getFile("bin_test/a1/a/A.class")
.delete();
} catch (Exception e) {}
try {
IO.getFile("bin_test/a1/a/B.class")
.delete();
} catch (Exception e) {}
try {
IO.getFile("bin_test/a1/a")
.delete();
} catch (Exception e) {}
try {
IO.getFile("bin_test/a1")
.delete();
} catch (Exception e) {}
public void testRemoveClassFromPackage(@InjectTemporaryDirectory
File tmp) throws Exception {
try (Builder b = new Builder()) {
IO.mkdirs(IO.getFile(tmp, "a"));
IO.copy(IO.getFile("bin_test/a/A.class"), IO.getFile(tmp, "a/A.class"));
IO.copy(IO.getFile("bin_test/a/B.class"), IO.getFile(tmp, "a/B.class"));
Jar classpath = new Jar(tmp);
b.addClasspath(classpath);
b.setPrivatePackage("a");
Jar result = b.build();
Resource ra = result.getResource("a/A.class");
assertThat(ra).isNotNull();
Resource rb = result.getResource("a/B.class");
assertThat(rb).isNotNull();
long lm1 = result.lastModified();
assertThat(lm1).as("Last modified date of bundle > 0")
.isGreaterThan(0L);

// windows has a very low resolution sometimes
Thread.sleep(IO.isWindows() ? 1000 : 100);

IO.delete(IO.getFile(tmp, "a/B.class"));
classpath.remove("a/B.class");
classpath.updateModified(System.currentTimeMillis(), "Removed file B");
result = b.build();
long lm2 = result.lastModified();
assertThat(lm2).as("Last modified date of bundle has increased after deleting class from package")
.isGreaterThan(lm1);

// windows has a very low resolution sometimes
Thread.sleep(IO.isWindows() ? 1000 : 100);

IO.delete(IO.getFile(tmp, "a/A.class"));
classpath.remove("a/A.class");
classpath.updateModified(System.currentTimeMillis(), "Removed file A");

// windows has a very low resolution sometimes
Thread.sleep(IO.isWindows() ? 1000 : 100);

result = b.build();
long lm3 = result.lastModified();
assertThat(lm3).as("Last modified date of bundle has increased after deleting last class from package")
.isGreaterThan(lm2);
}
}

Expand Down Expand Up @@ -943,7 +913,7 @@ public void testDigests(@InjectTemporaryDirectory
File tmp) throws Exception {
Builder b = new Builder();
try {
b.addClasspath(IO.getFile(new File(""), "jar/osgi.jar"));
b.addClasspath(IO.getFile("jar/osgi.jar"));
b.setExportPackage("org.osgi.framework");
b.setProperty(Constants.DIGESTS, "MD5, SHA1");
Jar jar = b.build();
Expand Down Expand Up @@ -1860,7 +1830,7 @@ public void testNoManifest(@InjectTemporaryDirectory
Jar jar = b.build();
assertTrue(b.check());

File f = new File(tmp, "tmp.jar");
File f = IO.getFile(tmp, "tmp.jar");
jar.write(f);

JarInputStream jin = new JarInputStream(new FileInputStream(f));
Expand Down Expand Up @@ -2402,11 +2372,11 @@ public void testConduit() throws Exception {
}

@Test
public void testSignedJarConduit() throws Exception {
public void testSignedJarConduit(@InjectTemporaryDirectory
File tmp) throws Exception {
Properties p = new Properties();
p.setProperty("-conduit", "jar/osgi-3.0.0.jar");
Builder b = new Builder();
try {
try (Builder b = new Builder()) {
b.setProperties(p);
Jar jars[] = b.builds();
assertTrue(b.check());
Expand All @@ -2417,17 +2387,14 @@ public void testSignedJarConduit() throws Exception {
Resource r = jar.getResource("META-INF/OSGI.RSA");
assertNotNull(r);

File f = new File("tmp.jar");
f.deleteOnExit();
File f = IO.getFile(tmp, "tmp.jar");
jar.write(f);

try (Jar wj = new Jar(f)) {
Resource wr = wj.getResource("META-INF/OSGI.RSA");
assertNotNull(wr);
assertEquals(wj.getSHA256(), jar.getSHA256());
}
} finally {
b.close();
}
}

Expand Down Expand Up @@ -2911,8 +2878,7 @@ public void testImportRangeCalculatedFromClasspath_2() throws Exception {
Properties base = new Properties();
base.put(Constants.IMPORT_PACKAGE, "javax.servlet,javax.servlet.http");

String pwd = System.getProperty("user.dir");
base.put("pwd", new File(pwd).toURI()
base.put("pwd", IO.work.toURI()
.toString());
base.put("-classpath", "${pwd}/jar/jsp-api.jar,${pwd}/jar/servlet-api.jar");

Expand Down Expand Up @@ -2977,8 +2943,7 @@ public void testImportRangeCalculatedFromClasspath_4() throws Exception {
Properties base = new Properties();
base.put(Constants.IMPORT_PACKAGE, "javax.servlet,javax.servlet.http");

String pwd = System.getProperty("user.dir");
base.put("pwd", new File(pwd).toURI()
base.put("pwd", IO.work.toURI()
.toString());
base.put("-classpath", "${pwd}/jar/jsp-api.jar,${pwd}/jar/servlet-api.jar");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void refresh() throws CoreException {
resources = null;
}

private static final IClasspathAttribute TEST = JavaCore.newClasspathAttribute("test",
static final IClasspathAttribute TEST = JavaCore.newClasspathAttribute("test",
Boolean.TRUE.toString());
private static final IClasspathAttribute PROJECT = JavaCore.newClasspathAttribute(Constants.VERSION_ATTRIBUTE,
Constants.VERSION_ATTR_PROJECT);
Expand Down Expand Up @@ -146,7 +146,7 @@ IRuntimeClasspathEntry[] getRuntimeClasspathEntries() throws JavaModelException
return runtime.toArray(EMPTY_RUNTIMEENTRIES);
}

private static boolean hasAttribute(IClasspathEntry cpe, IClasspathAttribute attr) {
static boolean hasAttribute(IClasspathEntry cpe, IClasspathAttribute attr) {
return Arrays.stream(cpe.getExtraAttributes())
.anyMatch(attr::equals);
}
Expand Down
Loading

0 comments on commit 4f2aa03

Please sign in to comment.