1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/*
Derby - Class org.apache.JarDriftTest
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyBuild;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/* Notes:
* -- this is run as the last depends in the ant buildjars target.
* -- the jar file location is taken relative to that position and not
* related to the classpath setting.
* -- we check on sane or insane by looking in the file
* generated/java/org/apache/derby/shared/common/sanity/state.properties
* -- source file and '*lastcontents files will be in the source distro,
* but they do not need to go in any jar files.
* -- presence of derbyTesting.jar is optional.
* -- to update the lastcontents list when a new class is being added,
* run this class with the 'refresh' option, or ant refreshjardriftcheck
* -- this implementation should work with just 1.6, although then
* a number of classes (Java 8 support) will not get built. So only
* run the refresh with the highest level jvm.
* -- we are only worrying about class files, even ignoring inner classes
* -- we're not unravelling any jars from inside the jars
*/
public class JarDriftTest {
static boolean failbuild = false;
/**
* <p>
* Like the MessageBundleTest, let Ant conjure us up.
* </p>
*/
public JarDriftTest()
{}
protected PrintWriter pwOut;
protected Hashtable<String, Object> classpathHash;
// if run with -Dverbose=true
protected static boolean verbose = Boolean.getBoolean("verbose");
protected String classpath[] = null;
static boolean updateFiles=false;
static String sanityState;
static String topOfTree;
static String[] baseJars = {"derby.jar", "derbyclient.jar", "derbynet.jar",
"derbytools.jar", "derbyoptionaltools.jar",
"derbyrun.jar", "derbyTesting.jar"};
static HashSet<String> pastJarContents = new HashSet<String>();
static HashSet<String> builtJarContents = new HashSet<String>();
public static void main(String [] args) throws Exception
{
if (args.length > 0
&& args[0].equalsIgnoreCase("refresh"))
updateFiles=true;
JarDriftTest t = new JarDriftTest();
// find local path
topOfTree = System.getProperty("user.dir");
if (verbose)
System.out.println("top of build: " + topOfTree);
sanityState = sanityState();
try {
for (int i = 0 ; i< baseJars.length ; i++) {
t.jarCheck(baseJars[i]);
}
} catch (Exception e) {
System.out.println("jar drift check failed: ");
e.printStackTrace();
}
if (failbuild)
throw new Exception(
"\njar drift check failed; see DERBY-6471 for info. \n" +
"See error in build output or call ant jardriftcheck. \n" +
"If the new class is expected run ant refreshjardriftcheck.\n"+
"NB: Run the refresh for both sane and insane builds. \n" +
" Use the highest supported JVM (currently Java 8) \n " +
" to ensure all classes are built.\n");
}
protected void loadJarContents(String jarName)
throws Exception {
if (verbose)
System.out.println("jar: " + jarName);
String jarFileLocation = topOfTree + File.separator + "jars";
if (sanityState != null)
{
jarFileLocation=jarFileLocation + File.separator + sanityState;
if (verbose)
System.out.println(
"get the jar files from the dir: " + jarFileLocation);
}
else
System.out.println("oops sanity state is null, not sure why");
// use 1.6 style code
List<String> classNames=new ArrayList<String>();
try {
ZipInputStream zip=new ZipInputStream(
new FileInputStream(jarFileLocation + File.separator + jarName));
for (ZipEntry entry=zip.getNextEntry() ; entry!=null ; entry=zip.getNextEntry())
{
String className = entry.getName();
if (className.endsWith(".class") && !entry.isDirectory()) {
// ignore classes with a '$' in the name - these are inner
// classes and it is possible that more get created if the
// base class grows, or different compilers may add more
// and it seems unlikely one of the inner classes would
// drift into another jar without its parent drifting.
if (className.contains("$"))
continue;
className = entry.getName().replace(File.separator, ".");
// replace some other possible separator chars in case the
// character is OS, vendor, or zip implementation dependent.
className = className.replace("/", ".");
className = className.replace("//", ".");
className = className.replace("\\", ".");
classNames.add(className.toString());
}
}
} catch (IOException ioe ) {
// in all other cases, the process should/will fail but there is
// a target to build without derbyTesting.jar, so it may be ok
if (!jarName.equalsIgnoreCase("derbyTesting.jar"))
throw ioe;
}
// load the contents into builtJarContents
builtJarContents.addAll(classNames);
// write out the contents ?
if (updateFiles) {
String fs = File.separator;
String outputFile = topOfTree + fs + "java" + fs + "build" + fs +
"org" + fs + "apache" + fs + "derbyBuild" + fs +
"lastgoodjarcontents" + fs +
sanityState + "." + jarName + ".lastcontents";
PrintWriter pwf = new PrintWriter(outputFile);
for (String className : classNames)
pwf.println(className);
pwf.close();
}
}
protected static String sanityState() throws Exception {
// need to check on sane vs. insane - access file
// generated/java/org/apache/derby/shared/common/sanity/state.properties
// and look for 'false' or 'true' (the file should only have
// sanity=[false|true] except for some header/timestamp info).
String fs = File.separator;
String sanityFileName =
topOfTree + fs + "generated" + fs + "java" + fs + "org" + fs +
"apache" + fs + "derby" + fs + "shared" + fs + "common" + fs +
"sanity" + fs + "state.properties";
File sanityFile = new File(sanityFileName);
String sanityContents = readFile(sanityFile);
if (sanityContents != null && sanityContents.length()> 0)
{
if (sanityContents.contains("false"))
sanityState = "insane";
else if (sanityContents.contains("true"))
sanityState = "sane";
}
else
{
throw new Exception ("oops, something wrong getting the sanity state");
}
return sanityState;
}
private static String readFile(File file) throws IOException{
StringBuffer strbuf = new StringBuffer();
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
strbuf.append(line);
strbuf.append("\n");
}
fr.close();
/* if (verbose) {
System.out.println("Contents of file:");
System.out.println(strbuf.toString()); */
} catch (IOException e) {
e.printStackTrace();
}
return strbuf.toString();
}
protected void loadPastContents(String jarName) throws Exception {
String fs = File.separator;
String inputFile = topOfTree + fs + "java" + fs + "build" + fs +
"org" + fs + "apache" + fs + "derbyBuild" + fs +
"lastgoodjarcontents"+ fs +
sanityState + "." + jarName + ".lastcontents";
if (verbose)
System.out.println("looking through " + inputFile);
String pastJarContentsString = readFile(new File(inputFile));
// split up the string at each \n
BufferedReader br =
new BufferedReader(new StringReader(pastJarContentsString));
String className = null;
while ((className = br.readLine()) != null){
if (verbose)
System.out.println("found/added: " + className);
pastJarContents.add(className);
}
}
protected void jarCheck(String jarName) throws Exception {
builtJarContents.clear();
pastJarContents.clear();
loadJarContents(jarName);
loadPastContents(jarName);
if (verbose)
System.out.println("\nnow comparing for jar: " + jarName);
Iterator it = builtJarContents.iterator();
while ( it.hasNext() ) {
String objectName = (String)it.next();
if (objectName.contains("class"))
{
if (verbose)
System.out.println("objectname: " + objectName);
if ( ! pastJarContents.contains(objectName) ) {
// Don't fail out on the first one, we want to catch
// all of them. Just note there was a failure and continue
failbuild=true;
System.err.println("ERROR: class " + objectName + " in\n" +
" " + jarName + " was not previously there.\n");
}
}
}
}
}
|